Parcourir la source

Improved the tests.

Kamil Piechaczek il y a 7 ans
Parent
commit
c563e12083

+ 11 - 8
packages/ckeditor5-editor-balloon/src/ballooneditor.js

@@ -19,7 +19,6 @@ import ElementApiMixin from '@ckeditor/ckeditor5-core/src/editor/utils/elementap
 import attachToForm from '@ckeditor/ckeditor5-core/src/editor/utils/attachtoform';
 import mix from '@ckeditor/ckeditor5-utils/src/mix';
 import isElement from '@ckeditor/ckeditor5-utils/src/lib/lodash/isElement';
-import global from '@ckeditor/ckeditor5-utils/src/dom/global';
 
 /**
  * The {@glink builds/guides/overview#balloon-editor balloon editor} implementation (Medium-like editor).
@@ -77,8 +76,6 @@ export default class BalloonEditor extends Editor {
 
 		if ( isElement( sourceElementOrData ) ) {
 			this.sourceElement = sourceElementOrData;
-		} else {
-			this.sourceElement = global.document.createElement( 'div' );
 		}
 
 		this.config.get( 'plugins' ).push( BalloonToolbar );
@@ -94,9 +91,7 @@ export default class BalloonEditor extends Editor {
 	}
 
 	/**
-	 * An HTML element that represents the whole editor. See the {@link module:core/editor/editorwithui~EditorWithUI} interface.
-	 *
-	 * @returns {HTMLElement|null}
+	 * @inheritDoc
 	 */
 	get element() {
 		return this.ui.view.editable.element;
@@ -117,7 +112,11 @@ export default class BalloonEditor extends Editor {
 		this.ui.destroy();
 
 		return super.destroy()
-			.then( () => setDataInElement( this.sourceElement, data ) );
+			.then( () => {
+				if ( this.sourceElement ) {
+					setDataInElement( this.sourceElement, data );
+				}
+			} );
 	}
 
 	/**
@@ -195,7 +194,11 @@ export default class BalloonEditor extends Editor {
 						editor.ui.init();
 						editor.fire( 'uiReady' );
 					} )
-					.then( () => editor.data.init( isElement( sourceElementOrData ) ? getDataFromElement( sourceElementOrData ) : sourceElementOrData ) )
+					.then( () => {
+						const data = isElement( sourceElementOrData ) ? getDataFromElement( sourceElementOrData ) : sourceElementOrData;
+
+						return editor.data.init( data );
+					} )
 					.then( () => {
 						editor.fire( 'dataReady' );
 						editor.fire( 'ready' );

+ 20 - 9
packages/ckeditor5-editor-balloon/tests/ballooneditor.js

@@ -3,7 +3,7 @@
  * For licensing, see LICENSE.md.
  */
 
-/* globals document, Event, HTMLElement */
+/* globals document, Event */
 
 import BalloonEditorUI from '../src/ballooneditorui';
 import BalloonEditorUIView from '../src/ballooneditoruiview';
@@ -111,16 +111,19 @@ describe( 'BalloonEditor', () => {
 			} );
 		} );
 
-		it( 'editor.sourceElement should contain created div element', () => {
-			return BalloonEditor.create( '<p>Hello world!</p>', {
-				plugins: [ Paragraph ]
-			} ).then( editor => {
-				expect( editor.sourceElement ).to.be.instanceOf( HTMLElement );
-				expect( editor.sourceElement.tagName ).to.equal( 'DIV' );
-			} );
+		it( 'should have undefined the #sourceElement if editor was initialized with data', () => {
+			return BalloonEditor
+				.create( '<p>Foo.</p>', {
+					plugins: [ Paragraph, Bold ]
+				} )
+				.then( newEditor => {
+					expect( newEditor.sourceElement ).to.be.undefined;
+
+					return newEditor.destroy();
+				} );
 		} );
 
-		it( 'editor.element should contain created div element (the whole editor with UI)', () => {
+		it( 'editor.element should contain the whole editor (with UI) element', () => {
 			return BalloonEditor.create( '<p>Hello world!</p>', {
 				plugins: [ Paragraph ]
 			} ).then( editor => {
@@ -304,5 +307,13 @@ describe( 'BalloonEditor', () => {
 						.to.equal( '<p>a</p><heading>b</heading>' );
 				} );
 		} );
+
+		it( 'should not throw an error if editor was initialized with the data', () => {
+			return BalloonEditor
+				.create( '<p>Foo.</p>', {
+					plugins: [ Paragraph, Bold ]
+				} )
+				.then( newEditor => newEditor.destroy() );
+		} );
 	} );
 } );