pass object to DocumentUpdater.updateProjectStructure instead of separate arrays

This commit is contained in:
Hayden Faulds
2017-12-04 09:42:10 +00:00
parent 1405b645f3
commit 4094801f18
4 changed files with 52 additions and 51 deletions
@@ -396,7 +396,7 @@ describe 'DocumentUpdaterHandler', ->
@settings.apis.project_history.enabled = false
@request.post = sinon.stub()
@handler.updateProjectStructure @project_id, @user_id, @oldDocs, @newDocs, @oldFiles, @newFiles, @callback
@handler.updateProjectStructure @project_id, @user_id, {}, @callback
it 'does not make a web request', ->
@request.post.called.should.equal false
@@ -414,17 +414,17 @@ describe 'DocumentUpdaterHandler', ->
it 'should send the structure update to the document updater', (done) ->
@docIdA = new ObjectId()
@docIdB = new ObjectId()
@oldDocs = [
{ path: '/old_a', doc: _id: @docIdA }
{ path: '/old_b', doc: _id: @docIdB }
]
# create new instances of the same ObjectIds so that == doens't pass
@newDocs = [
{ path: '/old_a', doc: _id: new ObjectId(@docIdA.toString()) }
{ path: '/new_b', doc: _id: new ObjectId(@docIdB.toString()) }
]
@oldFiles = []
@newFiles = []
@changes = {
oldDocs: [
{ path: '/old_a', doc: _id: @docIdA }
{ path: '/old_b', doc: _id: @docIdB }
]
# create new instances of the same ObjectIds so that == doesn't pass
newDocs: [
{ path: '/old_a', doc: _id: new ObjectId(@docIdA.toString()) }
{ path: '/new_b', doc: _id: new ObjectId(@docIdB.toString()) }
]
}
docUpdates = [
id: @docIdB.toString(),
@@ -432,7 +432,7 @@ describe 'DocumentUpdaterHandler', ->
newPathname: "/new_b"
]
@handler.updateProjectStructure @project_id, @user_id, @oldDocs, @newDocs, @oldFiles, @newFiles, () =>
@handler.updateProjectStructure @project_id, @user_id, @changes, () =>
@request.post
.calledWith(url: @url, json: {docUpdates, fileUpdates: [], userId: @user_id})
.should.equal true
@@ -441,12 +441,9 @@ describe 'DocumentUpdaterHandler', ->
describe "when a doc has been added", ->
it 'should send the structure update to the document updater', (done) ->
@docId = new ObjectId()
@oldDocs = []
@newDocs = [
@changes = newDocs: [
{ path: '/foo', docLines: 'a\nb', doc: _id: @docId }
]
@oldFiles = []
@newFiles = []
docUpdates = [
id: @docId.toString(),
@@ -455,7 +452,7 @@ describe 'DocumentUpdaterHandler', ->
url: undefined
]
@handler.updateProjectStructure @project_id, @user_id, @oldDocs, @newDocs, @oldFiles, @newFiles, () =>
@handler.updateProjectStructure @project_id, @user_id, @changes, () =>
@request.post
.calledWith(url: @url, json: {docUpdates, fileUpdates: [], userId: @user_id})
.should.equal true
@@ -464,10 +461,7 @@ describe 'DocumentUpdaterHandler', ->
describe "when a file has been added", ->
it 'should send the structure update to the document updater', (done) ->
@fileId = new ObjectId()
@oldDocs = []
@newDocs = []
@oldFiles = []
@newFiles = [
@changes = newFiles: [
{ path: '/bar', url: 'filestore.example.com/file', file: _id: @fileId }
]
@@ -478,7 +472,7 @@ describe 'DocumentUpdaterHandler', ->
docLines: undefined
]
@handler.updateProjectStructure @project_id, @user_id, @oldDocs, @newDocs, @oldFiles, @newFiles, () =>
@handler.updateProjectStructure @project_id, @user_id, @changes, () =>
@request.post
.calledWith(url: @url, json: {docUpdates: [], fileUpdates, userId: @user_id})
.should.equal true
@@ -487,14 +481,11 @@ describe 'DocumentUpdaterHandler', ->
describe "when a doc has been deleted", ->
it 'should do nothing', (done) ->
@docId = new ObjectId()
@oldDocs = [
@changes = oldDocs: [
{ path: '/foo', docLines: 'a\nb', doc: _id: @docId }
]
@newDocs = []
@oldFiles = []
@newFiles = []
@handler.updateProjectStructure @project_id, @user_id, @oldDocs, @newDocs, @oldFiles, @newFiles, () =>
@handler.updateProjectStructure @project_id, @user_id, @changes, () =>
@request.post.called.should.equal false
done()
@@ -69,7 +69,7 @@ describe 'ProjectEntityHandler', ->
@settings =
maxEntitiesPerProject:200
@documentUpdaterHandler =
updateProjectStructure: sinon.stub().callsArg(6)
updateProjectStructure: sinon.stub().yields()
deleteDoc: sinon.stub().callsArg(2)
@ProjectEntityHandler = SandboxedModule.require modulePath, requires:
'../../models/Project': Project:@ProjectModel
@@ -274,7 +274,7 @@ describe 'ProjectEntityHandler', ->
it "should should send the update to the doc updater", ->
@documentUpdaterHandler.updateProjectStructure
.calledWith(project_id, userId, @oldDocs, @newDocs, @oldFiles, @newFiles)
.calledWith(project_id, userId, {@oldDocs, @newDocs, @oldFiles, @newFiles})
.should.equal true
it 'should remove the element from its current position', ->
@@ -326,7 +326,7 @@ describe 'ProjectEntityHandler', ->
it "should should send the update to the doc updater", ->
@documentUpdaterHandler.updateProjectStructure
.calledWith(project_id, userId, @oldDocs, @newDocs, @oldFiles, @newFiles)
.calledWith(project_id, userId, {@oldDocs, @newDocs, @oldFiles, @newFiles})
.should.equal true
it 'should remove the element from its current position', ->
@@ -487,12 +487,13 @@ describe 'ProjectEntityHandler', ->
.should.equal true
it "should should send the change in project structure to the doc updater", () ->
newDoc =
newDocs = [
doc: @doc
path: @path
docLines: @lines.join('\n')
]
@documentUpdaterHandler.updateProjectStructure
.calledWith(project_id, userId, [], [newDoc], [], [])
.calledWith(project_id, userId, {newDocs})
.should.equal true
describe "restoreDoc", ->
@@ -570,9 +571,10 @@ describe 'ProjectEntityHandler', ->
@ProjectEntityHandler.addFile project_id, folder_id, fileName, {}, userId, (err, fileRef, parentFolder)->
it "should should send the change in project structure to the doc updater", (done) ->
@documentUpdaterHandler.updateProjectStructure = (passed_project_id, passed_user_id, oldDocs, newDocs, oldFiles, newFiles) =>
@documentUpdaterHandler.updateProjectStructure = (passed_project_id, passed_user_id, changes) =>
passed_project_id.should.equal project_id
passed_user_id.should.equal userId
{ newFiles } = changes
newFiles.length.should.equal 1
newFile = newFiles[0]
newFile.file.name.should.equal fileName
@@ -635,9 +637,10 @@ describe 'ProjectEntityHandler', ->
@ProjectEntityHandler.replaceFile project_id, @file_id, @fsPath, userId, =>
it "should should send the old and new project structure to the doc updater", (done) ->
@documentUpdaterHandler.updateProjectStructure = (passed_project_id, passed_user_id, oldDocs, newDocs, oldFiles, newFiles) =>
@documentUpdaterHandler.updateProjectStructure = (passed_project_id, passed_user_id, changes) =>
passed_project_id.should.equal project_id
passed_user_id.should.equal userId
{ newFiles } = changes
newFiles.length.should.equal 1
newFile = newFiles[0]
newFile.file.name.should.equal @fileName
@@ -1029,9 +1032,10 @@ describe 'ProjectEntityHandler', ->
@ProjectEntityHandler.copyFileFromExistingProjectWithProject @project, folder_id, oldProject_id, oldFileRef, userId, (err, fileRef, parentFolder)->
it "should should send the change in project structure to the doc updater", (done) ->
@documentUpdaterHandler.updateProjectStructure = (passed_project_id, passed_user_id, oldDocs, newDocs, oldFiles, newFiles) =>
@documentUpdaterHandler.updateProjectStructure = (passed_project_id, passed_user_id, changes) =>
passed_project_id.should.equal project_id
passed_user_id.should.equal userId
{ newFiles } = changes
newFiles.length.should.equal 1
newFile = newFiles[0]
newFile.file.name.should.equal fileName
@@ -1060,12 +1064,12 @@ describe 'ProjectEntityHandler', ->
@projectLocator.findElement = sinon.stub().callsArgWith(1, null, @entity = { _id: @entity_id, name:"old.tex", rev:4 }, @path)
@tpdsUpdateSender.moveEntity = sinon.stub()
@ProjectModel.findOneAndUpdate = sinon.stub().callsArgWith(3, null, @project)
@documentUpdaterHandler.updateProjectStructure = sinon.stub().callsArg(6)
@documentUpdaterHandler.updateProjectStructure = sinon.stub().yields()
it "should should send the old and new project structure to the doc updater", (done) ->
@ProjectEntityHandler.renameEntity project_id, @entity_id, @entityType, @newName, userId, =>
@documentUpdaterHandler.updateProjectStructure
.calledWith(project_id, userId, @oldDocs, @newDocs, @oldFiles, @newFiles)
.calledWith(project_id, userId, {@oldDocs, @newDocs, @oldFiles, @newFiles})
.should.equal true
done()