Procházet zdrojové kódy

Added more error handling tests.

Maciej Bukowski před 6 roky
rodič
revize
a3dda1d9a9
1 změnil soubory, kde provedl 23 přidání a 47 odebrání
  1. 23 47
      packages/ckeditor5-watchdog/tests/watchdog.js

+ 23 - 47
packages/ckeditor5-watchdog/tests/watchdog.js

@@ -5,26 +5,17 @@
 
 import Watchdog from '../src/watchdog';
 import VirtualTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/virtualtesteditor';
-import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
 import CKEditorError from '@ckeditor/ckeditor5-utils/src/ckeditorerror';
 
 describe( 'Watchdog', () => {
-	testUtils.createSinonSandbox();
-
-	beforeEach( () => {
-
-	} );
-
-	afterEach( () => {
-
-	} );
+	afterEach( () => sinon.restore() );
 
 	describe( 'create()', () => {
 		it( 'should create an editor instance', () => {
 			const watchdog = new Watchdog();
 
-			const editorCreateSpy = testUtils.sinon.spy( VirtualTestEditor, 'create' );
-			const editorDestroySpy = testUtils.sinon.spy( VirtualTestEditor.prototype, 'destroy' );
+			const editorCreateSpy = sinon.spy( VirtualTestEditor, 'create' );
+			const editorDestroySpy = sinon.spy( VirtualTestEditor.prototype, 'destroy' );
 
 			watchdog.setCreator( ( el, config ) => VirtualTestEditor.create( el, config ) );
 			watchdog.setDestructor( editor => editor.destroy() );
@@ -47,8 +38,8 @@ describe( 'Watchdog', () => {
 		it( 'should restart the editor', () => {
 			const watchdog = new Watchdog();
 
-			const editorCreateSpy = testUtils.sinon.spy( VirtualTestEditor, 'create' );
-			const editorDestroySpy = testUtils.sinon.spy( VirtualTestEditor.prototype, 'destroy' );
+			const editorCreateSpy = sinon.spy( VirtualTestEditor, 'create' );
+			const editorDestroySpy = sinon.spy( VirtualTestEditor.prototype, 'destroy' );
 
 			watchdog.setCreator( ( el, config ) => VirtualTestEditor.create( el, config ) );
 			watchdog.setDestructor( editor => editor.destroy() );
@@ -156,7 +147,7 @@ describe( 'Watchdog', () => {
 				);
 		} );
 
-		it( 'Watchdog should catch editor errors and restart the editor during the runtime', done => {
+		it( 'Watchdog should catch editor errors and restart the editor during the runtime', () => {
 			const watchdog = new Watchdog();
 			const FakeEditor = getFakeEditor();
 
@@ -167,35 +158,43 @@ describe( 'Watchdog', () => {
 			const originalErrorHandler = window.onerror;
 			window.onerror = undefined;
 
-			watchdog.create( document.createElement( 'div' ) ).then( () => {
+			return watchdog.create( document.createElement( 'div' ) ).then( () => {
 				setTimeout( () => { watchdog.editor.throwEditorError() } );
 
-				watchdog.on( 'restart', () => {
-					window.onerror = originalErrorHandler;
-					done();
-				} )
+				return new Promise( res => {
+					watchdog.on( 'restart', () => {
+						window.onerror = originalErrorHandler;
+						res();
+					} );
+				} );
 			} );
 		} );
 
-		it( 'Watchdog should catch editor errors and restart the editor during the runtime', () => {
+		it( 'Watchdog should not catch non-editor errors', () => {
 			const watchdog = new Watchdog();
 			const FakeEditor = getFakeEditor();
 
 			watchdog.setCreator( ( el, config ) => FakeEditor.create( el, config ) );
 			watchdog.setDestructor( editor => editor.destroy() );
 
+			const editorErrorSpy = sinon.spy();
+			watchdog.on( 'error', editorErrorSpy );
+
 			// sinon.stub( window, 'onerror' ).value( undefined ); and similar don't work.
 			const originalErrorHandler = window.onerror;
 			window.onerror = undefined;
 
 			return watchdog.create( document.createElement( 'div' ) ).then( () => {
-				setTimeout( () => { throw new CKEditorError( 'foo', undefined, watchdog.editor ); } );
+				setTimeout( () => { throw new Error( 'foo' ); } );
 
 				return new Promise( res => {
-					watchdog.on( 'restart', () => {
+					setTimeout( () => {
 						window.onerror = originalErrorHandler;
+
+						sinon.assert.notCalled( editorErrorSpy );
+
 						res();
-					} )
+					}, 5 );
 				} );
 			} );
 		} );
@@ -243,29 +242,6 @@ describe( 'Watchdog', () => {
 			} );
 		} );
 
-		it( 'Watchdog should catch editor errors and restart the editor during the runtime', () => {
-			const watchdog = new Watchdog();
-			const FakeEditor = getFakeEditor();
-
-			watchdog.setCreator( ( el, config ) => FakeEditor.create( el, config ) );
-			watchdog.setDestructor( editor => editor.destroy() );
-
-			// sinon.stub( window, 'onerror' ).value( undefined ); and similar don't work.
-			const originalErrorHandler = window.onerror;
-			window.onerror = undefined;
-
-			return watchdog.create( document.createElement( 'div' ) ).then( () => {
-				setTimeout( () => { throw new CKEditorError( 'foo', undefined, watchdog.editor ); } );
-
-				return new Promise( res => {
-					watchdog.on( 'restart', () => {
-						window.onerror = originalErrorHandler;
-						res();
-					} )
-				} );
-			} );
-		} );
-
 		it( 'Watchdog should catch editor errors and restart the editor during the runtime if the editor can be found from the ctx', () => {
 			const watchdog = new Watchdog();
 			const FakeEditor = getFakeEditor();