Browse Source

Merge pull request #184 from ckeditor/t/ckeditor5-ui-default/12

t/ckeditor5-ui-default/12: Implement dropdown component
Piotrek Koszuliński 9 years ago
parent
commit
676c57ffc8
2 changed files with 87 additions and 0 deletions
  1. 42 0
      tests/_utils-tests/utils.js
  2. 45 0
      tests/_utils/utils.js

+ 42 - 0
tests/_utils-tests/utils.js

@@ -55,3 +55,45 @@ describe( 'testUtils.defineEditorCreatorMock()', () => {
 		expect( TestCreator3.prototype ).to.have.property( 'destroy', destroyFn3 );
 	} );
 } );
+
+describe( 'testUtils.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;
+			} );
+		} );
+	} );
+} );

+ 45 - 0
tests/_utils/utils.js

@@ -7,6 +7,10 @@
 
 import moduleUtils from '/tests/ckeditor5/_utils/module.js';
 
+import View from '/ckeditor5/ui/view.js';
+import Controller from '/ckeditor5/ui/controller.js';
+import ControllerCollection from '/ckeditor5/ui/controllercollection.js';
+
 /**
  * General test utils for CKEditor.
  */
@@ -60,6 +64,47 @@ const utils = {
 
 			return TestCreator;
 		} );
+	},
+
+	/**
+	 * 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;
+		} );
 	}
 };