Ver Fonte

Tests and documentation for the Editable.

Piotrek Koszuliński há 9 anos atrás
pai
commit
1fe245d456

+ 5 - 10
packages/ckeditor5-engine/src/editable/editable.js

@@ -41,14 +41,6 @@ export default class Editable extends Controller {
 		 */
 		this.set( 'isFocused', false );
 
-		/**
-		 * The parent editable of this editable.
-		 *
-		 * @readonly
-		 * @property {Editable} parent
-		 */
-		this.parent = null;
-
 		/**
 		 * @private {Model} _viewModel
 		 */
@@ -56,6 +48,9 @@ export default class Editable extends Controller {
 
 	/**
 	 * The model for the editable view.
+	 *
+	 * @readonly
+	 * @property {core.ui.Model} viewModel
 	 */
 	get viewModel() {
 		if ( this._viewModel ) {
@@ -79,7 +74,7 @@ export default class Editable extends Controller {
 	 * @param {String} data HTML to be loaded.
 	 */
 	setData( data ) {
-		this.view.setData( data );
+		this.view.editableElement.innerHTML = data;
 	}
 
 	/**
@@ -88,7 +83,7 @@ export default class Editable extends Controller {
 	 * @returns {String} HTML string.
 	 */
 	getData() {
-		return this.view.getData();
+		return this.view.editableElement.innerHTML;
 	}
 }
 

+ 1 - 18
packages/ckeditor5-engine/src/editable/editableview.js

@@ -12,6 +12,7 @@ export default class EditableView extends View {
 	/**
 	 * The element which is the main editable element (usually the one with `contentEditable="true"`).
 	 *
+	 * @readonly
 	 * @property {HTMLElement} editableElement
 	 */
 
@@ -37,22 +38,4 @@ export default class EditableView extends View {
 			editableElement.contentEditable = value;
 		} );
 	}
-
-	/**
-	 * Temporary implementation (waiting for integration with the data model).
-	 *
-	 * @param {String} data HTML to be loaded.
-	 */
-	setData( data ) {
-		this.editableElement.innerHTML = data;
-	}
-
-	/**
-	 * Temporary implementation (waiting for integration with the data model).
-	 *
-	 * @returns {String} HTML string.
-	 */
-	getData() {
-		return this.editableElement.innerHTML;
-	}
 }

+ 34 - 0
packages/ckeditor5-engine/tests/_utils/utils.js

@@ -101,6 +101,40 @@ const utils = {
 		} );
 
 		return observer;
+	},
+
+	/**
+	 * Checkes wether observable properties are properly bound to each other.
+	 *
+	 * Syntax given that observable `A` is bound to observables [`B`, `C`, ...]:
+	 *
+	 *		assertBinding( A,
+	 *			{ initial `A` attributes },
+	 *			[
+	 *				[ B, { new `B` attributes } ],
+	 *				[ C, { new `C` attributes } ],
+	 *				...
+	 *			],
+	 *			{ `A` attributes after [`B`, 'C', ...] changed }
+	 *		);
+	 */
+	assertBinding( observable, stateBefore, data, stateAfter ) {
+		let key, pair;
+
+		for ( key in stateBefore ) {
+			expect( observable[ key ] ).to.be.equal( stateBefore[ key ] );
+		}
+
+		// Change attributes of bound observables.
+		for ( pair of data ) {
+			for ( key in pair[ 1 ] ) {
+				pair[ 0 ][ key ] = pair[ 1 ][ key ];
+			}
+		}
+
+		for ( key in stateAfter ) {
+			expect( observable[ key ] ).to.be.equal( stateAfter[ key ] );
+		}
 	}
 };
 

+ 77 - 0
packages/ckeditor5-engine/tests/editable/editable.js

