WriteOpsTests.coffee 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. sinon = require('sinon')
  2. chai = require('chai')
  3. should = chai.should()
  4. modulePath = "../../../../app/js/ShareJsDB.js"
  5. SandboxedModule = require('sandboxed-module')
  6. describe "ShareJsDB.writeOps", ->
  7. beforeEach ->
  8. @project_id = "project-id"
  9. @doc_id = "document-id"
  10. @doc_key = "#{@project_id}:#{@doc_id}"
  11. @callback = sinon.stub()
  12. @opData =
  13. op: {p: 20, t: "foo"}
  14. meta: {source: "bar"}
  15. @ShareJsDB = SandboxedModule.require modulePath, requires:
  16. "./RedisManager": @RedisManager = {}
  17. "./DocOpsManager": @DocOpsManager = {}
  18. "./DocumentManager": {}
  19. "logger-sharelatex": @logger = {error: sinon.stub()}
  20. describe "writing an op", ->
  21. beforeEach ->
  22. @version = 42
  23. @opData.v = @version
  24. @DocOpsManager.pushDocOp = sinon.stub().callsArgWith(3, null, @version+1)
  25. @ShareJsDB.writeOp @doc_key, @opData, @callback
  26. it "should write the op to redis", ->
  27. @DocOpsManager.pushDocOp
  28. .calledWith(@project_id, @doc_id, @opData)
  29. .should.equal true
  30. it "should call the callback without an error", ->
  31. @callback.called.should.equal true
  32. (@callback.args[0][0]?).should.equal false
  33. describe "writing an op at the wrong version", ->
  34. beforeEach ->
  35. @version = 42
  36. @mismatch = 5
  37. @opData.v = @version
  38. @DocOpsManager.pushDocOp = sinon.stub().callsArgWith(3, null, @version + @mismatch)
  39. @ShareJsDB.writeOp @doc_key, @opData, @callback
  40. it "should call the callback with an error", ->
  41. @callback.calledWith(new Error()).should.equal true