Преглед изворни кода

Tests: Fixed leaked DOM nodes. Added commonly used methods for cleaning up editors by other test suites.

Marek Lewandowski пре 6 година
родитељ
комит
61855eae8d

+ 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 testUtils from '../../tests/_utils/utils';
 import { assertCKEditorError } from '@ckeditor/ckeditor5-utils/tests/_utils/utils';
+import { removeEditorBodyOrphans } from '@ckeditor/ckeditor5-core/tests/_utils/cleanup';
 
 describe( 'ClassicTestEditor', () => {
 	let editorElement;
@@ -89,6 +90,8 @@ describe( 'ClassicTestEditor', () => {
 
 					expect( editor.config.get( 'foo' ) ).to.equal( 1 );
 					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() ).to.equal( view.editable.element );
 					expect( view.editable.name ).to.equal( 'main' );
+
+					return editor.destroy();
 				} );
 		} );
 
@@ -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();
 				} );
 		} );
 
@@ -226,6 +233,7 @@ describe( 'ClassicTestEditor', () => {
 					throw new Error( 'It should throw an error' );
 				}, err => {
 					assertCKEditorError( err, /^editor-create-initial-data:/, null );
+					removeEditorBodyOrphans();
 				} );
 		} );
 	} );

+ 37 - 0
packages/ckeditor5-core/tests/_utils/cleanup.js

@@ -0,0 +1,37 @@
+/**
+ * @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();
+	}
+}
+
+/**
+ * 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.
+ */
+export 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 );
+}