浏览代码

Moved utils#createTestUIController helper to ckeditor5-ui.

Aleksander Nowodzinski 9 年之前
父节点
当前提交
41db9853fa
共有 2 个文件被更改,包括 110 次插入0 次删除
  1. 52 0
      packages/ckeditor5-ui/tests/_utils-tests/utils.js
  2. 58 0
      packages/ckeditor5-ui/tests/_utils/utils.js

+ 52 - 0
packages/ckeditor5-ui/tests/_utils-tests/utils.js

@@ -0,0 +1,52 @@
+/**
+ * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
+ * For licensing, see LICENSE.md.
+ */
+
+'use strict';
+
+import testUtils from '/tests/ui/_utils/utils.js';
+
+describe( 'utils', () => {
+	describe( 'createTestUIController', () => {
+		it( 'returns a promise', () => {
+			expect( testUtils.createTestUIController() ).to.be.instanceof( Promise );
+		} );
+
+		describe( 'controller instance', () => {
+			it( 'comes with a view', () => {
+				const promise = testUtils.createTestUIController();
+
+				return promise.then( controller => {
+					expect( controller.view.element ).to.equal( document.body );
+				} );
+			} );
+
+			it( 'creates collections and regions', () => {
+				const promise = testUtils.createTestUIController( {
+					foo: el => el.firstChild,
+					bar: el => el.lastChild,
+				} );
+
+				promise.then( controller => {
+					expect( controller.collections.get( 'foo' ) ).to.be.not.undefined;
+					expect( controller.collections.get( 'bar' ) ).to.be.not.undefined;
+
+					expect( controller.view.regions.get( 'foo' ).element ).to.equal( document.body.firstChild );
+					expect( controller.view.regions.get( 'bar' ).element ).to.equal( document.body.lastChild );
+				} );
+			} );
+
+			it( 'is ready', () => {
+				const promise = testUtils.createTestUIController( {
+					foo: el => el.firstChild,
+					bar: el => el.lastChild,
+				} );
+
+				promise.then( controller => {
+					expect( controller.ready ).to.be.true;
+				} );
+			} );
+		} );
+	} );
+} );

+ 58 - 0
packages/ckeditor5-ui/tests/_utils/utils.js

@@ -0,0 +1,58 @@
+/**
+ * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
+ * For licensing, see LICENSE.md.
+ */
+
+'use strict';
+
+import View from '/ckeditor5/ui/view.js';
+import Controller from '/ckeditor5/ui/controller.js';
+import ControllerCollection from '/ckeditor5/ui/controllercollection.js';
+
+/**
+ * Test utils for CKEditor UI.
+ */
+const utils = {
+	/**
+	 * Returns UI controller for given region/DOM selector pairs, which {@link ui.Controller#view}
+	 * is `document.body`. It is useful for manual tests which engage various UI components and/or
+	 * UI {@link ui.Controller} instances, where initialization and the process of insertion into
+	 * DOM could be problematic i.e. because of the number of instances.
+	 *
+	 * Usage:
+	 *
+	 *		// Get the controller.
+	 *		const controller = testUtils.createTestUIController();
+	 *
+	 *		// Then use it to organize and initialize children.
+	 *		controller.add( 'some-collection', childControllerInstance );
+	 *
+	 * @param {Object} regions An object literal with `regionName: DOM Selector pairs`.
+	 * See {@link ui.View#register}.
+	 */
+	createTestUIController( regions ) {
+		const TestUIView = class extends View {
+			constructor() {
+				super();
+
+				this.element = document.body;
+
+				for ( let r in regions ) {
+					this.register( r, regions[ r ] );
+				}
+			}
+		};
+
+		const controller = new Controller( null, new TestUIView() );
+
+		for ( let r in regions ) {
+			controller.collections.add( new ControllerCollection( r ) );
+		}
+
+		return controller.init().then( () => {
+			return controller;
+		} );
+	}
+};
+
+export default utils;