flushDocIfLoadedTests.coffee 2.2 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 "DocumentManager.flushDocIfLoaded", ->
  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. @callback = sinon.stub()
  22. describe "when the doc is in Redis", ->
  23. beforeEach ->
  24. @RedisManager.getDoc = sinon.stub().callsArgWith(1, null, @lines, @version)
  25. @PersistenceManager.setDoc = sinon.stub().callsArgWith(4)
  26. @DocumentManager.flushDocIfLoaded @project_id, @doc_id, @callback
  27. it "should get the doc from redis", ->
  28. @RedisManager.getDoc
  29. .calledWith(@doc_id)
  30. .should.equal true
  31. it "should write the doc lines to the persistence layer", ->
  32. @PersistenceManager.setDoc
  33. .calledWith(@project_id, @doc_id, @lines, @version)
  34. .should.equal true
  35. it "should call the callback without error", ->
  36. @callback.calledWith(null).should.equal true
  37. it "should time the execution", ->
  38. @Metrics.Timer::done.called.should.equal true
  39. describe "when the document is not in Redis", ->
  40. beforeEach ->
  41. @RedisManager.getDoc = sinon.stub().callsArgWith(1, null, null, null)
  42. @PersistenceManager.setDoc = sinon.stub().callsArgWith(4)
  43. @DocOpsManager.flushDocOpsToMongo = sinon.stub().callsArgWith(2)
  44. @DocumentManager.flushDocIfLoaded @project_id, @doc_id, @callback
  45. it "should get the doc from redis", ->
  46. @RedisManager.getDoc
  47. .calledWith(@doc_id)
  48. .should.equal true
  49. it "should not write anything to the persistence layer", ->
  50. @PersistenceManager.setDoc.called.should.equal false
  51. @DocOpsManager.flushDocOpsToMongo.called.should.equal false
  52. it "should call the callback without error", ->
  53. @callback.calledWith(null).should.equal true
  54. it "should time the execution", ->
  55. @Metrics.Timer::done.called.should.equal true