| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105 |
- sinon = require('sinon')
- chai = require('chai')
- should = chai.should()
- modulePath = "../../../../app/js/DocumentManager.js"
- SandboxedModule = require('sandboxed-module')
- describe "DocumentManager - setDoc", ->
- beforeEach ->
- @DocumentManager = SandboxedModule.require modulePath, requires:
- "./RedisManager": @RedisManager = {}
- "./PersistenceManager": @PersistenceManager = {}
- "./DiffCodec": @DiffCodec = {}
- "./DocOpsManager":{}
- "./UpdateManager": @UpdateManager = {}
- "logger-sharelatex": @logger = {log: sinon.stub()}
- "./Metrics": @Metrics =
- Timer: class Timer
- done: sinon.stub()
- @project_id = "project-id-123"
- @doc_id = "doc-id-123"
- @version = 42
- @ops = ["mock-ops"]
- @callback = sinon.stub()
- describe "with plain tex lines", ->
- beforeEach ->
- @beforeLines = ["before", "lines"]
- @afterLines = ["after", "lines"]
- describe "successfully", ->
- beforeEach ->
- @DocumentManager.getDoc = sinon.stub().callsArgWith(2, null, @beforeLines, @version)
- @DiffCodec.diffAsShareJsOp = sinon.stub().callsArgWith(2, null, @ops)
- @UpdateManager.applyUpdates = sinon.stub().callsArgWith(3, null)
- @DocumentManager.flushDocIfLoaded = sinon.stub().callsArg(2)
- @DocumentManager.setDoc @project_id, @doc_id, @afterLines, @callback
- it "should get the current doc lines", ->
- @DocumentManager.getDoc
- .calledWith(@project_id, @doc_id)
- .should.equal true
- it "should return a diff of the old and new lines", ->
- @DiffCodec.diffAsShareJsOp
- .calledWith(@beforeLines, @afterLines)
- .should.equal true
- it "should apply the diff as a ShareJS op", ->
- @UpdateManager.applyUpdates
- .calledWith(@project_id, @doc_id, [doc: @doc_id, v: @version, op: @ops, meta: { type: "external" }])
- .should.equal true
- it "should flush the doc to Mongo", ->
- @DocumentManager.flushDocIfLoaded
- .calledWith(@project_id, @doc_id)
- .should.equal true
-
- it "should call the callback", ->
- @callback.calledWith(null).should.equal true
- it "should time the execution", ->
- @Metrics.Timer::done.called.should.equal true
- describe "with json lines", ->
- beforeEach ->
- @beforeLines = [text: "before", text: "lines"]
- @afterLines = ["after", "lines"]
-
- describe "successfully", ->
- beforeEach ->
- @DocumentManager.getDoc = sinon.stub().callsArgWith(2, null, @beforeLines, @version)
- @DiffCodec.diffAsShareJsOp = sinon.stub().callsArgWith(2, null, @ops)
- @UpdateManager.applyUpdates = sinon.stub().callsArgWith(3, null)
- @DocumentManager.flushDocIfLoaded = sinon.stub().callsArg(2)
- @DocumentManager.setDoc @project_id, @doc_id, @afterLines, @callback
- it "should get the current doc lines", ->
- @DocumentManager.getDoc
- .calledWith(@project_id, @doc_id)
- .should.equal true
- it "should return not try to get a diff", ->
- @DiffCodec.diffAsShareJsOp.called.should.equal false
- it "should call the callback", ->
- @callback.calledWith(null).should.equal true
- describe "without new lines", ->
- beforeEach ->
- @DocumentManager.getDoc = sinon.stub().callsArgWith(2, null, @beforeLines, @version)
- @DocumentManager.setDoc @project_id, @doc_id, null, @callback
- it "should return teh callback with an error", ->
- @callback.calledWith(new Error("No lines were passed to setDoc"))
-
- it "should not try to get the doc lines", ->
- @DocumentManager.getDoc.called.should.equal false
-
-
-
|