Browse Source

Tests and docs.

Piotrek Koszuliński 9 years ago
parent
commit
5329f25f58

+ 19 - 0
packages/ckeditor5-ui/src/editable/editable.js

@@ -84,3 +84,22 @@ export default class Editable extends Controller {
 }
 
 utils.mix( Editable, ObservableMixin );
+
+/**
+ * The editable model interface.
+ *
+ * @memberOf core.editable
+ * @interface EditableModel
+ */
+
+/**
+ * Whether the editable has focus.
+ *
+ * @member {Boolean} core.editable.EditableModel#isFocused
+ */
+
+/**
+ * Whether the editable is not in read-only mode.
+ *
+ * @member {Boolean} core.editable.EditableModel#isEditable
+ */

+ 2 - 0
packages/ckeditor5-ui/src/editable/editableview.js

@@ -21,6 +21,8 @@ export default class EditableView extends View {
 	 */
 
 	/**
+	 * Sets the {@link #editableElement} property and applies necessary bindings to it.
+	 *
 	 * @param {HTMLElement} editableElement
 	 */
 	setEditableElement( editableElement ) {

+ 32 - 1
packages/ckeditor5-ui/src/ui/button/button.js

@@ -18,6 +18,37 @@ export default class Button extends Controller {
 	constructor( model, view ) {
 		super( model, view );
 
-		view.on( 'clicked', () => model.fire( 'executed' ) );
+		view.on( 'click', () => model.fire( 'execute' ) );
 	}
 }
+
+/**
+ * The basic button model interface.
+ *
+ * @memberOf core.ui.button
+ * @interface ButtonModel
+ */
+
+/**
+ * The label of the button.
+ *
+ * @member {String} core.ui.button.ButtonModel#label
+ */
+
+/**
+ * Whether the button is "on" (e.g. some feature which this button represents is currently enabled).
+ *
+ * @member {Boolean} core.ui.button.ButtonModel#isOn
+ */
+
+/**
+ * Whether the button is enabled (can be clicked).
+ *
+ * @member {Boolean} core.ui.button.ButtonModel#isEnabled
+ */
+
+/**
+ * Fired when the button action should be executed.
+ *
+ * @event core.ui.button.ButtonModel#execute
+ */

+ 7 - 1
packages/ckeditor5-ui/src/ui/button/buttonview.js

@@ -46,10 +46,16 @@ export default class ButtonView extends View {
 					// We can't make the button disabled using the disabled attribute, because it won't be focusable.
 					// Though, shouldn't this condition be moved to the button controller?
 					if ( model.isEnabled ) {
-						this.fire( 'clicked' );
+						this.fire( 'click' );
 					}
 				}
 			}
 		};
 	}
 }
+
+/**
+ * Fired when the button is being clicked. It won't be fired when the button is disabled.
+ *
+ * @event core.ui.button.ButtonView#click
+ */

+ 1 - 1
packages/ckeditor5-ui/src/ui/componentfactory.js

@@ -8,7 +8,7 @@
 import CKEditorError from '../ckeditorerror.js';
 
 /**
- * Class implementin a UI component factory.
+ * Class implementing the UI component factory.
  *
  * Factories of specific UI components can be registered under their unique names. Registered
  * components can be later instantiated by providing the name of the component. The model is shared between all

+ 6 - 0
packages/ckeditor5-ui/tests/editorui/editorui.js

@@ -7,6 +7,8 @@
 
 import Editor from '/ckeditor5/core/editor.js';
 import EditorUI from '/ckeditor5/core/editorui/editorui.js';
+import ComponentFactory from '/ckeditor5/core/ui/componentfactory.js';
+import ControllerCollection from '/ckeditor5/core/ui/controllercollection.js';
 
 describe( 'EditorUI', () => {
 	let editor, editorUI;
@@ -19,6 +21,10 @@ describe( 'EditorUI', () => {
 	describe( 'constructor', () => {
 		it( 'sets all the properties', () => {
 			expect( editorUI ).to.have.property( 'editor', editor );
+
+			expect( editorUI.featureComponents ).to.be.instanceof( ComponentFactory );
+
+			expect( editorUI.collections.get( 'body' ) ).to.be.instanceof( ControllerCollection );
 		} );
 	} );
 } );

+ 38 - 0
packages/ckeditor5-ui/tests/editorui/editoruiview.js

@@ -0,0 +1,38 @@
+/**
+ * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
+ * For licensing, see LICENSE.md.
+ */
+
+'use strict';
+
+import EditorUIView from '/ckeditor5/core/editorui/editoruiview.js';
+import Model from '/ckeditor5/core/ui/model.js';
+
+describe( 'EditorUIView', () => {
+	let editorUIView;
+
+	beforeEach( () => {
+		editorUIView = new EditorUIView( new Model() );
+
+		return editorUIView.init();
+	} );
+
+	describe( 'constructor', () => {
+		it( 'creates the body region', () => {
+			const el = editorUIView.regions.get( 'body' ).element;
+
+			expect( el.parentNode ).to.equal( document.body );
+			expect( el.nextSibling ).to.be.null;
+		} );
+	} );
+
+	describe( 'destroy', () => {
+		it( 'removes the body region container', () => {
+			const el = editorUIView.regions.get( 'body' ).element;
+
+			editorUIView.destroy();
+
+			expect( el.parentNode ).to.be.null;
+		} );
+	} );
+} );

