Просмотр исходного кода

handle file additions in DocumentUpdaterHandler.updateProjectStructure

Hayden Faulds 9 лет назад
Родитель
Сommit
0178f78249

+ 12 - 0
services/web/app/coffee/Features/DocumentUpdater/DocumentUpdaterHandler.coffee

@@ -244,6 +244,18 @@ module.exports = DocumentUpdaterHandler =
 					pathname: oldEntity.path
 					newPathname: newEntity.path
 
+		for newEntity in newEntities
+			id = newEntity[entityType]._id
+			oldEntity = _.find oldEntities, (oldEntity) ->
+				oldEntity[entityType]._id.toString() == id.toString()
+
+			if !oldEntity?
+				updates.push
+					id: id
+					pathname: newEntity.path
+					docLines: newEntity.docLines
+					url: newEntity.url
+
 		updates
 
 PENDINGUPDATESKEY = "PendingUpdates"

+ 74 - 28
services/web/test/unit/coffee/DocumentUpdater/DocumentUpdaterHandlerTests.coffee

@@ -390,19 +390,6 @@ describe 'DocumentUpdaterHandler', ->
 	describe "updateProjectStructure ", ->
 		beforeEach ->
 			@user_id = 1234
-			@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 = []
 
 		describe "with project history disabled", ->
 			beforeEach ->
@@ -420,20 +407,79 @@ describe 'DocumentUpdaterHandler', ->
 		describe "with project history enabled", ->
 			beforeEach ->
 				@settings.apis.project_history.enabled = true
+				@url = "#{@settings.apis.documentupdater.url}/project/#{@project_id}"
 				@request.post = sinon.stub().callsArgWith(1, null, {statusCode: 204}, "")
-				@handler.updateProjectStructure @project_id, @user_id, @oldDocs, @newDocs, @oldFiles, @newFiles, @callback
-
-			it 'should send the structure update to the document updater', ->
-				docUpdates = [
-					id: @docIdB,
-					pathname: "/old_b"
-					newPathname: "/new_b"
-				]
-
-				url = "#{@settings.apis.documentupdater.url}/project/#{@project_id}"
-				@request.post
-					.calledWith(url: url, json: {docUpdates, fileUpdates: [], userId: @user_id})
-					.should.equal true
 
-			it "should call the callback with no error", ->
-				@callback.calledWith(null).should.equal true
+			describe "when an entity has changed name", ->
+				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 = []
+
+					docUpdates = [
+						id: @docIdB,
+						pathname: "/old_b"
+						newPathname: "/new_b"
+					]
+
+					@handler.updateProjectStructure @project_id, @user_id, @oldDocs, @newDocs, @oldFiles, @newFiles, () =>
+						@request.post
+							.calledWith(url: @url, json: {docUpdates, fileUpdates: [], userId: @user_id})
+							.should.equal true
+						done()
+
+			describe "when a doc has been added", ->
+				it 'should send the structure update to the document updater', (done) ->
+					@docId = new ObjectId()
+					@oldDocs = []
+					@newDocs = [
+						{ path: '/foo', docLines: 'a\nb', doc: _id: @docId }
+					]
+					@oldFiles = []
+					@newFiles = []
+
+					docUpdates = [
+						id: @docId,
+						pathname: "/foo"
+						docLines: 'a\nb'
+						url: undefined
+					]
+
+					@handler.updateProjectStructure @project_id, @user_id, @oldDocs, @newDocs, @oldFiles, @newFiles, () =>
+						@request.post
+							.calledWith(url: @url, json: {docUpdates, fileUpdates: [], userId: @user_id})
+							.should.equal true
+						done()
+
+			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 = [
+						{ path: '/bar', url: 'filestore.example.com/file', file: _id: @fileId }
+					]
+
+					fileUpdates = [
+						id: @fileId,
+						pathname: "/bar"
+						url: 'filestore.example.com/file'
+						docLines: undefined
+					]
+
+					@handler.updateProjectStructure @project_id, @user_id, @oldDocs, @newDocs, @oldFiles, @newFiles, () =>
+						@request.post
+							.calledWith(url: @url, json: {docUpdates: [], fileUpdates, userId: @user_id})
+							.should.equal true
+						done()