Skip to content
This repository has been archived by the owner on Jun 26, 2020. It is now read-only.

Commit

Permalink
Tests: Added tests to check the placeholder feature of the UI. Code r…
Browse files Browse the repository at this point in the history
…efactoring.
  • Loading branch information
oleq committed Jan 28, 2019
1 parent a9f7c18 commit 2624c8e
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 25 deletions.
2 changes: 1 addition & 1 deletion src/decouplededitor.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ export default class DecoupledEditor extends Editor {

this.model.document.createRoot();

const view = new DecoupledEditorUIView( this.locale, this.sourceElement, this.editing.view );
const view = new DecoupledEditorUIView( this.locale, this.editing.view, this.sourceElement );
this.ui = new DecoupledEditorUI( this, view );
}

Expand Down
3 changes: 2 additions & 1 deletion src/decouplededitoruiview.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,11 @@ export default class DecoupledEditorUIView extends EditorUIView {
* Creates an instance of the decoupled editor UI view.
*
* @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
* {@link module:ui/editableui/editableuiview~EditableUIView}. Otherwise, the given element will be used.
*/
constructor( locale, editableElement, editingView ) {
constructor( locale, editingView, editableElement ) {
super( locale );

/**
Expand Down
93 changes: 73 additions & 20 deletions tests/decouplededitorui.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,13 @@ import View from '@ckeditor/ckeditor5-ui/src/view';
import VirtualTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/virtualtesteditor';
import DecoupledEditorUI from '../src/decouplededitorui';
import EditorUI from '@ckeditor/ckeditor5-core/src/editor/editorui';
import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
import DecoupledEditorUIView from '../src/decouplededitoruiview';

import { keyCodes } from '@ckeditor/ckeditor5-utils/src/keyboard';
import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
import utils from '@ckeditor/ckeditor5-utils/tests/_utils/utils';
import { isElement } from 'lodash-es';

describe( 'DecoupledEditorUI', () => {
let editor, view, ui, viewElement;
Expand All @@ -23,7 +25,7 @@ describe( 'DecoupledEditorUI', () => {

beforeEach( () => {
return VirtualDecoupledTestEditor
.create( {
.create( '', {
toolbar: [ 'foo', 'bar' ],
} )
.then( newEditor => {
Expand Down Expand Up @@ -69,34 +71,81 @@ describe( 'DecoupledEditorUI', () => {
view.editable,
{ isFocused: false },
[
[ editor.editing.view.document, { isFocused: true } ]
[ ui.focusTracker, { isFocused: true } ]
],
{ isFocused: true }
);
} );

it( 'binds view.editable#isReadOnly', () => {
const editable = editor.editing.view.document.getRoot();
it( 'attaches editable UI as view\'s DOM root', () => {
expect( editor.editing.view.getDomRoot() ).to.equal( view.editable.element );
} );
} );

utils.assertBinding(
view.editable,
{ isReadOnly: false },
[
[ editable, { isReadOnly: true } ]
],
{ isReadOnly: true }
);
describe( 'placeholder', () => {
it( 'sets placeholder from editor.config.placeholder', () => {
return VirtualDecoupledTestEditor
.create( 'foo', {
extraPlugins: [ 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( 'attaches editable UI as view\'s DOM root', () => {
expect( editor.editing.view.getDomRoot() ).to.equal( view.editable.element );
it( 'sets placeholder from "placeholder" attribute of a passed element', () => {
const element = document.createElement( 'div' );

element.setAttribute( 'placeholder', 'placeholder-text' );

return VirtualDecoupledTestEditor
.create( element, {
extraPlugins: [ 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 VirtualDecoupledTestEditor
.create( element, {
placeholder: 'config takes precedence',
extraPlugins: [ 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();
} );
} );
} );

describe( 'view.toolbar#items', () => {
it( 'are filled with the config.toolbar (specified as an Array)', () => {
return VirtualDecoupledTestEditor
.create( {
.create( '', {
toolbar: [ 'foo', 'bar' ]
} )
.then( editor => {
Expand All @@ -111,7 +160,7 @@ describe( 'DecoupledEditorUI', () => {

it( 'are filled with the config.toolbar (specified as an Object)', () => {
return VirtualDecoupledTestEditor
.create( {
.create( '', {
toolbar: {
items: [ 'foo', 'bar' ],
viewportTopOffset: 100
Expand Down Expand Up @@ -185,10 +234,14 @@ function viewCreator( name ) {
}

class VirtualDecoupledTestEditor extends VirtualTestEditor {
constructor( config ) {
constructor( sourceElementOrData, config ) {
super( config );

const view = new DecoupledEditorUIView( this.locale );
if ( isElement( sourceElementOrData ) ) {
this.sourceElement = sourceElementOrData;
}

const view = new DecoupledEditorUIView( this.locale, this.editing.view );
this.ui = new DecoupledEditorUI( this, view );

this.ui.componentFactory.add( 'foo', viewCreator( 'foo' ) );
Expand All @@ -201,9 +254,9 @@ class VirtualDecoupledTestEditor extends VirtualTestEditor {
return super.destroy();
}

static create( config ) {
static create( sourceElementOrData, config ) {
return new Promise( resolve => {
const editor = new this( config );
const editor = new this( sourceElementOrData, config );

resolve(
editor.initPlugins()
Expand Down
18 changes: 15 additions & 3 deletions tests/decouplededitoruiview.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,24 @@
/* globals document */

import DecoupledEditorUIView from '../src/decouplededitoruiview';
import EditingView from '@ckeditor/ckeditor5-engine/src/view/view';
import ViewRootEditableElement from '@ckeditor/ckeditor5-engine/src/view/rooteditableelement';
import ToolbarView from '@ckeditor/ckeditor5-ui/src/toolbar/toolbarview';
import InlineEditableUIView from '@ckeditor/ckeditor5-ui/src/editableui/inline/inlineeditableuiview';
import Locale from '@ckeditor/ckeditor5-utils/src/locale';

import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';

describe( 'DecoupledEditorUIView', () => {
let locale, view;
let locale, view, editingView, editingViewRoot;

testUtils.createSinonSandbox();

beforeEach( () => {
locale = new Locale( 'en' );
view = new DecoupledEditorUIView( locale );
setUpEditingView();
view = new DecoupledEditorUIView( locale, editingView );
view.editable.name = editingViewRoot.rootName;
} );

describe( 'constructor()', () => {
Expand Down Expand Up @@ -57,7 +61,8 @@ describe( 'DecoupledEditorUIView', () => {

it( 'can be created out of an existing DOM element', () => {
const editableElement = document.createElement( 'div' );
const testView = new DecoupledEditorUIView( locale, editableElement );
const testView = new DecoupledEditorUIView( locale, editingView, editableElement );
testView.editable.name = editingViewRoot.rootName;

testView.render();

Expand Down Expand Up @@ -125,4 +130,11 @@ describe( 'DecoupledEditorUIView', () => {
view.editable.element.remove();
} );
} );

function setUpEditingView() {
editingView = new EditingView();
editingViewRoot = new ViewRootEditableElement( 'div' );
editingViewRoot._document = editingView.document;
editingView.document.roots.add( editingViewRoot );
}
} );

0 comments on commit 2624c8e

Please sign in to comment.