2014-02-12 10:40:42 +00:00
|
|
|
sinon = require "sinon"
|
|
|
|
chai = require("chai")
|
|
|
|
chai.should()
|
|
|
|
async = require "async"
|
2014-05-14 15:04:25 +00:00
|
|
|
{db, ObjectId} = require "../../../app/js/mongojs"
|
2014-02-12 10:40:42 +00:00
|
|
|
|
|
|
|
MockWebApi = require "./helpers/MockWebApi"
|
|
|
|
DocUpdaterClient = require "./helpers/DocUpdaterClient"
|
|
|
|
|
|
|
|
describe "Flushing a doc to Mongo", ->
|
|
|
|
before ->
|
|
|
|
@lines = ["one", "two", "three"]
|
2014-02-10 15:17:08 +00:00
|
|
|
@version = 42
|
2014-02-12 10:40:42 +00:00
|
|
|
@update =
|
|
|
|
doc: @doc_id
|
|
|
|
op: [{
|
|
|
|
i: "one and a half\n"
|
|
|
|
p: 4
|
|
|
|
}]
|
2014-02-10 15:17:08 +00:00
|
|
|
v: @version
|
2014-02-12 10:40:42 +00:00
|
|
|
@result = ["one", "one and a half", "two", "three"]
|
|
|
|
|
|
|
|
describe "when the updated doc exists in the doc updater", ->
|
|
|
|
before (done) ->
|
|
|
|
[@project_id, @doc_id] = [DocUpdaterClient.randomId(), DocUpdaterClient.randomId()]
|
|
|
|
sinon.spy MockWebApi, "setDocumentLines"
|
|
|
|
|
2014-05-15 10:13:16 +00:00
|
|
|
MockWebApi.insertDoc @project_id, @doc_id, lines: @lines
|
|
|
|
db.docOps.insert {
|
|
|
|
doc_id: ObjectId(@doc_id)
|
|
|
|
version: @version
|
|
|
|
}, (error) =>
|
2014-02-12 10:40:42 +00:00
|
|
|
throw error if error?
|
2014-05-15 10:13:16 +00:00
|
|
|
DocUpdaterClient.sendUpdates @project_id, @doc_id, [@update], (error) =>
|
|
|
|
throw error if error?
|
|
|
|
setTimeout () =>
|
|
|
|
DocUpdaterClient.flushDoc @project_id, @doc_id, done
|
|
|
|
, 200
|
2014-02-12 10:40:42 +00:00
|
|
|
|
|
|
|
after ->
|
|
|
|
MockWebApi.setDocumentLines.restore()
|
|
|
|
|
2014-02-10 15:17:08 +00:00
|
|
|
it "should flush the updated doc lines to the web api", ->
|
2014-02-12 10:40:42 +00:00
|
|
|
MockWebApi.setDocumentLines
|
|
|
|
.calledWith(@project_id, @doc_id, @result)
|
|
|
|
.should.equal true
|
|
|
|
|
2014-05-14 15:04:25 +00:00
|
|
|
it "should store the updated doc version into mongo", (done) ->
|
|
|
|
db.docOps.find {
|
|
|
|
doc_id: ObjectId(@doc_id)
|
|
|
|
}, {
|
|
|
|
version: 1
|
|
|
|
}, (error, docs) =>
|
|
|
|
doc = docs[0]
|
|
|
|
doc.version.should.equal @version + 1
|
|
|
|
done()
|
|
|
|
|
|
|
|
|
2014-02-12 10:40:42 +00: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
|
|
|
|
}
|
|
|
|
sinon.spy MockWebApi, "setDocumentLines"
|
|
|
|
DocUpdaterClient.flushDoc @project_id, @doc_id, done
|
|
|
|
|
|
|
|
after ->
|
|
|
|
MockWebApi.setDocumentLines.restore()
|
|
|
|
|
|
|
|
it "should not flush the doc to the web api", ->
|
|
|
|
MockWebApi.setDocumentLines.called.should.equal false
|
|
|
|
|