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 #207 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 committed Jan 7, 2020
2 parents 8a88cb5 + 4e3c55e commit 8376e9c
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 0 deletions.
8 changes: 8 additions & 0 deletions tests/_utils-tests/classictesteditor.js
Expand Up @@ -23,6 +23,7 @@ import RootElement from '@ckeditor/ckeditor5-engine/src/model/rootelement';
import { getData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
import testUtils from '../../tests/_utils/utils';
import { assertCKEditorError } from '@ckeditor/ckeditor5-utils/tests/_utils/utils';
import { removeEditorBodyOrphans } from '../_utils/cleanup';

describe( 'ClassicTestEditor', () => {
let editorElement;
Expand Down Expand Up @@ -89,6 +90,8 @@ describe( 'ClassicTestEditor', () => {

expect( editor.config.get( 'foo' ) ).to.equal( 1 );
expect( editor.sourceElement ).to.equal( editorElement );

return editor.destroy();
} );
} );

Expand All @@ -102,6 +105,8 @@ describe( 'ClassicTestEditor', () => {
expect( ui.getEditableElement().tagName ).to.equal( 'DIV' );
expect( ui.getEditableElement() ).to.equal( view.editable.element );
expect( view.editable.name ).to.equal( 'main' );

return editor.destroy();
} );
} );

Expand All @@ -117,6 +122,8 @@ describe( 'ClassicTestEditor', () => {
return ClassicTestEditor.create( editorElement, { plugins: [ PluginTextInRoot ] } )
.then( editor => {
expect( getData( editor.model, { withoutSelection: true } ) ).to.equal( 'foo' );

return editor.destroy();
} );
} );

Expand Down Expand Up @@ -226,6 +233,7 @@ describe( 'ClassicTestEditor', () => {
throw new Error( 'It should throw an error' );
}, err => {
assertCKEditorError( err, /^editor-create-initial-data:/, null );
removeEditorBodyOrphans();
} );
} );
} );
Expand Down
27 changes: 27 additions & 0 deletions tests/_utils-tests/cleanup.js
@@ -0,0 +1,27 @@
/**
* @license Copyright (c) 2003-2019, CKSource - Frederico Knabben. All rights reserved.
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
*/

/* globals document */

import Locale from '@ckeditor/ckeditor5-utils/src/locale';
import EditorUIView from '@ckeditor/ckeditor5-ui/src/editorui/editoruiview';
import { removeEditorBodyOrphans } from '../_utils/cleanup';

describe( 'cleanup util', () => {
describe( 'removeEditorBodyOrphans()', () => {
const locale = new Locale();
const uiViews = [ new EditorUIView( locale ), new EditorUIView( locale ) ];

for ( const view of uiViews ) {
view.render();
}

expect( document.querySelectorAll( '.ck-body' ) ).to.have.length( 2 );

removeEditorBodyOrphans();

expect( document.querySelectorAll( '.ck-body' ) ).to.have.length( 0 );
} );
} );
19 changes: 19 additions & 0 deletions tests/_utils/cleanup.js
@@ -0,0 +1,19 @@
/**
* @license Copyright (c) 2003-2020, CKSource - Frederico Knabben. All rights reserved.
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
*/

/* global document */

/**
* Removes all the `.ck-body` elements available in the DOM.
*
* It is commonly used to cleanup after editors that test editor crashes.
*
* See https://github.com/ckeditor/ckeditor5/issues/6018 for more details.
*/
export function removeEditorBodyOrphans() {
for ( const bodyOrphan of document.querySelectorAll( '.ck-body' ) ) {
bodyOrphan.remove();
}
}

0 comments on commit 8376e9c

Please sign in to comment.