Browse Source

Tests: Added tests to check the placeholder feature of the UI. Code refactoring.

Aleksander Nowodzinski 7 years ago
parent
commit
8234357911

+ 1 - 1
packages/ckeditor5-editor-balloon/src/ballooneditor.js

@@ -77,7 +77,7 @@ export default class BalloonEditor extends Editor {
 
 
 		this.model.document.createRoot();
 		this.model.document.createRoot();
 
 
-		const view = new BalloonEditorUIView( this.locale, this.sourceElement, this.editing.view );
+		const view = new BalloonEditorUIView( this.locale, this.editing.view, this.sourceElement );
 		this.ui = new BalloonEditorUI( this, view );
 		this.ui = new BalloonEditorUI( this, view );
 
 
 		attachToForm( this );
 		attachToForm( this );

+ 2 - 1
packages/ckeditor5-editor-balloon/src/ballooneditorui.js

@@ -125,7 +125,8 @@ export default class BalloonEditorUI extends EditorUI {
 		const editingView = editor.editing.view;
 		const editingView = editor.editing.view;
 		const editingRoot = editingView.document.getRoot();
 		const editingRoot = editingView.document.getRoot();
 
 
-		const placeholderText = editor.config.get( 'placeholder' ) || editor.sourceElement.getAttribute( 'placeholder' );
+		const placeholderText = editor.config.get( 'placeholder' ) ||
+			editor.sourceElement && editor.sourceElement.getAttribute( 'placeholder' );
 
 
 		if ( placeholderText ) {
 		if ( placeholderText ) {
 			enablePlaceholder( {
 			enablePlaceholder( {

+ 2 - 1
packages/ckeditor5-editor-balloon/src/ballooneditoruiview.js

@@ -20,10 +20,11 @@ export default class BalloonEditorUIView extends EditorUIView {
 	 * Creates an instance of the balloon editor UI view.
 	 * Creates an instance of the balloon editor UI view.
 	 *
 	 *
 	 * @param {module:utils/locale~Locale} locale The {@link module:core/editor/editor~Editor#locale} instance.
 	 * @param {module:utils/locale~Locale} locale The {@link module:core/editor/editor~Editor#locale} instance.
+	 * @param {module:engine/view/view~View} editingView The editing view instance this view is related to.
 	 * @param {HTMLElement} [editableElement] The editable element. If not specified, it will be automatically created by
 	 * @param {HTMLElement} [editableElement] The editable element. If not specified, it will be automatically created by
 	 * {@link module:ui/editableui/editableuiview~EditableUIView}. Otherwise, the given element will be used.
 	 * {@link module:ui/editableui/editableuiview~EditableUIView}. Otherwise, the given element will be used.
 	 */
 	 */
-	constructor( locale, editableElement, editingView ) {
+	constructor( locale, editingView, editableElement ) {
 		super( locale );
 		super( locale );
 
 
 		/**
 		/**

+ 70 - 15
packages/ckeditor5-editor-balloon/tests/ballooneditorui.js

@@ -3,14 +3,16 @@
  * For licensing, see LICENSE.md.
  * For licensing, see LICENSE.md.
  */
  */
 
 
-/* globals Event */
+/* globals document, Event */
 
 
 import BalloonEditorUI from '../src/ballooneditorui';
 import BalloonEditorUI from '../src/ballooneditorui';
 import EditorUI from '@ckeditor/ckeditor5-core/src/editor/editorui';
 import EditorUI from '@ckeditor/ckeditor5-core/src/editor/editorui';
 import BalloonEditorUIView from '../src/ballooneditoruiview';
 import BalloonEditorUIView from '../src/ballooneditoruiview';
 import VirtualTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/virtualtesteditor';
 import VirtualTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/virtualtesteditor';
 import BalloonToolbar from '@ckeditor/ckeditor5-ui/src/toolbar/balloon/balloontoolbar';
 import BalloonToolbar from '@ckeditor/ckeditor5-ui/src/toolbar/balloon/balloontoolbar';
+import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
 import { keyCodes } from '@ckeditor/ckeditor5-utils/src/keyboard';
 import { keyCodes } from '@ckeditor/ckeditor5-utils/src/keyboard';
+import { isElement } from 'lodash-es';
 
 
 import utils from '@ckeditor/ckeditor5-utils/tests/_utils/utils';
 import utils from '@ckeditor/ckeditor5-utils/tests/_utils/utils';
 import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
 import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
@@ -22,7 +24,7 @@ describe( 'BalloonEditorUI', () => {
 
 
 	beforeEach( () => {
 	beforeEach( () => {
 		return VirtualBalloonTestEditor
 		return VirtualBalloonTestEditor
-			.create( {
+			.create( 'foo', {
 				plugins: [ BalloonToolbar ]
 				plugins: [ BalloonToolbar ]
 			} )
 			} )
 			.then( newEditor => {
 			.then( newEditor => {
@@ -111,16 +113,65 @@ describe( 'BalloonEditorUI', () => {
 					{ isFocused: true }
 					{ isFocused: true }
 				);
 				);
 			} );
 			} );
+		} );
 
 
-			it( 'binds view.editable#isReadOnly', () => {
-				utils.assertBinding(
-					view.editable,
-					{ isReadOnly: false },
-					[
-						[ editable, { isReadOnly: true } ]
-					],
-					{ isReadOnly: true }
-				);
+		describe( 'placeholder', () => {
+			it( 'sets placeholder from editor.config.placeholder', () => {
+				return VirtualBalloonTestEditor
+					.create( 'foo', {
+						extraPlugins: [ BalloonToolbar, Paragraph ],
+						placeholder: 'placeholder-text',
+					} )
+					.then( newEditor => {
+						editor = newEditor;
+
+						const firstChild = editor.editing.view.document.getRoot().getChild( 0 );
+
+						expect( firstChild.getAttribute( 'data-placeholder' ) ).to.equal( 'placeholder-text' );
+
+						return editor.destroy();
+					} );
+			} );
+
+			it( 'sets placeholder from "placeholder" attribute of a passed element', () => {
+				const element = document.createElement( 'div' );
+
+				element.setAttribute( 'placeholder', 'placeholder-text' );
+
+				return VirtualBalloonTestEditor
+					.create( element, {
+						extraPlugins: [ BalloonToolbar, Paragraph ]
+					} )
+					.then( newEditor => {
+						editor = newEditor;
+
+						const firstChild = editor.editing.view.document.getRoot().getChild( 0 );
+
+						expect( firstChild.getAttribute( 'data-placeholder' ) ).to.equal( 'placeholder-text' );
+
+						return editor.destroy();
+					} );
+			} );
+
+			it( 'uses editor.config.placeholder rather than "placeholder" attribute of a passed element', () => {
+				const element = document.createElement( 'div' );
+
+				element.setAttribute( 'placeholder', 'placeholder-text' );
+
+				return VirtualBalloonTestEditor
+					.create( element, {
+						placeholder: 'config takes precedence',
+						extraPlugins: [ BalloonToolbar, Paragraph ]
+					} )
+					.then( newEditor => {
+						editor = newEditor;
+
+						const firstChild = editor.editing.view.document.getRoot().getChild( 0 );
+
+						expect( firstChild.getAttribute( 'data-placeholder' ) ).to.equal( 'config takes precedence' );
+
+						return editor.destroy();
+					} );
 			} );
 			} );
 		} );
 		} );
 	} );
 	} );
@@ -147,10 +198,14 @@ describe( 'BalloonEditorUI', () => {
 } );
 } );
 
 
 class VirtualBalloonTestEditor extends VirtualTestEditor {
 class VirtualBalloonTestEditor extends VirtualTestEditor {
-	constructor( config ) {
+	constructor( sourceElementOrData, config ) {
 		super( config );
 		super( config );
 
 
-		const view = new BalloonEditorUIView( this.locale );
+		if ( isElement( sourceElementOrData ) ) {
+			this.sourceElement = sourceElementOrData;
+		}
+
+		const view = new BalloonEditorUIView( this.locale, this.editing.view );
 		this.ui = new BalloonEditorUI( this, view );
 		this.ui = new BalloonEditorUI( this, view );
 	}
 	}
 
 
@@ -160,9 +215,9 @@ class VirtualBalloonTestEditor extends VirtualTestEditor {
 		return super.destroy();
 		return super.destroy();
 	}
 	}
 
 
-	static create( config ) {
+	static create( sourceElementOrData, config ) {
 		return new Promise( resolve => {
 		return new Promise( resolve => {
-			const editor = new this( config );
+			const editor = new this( sourceElementOrData, config );
 
 
 			resolve(
 			resolve(
 				editor.initPlugins()
 				editor.initPlugins()

+ 13 - 2
packages/ckeditor5-editor-balloon/tests/ballooneditoruiview.js

@@ -3,6 +3,8 @@
  * For licensing, see LICENSE.md.
  * For licensing, see LICENSE.md.
  */
  */
 
 
+import EditingView from '@ckeditor/ckeditor5-engine/src/view/view';
+import ViewRootEditableElement from '@ckeditor/ckeditor5-engine/src/view/rooteditableelement';
 import BalloonEditorUIView from '../src/ballooneditoruiview';
 import BalloonEditorUIView from '../src/ballooneditoruiview';
 import InlineEditableUIView from '@ckeditor/ckeditor5-ui/src/editableui/inline/inlineeditableuiview';
 import InlineEditableUIView from '@ckeditor/ckeditor5-ui/src/editableui/inline/inlineeditableuiview';
 import Locale from '@ckeditor/ckeditor5-utils/src/locale';
 import Locale from '@ckeditor/ckeditor5-utils/src/locale';
@@ -10,13 +12,15 @@ import Locale from '@ckeditor/ckeditor5-utils/src/locale';
 import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
 import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
 
 
 describe( 'BalloonEditorUIView', () => {
 describe( 'BalloonEditorUIView', () => {
-	let locale, view;
+	let locale, view, editingView, editingViewRoot;
 
 
 	testUtils.createSinonSandbox();
 	testUtils.createSinonSandbox();
 
 
 	beforeEach( () => {
 	beforeEach( () => {
 		locale = new Locale( 'en' );
 		locale = new Locale( 'en' );
-		view = new BalloonEditorUIView( locale );
+		setUpEditingView();
+		view = new BalloonEditorUIView( locale, editingView );
+		view.editable.name = editingViewRoot.rootName;
 	} );
 	} );
 
 
 	describe( 'constructor()', () => {
 	describe( 'constructor()', () => {
@@ -44,4 +48,11 @@ describe( 'BalloonEditorUIView', () => {
 			sinon.assert.calledOnce( spy );
 			sinon.assert.calledOnce( spy );
 		} );
 		} );
 	} );
 	} );
+
+	function setUpEditingView() {
+		editingView = new EditingView();
+		editingViewRoot = new ViewRootEditableElement( 'div' );
+		editingViewRoot._document = editingView.document;
+		editingView.document.roots.add( editingViewRoot );
+	}
 } );
 } );