Selaa lähdekoodia

Added support for `Watchdog.for()`.

Maciej Bukowski 6 vuotta sitten
vanhempi
commit
3ab0333c00

+ 33 - 10
packages/ckeditor5-watchdog/src/watchdog.js

@@ -22,14 +22,20 @@ import CKEditorError from '@ckeditor/ckeditor5-utils/src/ckeditorerror';
  *
  * It does not handle errors during editor initialization and editor destruction.
  *
- * Basic Usage:
+ * Basic usage:
+ *
+ * 		const watchdog = Watchdog.for( ClassicEditor );
+ *
+ * 		watchdog.create( elementOrData, editorConfig ).then( editor => {} );
+ *
+ * Full usage:
  *
  *		const watchdog = new Watchdog();
  *
- *		watchdog.setCreator( ( el, config ) => ClassicEditor.create( el, config ) );
+ *		watchdog.setCreator( ( elementOrData, editorConfig ) => ClassicEditor.create( elementOrData, editorConfig ) );
  *		watchdog.setDestructor( editor => editor.destroy() );
  *
- *		watchdog.create().then( editor => {} );
+ *		watchdog.create( elementOrData, editorConfig ).then( editor => {} );
  */
 export default class Watchdog {
 	/**
@@ -113,10 +119,10 @@ export default class Watchdog {
 		 */
 
 		/**
-		* The editor source element.
+		* The editor source element or data.
 		*
 		* @private
-		* @member {HTMLElement} _element
+		* @member {HTMLElement|String} _elementOrData
 		*/
 
 		/**
@@ -177,7 +183,7 @@ export default class Watchdog {
 					initialData: this._data
 				} );
 
-				return this.create( this._element, updatedConfig );
+				return this.create( this._elementOrData, updatedConfig );
 			} )
 			.then( () => {
 				this.fire( 'restart' );
@@ -187,12 +193,12 @@ export default class Watchdog {
 	/**
 	 * Creates a watched editor instance using the creator passed to {@link #setCreator} method.
 	 *
-	 * @param {HTMLElement|module:core/editor/editorconfig~EditorConfig} element
+	 * @param {HTMLElement|String} elementOrData
 	 * @param {module:core/editor/editorconfig~EditorConfig} [config]
 	 *
 	 * @returns {Promise.<Watchdog>}
 	 */
-	create( element, config ) {
+	create( elementOrData, config ) {
 		if ( !this._creator ) {
 			/**
 			 * @error watchdog-creator-not-defined
@@ -219,11 +225,11 @@ export default class Watchdog {
 			);
 		}
 
-		this._element = element;
+		this._elementOrData = elementOrData;
 		this._config = config;
 
 		return Promise.resolve()
-			.then( () => this._creator( element, config ) )
+			.then( () => this._creator( elementOrData, config ) )
 			.then( editor => {
 				this._editor = editor;
 
@@ -326,6 +332,23 @@ export default class Watchdog {
 	}
 
 	/**
+	 * A shortcut method for creating the Watchdog.
+	 *
+	 * 		const watchdog = Watchdog.for( ClassicEditor );
+	 *
+	 * 		watchdog.create( elementOrData, config ).then( editor => {} );
+	 *
+	 * @param {*} Editor
+	 */
+	static for( Editor ) {
+		const watchdog = new Watchdog();
+		watchdog.setCreator( ( el, config ) => Editor.create( el, config ) );
+		watchdog.setDestructor( editor => editor.destroy() );
+
+		return watchdog;
+	}
+
+	/**
 	 * Fired when an error occurs and the watchdog will be restarting the editor.
 	 *
 	 * @event error

+ 48 - 16
packages/ckeditor5-watchdog/tests/watchdog.js

@@ -172,7 +172,7 @@ describe( 'Watchdog', () => {
 			watchdog.setCreator( ( el, config ) => FakeEditor.create( el, config ) );
 			watchdog.setDestructor( editor => editor.destroy() );
 
-			// sinon.stub( window, 'onerror' ).value( undefined ); and similar don't work.
+			// sinon.stub( window, 'onerror' ).value( undefined ); and similar do not work.
 			const originalErrorHandler = window.onerror;
 			const windowErrorSpy = sinon.spy();
 			window.onerror = windowErrorSpy;
@@ -199,7 +199,7 @@ describe( 'Watchdog', () => {
 			watchdog.setCreator( ( el, config ) => FakeEditor.create( el, config ) );
 			watchdog.setDestructor( editor => editor.destroy() );
 
-			// sinon.stub( window, 'onerror' ).value( undefined ); and similar don't work.
+			// sinon.stub( window, 'onerror' ).value( undefined ); and similar do not work.
 			const originalErrorHandler = window.onerror;
 			window.onerror = undefined;
 
@@ -225,7 +225,7 @@ describe( 'Watchdog', () => {
 			const editorErrorSpy = sinon.spy();
 			watchdog.on( 'error', editorErrorSpy );
 
-			// sinon.stub( window, 'onerror' ).value( undefined ); and similar don't work.
+			// sinon.stub( window, 'onerror' ).value( undefined ); and similar do not work.
 			const originalErrorHandler = window.onerror;
 			window.onerror = undefined;
 
@@ -256,7 +256,7 @@ describe( 'Watchdog', () => {
 			watchdog2.setCreator( ( el, config ) => FakeEditor.create( el, config ) );
 			watchdog2.setDestructor( editor => editor.destroy() );
 
-			// sinon.stub( window, 'onerror' ).value( undefined ); and similar don't work.
+			// sinon.stub( window, 'onerror' ).value( undefined ); and similar do not work.
 			const originalErrorHandler = window.onerror;
 			window.onerror = undefined;
 
@@ -293,7 +293,7 @@ describe( 'Watchdog', () => {
 			watchdog.setCreator( ( el, config ) => FakeEditor.create( el, config ) );
 			watchdog.setDestructor( editor => editor.destroy() );
 
-			// sinon.stub( window, 'onerror' ).value( undefined ); and similar don't work.
+			// sinon.stub( window, 'onerror' ).value( undefined ); and similar do not work.
 			const originalErrorHandler = window.onerror;
 			window.onerror = undefined;
 
@@ -316,7 +316,7 @@ describe( 'Watchdog', () => {
 			watchdog.setCreator( ( el, config ) => FakeEditor.create( el, config ) );
 			watchdog.setDestructor( editor => editor.destroy() );
 
-			// sinon.stub( window, 'onerror' ).value( undefined ); and similar don't work.
+			// sinon.stub( window, 'onerror' ).value( undefined ); and similar do not work.
 			const originalErrorHandler = window.onerror;
 			window.onerror = undefined;
 
@@ -354,7 +354,7 @@ describe( 'Watchdog', () => {
 			const restartSpy = sinon.spy();
 			watchdog.on( 'restart', restartSpy );
 
-			// sinon.stub( window, 'onerror' ).value( undefined ); and similar don't work.
+			// sinon.stub( window, 'onerror' ).value( undefined ); and similar do not work.
 			const originalErrorHandler = window.onerror;
 			window.onerror = undefined;
 
@@ -390,7 +390,7 @@ describe( 'Watchdog', () => {
 			const restartSpy = sinon.spy();
 			watchdog.on( 'restart', restartSpy );
 
-			// sinon.stub( window, 'onerror' ).value( undefined ); and similar don't work.
+			// sinon.stub( window, 'onerror' ).value( undefined ); and similar do not work.
 			const originalErrorHandler = window.onerror;
 			window.onerror = undefined;
 
@@ -420,7 +420,7 @@ describe( 'Watchdog', () => {
 			watchdog.setCreator( ( el, config ) => FakeEditor.create( el, config ) );
 			watchdog.setDestructor( editor => editor.destroy() );
 
-			// sinon.stub( window, 'onerror' ).value( undefined ); and similar don't work.
+			// sinon.stub( window, 'onerror' ).value( undefined ); and similar do not work.
 			const originalErrorHandler = window.onerror;
 			window.onerror = undefined;
 
@@ -453,7 +453,7 @@ describe( 'Watchdog', () => {
 			watchdog.setCreator( ( el, config ) => FakeEditor.create( el, config ) );
 			watchdog.setDestructor( editor => editor.destroy() );
 
-			// sinon.stub( window, 'onerror' ).value( undefined ); and similar don't work.
+			// sinon.stub( window, 'onerror' ).value( undefined ); and similar do not work.
 			const originalErrorHandler = window.onerror;
 			window.onerror = undefined;
 
@@ -481,7 +481,7 @@ describe( 'Watchdog', () => {
 			watchdog.setCreator( ( el, config ) => FakeEditor.create( el, config ) );
 			watchdog.setDestructor( editor => editor.destroy() );
 
-			// sinon.stub( window, 'onerror' ).value( undefined ); and similar don't work.
+			// sinon.stub( window, 'onerror' ).value( undefined ); and similar do not work.
 			const originalErrorHandler = window.onerror;
 			window.onerror = undefined;
 
@@ -515,7 +515,7 @@ describe( 'Watchdog', () => {
 			watchdog.setCreator( ( el, config ) => FakeEditor.create( el, config ) );
 			watchdog.setDestructor( editor => editor.destroy() );
 
-			// sinon.stub( window, 'onerror' ).value( undefined ); and similar don't work.
+			// sinon.stub( window, 'onerror' ).value( undefined ); and similar do not work.
 			const originalErrorHandler = window.onerror;
 			window.onerror = undefined;
 
@@ -550,7 +550,7 @@ describe( 'Watchdog', () => {
 			watchdog.setCreator( ( el, config ) => FakeEditor.create( el, config ) );
 			watchdog.setDestructor( editor => editor.destroy() );
 
-			// sinon.stub( window, 'onerror' ).value( undefined ); and similar don't work.
+			// sinon.stub( window, 'onerror' ).value( undefined ); and similar do not work.
 			const originalErrorHandler = window.onerror;
 			window.onerror = undefined;
 
@@ -592,14 +592,46 @@ describe( 'Watchdog', () => {
 		} );
 	} );
 
+	describe( 'static for()', () => {
+		it( 'should be a shortcut method for creating the watchdog', () => {
+			const watchdog = Watchdog.for( FakeEditor );
+
+			// sinon.stub( window, 'onerror' ).value( undefined ); and similar do not work.
+			const originalErrorHandler = window.onerror;
+			window.onerror = undefined;
+
+			return watchdog.create( document.createElement( 'div' ), {
+				initialData: '<p>foo</p>',
+				plugins: [ Paragraph ]
+			} ).then( () => {
+				const oldEditor = watchdog.editor;
+				expect( watchdog.editor ).to.be.an.instanceOf( FakeEditor );
+
+				setTimeout( () => throwCKEditorError( 'foo', watchdog.editor ) );
+
+				return new Promise( res => {
+					watchdog.on( 'restart', () => {
+						window.onerror = originalErrorHandler;
+
+						expect( watchdog.editor ).to.be.an.instanceOf( FakeEditor );
+						expect( watchdog.editor ).to.not.equal( oldEditor );
+						expect( watchdog.editor.getData() ).to.equal( '<p>foo</p>' );
+
+						watchdog.destroy().then( res );
+					} );
+				} );
+			} );
+		} );
+	} );
+
 	describe( 'crashes', () => {
-		it( 'should be an array of caught errors by the Watchdog', () => {
+		it( 'should be an array of caught errors by the watchdog', () => {
 			const watchdog = new Watchdog();
 
 			watchdog.setCreator( ( el, config ) => FakeEditor.create( el, config ) );
 			watchdog.setDestructor( editor => editor.destroy() );
 
-			// sinon.stub( window, 'onerror' ).value( undefined ); and similar don't work.
+			// sinon.stub( window, 'onerror' ).value( undefined ); and similar do not work.
 			const originalErrorHandler = window.onerror;
 			window.onerror = undefined;
 
@@ -625,7 +657,7 @@ describe( 'Watchdog', () => {
 
 // Wrap Editor to align the API to the "full" editors.
 class FakeEditor extends VirtualTestEditor {
-	static create( elementOrData, config ) {
+	static create( _elementOrData, config ) {
 		return super.create( config );
 	}
 }