getDocAndRecentOpsTests.coffee 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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. "./TrackChangesManager": {}
  17. @project_id = "project-id-123"
  18. @doc_id = "doc-id-123"
  19. @lines = ["one", "two", "three"]
  20. @version = 42
  21. @fromVersion = 40
  22. @ops = ["mock-op-1", "mock-op-2"]
  23. @callback = sinon.stub()
  24. describe "with a previous version specified", ->
  25. beforeEach ->
  26. @DocumentManager.getDoc = sinon.stub().callsArgWith(2, null, @lines, @version)
  27. @DocOpsManager.getPreviousDocOps = sinon.stub().callsArgWith(4, null, @ops)
  28. @DocumentManager.getDocAndRecentOps @project_id, @doc_id, @fromVersion, @callback
  29. it "should get the doc", ->
  30. @DocumentManager.getDoc
  31. .calledWith(@project_id, @doc_id)
  32. .should.equal true
  33. it "should get the doc ops", ->
  34. @DocOpsManager.getPreviousDocOps
  35. .calledWith(@project_id, @doc_id, @fromVersion, @version)
  36. .should.equal true
  37. it "should call the callback with the doc info", ->
  38. @callback.calledWith(null, @lines, @version, @ops).should.equal true
  39. it "should time the execution", ->
  40. @Metrics.Timer::done.called.should.equal true
  41. describe "with no previous version specified", ->
  42. beforeEach ->
  43. @DocumentManager.getDoc = sinon.stub().callsArgWith(2, null, @lines, @version)
  44. @DocOpsManager.getPreviousDocOps = sinon.stub().callsArgWith(4, null, @ops)
  45. @DocumentManager.getDocAndRecentOps @project_id, @doc_id, -1, @callback
  46. it "should get the doc", ->
  47. @DocumentManager.getDoc
  48. .calledWith(@project_id, @doc_id)
  49. .should.equal true
  50. it "should not need to get the doc ops", ->
  51. @DocOpsManager.getPreviousDocOps.called.should.equal false
  52. it "should call the callback with the doc info", ->
  53. @callback.calledWith(null, @lines, @version, []).should.equal true
  54. it "should time the execution", ->
  55. @Metrics.Timer::done.called.should.equal true