+ 34 - 0
packages/ckeditor5-ui/tests/ui/button/button.js

@@ -0,0 +1,34 @@
+/**
+ * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
+ * For licensing, see LICENSE.md.
+ */
+
+/* bender-tags: ui, button */
+
+'use strict';
+
+import Button from '/ckeditor5/core/ui/button/button.js';
+import View from '/ckeditor5/core/ui/view.js';
+import Model from '/ckeditor5/core/ui/model.js';
+
+describe( 'Button', () => {
+	let model, button, view;
+
+	beforeEach( () => {
+		model = new Model();
+		view = new View();
+		button = new Button( model, view );
+	} );
+
+	describe( 'constructor', () => {
+		it( 'creates view#click -> model#execute binding', () => {
+			const spy = sinon.spy();
+
+			model.on( 'execute', spy );
+
+			view.fire( 'click' );
+
+			expect( spy.calledOnce ).to.be.true;
+		} );
+	} );
+} );

+ 84 - 0
packages/ckeditor5-ui/tests/ui/button/buttonview.js

@@ -0,0 +1,84 @@
+/**
+ * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
+ * For licensing, see LICENSE.md.
+ */
+
+/* bender-tags: ui, button */
+
+'use strict';
+
+import ButtonView from '/ckeditor5/core/ui/button/buttonview.js';
+import Model from '/ckeditor5/core/ui/model.js';
+
+describe( 'ButtonView', () => {
+	let model, view;
+
+	beforeEach( () => {
+		model = new Model( {
+			label: 'foo',
+			isEnabled: true,
+			isOn: false
+		} );
+		view = new ButtonView( model );
+
+		return view.init();
+	} );
+
+	describe( '<button> bindings', () => {
+		describe( 'class', () => {
+			it( 'is set initially', () => {
+				expect( view.element.classList.contains( 'ck-button' ) ).to.be.true( 'ck-button' );
+				expect( view.element.classList.contains( 'ck-enabled' ) ).to.be.true( 'ck-enabled' );
+				expect( view.element.classList.contains( 'ck-off' ) ).to.be.true( 'ck-off' );
+			} );
+
+			it( 'reacts on model.isEnabled', () => {
+				model.isEnabled = false;
+
+				expect( view.element.classList.contains( 'ck-disabled' ) ).to.be.true( 'ck-disabled' );
+			} );
+
+			it( 'reacts on model.isOn', () => {
+				model.isOn = true;
+
+				expect( view.element.classList.contains( 'ck-on' ) ).to.be.true( 'ck-on' );
+			} );
+		} );
+
+		describe( 'text', () => {
+			it( 'is set initially', () => {
+				expect( view.element.textContent ).to.equal( 'foo' );
+			} );
+
+			it( 'reacts on model.label', () => {
+				model.label = 'bar';
+
+				expect( view.element.textContent ).to.equal( 'bar' );
+			} );
+		} );
+
+		describe( 'mousedown event', () => {
+			it( 'should be prevented', () => {
+				const ret = view.element.dispatchEvent( new Event( 'mousedown', { cancelable: true } ) );
+
+				expect( ret ).to.be.false;
+			} );
+		} );
+
+		describe( 'click event', () => {
+			it( 'triggers click event if button is not disabled', () => {
+				const spy = sinon.spy();
+
+				view.on( 'click', spy );
+
+				view.element.dispatchEvent( new Event( 'click' ) );
+				expect( spy.callCount ).to.equal( 1 );
+
+				model.isEnabled = false;
+
+				view.element.dispatchEvent( new Event( 'click' ) );
+				expect( spy.callCount ).to.equal( 1 );
+			} );
+		} );
+	} );
+} );

