DeletingADocumentTests.coffee 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. sinon = require "sinon"
  2. chai = require("chai")
  3. chai.should()
  4. {db, ObjectId} = require "../../../app/js/mongojs"
  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. describe "when the updated doc exists in the doc updater", ->
  20. before (done) ->
  21. [@project_id, @doc_id] = [DocUpdaterClient.randomId(), DocUpdaterClient.randomId()]
  22. sinon.spy MockWebApi, "setDocumentLines"
  23. sinon.spy MockWebApi, "getDocument"
  24. MockWebApi.insertDoc @project_id, @doc_id, lines: @lines
  25. db.docOps.insert {
  26. doc_id: ObjectId(@doc_id)
  27. version: @version
  28. }, (error) =>
  29. throw error if error?
  30. DocUpdaterClient.preloadDoc @project_id, @doc_id, (error) =>
  31. throw error if error?
  32. DocUpdaterClient.sendUpdate @project_id, @doc_id, @update, (error) =>
  33. throw error if error?
  34. setTimeout () =>
  35. DocUpdaterClient.deleteDoc @project_id, @doc_id, (error, res, body) =>
  36. @statusCode = res.statusCode
  37. done()
  38. , 200
  39. after ->
  40. MockWebApi.setDocumentLines.restore()
  41. MockWebApi.getDocument.restore()
  42. it "should return a 204 status code", ->
  43. @statusCode.should.equal 204
  44. it "should send the updated document to the web api", ->
  45. MockWebApi.setDocumentLines
  46. .calledWith(@project_id, @doc_id, @result)
  47. .should.equal true
  48. it "should write the version to mongo", (done) ->
  49. db.docOps.find {
  50. doc_id: ObjectId(@doc_id)
  51. }, {
  52. version: 1
  53. }, (error, docs) =>
  54. doc = docs[0]
  55. doc.version.should.equal @version + 1
  56. done()
  57. it "should need to reload the doc if read again", (done) ->
  58. MockWebApi.getDocument.called.should.equal.false
  59. DocUpdaterClient.getDoc @project_id, @doc_id, (error, res, doc) =>
  60. MockWebApi.getDocument
  61. .calledWith(@project_id, @doc_id)
  62. .should.equal true
  63. done()
  64. describe "when the doc is not in the doc updater", ->
  65. before (done) ->
  66. [@project_id, @doc_id] = [DocUpdaterClient.randomId(), DocUpdaterClient.randomId()]
  67. MockWebApi.insertDoc @project_id, @doc_id, {
  68. lines: @lines
  69. }
  70. sinon.spy MockWebApi, "setDocumentLines"
  71. sinon.spy MockWebApi, "getDocument"
  72. DocUpdaterClient.deleteDoc @project_id, @doc_id, (error, res, body) =>
  73. @statusCode = res.statusCode
  74. done()
  75. after ->
  76. MockWebApi.setDocumentLines.restore()
  77. MockWebApi.getDocument.restore()
  78. it "should return a 204 status code", ->
  79. @statusCode.should.equal 204
  80. it "should not need to send the updated document to the web api", ->
  81. MockWebApi.setDocumentLines.called.should.equal false
  82. it "should need to reload the doc if read again", (done) ->
  83. MockWebApi.getDocument.called.should.equal.false
  84. DocUpdaterClient.getDoc @project_id, @doc_id, (error, res, doc) =>
  85. MockWebApi.getDocument
  86. .calledWith(@project_id, @doc_id)
  87. .should.equal true
  88. done()