DeletingADocumentTests.coffee 3.4 KB

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