2014-02-12 05:40:42 -05:00
|
|
|
sinon = require "sinon"
|
|
|
|
chai = require("chai")
|
|
|
|
chai.should()
|
|
|
|
async = require "async"
|
|
|
|
|
|
|
|
MockWebApi = require "./helpers/MockWebApi"
|
|
|
|
DocUpdaterClient = require "./helpers/DocUpdaterClient"
|
|
|
|
|
|
|
|
describe "Flushing a doc to Mongo", ->
|
|
|
|
before ->
|
|
|
|
@lines = ["one", "two", "three"]
|
2014-02-10 10:17:08 -05:00
|
|
|
@version = 42
|
2014-02-12 05:40:42 -05:00
|
|
|
@update =
|
|
|
|
doc: @doc_id
|
|
|
|
op: [{
|
|
|
|
i: "one and a half\n"
|
|
|
|
p: 4
|
|
|
|
}]
|
2014-02-10 10:17:08 -05:00
|
|
|
v: @version
|
2014-02-12 05:40:42 -05:00
|
|
|
@result = ["one", "one and a half", "two", "three"]
|
|
|
|
MockWebApi.insertDoc @project_id, @doc_id, {
|
|
|
|
lines: @lines
|
2014-02-10 10:17:08 -05:00
|
|
|
version: @version
|
2014-02-12 05:40:42 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
describe "when the updated doc exists in the doc updater", ->
|
|
|
|
before (done) ->
|
|
|
|
[@project_id, @doc_id] = [DocUpdaterClient.randomId(), DocUpdaterClient.randomId()]
|
|
|
|
MockWebApi.insertDoc @project_id, @doc_id, {
|
|
|
|
lines: @lines
|
2014-02-10 10:17:08 -05:00
|
|
|
version: @version
|
2014-02-12 05:40:42 -05:00
|
|
|
}
|
|
|
|
sinon.spy MockWebApi, "setDocumentLines"
|
2014-02-10 10:17:08 -05:00
|
|
|
sinon.spy MockWebApi, "setDocumentVersion"
|
2014-02-12 05:40:42 -05:00
|
|
|
|
|
|
|
DocUpdaterClient.sendUpdates @project_id, @doc_id, [@update], (error) =>
|
|
|
|
throw error if error?
|
|
|
|
setTimeout () =>
|
|
|
|
DocUpdaterClient.flushDoc @project_id, @doc_id, done
|
|
|
|
, 200
|
|
|
|
|
|
|
|
after ->
|
|
|
|
MockWebApi.setDocumentLines.restore()
|
2014-02-10 10:17:08 -05:00
|
|
|
MockWebApi.setDocumentVersion.restore()
|
2014-02-12 05:40:42 -05:00
|
|
|
|
2014-02-10 10:17:08 -05:00
|
|
|
it "should flush the updated doc lines to the web api", ->
|
2014-02-12 05:40:42 -05:00
|
|
|
MockWebApi.setDocumentLines
|
|
|
|
.calledWith(@project_id, @doc_id, @result)
|
|
|
|
.should.equal true
|
|
|
|
|
2014-02-10 10:17:08 -05:00
|
|
|
it "should flush the updated doc version to the web api", ->
|
|
|
|
MockWebApi.setDocumentVersion
|
|
|
|
.calledWith(@project_id, @doc_id, @version + 1)
|
|
|
|
.should.equal true
|
2014-02-12 05:40:42 -05:00
|
|
|
|
|
|
|
describe "when the doc does not exist in the doc updater", ->
|
|
|
|
before (done) ->
|
|
|
|
[@project_id, @doc_id] = [DocUpdaterClient.randomId(), DocUpdaterClient.randomId()]
|
|
|
|
MockWebApi.insertDoc @project_id, @doc_id, {
|
|
|
|
lines: @lines
|
2014-02-10 10:17:08 -05:00
|
|
|
version: @version
|
2014-02-12 05:40:42 -05:00
|
|
|
}
|
|
|
|
sinon.spy MockWebApi, "setDocumentLines"
|
2014-02-10 10:17:08 -05:00
|
|
|
sinon.spy MockWebApi, "setDocumentVersion"
|
2014-02-12 05:40:42 -05:00
|
|
|
DocUpdaterClient.flushDoc @project_id, @doc_id, done
|
|
|
|
|
|
|
|
after ->
|
|
|
|
MockWebApi.setDocumentLines.restore()
|
2014-02-10 10:17:08 -05:00
|
|
|
MockWebApi.setDocumentVersion.restore()
|
2014-02-12 05:40:42 -05:00
|
|
|
|
|
|
|
it "should not flush the doc to the web api", ->
|
|
|
|
MockWebApi.setDocumentLines.called.should.equal false
|
2014-02-10 10:17:08 -05:00
|
|
|
MockWebApi.setDocumentVersion.called.should.equal false
|
|
|
|
|
2014-02-12 05:40:42 -05:00
|
|
|
|