Explorar el Código

Added test for restarting the editor.

Maciej Bukowski hace 6 años
padre
commit
a2d88597f3

+ 15 - 12
packages/ckeditor5-watchdog/src/watchdog.js

@@ -31,15 +31,24 @@ export default class Watchdog {
 
 		/**
 		 * @private
+		 * @type {Number}
+		 */
+		this._crashNumberLimit = crashNumberLimit || 3;
+
+		/**
+		 * @private
 		 * @type {Function}
 		 */
 		this._boundErrorWatcher = this._watchForEditorErrors.bind( this );
 
 		/**
+		 * Debounced save method. The `save()` method is called the specified `waitingTime` after `debouncedSave()` is called,
+		 * unless a new action happens in the meantime.
+		 *
 		 * @private
-		 * @type {Number}
+		 * @type {Function}
 		 */
-		this._crashNumberLimit = crashNumberLimit || 3;
+		this._debouncedSave = debounce( this._save.bind( this ), waitingTime || 5000 );
 
 		/**
 		 * @private
@@ -67,15 +76,6 @@ export default class Watchdog {
 		 * @private
 		 * @member {Number} _lastDocumentVersion
 		 */
-
-		/**
-		 * Debounced save method. The `save()` method is called the specified `waitingTime` after `debouncedSave()` is called,
-		 * unless a new action happens in the meantime.
-		 *
-		 * @private
-		 * @type {Function}
-		 */
-		this._debouncedSave = debounce( this._save.bind( this ), waitingTime || 5000 );
 	}
 
 	/**
@@ -154,7 +154,10 @@ export default class Watchdog {
 		return this._destructor( this._editor );
 	}
 
-	save() {
+	/**
+	 * @private
+	 */
+	_save() {
 		const version = this._editor.model.document.version;
 
 		// Change may not produce an operation, so the document's version

+ 88 - 14
packages/ckeditor5-watchdog/tests/watchdog.js

@@ -4,28 +4,41 @@
  */
 
 import Watchdog from '../src/watchdog';
-import { watch } from 'chokidar';
 
 describe( 'Watchdog', () => {
 	describe( 'simple scenarios', () => {
-		it( '#1', () => {
+		it( 'watchdog should expose `create()` and `destroy()` methods', () => {
 			const watchdog = new Watchdog();
+			const FakeEditor = getFakeEditor();
 
 			const editorCreateSpy = sinon.spy( FakeEditor, 'create' );
 			const editorDestroySpy = sinon.spy( FakeEditor.prototype, 'destroy' );
 
-			class FakeEditor {
-				static create( el, config ) {
-					this.el = el;
-					this.config = config;
+			watchdog.setCreator( ( el, config ) => FakeEditor.create( el, config ) );
+			watchdog.setDestructor( editor => editor.destroy() );
 
-					return Promise.resolve();
-				}
+			return watchdog.create()
+				.then( () => {
+					sinon.assert.calledOnce( editorCreateSpy );
+					sinon.assert.notCalled( editorDestroySpy );
 
-				destroy() {
-					return Promise.resolve();
-				}
-			}
+					return watchdog.destroy();
+				} )
+				.then( () => {
+					sinon.assert.calledOnce( editorCreateSpy );
+					sinon.assert.calledOnce( editorDestroySpy );
+				} )
+		} );
+
+	} );
+
+	describe( 'restart()', () => {
+		it( 'should restart the editor', () => {
+			const watchdog = new Watchdog();
+			const FakeEditor = getFakeEditor();
+
+			const editorCreateSpy = sinon.spy( FakeEditor, 'create' );
+			const editorDestroySpy = sinon.spy( FakeEditor.prototype, 'destroy' );
 
 			watchdog.setCreator( ( el, config ) => FakeEditor.create( el, config ) );
 			watchdog.setDestructor( editor => editor.destroy() );
@@ -35,12 +48,73 @@ describe( 'Watchdog', () => {
 					sinon.assert.calledOnce( editorCreateSpy );
 					sinon.assert.notCalled( editorDestroySpy );
 
-					return watchdog.destroy();
+					return watchdog.restart();
 				} )
 				.then( () => {
-					sinon.assert.calledOnce( editorCreateSpy );
+					sinon.assert.calledTwice( editorCreateSpy );
 					sinon.assert.calledOnce( editorDestroySpy );
+
+					return watchdog.destroy();
+				} );
+		} );
+	} );
+
+	describe( 'editor', () => {
+		it( 'should be the current editor used by the Watchdog', () => {
+			const watchdog = new Watchdog();
+			const FakeEditor = getFakeEditor();
+
+			watchdog.setCreator( ( el, config ) => FakeEditor.create( el, config ) );
+			watchdog.setDestructor( editor => editor.destroy() );
+
+			expect( watchdog.editor ).to.be.undefined;
+
+			let oldEditor;
+
+			return watchdog.create()
+				.then( () => {
+					oldEditor = watchdog.editor;
+					expect( watchdog.editor ).to.be.instanceOf( FakeEditor );
+
+					return watchdog.restart();
 				} )
+				.then( () => {
+					expect( watchdog.editor ).to.be.instanceOf( FakeEditor );
+					expect( watchdog.editor ).to.not.equal( oldEditor );
+
+					return watchdog.destroy();
+				} );
 		} );
 	} );
 } );
+
+function getFakeEditor() {
+	return class FakeEditor {
+		static create( el, config ) {
+			this.el = el;
+			this.config = config;
+
+			return Promise.resolve( new this() );
+		}
+
+		constructor() {
+			this.model = {
+				document: {
+					version: 0
+				}
+			};
+		}
+
+		getData() {
+			return 'foo';
+		}
+
+		setData() {
+
+		}
+
+		destroy() {
+			return Promise.resolve();
+		}
+	}
+}