Przeglądaj źródła

Some fixes and small improvements.

Maciej Bukowski 6 lat temu
rodzic
commit
5018237483

+ 11 - 8
packages/ckeditor5-watchdog/src/contextwatchdog.js

@@ -89,7 +89,11 @@ export default class ContextWatchdog extends Watchdog {
 
 					this._watchdogs.set( itemName, watchdog );
 
-					await watchdog.create( itemConfig.sourceElementOrData, itemConfig.config );
+					const editorConfig = watchdog._cloneConfig( itemConfig.config );
+
+					editorConfig.context = this._context;
+
+					await watchdog.create( itemConfig.sourceElementOrData, editorConfig );
 				} else {
 					throw new Error( `Not supported item type: '${ itemConfig.type }'.` );
 				}
@@ -112,7 +116,7 @@ export default class ContextWatchdog extends Watchdog {
 				this._watchdogs.delete( itemName );
 
 				if ( !watchdog ) {
-					throw new Error( 'Watchdog with the given name was not added: ' + itemName );
+					throw new Error( `There is no watchdog named: '${ itemName }'.` );
 				}
 
 				await watchdog.destroy();
@@ -166,17 +170,16 @@ export default class ContextWatchdog extends Watchdog {
 
 			await Promise.all(
 				Array.from( this._watchdogs.values() )
-					.map( watchdog => this._setupWatchdog( watchdog ) )
+					.map( async watchdog => {
+						watchdog.updateContext( this._context );
+						await watchdog.create();
+					} )
 			);
 
 			this.state = 'ready';
 		}, isInternal );
 	}
 
-	async _setupWatchdog( watchdog ) {
-		watchdog.updateContext( this._context );
-	}
-
 	async _destroy( isInternal = false ) {
 		await this._actionQueue.enqueue( async () => {
 			this._stopErrorHandling();
@@ -261,7 +264,7 @@ class ActionQueue {
 		}
 	}
 
-	async clear() {
+	clear() {
 		this._queuedActions = [];
 	}
 }

+ 5 - 4
packages/ckeditor5-watchdog/src/editorwatchdog.js

@@ -119,7 +119,10 @@ export default class EditorWatchdog extends Watchdog {
 	 * @param {Function} destructor
 	 */
 
-	/** @param {Context} */
+	/**
+	 * @protected
+	 * @param {Context}
+	 */
 	updateContext( context ) {
 		this._config.context = context;
 	}
@@ -186,8 +189,6 @@ export default class EditorWatchdog extends Watchdog {
 		// when an error occurs in one of these editors, the watchdog will restart all of them.
 		this._config = this._cloneConfig( config );
 
-		// console.log( this._config.context.toString() );
-
 		const editor = await this._creator( elementOrData, this._config );
 
 		this._editor = editor;
@@ -289,7 +290,7 @@ export default class EditorWatchdog extends Watchdog {
 			}
 
 			if ( key === 'context' ) {
-				return context;
+				return value;
 			}
 		} );
 	}

+ 82 - 3
packages/ckeditor5-watchdog/tests/contextwatchdog.js

@@ -75,7 +75,7 @@ describe( 'ContextWatchdog', () => {
 		await mainWatchdog.destroy();
 	} );
 
-	describe( 'for no items added', () => {
+	describe( 'for scenario with no items', () => {
 		it( 'should create only context', async () => {
 			mainWatchdog = ContextWatchdog.for( Context, {} );
 
@@ -140,8 +140,8 @@ describe( 'ContextWatchdog', () => {
 		} );
 	} );
 
-	describe( 'for multiple editors', () => {
-		it( 'should allow adding multiple items without waiting', async () => {
+	describe( 'for multiple items scenario', () => {
+		it( 'should allow adding multiple items without waiting for promises', async () => {
 			mainWatchdog = ContextWatchdog.for( Context, {} );
 
 			mainWatchdog.add( {
@@ -203,6 +203,47 @@ describe( 'ContextWatchdog', () => {
 			expect( err.message ).to.match( /Watchdog with the given name is already added: 'editor1'./ );
 		} );
 
+		it( 'should throw when not added item is removed', async () => {
+			mainWatchdog = ContextWatchdog.for( Context, {} );
+
+			await mainWatchdog.waitForReady();
+
+			let err;
+
+			try {
+				await mainWatchdog.remove( [ 'foo' ] );
+			} catch ( _err ) {
+				err = _err;
+			}
+
+			mainWatchdog._actionQueue.clear();
+
+			await mainWatchdog.destroy();
+
+			expect( err ).to.be.instanceOf( Error );
+			expect( err.message ).to.match( /There is no watchdog named: 'foo'./ );
+		} );
+
+		it( 'should allow setting editor custom destructors', async () => {
+			mainWatchdog = ContextWatchdog.for( Context, {} );
+
+			const destructorSpy = sinon.spy( editor => editor.destroy() );
+
+			mainWatchdog.add( {
+				editor1: {
+					type: 'editor',
+					creator: ( el, config ) => ClassicTestEditor.create( el, config ),
+					destructor: destructorSpy,
+					sourceElementOrData: element1,
+					config: {},
+				},
+			} );
+
+			await mainWatchdog.destroy();
+
+			sinon.assert.calledOnce( destructorSpy );
+		} );
+
 		it( 'should throw when the item is of not known type', async () => {
 			mainWatchdog = ContextWatchdog.for( Context, {} );
 
@@ -295,9 +336,47 @@ describe( 'ContextWatchdog', () => {
 
 				sinon.assert.calledOnce( restartSpy );
 
+				expect( mainWatchdog._watchdogs.get( 'editor1' ).state ).to.equal( 'ready' );
 				expect( mainWatchdog.context ).to.not.equal( oldContext );
 
 				window.onerror = originalErrorHandler;
+
+				await mainWatchdog.destroy();
+			} );
+
+			it( 'should restart the editor if an error happens inside the editor', async () => {
+				mainWatchdog = ContextWatchdog.for( Context, {} );
+				mainWatchdog.add( {
+					editor1: {
+						type: 'editor',
+						creator: ( el, config ) => ClassicTestEditor.create( el, config ),
+						sourceElementOrData: element1,
+						config: {}
+					}
+				} );
+
+				await mainWatchdog.waitForReady();
+
+				const oldContext = mainWatchdog.context;
+				const restartSpy = sinon.spy();
+				const originalErrorHandler = window.onerror;
+
+				window.onerror = sinon.spy();
+
+				mainWatchdog.on( 'restart', restartSpy );
+
+				setTimeout( () => throwCKEditorError( 'foo', mainWatchdog.context ) );
+
+				await waitCycle();
+
+				window.onerror = originalErrorHandler;
+
+				sinon.assert.calledOnce( restartSpy );
+
+				expect( mainWatchdog._watchdogs.get( 'editor1' ).state ).to.equal( 'ready' );
+				expect( mainWatchdog.context ).to.not.equal( oldContext );
+
+				await mainWatchdog.destroy();
 			} );
 		} );
 	} );