2014-02-12 10:40:42 +00:00
|
|
|
sinon = require "sinon"
|
|
|
|
chai = require("chai")
|
|
|
|
chai.should()
|
|
|
|
|
2016-01-20 15:05:31 +00:00
|
|
|
MockTrackChangesApi = require "./helpers/MockTrackChangesApi"
|
2017-10-05 14:14:35 +00:00
|
|
|
MockProjectHistoryApi = require "./helpers/MockProjectHistoryApi"
|
2014-02-12 10:40:42 +00:00
|
|
|
MockWebApi = require "./helpers/MockWebApi"
|
|
|
|
DocUpdaterClient = require "./helpers/DocUpdaterClient"
|
2018-02-15 16:28:40 +00:00
|
|
|
DocUpdaterApp = require "./helpers/DocUpdaterApp"
|
2014-02-12 10:40:42 +00:00
|
|
|
|
|
|
|
describe "Deleting a document", ->
|
2018-02-15 16:28:40 +00:00
|
|
|
before (done) ->
|
2014-02-12 10:40:42 +00:00
|
|
|
@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"]
|
2017-10-05 14:14:35 +00:00
|
|
|
|
2016-01-20 15:05:31 +00:00
|
|
|
sinon.spy MockTrackChangesApi, "flushDoc"
|
2017-10-05 14:14:35 +00:00
|
|
|
sinon.spy MockProjectHistoryApi, "flushProject"
|
2018-02-15 16:28:40 +00:00
|
|
|
DocUpdaterApp.ensureRunning(done)
|
2017-10-05 14:14:35 +00:00
|
|
|
|
2016-01-20 15:05:31 +00:00
|
|
|
after ->
|
|
|
|
MockTrackChangesApi.flushDoc.restore()
|
2017-10-05 14:14:35 +00:00
|
|
|
MockProjectHistoryApi.flushProject.restore()
|
2014-02-12 10:40:42 +00:00
|
|
|
|
|
|
|
describe "when the updated doc exists in the doc updater", ->
|
|
|
|
before (done) ->
|
|
|
|
[@project_id, @doc_id] = [DocUpdaterClient.randomId(), DocUpdaterClient.randomId()]
|
2016-11-29 17:06:23 +00:00
|
|
|
sinon.spy MockWebApi, "setDocument"
|
2014-02-12 10:40:42 +00:00
|
|
|
sinon.spy MockWebApi, "getDocument"
|
2014-05-15 10:13:16 +00:00
|
|
|
|
2016-11-29 17:06:23 +00:00
|
|
|
MockWebApi.insertDoc @project_id, @doc_id, {lines: @lines, version: @version}
|
|
|
|
DocUpdaterClient.preloadDoc @project_id, @doc_id, (error) =>
|
2014-02-12 10:40:42 +00:00
|
|
|
throw error if error?
|
2016-11-29 17:06:23 +00:00
|
|
|
DocUpdaterClient.sendUpdate @project_id, @doc_id, @update, (error) =>
|
2014-02-12 10:40:42 +00:00
|
|
|
throw error if error?
|
2016-11-29 17:06:23 +00:00
|
|
|
setTimeout () =>
|
|
|
|
DocUpdaterClient.deleteDoc @project_id, @doc_id, (error, res, body) =>
|
|
|
|
@statusCode = res.statusCode
|
|
|
|
setTimeout done, 200
|
|
|
|
, 200
|
2014-02-12 10:40:42 +00:00
|
|
|
|
|
|
|
after ->
|
2016-11-29 17:06:23 +00:00
|
|
|
MockWebApi.setDocument.restore()
|
2014-02-12 10:40:42 +00:00
|
|
|
MockWebApi.getDocument.restore()
|
|
|
|
|
|
|
|
it "should return a 204 status code", ->
|
|
|
|
@statusCode.should.equal 204
|
|
|
|
|
2016-11-29 17:06:23 +00:00
|
|
|
it "should send the updated document and version to the web api", ->
|
|
|
|
MockWebApi.setDocument
|
|
|
|
.calledWith(@project_id, @doc_id, @result, @version + 1)
|
2014-02-12 10:40:42 +00:00
|
|
|
.should.equal true
|
|
|
|
|
|
|
|
it "should need to reload the doc if read again", (done) ->
|
|
|
|
MockWebApi.getDocument.called.should.equal.false
|
|
|
|
DocUpdaterClient.getDoc @project_id, @doc_id, (error, res, doc) =>
|
|
|
|
MockWebApi.getDocument
|
|
|
|
.calledWith(@project_id, @doc_id)
|
|
|
|
.should.equal true
|
|
|
|
done()
|
2017-10-05 14:14:35 +00:00
|
|
|
|
2016-01-20 15:05:31 +00:00
|
|
|
it "should flush track changes", ->
|
|
|
|
MockTrackChangesApi.flushDoc.calledWith(@doc_id).should.equal true
|
2014-02-12 10:40:42 +00:00
|
|
|
|
2017-10-05 14:14:35 +00:00
|
|
|
it "should flush project history", ->
|
|
|
|
MockProjectHistoryApi.flushProject.calledWith(@project_id).should.equal true
|
|
|
|
|
2014-02-12 10:40:42 +00:00
|
|
|
describe "when the doc is not in the doc updater", ->
|
|
|
|
before (done) ->
|
|
|
|
[@project_id, @doc_id] = [DocUpdaterClient.randomId(), DocUpdaterClient.randomId()]
|
|
|
|
MockWebApi.insertDoc @project_id, @doc_id, {
|
|
|
|
lines: @lines
|
|
|
|
}
|
2016-11-29 17:06:23 +00:00
|
|
|
sinon.spy MockWebApi, "setDocument"
|
2014-02-12 10:40:42 +00:00
|
|
|
sinon.spy MockWebApi, "getDocument"
|
|
|
|
DocUpdaterClient.deleteDoc @project_id, @doc_id, (error, res, body) =>
|
|
|
|
@statusCode = res.statusCode
|
2016-01-20 15:05:31 +00:00
|
|
|
setTimeout done, 200
|
2014-02-12 10:40:42 +00:00
|
|
|
|
|
|
|
after ->
|
2016-11-29 17:06:23 +00:00
|
|
|
MockWebApi.setDocument.restore()
|
2014-02-12 10:40:42 +00:00
|
|
|
MockWebApi.getDocument.restore()
|
|
|
|
|
|
|
|
it "should return a 204 status code", ->
|
|
|
|
@statusCode.should.equal 204
|
|
|
|
|
|
|
|
it "should not need to send the updated document to the web api", ->
|
2016-11-29 17:06:23 +00:00
|
|
|
MockWebApi.setDocument.called.should.equal false
|
2014-02-12 10:40:42 +00:00
|
|
|
|
|
|
|
it "should need to reload the doc if read again", (done) ->
|
|
|
|
MockWebApi.getDocument.called.should.equal.false
|
|
|
|
DocUpdaterClient.getDoc @project_id, @doc_id, (error, res, doc) =>
|
|
|
|
MockWebApi.getDocument
|
|
|
|
.calledWith(@project_id, @doc_id)
|
|
|
|
.should.equal true
|
|
|
|
done()
|
2017-10-05 14:14:35 +00:00
|
|
|
|
2016-01-20 15:05:31 +00:00
|
|
|
it "should flush track changes", ->
|
|
|
|
MockTrackChangesApi.flushDoc.calledWith(@doc_id).should.equal true
|
2014-02-12 10:40:42 +00:00
|
|
|
|
2017-10-05 14:14:35 +00:00
|
|
|
it "should flush project history", ->
|
|
|
|
MockProjectHistoryApi.flushProject.calledWith(@project_id).should.equal true
|