FlushingAProjectTests.coffee 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. sinon = require "sinon"
  2. chai = require("chai")
  3. chai.should()
  4. async = require "async"
  5. MockWebApi = require "./helpers/MockWebApi"
  6. DocUpdaterClient = require "./helpers/DocUpdaterClient"
  7. DocUpdaterApp = require "./helpers/DocUpdaterApp"
  8. describe "Flushing a project", ->
  9. before (done) ->
  10. @project_id = DocUpdaterClient.randomId()
  11. @docs = [{
  12. id: doc_id0 = DocUpdaterClient.randomId()
  13. lines: ["one", "two", "three"]
  14. update:
  15. doc: doc_id0
  16. op: [{
  17. i: "one and a half\n"
  18. p: 4
  19. }]
  20. v: 0
  21. updatedLines: ["one", "one and a half", "two", "three"]
  22. }, {
  23. id: doc_id1 = DocUpdaterClient.randomId()
  24. lines: ["four", "five", "six"]
  25. update:
  26. doc: doc_id1
  27. op: [{
  28. i: "four and a half\n"
  29. p: 5
  30. }]
  31. v: 0
  32. updatedLines: ["four", "four and a half", "five", "six"]
  33. }]
  34. for doc in @docs
  35. MockWebApi.insertDoc @project_id, doc.id, {
  36. lines: doc.lines
  37. version: doc.update.v
  38. }
  39. DocUpdaterApp.ensureRunning(done)
  40. describe "with documents which have been updated", ->
  41. before (done) ->
  42. sinon.spy MockWebApi, "setDocument"
  43. async.series @docs.map((doc) =>
  44. (callback) =>
  45. DocUpdaterClient.preloadDoc @project_id, doc.id, (error) =>
  46. return callback(error) if error?
  47. DocUpdaterClient.sendUpdate @project_id, doc.id, doc.update, (error) =>
  48. callback(error)
  49. ), (error) =>
  50. throw error if error?
  51. setTimeout () =>
  52. DocUpdaterClient.flushProject @project_id, (error, res, body) =>
  53. @statusCode = res.statusCode
  54. done()
  55. , 200
  56. after ->
  57. MockWebApi.setDocument.restore()
  58. it "should return a 204 status code", ->
  59. @statusCode.should.equal 204
  60. it "should send each document to the web api", ->
  61. for doc in @docs
  62. MockWebApi.setDocument
  63. .calledWith(@project_id, doc.id, doc.updatedLines)
  64. .should.equal true
  65. it "should update the lines in the doc updater", (done) ->
  66. async.series @docs.map((doc) =>
  67. (callback) =>
  68. DocUpdaterClient.getDoc @project_id, doc.id, (error, res, returnedDoc) =>
  69. returnedDoc.lines.should.deep.equal doc.updatedLines
  70. callback()
  71. ), done