GettingADocumentTests.coffee 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. sinon = require "sinon"
  2. chai = require("chai")
  3. chai.should()
  4. MockWebApi = require "./helpers/MockWebApi"
  5. DocUpdaterClient = require "./helpers/DocUpdaterClient"
  6. describe "Getting a document", ->
  7. describe "when the document is not loaded", ->
  8. before (done) ->
  9. [@project_id, @doc_id] = [DocUpdaterClient.randomId(), DocUpdaterClient.randomId()]
  10. MockWebApi.insertDoc @project_id, @doc_id, {
  11. lines: @lines = ["one", "two", "three"]
  12. version: @version = 42
  13. }
  14. sinon.spy MockWebApi, "getDocument"
  15. DocUpdaterClient.getDoc @project_id, @doc_id, (error, res, @returnedDoc) => done()
  16. after ->
  17. MockWebApi.getDocument.restore()
  18. it "should load the document from the web API", ->
  19. MockWebApi.getDocument
  20. .calledWith(@project_id, @doc_id)
  21. .should.equal true
  22. it "should return the document lines", ->
  23. @returnedDoc.lines.should.deep.equal @lines
  24. it "should return the document at its current version", ->
  25. @returnedDoc.version.should.equal @version
  26. describe "when the document is already loaded", ->
  27. before (done) ->
  28. [@project_id, @doc_id] = [DocUpdaterClient.randomId(), DocUpdaterClient.randomId()]
  29. MockWebApi.insertDoc @project_id, @doc_id, {
  30. lines: @lines = ["one", "two", "three"]
  31. }
  32. DocUpdaterClient.preloadDoc @project_id, @doc_id, (error) =>
  33. throw error if error?
  34. sinon.spy MockWebApi, "getDocument"
  35. DocUpdaterClient.getDoc @project_id, @doc_id, (error, res, @returnedDoc) => done()
  36. after ->
  37. MockWebApi.getDocument.restore()
  38. it "should not load the document from the web API", ->
  39. MockWebApi.getDocument.called.should.equal false
  40. it "should return the document lines", ->
  41. @returnedDoc.lines.should.deep.equal @lines
  42. describe "when the request asks for some recent ops", ->
  43. before (done) ->
  44. [@project_id, @doc_id] = [DocUpdaterClient.randomId(), DocUpdaterClient.randomId()]
  45. MockWebApi.insertDoc @project_id, @doc_id, {
  46. lines: @lines = ["one", "two", "three"]
  47. version: 0
  48. }
  49. @updates = for v in [0..99]
  50. doc_id: @doc_id,
  51. op: [i: v.toString(), p: 0]
  52. v: v
  53. DocUpdaterClient.sendUpdates @project_id, @doc_id, @updates, (error) =>
  54. throw error if error?
  55. sinon.spy MockWebApi, "getDocument"
  56. DocUpdaterClient.getDocAndRecentOps @project_id, @doc_id, 90, (error, res, @returnedDoc) => done()
  57. after ->
  58. MockWebApi.getDocument.restore()
  59. it "should return the recent ops", ->
  60. @returnedDoc.ops.length.should.equal 10
  61. for update, i in @updates.slice(90, -1)
  62. @returnedDoc.ops[i].op.should.deep.equal update.op
  63. describe "when the document does not exist", ->
  64. before (done) ->
  65. [@project_id, @doc_id] = [DocUpdaterClient.randomId(), DocUpdaterClient.randomId()]
  66. DocUpdaterClient.getDoc @project_id, @doc_id, (error, res, doc) =>
  67. @statusCode = res.statusCode
  68. done()
  69. it "should return 404", ->
  70. @statusCode.should.equal 404
  71. describe "when the web api returns an error", ->
  72. before (done) ->
  73. [@project_id, @doc_id] = [DocUpdaterClient.randomId(), DocUpdaterClient.randomId()]
  74. sinon.stub MockWebApi, "getDocument", (project_id, doc_id, callback = (error, doc) ->) ->
  75. callback new Error("oops")
  76. DocUpdaterClient.getDoc @project_id, @doc_id, (error, res, doc) =>
  77. @statusCode = res.statusCode
  78. done()
  79. after ->
  80. MockWebApi.getDocument.restore()
  81. it "should return 500", ->
  82. @statusCode.should.equal 500