FlushingAProjectTests.coffee 2.0 KB

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