WriteOpsTests.coffee 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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. op =
  27. op: @opData.op
  28. meta: @opData.meta
  29. @DocOpsManager.pushDocOp
  30. .calledWith(@project_id, @doc_id, op)
  31. .should.equal true
  32. it "should call the callback without an error", ->
  33. @callback.called.should.equal true
  34. (@callback.args[0][0]?).should.equal false
  35. describe "writing an op at the wrong version", ->
  36. beforeEach ->
  37. @version = 42
  38. @mismatch = 5
  39. @opData.v = @version
  40. @DocOpsManager.pushDocOp = sinon.stub().callsArgWith(3, null, @version + @mismatch)
  41. @ShareJsDB.writeOp @doc_key, @opData, @callback
  42. it "should call the callback with an error", ->
  43. @callback.calledWith(sinon.match.string).should.equal true