setDocTests.coffee 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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 - setDoc", ->
  7. beforeEach ->
  8. @DocumentManager = SandboxedModule.require modulePath, requires:
  9. "./RedisManager": @RedisManager = {}
  10. "./PersistenceManager": @PersistenceManager = {}
  11. "./DiffCodec": @DiffCodec = {}
  12. "./DocOpsManager":{}
  13. "./UpdateManager": @UpdateManager = {}
  14. "logger-sharelatex": @logger = {log: sinon.stub()}
  15. "./Metrics": @Metrics =
  16. Timer: class Timer
  17. done: sinon.stub()
  18. @project_id = "project-id-123"
  19. @doc_id = "doc-id-123"
  20. @version = 42
  21. @ops = ["mock-ops"]
  22. @callback = sinon.stub()
  23. describe "with plain tex lines", ->
  24. beforeEach ->
  25. @beforeLines = ["before", "lines"]
  26. @afterLines = ["after", "lines"]
  27. describe "successfully", ->
  28. beforeEach ->
  29. @DocumentManager.getDoc = sinon.stub().callsArgWith(2, null, @beforeLines, @version)
  30. @DiffCodec.diffAsShareJsOp = sinon.stub().callsArgWith(2, null, @ops)
  31. @UpdateManager.applyUpdates = sinon.stub().callsArgWith(3, null)
  32. @DocumentManager.flushDocIfLoaded = sinon.stub().callsArg(2)
  33. @DocumentManager.setDoc @project_id, @doc_id, @afterLines, @callback
  34. it "should get the current doc lines", ->
  35. @DocumentManager.getDoc
  36. .calledWith(@project_id, @doc_id)
  37. .should.equal true
  38. it "should return a diff of the old and new lines", ->
  39. @DiffCodec.diffAsShareJsOp
  40. .calledWith(@beforeLines, @afterLines)
  41. .should.equal true
  42. it "should apply the diff as a ShareJS op", ->
  43. @UpdateManager.applyUpdates
  44. .calledWith(@project_id, @doc_id, [doc: @doc_id, v: @version, op: @ops, meta: { type: "external" }])
  45. .should.equal true
  46. it "should flush the doc to Mongo", ->
  47. @DocumentManager.flushDocIfLoaded
  48. .calledWith(@project_id, @doc_id)
  49. .should.equal true
  50. it "should call the callback", ->
  51. @callback.calledWith(null).should.equal true
  52. it "should time the execution", ->
  53. @Metrics.Timer::done.called.should.equal true
  54. describe "with json lines", ->
  55. beforeEach ->
  56. @beforeLines = [text: "before", text: "lines"]
  57. @afterLines = ["after", "lines"]
  58. describe "successfully", ->
  59. beforeEach ->
  60. @DocumentManager.getDoc = sinon.stub().callsArgWith(2, null, @beforeLines, @version)
  61. @DiffCodec.diffAsShareJsOp = sinon.stub().callsArgWith(2, null, @ops)
  62. @UpdateManager.applyUpdates = sinon.stub().callsArgWith(3, null)
  63. @DocumentManager.flushDocIfLoaded = sinon.stub().callsArg(2)
  64. @DocumentManager.setDoc @project_id, @doc_id, @afterLines, @callback
  65. it "should get the current doc lines", ->
  66. @DocumentManager.getDoc
  67. .calledWith(@project_id, @doc_id)
  68. .should.equal true
  69. it "should return not try to get a diff", ->
  70. @DiffCodec.diffAsShareJsOp.called.should.equal false
  71. it "should call the callback", ->
  72. @callback.calledWith(null).should.equal true
  73. describe "without new lines", ->
  74. beforeEach ->
  75. @DocumentManager.getDoc = sinon.stub().callsArgWith(2, null, @beforeLines, @version)
  76. @DocumentManager.setDoc @project_id, @doc_id, null, @callback
  77. it "should return teh callback with an error", ->
  78. @callback.calledWith(new Error("No lines were passed to setDoc"))
  79. it "should not try to get the doc lines", ->
  80. @DocumentManager.getDoc.called.should.equal false