瀏覽代碼

Added simple test.

Maciej Bukowski 8 年之前
父節點
當前提交
8405df9399

+ 73 - 68
packages/ckeditor5-engine/src/dev-utils/deltareplayer.js

@@ -12,76 +12,81 @@
 import DeltaFactory from '../model/delta/deltafactory';
 
 /**
- * DeltaReplayer is a dev-tool created for easily replaying operations on the document from stringified deltas
+ * DeltaReplayer is a dev-tool created for easily replaying operations on the document from stringified deltas.
  */
 export default class DeltaReplayer {
-    /**
-     * @param {module:engine/model/document~Document} document 
-     * @param {String} logSeparator Separator between deltas.
-     * @param {String} stringifiedDeltas Deltas to replay.
-     */
-    constructor( document, logSeparator, stringifiedDeltas ) {
-        this._document = document;
-        this._deltaToReplay = [];
-        this._logSeparator = logSeparator;
-        this.setStringifiedDeltas( stringifiedDeltas );
-    }
-
-    /**
-     * @param {String} stringifiedDeltas Deltas to replay.
-     */
-    setStringifiedDeltas( stringifiedDeltas ) {
-        this._deltaToReplay = stringifiedDeltas
+	/**
+	 * @param {module:engine/model/document~Document} document.
+	 * @param {String} logSeparator Separator between deltas.
+	 * @param {String} stringifiedDeltas Deltas to replay.
+	 */
+	constructor( document, logSeparator, stringifiedDeltas ) {
+		this._document = document;
+		this._logSeparator = logSeparator;
+		this.setStringifiedDeltas( stringifiedDeltas );
+	}
+
+	/**
+	 * @param {String} stringifiedDeltas Deltas to replay.
+	 */
+	setStringifiedDeltas( stringifiedDeltas ) {
+		if ( stringifiedDeltas === '' ) {
+			this._deltaToReplay = [];
+
+			return;
+		}
+
+		this._deltaToReplay = stringifiedDeltas
 			.split( this._logSeparator )
 			.map( stringifiedDelta => JSON.parse( stringifiedDelta ) )
 			.map( jsonDelta => DeltaFactory.fromJSON( jsonDelta, this._document ) );
-    }
-
-    /**
-     * @param {Number} timeInterval
-     */
-    play( timeInterval = 1000 ) {
-        if ( this._deltaToReplay.length === 0 ) {
-            return;
-        }
-
-        this.applyNextDelta().then( () => {
-            setTimeout( () => this.play(), timeInterval );
-        } );
-    }
-
-    /**
-     * @returns {Promise}
-     */
-    applyNextDelta() {
-        const document = this._document;
-
-        return new Promise( ( res, rej ) => {
-            document.enqueueChanges( () => {
-                const delta = this._deltaToReplay.shift();
-
-                if ( !delta ) {
-                    return rej( new Error('No deltas to replay') );
-                }
-
-                const batch = document.batch();
-                batch.addDelta( delta );
-
-                for ( const operation of delta.operations ) {
-                    document.applyOperation( operation );
-                }
-
-                res();
-            } );
-        } );
-    }
-
-    /**
-     * @returns {Promise}
-     */
-    applyAllDeltas() {
-        return this.applyNextDelta()
-            .then( () => this.applyAllDeltas() )
-            .catch( err => console.warn( err ) );
-    }
-}
+	}
+
+	/**
+	 * @param {Number} timeInterval
+	 */
+	play( timeInterval = 1000 ) {
+		if ( this._deltaToReplay.length === 0 ) {
+			return;
+		}
+
+		this.applyNextDelta().then( () => {
+			setTimeout( () => this.play(), timeInterval );
+		} );
+	}
+
+	/**
+	 * @returns {Promise}
+	 */
+	applyNextDelta() {
+		const document = this._document;
+
+		return new Promise( ( res, rej ) => {
+			document.enqueueChanges( () => {
+				const delta = this._deltaToReplay.shift();
+
+				if ( !delta ) {
+					return rej( new Error( 'No deltas to replay' ) );
+				}
+
+				const batch = document.batch();
+				batch.addDelta( delta );
+
+				for ( const operation of delta.operations ) {
+					document.applyOperation( operation );
+				}
+
+				res();
+			} );
+		} );
+	}
+
+	/**
+	 * @returns {Promise}
+	 */
+	applyAllDeltas() {
+		return this.applyNextDelta()
+			.then( () => this.applyAllDeltas() )
+			.catch( err => console.warn( err ) );
+	}
+}

+ 20 - 0
packages/ckeditor5-engine/tests/dev-utils/deltareplayer.js

@@ -0,0 +1,20 @@
+/**
+ * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
+ * For licensing, see LICENSE.md.
+ */
+
+import DeltaReplayer from '../../src/dev-utils/deltareplayer';
+
+describe( 'DeltaReplayer', () => {
+	describe( 'constructor()', () => {
+		it( 'should be able to initialize replayer with no deltas', () => {
+			const fakeDocument = {};
+			const stringifiedDeltas = '';
+			const deltaReplayer = new DeltaReplayer( fakeDocument, '---', stringifiedDeltas );
+
+			expect( deltaReplayer._deltaToReplay ).to.deep.equal( [] );
+			expect( deltaReplayer._document ).to.deep.equal( fakeDocument );
+			expect( deltaReplayer._logSeparator ).to.deep.equal( '---' );
+		} );
+	} );
+} );