Ver código fonte

Added test for the `Watchdog#destroy()` method.

Maciej Bukowski 6 anos atrás
pai
commit
531bc4947b

+ 7 - 2
packages/ckeditor5-watchdog/src/watchdog.js

@@ -95,7 +95,10 @@ export default class Watchdog {
 		 * @private
 		 * @type {Function}
 		 */
-		this._throttledSave = throttle( this._save.bind( this ), config.saveInterval || 5000 );
+		this._throttledSave = throttle(
+			this._save.bind( this ),
+			typeof config.saveInterval === 'number' ? config.saveInterval : 5000
+		);
 
 		/**
 		 * The current editor instance.
@@ -266,7 +269,7 @@ export default class Watchdog {
 		window.removeEventListener( 'error', this._boundErrorHandler );
 		this.stopListening( this._editor.model.document, 'change:data', this._throttledSave );
 
-		// Save data if there are remaining changes.
+		// Save data if there is a remaining editor data change.
 		this._throttledSave.flush();
 
 		return Promise.resolve()
@@ -285,6 +288,8 @@ export default class Watchdog {
 	_save() {
 		const version = this._editor.model.document.version;
 
+		console.log( 'saving' );
+
 		// Change may not produce an operation, so the document's version
 		// can be the same after that change.
 		if ( version === this._lastDocumentVersion ) {

+ 40 - 2
packages/ckeditor5-watchdog/tests/watchdog.js

@@ -369,7 +369,7 @@ describe( 'Watchdog', () => {
 		} );
 
 		it( 'Watchdog should crash permanently if the `crashNumberLimit` is reached' +
-		' and the average time between errors is lower than `minimumNonErrorTimePeriod` (default values)', () => {
+			' and the average time between errors is lower than `minimumNonErrorTimePeriod` (default values)', () => {
 			const watchdog = new Watchdog();
 
 			watchdog.setCreator( ( el, config ) => ClassicTestEditor.create( el, config ) );
@@ -406,7 +406,7 @@ describe( 'Watchdog', () => {
 		} );
 
 		it( 'Watchdog should crash permanently if the `crashNumberLimit` is reached' +
-		' and the average time between errors is lower than `minimumNonErrorTimePeriod` (custom values)', () => {
+			' and the average time between errors is lower than `minimumNonErrorTimePeriod` (custom values)', () => {
 			const watchdog = new Watchdog( { crashNumberLimit: 2, minimumNonErrorTimePeriod: 1000 } );
 
 			watchdog.setCreator( ( el, config ) => ClassicTestEditor.create( el, config ) );
@@ -695,6 +695,44 @@ describe( 'Watchdog', () => {
 		} );
 	} );
 
+	describe( 'destroy()', () => {
+		// See #19.
+		it( 'should clean internal stuff', () => {
+			// 30ms should be enough to make the two data changes split into two data save actions.
+			// This will ensure that the second data save action will be put off in time.
+			const SAVE_INTERVAL = 30;
+
+			const watchdog = Watchdog.for( ClassicTestEditor, {
+				saveInterval: SAVE_INTERVAL,
+			} );
+
+			return watchdog.create( element, {
+				initialData: '<p>foo</p>',
+				plugins: [ Paragraph ]
+			} ).then( () => {
+				const doc = watchdog.editor.model.document;
+
+				watchdog.editor.model.change( writer => {
+					writer.insertText( 'bar', writer.createPositionAt( doc.getRoot(), 1 ) );
+				} );
+
+				watchdog.editor.model.change( writer => {
+					writer.insertText( 'foo', writer.createPositionAt( doc.getRoot(), 1 ) );
+				} );
+			} ).then( () => {
+				return watchdog.destroy();
+			} ).then( () => {
+				// Wait to ensure that the throttled save is cleared and won't be executed
+				// on the non-existing editor.
+				return new Promise( res => setTimeout( res, SAVE_INTERVAL ) );
+			} ).then( () => {
+				expect( watchdog.editor ).to.equal( null );
+				expect( watchdog.state ).to.equal( 'destroyed' );
+				expect( watchdog.crashes ).to.deep.equal( [] );
+			} );
+		} );
+	} );
+
 	describe( 'static for()', () => {
 		it( 'should be a shortcut method for creating the watchdog', () => {
 			const watchdog = Watchdog.for( ClassicTestEditor );