Skip to content
This repository has been archived by the owner on Jun 26, 2020. It is now read-only.

Commit

Permalink
Merge pull request #32 from ckeditor/i/6002
Browse files Browse the repository at this point in the history
Tests: Fixed tests leaking editor instances and DOM elements. See ckeditor/ckeditor5#6002.
  • Loading branch information
oleq authored Jan 7, 2020
2 parents ae8c7b3 + 8feb228 commit 72628be
Showing 1 changed file with 41 additions and 8 deletions.
49 changes: 41 additions & 8 deletions tests/watchdog.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,6 @@ describe( 'Watchdog', () => {
);
} );

it( 'should not throw an error when the destructor is not defined', () => {
const watchdog = new Watchdog();
watchdog.setCreator( ( el, config ) => ClassicTestEditor.create( el, config ) );

expect( () => watchdog.create() ).to.not.throw();
} );

it( 'should properly copy the config', () => {
const watchdog = new Watchdog();
watchdog.setCreator( ( el, config ) => ClassicTestEditor.create( el, config ) );
Expand All @@ -77,6 +70,8 @@ describe( 'Watchdog', () => {
return watchdog.create( element, config ).then( () => {
expect( watchdog.editor.config._config.foo ).to.not.equal( config.foo );
expect( watchdog.editor.config._config.bar ).to.equal( config.bar );

return watchdog.destroy();
} );
} );

Expand Down Expand Up @@ -164,6 +159,8 @@ describe( 'Watchdog', () => {
err => {
expect( err ).to.be.instanceOf( Error );
expect( err.message ).to.equal( 'foo' );

return destroyEditorOrphans();
}
);
} );
Expand All @@ -182,6 +179,8 @@ describe( 'Watchdog', () => {
err => {
expect( err ).to.be.instanceOf( Error );
expect( err.message ).to.equal( 'foo' );

return destroyEditorOrphans();
}
);
} );
Expand Down Expand Up @@ -651,6 +650,9 @@ describe( 'Watchdog', () => {
const editorGetDataError = new Error( 'Some error' );
const getDataStub = sinon.stub( watchdog.editor.data, 'get' )
.throwsException( editorGetDataError );
// Keep the reference to cleanly destroy it at in the end, as during the TC it
// throws an exception during destruction.
const firstEditor = watchdog.editor;

setTimeout( () => throwCKEditorError( 'foo', watchdog.editor ) );

Expand Down Expand Up @@ -681,7 +683,10 @@ describe( 'Watchdog', () => {
'An error happened during the editor destructing.'
);

watchdog.destroy().then( res );
watchdog.destroy().then( () => {
getDataStub.restore();
return firstEditor.destroy();
} ).then( res );
} );
} );
} );
Expand Down Expand Up @@ -715,6 +720,22 @@ describe( 'Watchdog', () => {
} );
} );
} );

// Searches for orphaned editors based on DOM.
//
// This is useful if in your tests you have no access to editor, instance because editor
// creation method doesn't complete in a graceful manner.
function destroyEditorOrphans() {
const promises = [];

for ( const editableOrphan of document.querySelectorAll( '.ck-editor__editable' ) ) {
if ( editableOrphan.ckeditorInstance ) {
promises.push( editableOrphan.ckeditorInstance.destroy() );
}
}

return Promise.all( promises );
}
} );

describe( 'async error handling', () => {
Expand Down Expand Up @@ -929,6 +950,15 @@ describe( 'Watchdog', () => {
} );

describe( 'state', () => {
let orphanEditors = [];

afterEach( () => {
return Promise.all( orphanEditors.map( editor => editor.destroy() ) )
.then( () => {
orphanEditors = [];
} );
} );

it( 'should reflect the state of the watchdog', () => {
const watchdog = Watchdog.for( ClassicTestEditor );

Expand All @@ -939,6 +969,7 @@ describe( 'Watchdog', () => {
expect( watchdog.state ).to.equal( 'initializing' );

return watchdog.create( element ).then( () => {
orphanEditors.push( watchdog.editor );
expect( watchdog.state ).to.equal( 'ready' );

return watchdog.create( element ).then( () => {
Expand Down Expand Up @@ -975,6 +1006,8 @@ describe( 'Watchdog', () => {
window.onerror = undefined;

return watchdog.create( element ).then( () => {
orphanEditors.push( watchdog.editor );

return watchdog.create( element ).then( () => {
setTimeout( () => throwCKEditorError( 'foo', watchdog.editor ) );
setTimeout( () => throwCKEditorError( 'bar', watchdog.editor ) );
Expand Down

0 comments on commit 72628be

Please sign in to comment.