Explorar el Código

Added test for the DeltaReplayer applyNextDelta and applyAllDeltas.

Maciej Bukowski hace 8 años
padre
commit
ff889e09b9

+ 7 - 2
packages/ckeditor5-engine/src/dev-utils/deltareplayer.js

@@ -41,6 +41,10 @@ export default class DeltaReplayer {
 			.map( stringifiedDelta => JSON.parse( stringifiedDelta ) );
 	}
 
+	getDeltasToReplay() {
+		return this._deltasToReplay;
+	}
+
 	/**
 	 * Applies all deltas with delay between actions.
 	 *
@@ -88,12 +92,13 @@ export default class DeltaReplayer {
 		return new Promise( ( res, rej ) => {
 			document.enqueueChanges( () => {
 				const jsonDelta = this._deltasToReplay.shift();
-				const delta = DeltaFactory.fromJSON( jsonDelta, this._document );
 
-				if ( !delta ) {
+				if ( !jsonDelta ) {
 					return rej( new Error( 'No deltas to replay' ) );
 				}
 
+				const delta = DeltaFactory.fromJSON( jsonDelta, this._document );
+
 				const batch = document.batch();
 				batch.addDelta( delta );
 

+ 106 - 26
packages/ckeditor5-engine/tests/dev-utils/deltareplayer.js

@@ -9,40 +9,120 @@ import Document from '../../src/model/document';
 describe( 'DeltaReplayer', () => {
 	describe( 'constructor()', () => {
 		it( 'should be able to initialize replayer without deltas', () => {
-			const fakeDocument = {};
+			const doc = getDocument();
 			const stringifiedDeltas = '';
-			const deltaReplayer = new DeltaReplayer( fakeDocument, '---', stringifiedDeltas );
+			const deltaReplayer = new DeltaReplayer( doc, '---', stringifiedDeltas );
 
-			expect( deltaReplayer._deltasToReplay ).to.deep.equal( [] );
-			expect( deltaReplayer._document ).to.deep.equal( fakeDocument );
-			expect( deltaReplayer._logSeparator ).to.deep.equal( '---' );
+			expect( deltaReplayer.getDeltasToReplay() ).to.deep.equal( [] );
 		} );
 
 		it( 'should be able to initialize replayer with deltas', () => {
-			const doc = new Document();
-			doc.createRoot( 'main' );
-
-			const delta = {
-				operations: [ {
-					baseVersion: 0,
-					position: {
-						root: 'main',
-						path: [ 0 ]
-					},
-					nodes: [ {
-						name: 'heading1',
-						children: [ {
-							data: 'The great world of open Web standards'
-						} ]
-					} ],
-					__className: 'engine.model.operation.InsertOperation'
-				} ],
-				__className: 'engine.model.delta.InsertDelta'
-			};
+			const doc = getDocument();
+			const delta = getFirstDelta();
 
 			const deltaReplayer = new DeltaReplayer( doc, '---', JSON.stringify( delta ) );
 
-			expect( deltaReplayer._deltasToReplay ).to.deep.equal( [ delta ] );
+			expect( deltaReplayer.getDeltasToReplay() ).to.deep.equal( [ delta ] );
+		} );
+	} );
+
+	describe( 'applyNextDelta()', () => {
+		it( 'should remove first delta from stack', () => {
+			const doc = getDocument();
+			const delta = getFirstDelta();
+
+			const deltaReplayer = new DeltaReplayer( doc, '---', JSON.stringify( delta ) );
+
+			return deltaReplayer.applyNextDelta().then( () => {
+				expect( deltaReplayer.getDeltasToReplay() ).to.deep.equal( [] );
+			} );
+		} );
+
+		it( 'should apply first delta on the document', () => {
+			const doc = getDocument();
+			const delta = getFirstDelta();
+
+			const deltaReplayer = new DeltaReplayer( doc, '---', JSON.stringify( delta ) );
+
+			return deltaReplayer.applyNextDelta().then( () => {
+				expect( Array.from( doc.getRoot().getChildren() ).length ).to.equal( 1 );
+			} );
+		} );
+
+		it( 'should throw an error if 0 deltas are provided', () => {
+			const doc = getDocument();
+			const deltaReplayer = new DeltaReplayer( doc, '---', '' );
+
+			return deltaReplayer.applyNextDelta().then( () => {
+				throw new Error( 'This should throw an error' );
+			}, ( err ) => {
+				expect( err instanceof Error ).to.equal( true );
+				expect( err.message ).to.equal( 'No deltas to replay' );
+			} );
+		} );
+	} );
+
+	describe( 'applyAllDeltas', () => {
+		it( 'should apply all deltas on the document', () => {
+			const doc = getDocument();
+
+			const stringifiedDeltas = [ getFirstDelta(), getSecondDelta() ]
+				.map( d => JSON.stringify( d ) )
+				.join( '---' );
+
+			const deltaReplayer = new DeltaReplayer( doc, '---', stringifiedDeltas );
+
+			return deltaReplayer.applyAllDeltas().then( () => {
+				expect( Array.from( doc.getRoot().getChildren() ).length ).to.equal( 2 );
+				expect( deltaReplayer.getDeltasToReplay().length ).to.equal( 0 );
+			} );
 		} );
 	} );
 } );
+
+function getDocument() {
+	const doc = new Document();
+	doc.createRoot( 'main' );
+
+	return doc;
+}
+
+function getFirstDelta() {
+	return {
+		operations: [ {
+			baseVersion: 0,
+			position: {
+				root: 'main',
+				path: [ 0 ]
+			},
+			nodes: [ {
+				name: 'heading1',
+				children: [ {
+					data: 'The great world of open Web standards'
+				} ]
+			} ],
+			__className: 'engine.model.operation.InsertOperation'
+		} ],
+		__className: 'engine.model.delta.InsertDelta'
+	};
+}
+
+function getSecondDelta() {
+	return {
+		operations: [ {
+			baseVersion: 1,
+			position: {
+				root: 'main',
+				path: [ 1 ]
+			},
+			nodes: [ {
+				name: 'heading1',
+				children: [ {
+					data: 'The great world of open Web standards'
+				} ]
+			} ],
+			__className: 'engine.model.operation.InsertOperation'
+		} ],
+		__className: 'engine.model.delta.InsertDelta'
+	};
+}