@@ -0,0 +1,77 @@
+/**
+ * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
+ * For licensing, see LICENSE.md.
+ */
+
+'use strict';
+
+import Editor from '/ckeditor5/core/editor.js';
+import Editable from '/ckeditor5/core/editable/editable.js';
+import Model from '/ckeditor5/core/ui/model.js';
+import coreTestUtils from '/tests/core/_utils/utils.js';
+
+describe( 'Editable', () => {
+	let editable, editor;
+
+	beforeEach( () => {
+		editor = new Editor();
+		editable = new Editable( editor );
+	} );
+
+	describe( 'constructor', () => {
+		it( 'sets all the properties', () => {
+			expect( editable ).to.have.property( 'editor', editor );
+			expect( editable ).to.have.property( 'isFocused', false );
+			expect( editable ).to.have.property( 'isEditable', true );
+		} );
+	} );
+
+	describe( 'viewModel', () => {
+		it( 'returns a model instance', () => {
+			expect( editable.viewModel ).to.be.an.instanceof( Model );
+		} );
+
+		it( 'always returns the same instance', () => {
+			expect( editable.viewModel ).to.equal( editable.viewModel );
+		} );
+
+		it( 'constains editable attributes', () => {
+			expect( editable.viewModel ).to.have.property( 'isEditable', true );
+			expect( editable.viewModel ).to.have.property( 'isFocused', false );
+		} );
+
+		it( 'binds this.isFocused to editable', () => {
+			coreTestUtils.assertBinding(
+				editable,
+				{ isFocused: false },
+				[
+					[ editable.viewModel, { isFocused: true } ]
+				],
+				{ isFocused: true }
+			);
+		} );
+
+		it( 'binds editable.isEditable to itself', () => {
+			coreTestUtils.assertBinding(
+				editable.viewModel,
+				{ isEditable: true },
+				[
+					[ editable, { isEditable: false } ]
+				],
+				{ isEditable: false }
+			);
+		} );
+	} );
+
+	// These are temporary implementation, so tests do nothing beside ensuring 100% CC.
+	describe( 'getData() and setData()', () => {
+		it( 'exist', () => {
+			editable.view = {
+				editableElement: document.createElement( 'div' )
+			};
+
+			editable.getData();
+			editable.setData();
+		} );
+	} );
+} );

+ 3 - 31
packages/ckeditor5-engine/tests/observablemixin.js

@@ -6,12 +6,15 @@
 'use strict';
 
 import testUtils from '/tests/_utils/utils.js';
+import coreTestUtils from '/tests/core/_utils/utils.js';
 import ObservableMixin from '/ckeditor5/core/observablemixin.js';
 import EmitterMixin from '/ckeditor5/core/emittermixin.js';
 import EventInfo from '/ckeditor5/core/eventinfo.js';
 import CKEditorError from '/ckeditor5/core/ckeditorerror.js';
 import utils from '/ckeditor5/core/utils.js';
 
+const assertBinding = coreTestUtils.assertBinding;
+
 testUtils.createSinonSandbox();
 
 describe( 'ObservableMixin', () => {
@@ -781,35 +784,4 @@ describe( 'Observable', () => {
 			);
 		} );
 	} );
-
-	// Syntax given that observable `A` is bound to observables [`B`, `C`, ...]:
-	//
-	//		assertBinding( A,
-	//			{ initial `A` attributes },
-	//			[
-	//				[ B, { new `B` attributes } ],
-	//				[ C, { new `C` attributes } ],
-	//				...
-	//			],
-	//			{ `A` attributes after [`B`, 'C', ...] changed }
-	//		);
-	//
-	function assertBinding( observable, stateBefore, data, stateAfter ) {
-		let key, pair;
-
-		for ( key in stateBefore ) {
-			expect( observable[ key ] ).to.be.equal( stateBefore[ key ] );
-		}
-
-		// Change attributes of bound observables.
-		for ( pair of data ) {
-			for ( key in pair[ 1 ] ) {
-				pair[ 0 ][ key ] = pair[ 1 ][ key ];
-			}
-		}
-
-		for ( key in stateAfter ) {
-			expect( observable[ key ] ).to.be.equal( stateAfter[ key ] );
-		}
-	}
 } );