Parcourir la source

WIP - more tests.

Maciej Bukowski il y a 5 ans
Parent
commit
57677854e3

+ 9 - 1
packages/ckeditor5-watchdog/src/contextwatchdog.js

@@ -75,6 +75,10 @@ export default class ContextWatchdog extends Watchdog {
 			await Promise.all( Object.entries( items ).map( async ( [ itemName, itemConfig ] ) => {
 				let watchdog;
 
+				if ( this._watchdogs.has( itemName ) ) {
+					throw new Error( `Watchdog with the given name is already added: '${ itemName }'.` );
+				}
+
 				if ( itemConfig.type === 'editor' ) {
 					watchdog = new EditorWatchdog();
 					watchdog.setCreator( itemConfig.creator );
@@ -87,7 +91,7 @@ export default class ContextWatchdog extends Watchdog {
 
 					await watchdog.create( itemConfig.sourceElementOrData, itemConfig.config );
 				} else {
-					throw new Error( 'Not supported editor type ' + itemConfig.type );
+					throw new Error( `Not supported item type: '${ itemConfig.type }'.` );
 				}
 			} ) );
 
@@ -256,4 +260,8 @@ class ActionQueue {
 			}
 		}
 	}
+
+	async clear() {
+		this._queuedActions = [];
+	}
 }

+ 100 - 23
packages/ckeditor5-watchdog/tests/contextwatchdog.js

@@ -31,6 +31,29 @@ describe( 'ContextWatchdog', () => {
 		sinon.restore();
 	} );
 
+	it( 'should disable adding items once the Watchdog is destroyed', async () => {
+		mainWatchdog = ContextWatchdog.for( Context, {} );
+
+		await mainWatchdog.destroy();
+		let err;
+
+		try {
+			await mainWatchdog.add( {
+				editor2: {
+					type: 'editor',
+					creator: ( el, config ) => ClassicTestEditor.create( el, config ),
+					sourceElementOrData: element1,
+					config: {}
+				},
+			} );
+		} catch ( _err ) {
+			err = _err;
+		}
+
+		expect( err ).to.be.instanceOf( Error );
+		expect( err.message ).to.match( /Cannot add items do destroyed watchdog\./ );
+	} );
+
 	it.skip( 'case: editor, contextItem', async () => {
 		mainWatchdog = ContextWatchdog.for( Context, {} );
 
@@ -77,6 +100,19 @@ describe( 'ContextWatchdog', () => {
 			expect( mainWatchdog.state ).to.equal( 'destroyed' );
 		} );
 
+		it( 'should set custom destructor if provided', async () => {
+			const mainWatchdog = new ContextWatchdog();
+			const customDestructor = sinon.spy( context => context.destroy() );
+
+			mainWatchdog.setCreator( config => Context.create( config ) );
+			mainWatchdog.setDestructor( customDestructor );
+			mainWatchdog._create();
+
+			await mainWatchdog.destroy();
+
+			sinon.assert.calledOnce( customDestructor );
+		} );
+
 		describe( 'in case of error handling', () => {
 			it( 'should restart the `Context`', async () => {
 				mainWatchdog = ContextWatchdog.for( Context, {} );
@@ -131,6 +167,70 @@ describe( 'ContextWatchdog', () => {
 			await mainWatchdog.destroy();
 		} );
 
+		it( 'should throw when multiple items with the same name are added', async () => {
+			mainWatchdog = ContextWatchdog.for( Context, {} );
+
+			mainWatchdog.add( {
+				editor1: {
+					type: 'editor',
+					creator: ( el, config ) => ClassicTestEditor.create( el, config ),
+					sourceElementOrData: element1,
+					config: {}
+				},
+			} );
+
+			await mainWatchdog.waitForReady();
+
+			let err;
+			try {
+				await mainWatchdog.add( {
+					editor1: {
+						type: 'editor',
+						creator: ( el, config ) => ClassicTestEditor.create( el, config ),
+						sourceElementOrData: element1,
+						config: {}
+					},
+				} );
+			} catch ( _err ) {
+				err = _err;
+			}
+
+			mainWatchdog._actionQueue.clear();
+
+			await mainWatchdog.destroy();
+
+			expect( err ).to.be.instanceOf( Error );
+			expect( err.message ).to.match( /Watchdog with the given name is already added: 'editor1'./ );
+		} );
+
+		it( 'should throw when the item is of not known type', async () => {
+			mainWatchdog = ContextWatchdog.for( Context, {} );
+
+			await mainWatchdog.waitForReady();
+
+			let err;
+			try {
+				await mainWatchdog.add( {
+					editor1: {
+						type: 'foo',
+						creator: ( el, config ) => ClassicTestEditor.create( el, config ),
+						sourceElementOrData: element1,
+						config: {}
+					},
+				} );
+			} catch ( _err ) {
+				mainWatchdog._stopErrorHandling();
+				err = _err;
+			}
+
+			mainWatchdog._actionQueue.clear();
+
+			await mainWatchdog.destroy();
+
+			expect( err ).to.be.instanceOf( Error );
+			expect( err.message ).to.match( /Not supported item type: 'foo'\./ );
+		} );
+
 		it( 'should allow adding and removing items without waiting', async () => {
 			mainWatchdog = ContextWatchdog.for( Context, {} );
 
@@ -201,29 +301,6 @@ describe( 'ContextWatchdog', () => {
 			} );
 		} );
 	} );
-
-	it( 'case: recreating watchdog', async () => {
-		mainWatchdog = ContextWatchdog.for( Context, {} );
-
-		await mainWatchdog.destroy();
-		let err;
-
-		try {
-			await mainWatchdog.add( {
-				editor2: {
-					type: 'editor',
-					creator: ( el, config ) => ClassicTestEditor.create( el, config ),
-					sourceElementOrData: element1,
-					config: {}
-				},
-			} );
-		} catch ( _err ) {
-			err = _err;
-		}
-
-		expect( err ).to.be.instanceOf( Error );
-		expect( err.message ).to.match( /Cannot add items do destroyed watchdog\./ );
-	} );
 } );
 
 function throwCKEditorError( name, context ) {