DeletingADocumentTests.coffee 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. sinon = require "sinon"
  2. chai = require("chai")
  3. chai.should()
  4. MockTrackChangesApi = require "./helpers/MockTrackChangesApi"
  5. MockWebApi = require "./helpers/MockWebApi"
  6. DocUpdaterClient = require "./helpers/DocUpdaterClient"
  7. describe "Deleting a document", ->
  8. before ->
  9. @lines = ["one", "two", "three"]
  10. @version = 42
  11. @update =
  12. doc: @doc_id
  13. op: [{
  14. i: "one and a half\n"
  15. p: 4
  16. }]
  17. v: @version
  18. @result = ["one", "one and a half", "two", "three"]
  19. sinon.spy MockTrackChangesApi, "flushDoc"
  20. after ->
  21. MockTrackChangesApi.flushDoc.restore()
  22. describe "when the updated doc exists in the doc updater", ->
  23. before (done) ->
  24. [@project_id, @doc_id] = [DocUpdaterClient.randomId(), DocUpdaterClient.randomId()]
  25. sinon.spy MockWebApi, "setDocument"
  26. sinon.spy MockWebApi, "getDocument"
  27. MockWebApi.insertDoc @project_id, @doc_id, {lines: @lines, version: @version}
  28. DocUpdaterClient.preloadDoc @project_id, @doc_id, (error) =>
  29. throw error if error?
  30. DocUpdaterClient.sendUpdate @project_id, @doc_id, @update, (error) =>
  31. throw error if error?
  32. setTimeout () =>
  33. DocUpdaterClient.deleteDoc @project_id, @doc_id, (error, res, body) =>
  34. @statusCode = res.statusCode
  35. setTimeout done, 200
  36. , 200
  37. after ->
  38. MockWebApi.setDocument.restore()
  39. MockWebApi.getDocument.restore()
  40. it "should return a 204 status code", ->
  41. @statusCode.should.equal 204
  42. it "should send the updated document and version to the web api", ->
  43. MockWebApi.setDocument
  44. .calledWith(@project_id, @doc_id, @result, @version + 1)
  45. .should.equal true
  46. it "should need to reload the doc if read again", (done) ->
  47. MockWebApi.getDocument.called.should.equal.false
  48. DocUpdaterClient.getDoc @project_id, @doc_id, (error, res, doc) =>
  49. MockWebApi.getDocument
  50. .calledWith(@project_id, @doc_id)
  51. .should.equal true
  52. done()
  53. it "should flush track changes", ->
  54. MockTrackChangesApi.flushDoc.calledWith(@doc_id).should.equal true
  55. describe "when the doc is not in the doc updater", ->
  56. before (done) ->
  57. [@project_id, @doc_id] = [DocUpdaterClient.randomId(), DocUpdaterClient.randomId()]
  58. MockWebApi.insertDoc @project_id, @doc_id, {
  59. lines: @lines
  60. }
  61. sinon.spy MockWebApi, "setDocument"
  62. sinon.spy MockWebApi, "getDocument"
  63. DocUpdaterClient.deleteDoc @project_id, @doc_id, (error, res, body) =>
  64. @statusCode = res.statusCode
  65. setTimeout done, 200
  66. after ->
  67. MockWebApi.setDocument.restore()
  68. MockWebApi.getDocument.restore()
  69. it "should return a 204 status code", ->
  70. @statusCode.should.equal 204
  71. it "should not need to send the updated document to the web api", ->
  72. MockWebApi.setDocument.called.should.equal false
  73. it "should need to reload the doc if read again", (done) ->
  74. MockWebApi.getDocument.called.should.equal.false
  75. DocUpdaterClient.getDoc @project_id, @doc_id, (error, res, doc) =>
  76. MockWebApi.getDocument
  77. .calledWith(@project_id, @doc_id)
  78. .should.equal true
  79. done()
  80. it "should flush track changes", ->
  81. MockTrackChangesApi.flushDoc.calledWith(@doc_id).should.equal true