浏览代码

Tests: improved existing tests and added missing.

Piotr Jasiun 9 年之前
父节点
当前提交
6db2573e33

+ 1 - 1
packages/ckeditor5-engine/src/view/rooteditableelement.js

@@ -11,7 +11,7 @@ export default class RootEditableElement extends EditableElement {
 	/**
 	 * Creates an root editable element.
 	 */
-	constructor( document, name, rootName ) {
+	constructor( document, name, rootName = 'main' ) {
 		super( name );
 
 		/**

+ 20 - 12
packages/ckeditor5-engine/tests/view/node.js

@@ -9,6 +9,7 @@
 
 import Element from '/ckeditor5/engine/view/element.js';
 import Text from '/ckeditor5/engine/view/text.js';
+import RootEditableElement from '/ckeditor5/engine/view/rooteditableelement.js';
 import CKEditorError from '/ckeditor5/utils/ckeditorerror.js';
 
 describe( 'Node', () => {
@@ -133,29 +134,36 @@ describe( 'Node', () => {
 			expect( charA.getDocument() ).to.be.null;
 		} );
 
-		it( 'should return view attached to the element', () => {
-			const tvMock = {};
-			const element = new Element( 'p' );
-
-			element.setDocument( tvMock );
-
-			expect( element.getDocument() ).to.equal( tvMock );
-		} );
-
 		it( 'should return Document attached to the parent element', () => {
 			const docMock = {};
-			const parent = new Element( 'div' );
+			const parent = new RootEditableElement( docMock, 'div' );
 			const child = new Element( 'p' );
 
 			child.parent = parent;
 
-			parent.setDocument( docMock );
-
 			expect( parent.getDocument() ).to.equal( docMock );
 			expect( child.getDocument() ).to.equal( docMock );
 		} );
 	} );
 
+	describe( 'getRoot', () => {
+		it( 'should return this element if it has no parent', () => {
+			const child = new Element( 'p' );
+
+			expect( child.getRoot() ).to.equal( child );
+		} );
+
+		it( 'should return root element', () => {
+			const parent = new RootEditableElement( {}, 'div' );
+			const child = new Element( 'p' );
+
+			child.parent = parent;
+
+			expect( parent.getRoot() ).to.equal( parent );
+			expect( child.getRoot() ).to.equal( parent );
+		} );
+	} );
+
 	describe( 'remove', () => {
 		it( 'should remove node from its parent', () => {
 			const char = new Text( 'a' );

+ 32 - 0
packages/ckeditor5-engine/tests/view/observer/focusobserver.js

@@ -49,4 +49,36 @@ describe( 'FocusObserver', () => {
 			expect( data.domTarget ).to.equal( document.body );
 		} );
 	} );
+
+	describe( 'handle isFocused property of root elements', () => {
+		let domMain, domHeader, viewMain, viewHeader;
+
+		beforeEach( () => {
+			domMain = document.createElement( 'div' );
+			domHeader = document.createElement( 'h1' );
+
+			viewMain = viewDocument.createRoot( domMain );
+			viewHeader = viewDocument.createRoot( domHeader, 'header' );
+		} );
+
+		it( 'should change isFocused on focus event', () => {
+			observer.onDomEvent( { type: 'focus', target: domMain } );
+
+			expect( viewMain.isFocused ).to.be.true;
+			expect( viewHeader.isFocused ).to.be.false;
+		} );
+
+		it( 'should change isFocused on blur event', () => {
+			observer.onDomEvent( { type: 'focus', target: domMain } );
+
+			expect( viewMain.isFocused ).to.be.true;
+			expect( viewHeader.isFocused ).to.be.false;
+
+			observer.onDomEvent( { type: 'blur', target: domMain } );
+			observer.onDomEvent( { type: 'focus', target: domHeader } );
+
+			expect( viewMain.isFocused ).to.be.false;
+			expect( viewHeader.isFocused ).to.be.true;
+		} );
+	} );
 } );

+ 84 - 0
packages/ckeditor5-engine/tests/view/rooteditableelement.js

@@ -0,0 +1,84 @@
+/**
+ * @license Copyright (c) 2003-2015, CKSource - Frederico Knabben. All rights reserved.
+ * For licensing, see LICENSE.md.
+ */
+
+/* bender-tags: view */
+
+'use strict';
+
+import ContainerElement from '/ckeditor5/engine/view/containerelement.js';
+import EditableElement from '/ckeditor5/engine/view/editableelement.js';
+import RootEditableElement from '/ckeditor5/engine/view/rooteditableelement.js';
+
+describe( 'RootEditableElement', () => {
+	describe( 'constructor', () => {
+		it( 'should create an element with default root name', () => {
+			const root = new RootEditableElement( {}, 'div' );
+
+			expect( root ).to.be.instanceof( EditableElement );
+			expect( root ).to.be.instanceof( ContainerElement );
+
+			expect( root.rootName ).to.equal( 'main' );
+			expect( root.name ).to.equal( 'div' );
+
+			expect( root.isFocused ).to.be.false;
+			expect( root.isReadOnly ).to.be.false;
+		} );
+
+		it( 'should create an element with custom root name', () => {
+			const root = new RootEditableElement( {}, 'h1', 'header' );
+
+			expect( root.rootName ).to.equal( 'header' );
+			expect( root.name ).to.equal( 'h1' );
+
+			expect( root.isFocused ).to.be.false;
+			expect( root.isReadOnly ).to.be.false;
+		} );
+	} );
+
+	describe( 'isFocused', () => {
+		it( 'should be observable', () => {
+			const root = new RootEditableElement( {}, 'div' );
+
+			expect( root.isFocused ).to.be.false;
+
+			const isFocusedSpy = sinon.spy();
+
+			root.on( 'change:isFocused', isFocusedSpy );
+
+			root.isFocused = true;
+
+			expect( root.isFocused ).to.be.true;
+
+			expect( isFocusedSpy.calledOnce ).to.be.true;
+		} );
+	} );
+
+	describe( 'isReadOnly', () => {
+		it( 'should be observable', () => {
+			const root = new RootEditableElement( {}, 'div' );
+
+			expect( root.isReadOnly ).to.be.false;
+
+			const isReadOnlySpy = sinon.spy();
+
+			root.on( 'change:isReadOnly', isReadOnlySpy );
+
+			root.isReadOnly = true;
+
+			expect( root.isReadOnly ).to.be.true;
+
+			expect( isReadOnlySpy.calledOnce ).to.be.true;
+		} );
+	} );
+
+	describe( 'getDocument', ()=> {
+		it( 'should return document', () => {
+			const docMock = {};
+			const root = new RootEditableElement( docMock, 'div' );
+
+			expect( root.getDocument() ).to.equal( docMock );
+		} );
+	} );
+} );