DeletingADocumentTests.coffee 3.4 KB

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