getDocTests.coffee 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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.getDoc", ->
  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 exists in Redis", ->
  23. beforeEach ->
  24. @RedisManager.getDoc = sinon.stub().callsArgWith(1, null, @lines, @version)
  25. @DocumentManager.getDoc @project_id, @doc_id, @callback
  26. it "should get the doc from Redis", ->
  27. @RedisManager.getDoc
  28. .calledWith(@doc_id)
  29. .should.equal true
  30. it "should call the callback with the doc info", ->
  31. @callback.calledWith(null, @lines, @version, true).should.equal true
  32. it "should time the execution", ->
  33. @Metrics.Timer::done.called.should.equal true
  34. describe "when the doc does not exist in Redis", ->
  35. beforeEach ->
  36. @RedisManager.getDoc = sinon.stub().callsArgWith(1, null, null, null)
  37. @PersistenceManager.getDoc = sinon.stub().callsArgWith(2, null, @lines, @version)
  38. @RedisManager.putDocInMemory = sinon.stub().callsArg(4)
  39. @DocumentManager.getDoc @project_id, @doc_id, @callback
  40. it "should try to get the doc from Redis", ->
  41. @RedisManager.getDoc
  42. .calledWith(@doc_id)
  43. .should.equal true
  44. it "should get the doc from the PersistenceManager", ->
  45. @PersistenceManager.getDoc
  46. .calledWith(@project_id, @doc_id)
  47. .should.equal true
  48. it "should set the doc in Redis", ->
  49. @RedisManager.putDocInMemory
  50. .calledWith(@project_id, @doc_id, @lines, @version)
  51. .should.equal true
  52. it "should call the callback with the doc info", ->
  53. @callback.calledWith(null, @lines, @version, false).should.equal true
  54. it "should time the execution", ->
  55. @Metrics.Timer::done.called.should.equal true