瀏覽代碼

Unit test accept changes in the ranges manager.

Paulo Reis 9 年之前
父節點
當前提交
36d16a76e0
共有 1 個文件被更改,包括 88 次插入 和 7 次删除
  1. 88 7
      services/document-updater/test/unit/coffee/RangesManager/RangesManagerTests.coffee

+ 88 - 7
services/document-updater/test/unit/coffee/RangesManager/RangesManagerTests.coffee

@@ -10,6 +10,7 @@ describe "RangesManager", ->
 		@RangesManager = SandboxedModule.require modulePath,
 			requires:
 				"logger-sharelatex": @logger = { error: sinon.stub(), log: sinon.stub(), warn: sinon.stub() }
+
 		@doc_id = "doc-id-123"
 		@project_id = "project-id-123"
 		@user_id = "user-id-123"
@@ -181,24 +182,104 @@ describe "RangesManager", ->
 
 	describe "acceptChanges", ->
 		beforeEach ->
-			@ranges = { entries: "mock", comments: "mock" }
+			@RangesManager = SandboxedModule.require modulePath,
+				requires:
+					"logger-sharelatex": @logger = { error: sinon.stub(), log: sinon.stub(), warn: sinon.stub() }
+					"./RangesTracker":@RangesTracker = SandboxedModule.require "../../../../app/js/RangesTracker.js"
+
+			@ranges = {
+				comments: []
+				changes: [{
+					id: "a1"
+					op:
+						i: "lorem"
+						p: 0
+				}, {
+					id: "a2"
+					op:
+						i: "ipsum"
+						p: 10
+				}, {
+					id: "a3"
+					op:
+						i: "dolor"
+						p: 20
+				}, {
+					id: "a4"
+					op:
+						i: "sit"
+						p: 30
+				}, {
+					id: "a5"
+					op:
+						i: "amet"
+						p: 40
+				}]
+			}
+			@removeChangeIdsSpy = sinon.spy @RangesTracker.prototype, "removeChangeIds"
 
 		describe "successfully with a single change", ->
-			beforeEach ->
-				@change_id = "mock-change-id"
-				@RangesManager.acceptChanges [ @change_id ], @ranges 
+			beforeEach (done) ->
+				@change_ids = [ @ranges.changes[1].id ]
+				@RangesManager.acceptChanges @change_ids, @ranges, (err, ranges) => 
+					@rangesResponse = ranges
+					done()
 
 			it "should log the call with the correct number of changes", ->
 				@logger.log
 					.calledWith("accepting 1 changes in ranges")
 					.should.equal true
 
+			it "should delegate the change removal to the ranges tracker", ->
+				@removeChangeIdsSpy
+					.calledWith(@change_ids)
+					.should.equal true
+
+			it "should remove the change", ->
+				expect(@rangesResponse.changes
+					.find((change) => change.id ==  @ranges.changes[1].id))
+					.to.be.undefined
+
+			it "should return the original number of changes minus 1", ->
+				@rangesResponse.changes.length
+					.should.equal @ranges.changes.length - 1
+					
+			it "should not touch other changes", ->
+				for i in [ 0, 2, 3, 4]
+					expect(@rangesResponse.changes
+						.find((change) => change.id ==  @ranges.changes[i].id))
+						.to.deep.equal @ranges.changes[i]
+
 		describe "successfully with multiple changes", ->
-			beforeEach ->
-				@change_ids = [ "mock-change-id-1", "mock-change-id-2", "mock-change-id-3", "mock-change-id-4" ]
-				@RangesManager.acceptChanges @change_ids, @ranges
+			beforeEach (done) ->
+				@change_ids = [ @ranges.changes[1].id, @ranges.changes[3].id, @ranges.changes[4].id ]
+				@RangesManager.acceptChanges @change_ids, @ranges, (err, ranges) => 
+					@rangesResponse = ranges
+					done()
 
 			it "should log the call with the correct number of changes", ->
 				@logger.log
 					.calledWith("accepting #{ @change_ids.length } changes in ranges")
 					.should.equal true
+
+			it "should delegate the change removal to the ranges tracker", ->
+				@removeChangeIdsSpy
+					.calledWith(@change_ids)
+					.should.equal true
+
+			it "should remove the changes", ->
+				for i in [ 1, 3, 4]
+					expect(@rangesResponse.changes
+						.find((change) => change.id ==  @ranges.changes[1].id))
+						.to.be.undefined
+			
+			it "should return the original number of changes minus the number of accepted changes", ->
+				@rangesResponse.changes.length
+					.should.equal @ranges.changes.length - 3
+
+			it "should not touch other changes", ->
+				for i in [ 0, 2 ]
+					expect(@rangesResponse.changes
+						.find((change) => change.id ==  @ranges.changes[i].id))
+						.to.deep.equal @ranges.changes[i]
+