SettingADocumentTests.coffee 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. sinon = require "sinon"
  2. chai = require("chai")
  3. chai.should()
  4. MockWebApi = require "./helpers/MockWebApi"
  5. DocUpdaterClient = require "./helpers/DocUpdaterClient"
  6. describe "Setting a document", ->
  7. before ->
  8. [@project_id, @doc_id] = [DocUpdaterClient.randomId(), DocUpdaterClient.randomId()]
  9. @lines = ["one", "two", "three"]
  10. @update =
  11. doc: @doc_id
  12. op: [{
  13. i: "one and a half\n"
  14. p: 4
  15. }]
  16. v: 0
  17. @result = ["one", "one and a half", "two", "three"]
  18. @newLines = ["these", "are", "the", "new", "lines"]
  19. MockWebApi.insertDoc @project_id, @doc_id, {
  20. lines: @lines
  21. }
  22. describe "when the updated doc exists in the doc updater", ->
  23. before (done) ->
  24. sinon.spy MockWebApi, "setDocumentLines"
  25. DocUpdaterClient.preloadDoc @project_id, @doc_id, (error) =>
  26. throw error if error?
  27. DocUpdaterClient.sendUpdate @project_id, @doc_id, @update, (error) =>
  28. throw error if error?
  29. setTimeout () =>
  30. DocUpdaterClient.setDocLines @project_id, @doc_id, @newLines, (error, res, body) =>
  31. @statusCode = res.statusCode
  32. done()
  33. , 200
  34. after ->
  35. MockWebApi.setDocumentLines.restore()
  36. it "should return a 204 status code", ->
  37. @statusCode.should.equal 204
  38. it "should send the updated document to the web api", ->
  39. MockWebApi.setDocumentLines
  40. .calledWith(@project_id, @doc_id, @newLines)
  41. .should.equal true
  42. it "should update the lines in the doc updater", (done) ->
  43. DocUpdaterClient.getDoc @project_id, @doc_id, (error, res, doc) =>
  44. doc.lines.should.deep.equal @newLines
  45. done()
  46. it "should bump the version in the doc updater", (done) ->
  47. DocUpdaterClient.getDoc @project_id, @doc_id, (error, res, doc) =>
  48. doc.version.should.equal 2
  49. done()