WriteOpsTests.coffee 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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. describe "writing an op", ->
  20. beforeEach ->
  21. @version = 42
  22. @opData.v = @version
  23. @DocOpsManager.pushDocOp = sinon.stub().callsArgWith(3, null, @version+1)
  24. @ShareJsDB.writeOp @doc_key, @opData, @callback
  25. it "should write the op to redis", ->
  26. @DocOpsManager.pushDocOp
  27. .calledWith(@project_id, @doc_id, @opData)
  28. .should.equal true
  29. it "should call the callback without an error", ->
  30. @callback.called.should.equal true
  31. (@callback.args[0][0]?).should.equal false
  32. describe "writing an op at the wrong version", ->
  33. beforeEach ->
  34. @version = 42
  35. @mismatch = 5
  36. @opData.v = @version
  37. @DocOpsManager.pushDocOp = sinon.stub().callsArgWith(3, null, @version + @mismatch)
  38. @ShareJsDB.writeOp @doc_key, @opData, @callback
  39. it "should call the callback with an error", ->
  40. @callback.calledWith(new Error()).should.equal true