Bladeren bron

Merge pull request #207 from ckeditor/i/6002

Tests: Fixed tests leaking editor instances and DOM elements. See ckeditor/ckeditor5#6002.
Aleksander Nowodzinski 6 jaren geleden
bovenliggende
commit
323281314f

+ 8 - 0
packages/ckeditor5-core/tests/_utils-tests/classictesteditor.js

@@ -23,6 +23,7 @@ import RootElement from '@ckeditor/ckeditor5-engine/src/model/rootelement';
 import { getData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
 import { getData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
 import testUtils from '../../tests/_utils/utils';
 import testUtils from '../../tests/_utils/utils';
 import { assertCKEditorError } from '@ckeditor/ckeditor5-utils/tests/_utils/utils';
 import { assertCKEditorError } from '@ckeditor/ckeditor5-utils/tests/_utils/utils';
+import { removeEditorBodyOrphans } from '../_utils/cleanup';
 
 
 describe( 'ClassicTestEditor', () => {
 describe( 'ClassicTestEditor', () => {
 	let editorElement;
 	let editorElement;
@@ -89,6 +90,8 @@ describe( 'ClassicTestEditor', () => {
 
 
 					expect( editor.config.get( 'foo' ) ).to.equal( 1 );
 					expect( editor.config.get( 'foo' ) ).to.equal( 1 );
 					expect( editor.sourceElement ).to.equal( editorElement );
 					expect( editor.sourceElement ).to.equal( editorElement );
+
+					return editor.destroy();
 				} );
 				} );
 		} );
 		} );
 
 
@@ -102,6 +105,8 @@ describe( 'ClassicTestEditor', () => {
 					expect( ui.getEditableElement().tagName ).to.equal( 'DIV' );
 					expect( ui.getEditableElement().tagName ).to.equal( 'DIV' );
 					expect( ui.getEditableElement() ).to.equal( view.editable.element );
 					expect( ui.getEditableElement() ).to.equal( view.editable.element );
 					expect( view.editable.name ).to.equal( 'main' );
 					expect( view.editable.name ).to.equal( 'main' );
+
+					return editor.destroy();
 				} );
 				} );
 		} );
 		} );
 
 
@@ -117,6 +122,8 @@ describe( 'ClassicTestEditor', () => {
 			return ClassicTestEditor.create( editorElement, { plugins: [ PluginTextInRoot ] } )
 			return ClassicTestEditor.create( editorElement, { plugins: [ PluginTextInRoot ] } )
 				.then( editor => {
 				.then( editor => {
 					expect( getData( editor.model, { withoutSelection: true } ) ).to.equal( 'foo' );
 					expect( getData( editor.model, { withoutSelection: true } ) ).to.equal( 'foo' );
+
+					return editor.destroy();
 				} );
 				} );
 		} );
 		} );
 
 
@@ -226,6 +233,7 @@ describe( 'ClassicTestEditor', () => {
 					throw new Error( 'It should throw an error' );
 					throw new Error( 'It should throw an error' );
 				}, err => {
 				}, err => {
 					assertCKEditorError( err, /^editor-create-initial-data:/, null );
 					assertCKEditorError( err, /^editor-create-initial-data:/, null );
+					removeEditorBodyOrphans();
 				} );
 				} );
 		} );
 		} );
 	} );
 	} );

+ 27 - 0
packages/ckeditor5-core/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 - 0
packages/ckeditor5-core/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();
+	}
+}