2014-02-12 05:40:42 -05:00
|
|
|
sinon = require "sinon"
|
|
|
|
chai = require("chai")
|
|
|
|
chai.should()
|
2014-05-15 06:13:16 -04:00
|
|
|
{db, ObjectId} = require "../../../app/js/mongojs"
|
2014-02-12 05:40:42 -05:00
|
|
|
|
|
|
|
MockWebApi = require "./helpers/MockWebApi"
|
|
|
|
DocUpdaterClient = require "./helpers/DocUpdaterClient"
|
|
|
|
|
|
|
|
describe "Setting a document", ->
|
2014-05-15 06:13:16 -04:00
|
|
|
before (done) ->
|
2014-02-12 05:40:42 -05:00
|
|
|
[@project_id, @doc_id] = [DocUpdaterClient.randomId(), DocUpdaterClient.randomId()]
|
|
|
|
@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"]
|
|
|
|
@newLines = ["these", "are", "the", "new", "lines"]
|
2014-03-11 08:47:26 -04:00
|
|
|
@source = "dropbox"
|
|
|
|
@user_id = "user-id-123"
|
2014-05-15 06:13:16 -04:00
|
|
|
|
|
|
|
MockWebApi.insertDoc @project_id, @doc_id, lines: @lines
|
|
|
|
db.docOps.insert {
|
|
|
|
doc_id: ObjectId(@doc_id)
|
2014-02-10 10:17:08 -05:00
|
|
|
version: @version
|
2014-05-15 06:13:16 -04:00
|
|
|
}, (error) =>
|
|
|
|
throw error if error?
|
|
|
|
done()
|
|
|
|
|
2014-02-12 05:40:42 -05:00
|
|
|
|
|
|
|
describe "when the updated doc exists in the doc updater", ->
|
|
|
|
before (done) ->
|
|
|
|
sinon.spy MockWebApi, "setDocumentLines"
|
|
|
|
DocUpdaterClient.preloadDoc @project_id, @doc_id, (error) =>
|
|
|
|
throw error if error?
|
|
|
|
DocUpdaterClient.sendUpdate @project_id, @doc_id, @update, (error) =>
|
|
|
|
throw error if error?
|
|
|
|
setTimeout () =>
|
2014-03-11 08:47:26 -04:00
|
|
|
DocUpdaterClient.setDocLines @project_id, @doc_id, @newLines, @source, @user_id, (error, res, body) =>
|
2014-02-12 05:40:42 -05:00
|
|
|
@statusCode = res.statusCode
|
|
|
|
done()
|
|
|
|
, 200
|
|
|
|
|
|
|
|
after ->
|
|
|
|
MockWebApi.setDocumentLines.restore()
|
|
|
|
|
|
|
|
it "should return a 204 status code", ->
|
|
|
|
@statusCode.should.equal 204
|
|
|
|
|
2014-02-10 10:17:08 -05:00
|
|
|
it "should send the updated doc lines to the web api", ->
|
2014-02-12 05:40:42 -05:00
|
|
|
MockWebApi.setDocumentLines
|
|
|
|
.calledWith(@project_id, @doc_id, @newLines)
|
|
|
|
.should.equal true
|
|
|
|
|
|
|
|
it "should update the lines in the doc updater", (done) ->
|
|
|
|
DocUpdaterClient.getDoc @project_id, @doc_id, (error, res, doc) =>
|
|
|
|
doc.lines.should.deep.equal @newLines
|
|
|
|
done()
|
|
|
|
|
|
|
|
it "should bump the version in the doc updater", (done) ->
|
|
|
|
DocUpdaterClient.getDoc @project_id, @doc_id, (error, res, doc) =>
|
2014-02-10 10:17:08 -05:00
|
|
|
doc.version.should.equal @version + 2
|
2014-02-12 05:40:42 -05:00
|
|
|
done()
|
|
|
|
|