overleaf/services/document-updater/test/unit/coffee/DocumentManager/setDocTests.coffee

108 lines
3.1 KiB
CoffeeScript
Raw Normal View History

2014-02-12 05:40:42 -05:00
sinon = require('sinon')
chai = require('chai')
should = chai.should()
modulePath = "../../../../app/js/DocumentManager.js"
SandboxedModule = require('sandboxed-module')
describe "DocumentManager.setDoc", ->
2014-02-12 05:40:42 -05:00
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()
"./TrackChangesManager": {}
2014-02-12 05:40:42 -05:00
@project_id = "project-id-123"
@doc_id = "doc-id-123"
@version = 42
@ops = ["mock-ops"]
@callback = sinon.stub()
@source = "dropbox"
@user_id = "mock-user-id"
2014-02-12 05:40:42 -05:00
describe "with plain tex lines", ->
beforeEach ->
@beforeLines = ["before", "lines"]
@afterLines = ["after", "lines"]
@DocumentManager.getDoc = sinon.stub().callsArgWith(2, null, @beforeLines, @version, true)
@DiffCodec.diffAsShareJsOp = sinon.stub().callsArgWith(2, null, @ops)
@UpdateManager.applyUpdates = sinon.stub().callsArgWith(3, null)
@DocumentManager.flushDocIfLoaded = sinon.stub().callsArg(2)
@DocumentManager.flushAndDeleteDoc = sinon.stub().callsArg(2)
2014-02-12 05:40:42 -05:00
describe "when already loaded", ->
2014-02-12 05:40:42 -05:00
beforeEach ->
@DocumentManager.setDoc @project_id, @doc_id, @afterLines, @source, @user_id, @callback
2014-02-12 05:40:42 -05:00
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"
source: @source
user_id: @user_id
}
]
)
2014-02-12 05:40:42 -05:00
.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 "when not already loaded", ->
beforeEach ->
@DocumentManager.getDoc = sinon.stub().callsArgWith(2, null, @beforeLines, @version, false)
@DocumentManager.setDoc @project_id, @doc_id, @afterLines, @source, @user_id, @callback
2014-02-12 05:40:42 -05:00
it "should flush and delete the doc from the doc updater", ->
@DocumentManager.flushAndDeleteDoc
.calledWith(@project_id, @doc_id)
.should.equal true
2014-02-12 05:40:42 -05:00
describe "without new lines", ->
beforeEach ->
@DocumentManager.setDoc @project_id, @doc_id, null, @source, @user_id, @callback
it "should return the 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
2014-02-12 05:40:42 -05:00