SettingADocumentTests.coffee 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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 "Setting a document", ->
  8. before (done) ->
  9. [@project_id, @doc_id] = [DocUpdaterClient.randomId(), DocUpdaterClient.randomId()]
  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. @newLines = ["these", "are", "the", "new", "lines"]
  21. @source = "dropbox"
  22. @user_id = "user-id-123"
  23. MockWebApi.insertDoc @project_id, @doc_id, lines: @lines
  24. db.docOps.insert {
  25. doc_id: ObjectId(@doc_id)
  26. version: @version
  27. }, (error) =>
  28. throw error if error?
  29. done()
  30. describe "when the updated doc exists in the doc updater", ->
  31. before (done) ->
  32. sinon.spy MockWebApi, "setDocumentLines"
  33. DocUpdaterClient.preloadDoc @project_id, @doc_id, (error) =>
  34. throw error if error?
  35. DocUpdaterClient.sendUpdate @project_id, @doc_id, @update, (error) =>
  36. throw error if error?
  37. setTimeout () =>
  38. DocUpdaterClient.setDocLines @project_id, @doc_id, @newLines, @source, @user_id, (error, res, body) =>
  39. @statusCode = res.statusCode
  40. done()
  41. , 200
  42. after ->
  43. MockWebApi.setDocumentLines.restore()
  44. it "should return a 204 status code", ->
  45. @statusCode.should.equal 204
  46. it "should send the updated doc lines to the web api", ->
  47. MockWebApi.setDocumentLines
  48. .calledWith(@project_id, @doc_id, @newLines)
  49. .should.equal true
  50. it "should update the lines in the doc updater", (done) ->
  51. DocUpdaterClient.getDoc @project_id, @doc_id, (error, res, doc) =>
  52. doc.lines.should.deep.equal @newLines
  53. done()
  54. it "should bump the version in the doc updater", (done) ->
  55. DocUpdaterClient.getDoc @project_id, @doc_id, (error, res, doc) =>
  56. doc.version.should.equal @version + 2
  57. done()