getDocAndRecentOpsTests.coffee 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. sinon = require('sinon')
  2. chai = require('chai')
  3. should = chai.should()
  4. modulePath = "../../../../app/js/DocumentManager.js"
  5. SandboxedModule = require('sandboxed-module')
  6. describe "DocumentUpdater - getDocAndRecentOps", ->
  7. beforeEach ->
  8. @DocumentManager = SandboxedModule.require modulePath, requires:
  9. "./RedisManager": @RedisManager = {}
  10. "./PersistenceManager": @PersistenceManager = {}
  11. "./DocOpsManager": @DocOpsManager = {}
  12. "logger-sharelatex": @logger = {log: sinon.stub()}
  13. "./Metrics": @Metrics =
  14. Timer: class Timer
  15. done: sinon.stub()
  16. @project_id = "project-id-123"
  17. @doc_id = "doc-id-123"
  18. @lines = ["one", "two", "three"]
  19. @version = 42
  20. @fromVersion = 40
  21. @ops = ["mock-op-1", "mock-op-2"]
  22. @callback = sinon.stub()
  23. describe "with a previous version specified", ->
  24. beforeEach ->
  25. @DocumentManager.getDoc = sinon.stub().callsArgWith(2, null, @lines, @version)
  26. @DocOpsManager.getPreviousDocOps = sinon.stub().callsArgWith(4, null, @ops)
  27. @DocumentManager.getDocAndRecentOps @project_id, @doc_id, @fromVersion, @callback
  28. it "should get the doc", ->
  29. @DocumentManager.getDoc
  30. .calledWith(@project_id, @doc_id)
  31. .should.equal true
  32. it "should get the doc ops", ->
  33. @DocOpsManager.getPreviousDocOps
  34. .calledWith(@project_id, @doc_id, @fromVersion, @version)
  35. .should.equal true
  36. it "should call the callback with the doc info", ->
  37. @callback.calledWith(null, @lines, @version, @ops).should.equal true
  38. it "should time the execution", ->
  39. @Metrics.Timer::done.called.should.equal true
  40. describe "with no previous version specified", ->
  41. beforeEach ->
  42. @DocumentManager.getDoc = sinon.stub().callsArgWith(2, null, @lines, @version)
  43. @DocOpsManager.getPreviousDocOps = sinon.stub().callsArgWith(4, null, @ops)
  44. @DocumentManager.getDocAndRecentOps @project_id, @doc_id, -1, @callback
  45. it "should get the doc", ->
  46. @DocumentManager.getDoc
  47. .calledWith(@project_id, @doc_id)
  48. .should.equal true
  49. it "should not need to get the doc ops", ->
  50. @DocOpsManager.getPreviousDocOps.called.should.equal false
  51. it "should call the callback with the doc info", ->
  52. @callback.calledWith(null, @lines, @version, []).should.equal true
  53. it "should time the execution", ->
  54. @Metrics.Timer::done.called.should.equal true