+ 59 - 0
packages/ckeditor5-ui/tests/ui/componentfactory.js

@@ -0,0 +1,59 @@
+/**
+ * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
+ * For licensing, see LICENSE.md.
+ */
+
+/* bender-tags: ui */
+
+'use strict';
+
+import Editor from '/ckeditor5/core/editor.js';
+import ComponentFactory from '/ckeditor5/core/ui/componentfactory.js';
+import CKEditorError from '/ckeditor5/core/ckeditorerror.js';
+
+describe( 'ComponentFactory', () => {
+	let editor, factory;
+
+	beforeEach( () => {
+		editor = new Editor();
+		factory = new ComponentFactory( editor );
+	} );
+
+	describe( 'constructor', () => {
+		it( 'sets all the properties', () => {
+			expect( factory ).to.have.property( 'editor', editor );
+		} );
+	} );
+
+	describe( 'add', () => {
+		it( 'throws when trying to override already registered component', () => {
+			factory.add( 'foo', class {}, class {}, {} );
+
+			expect( () => {
+				factory.add( 'foo', class {}, class {}, {} );
+			} ).to.throw( CKEditorError, /^componentfactory-item-exists/ );
+		} );
+	} );
+
+	describe( 'create', () => {
+		it( 'creates an instance', () => {
+			class View {}
+
+			class Controller {
+				constructor( ed, model, view ) {
+					expect( ed ).to.equal( editor );
+					expect( model ).to.equal( model );
+					expect( view ).to.be.instanceof( View );
+				}
+			}
+
+			const model = {};
+
+			factory.add( 'foo', Controller, View, model );
+
+			const instance = factory.create( 'foo' );
+
+			expect( instance ).to.be.instanceof( Controller );
+		} );
+	} );
+} );

+ 2 - 0
packages/ckeditor5-ui/tests/ui/model.js

@@ -3,6 +3,8 @@
  * For licensing, see LICENSE.md.
  */
 
+/* bender-tags: ui */
+
 'use strict';
 
 import Model from '/ckeditor5/core/ui/model.js';

+ 25 - 0
packages/ckeditor5-ui/tests/ui/toolbar/toolbar.js

@@ -0,0 +1,25 @@
+/**
+ * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
+ * For licensing, see LICENSE.md.
+ */
+
+/* bender-tags: ui, toolbar */
+
+'use strict';
+
+import Toolbar from '/ckeditor5/core/ui/toolbar/toolbar.js';
+import ControllerCollection from '/ckeditor5/core/ui/controllercollection.js';
+
+describe( 'Toolbar', () => {
+	let toolbar;
+
+	beforeEach( () => {
+		toolbar = new Toolbar();
+	} );
+
+	describe( 'constructor', () => {
+		it( 'creates buttons collection', () => {
+			expect( toolbar.collections.get( 'buttons' ) ).to.be.instanceof( ControllerCollection );
+		} );
+	} );
+} );

+ 34 - 0
packages/ckeditor5-ui/tests/ui/toolbar/toolbarview.js

@@ -0,0 +1,34 @@
+/**
+ * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
+ * For licensing, see LICENSE.md.
+ */
+
+/* bender-tags: ui, toolbar */
+
+'use strict';
+
+import ToolbarView from '/ckeditor5/core/ui/toolbar/toolbarview.js';
+import Model from '/ckeditor5/core/ui/model.js';
+
+describe( 'ToolbarView', () => {
+	let model, view;
+
+	beforeEach( () => {
+		model = new Model();
+		view = new ToolbarView( model );
+
+		return view.init();
+	} );
+
+	describe( 'the main element bindings', () => {
+		it( 'is fine', () => {
+			expect( view.element.classList.contains( 'ck-toolbar' ) );
+		} );
+	} );
+
+	describe( 'buttons region', () => {
+		it( 'is bound to the main element', () => {
+			expect( view.regions.get( 'buttons' ).element ).to.equal( view.element );
+		} );
+	} );
+} );