Browse Source

Introduced basic mechanism for detecting and firing creators.

Piotrek Koszuliński 10 years ago
parent
commit
958924a5b4

+ 54 - 0
packages/ckeditor5-ui/tests/_tools/tools.js

@@ -0,0 +1,54 @@
+/**
+ * @license Copyright (c) 2003-2015, CKSource - Frederico Knabben. All rights reserved.
+ * For licensing, see LICENSE.md.
+ */
+
+/* globals bender, sinon */
+
+'use strict';
+
+( function() {
+	bender.tools.core = {
+		/**
+		 * Defines CKEditor plugin which is a mock of an editor creator.
+		 *
+		 * If `proto` is not set or it does not define `create()` and `destroy()` methods,
+		 * then they will be set to Sinon spies. Therefore the shortest usage is:
+		 *
+		 *	  bender.tools.defineEditorCreatorMock( 'test1' );
+		 *
+		 * The mocked creator is available under:
+		 *
+		 *	  editor.plugins.get( 'creator-thename' );
+		 *
+		 * @param {String} creatorName Name of the creator.
+		 * @param {Object} [proto] Prototype of the creator. Properties from the proto param will
+		 * be copied to the prototype of the creator.
+		 */
+		defineEditorCreatorMock: function( creatorName, proto ) {
+			CKEDITOR.define( 'plugin!creator-' + creatorName, [ 'creator' ], function( Creator ) {
+				return mockCreator( Creator );
+			} );
+
+			function mockCreator( Creator ) {
+				class TestCreator extends Creator {}
+
+				if ( proto ) {
+					for ( var propName in proto ) {
+						TestCreator.prototype[ propName ] = proto[ propName ];
+					}
+				}
+
+				if ( !TestCreator.prototype.create ) {
+					TestCreator.prototype.create = sinon.spy().named( creatorName + '-create' );
+				}
+
+				if ( !TestCreator.prototype.destroy ) {
+					TestCreator.prototype.destroy = sinon.spy().named( creatorName + '-destroy' );
+				}
+
+				return TestCreator;
+			}
+		}
+	};
+} )();

+ 60 - 0
packages/ckeditor5-ui/tests/bender/tools.js

@@ -0,0 +1,60 @@
+/**
+ * @license Copyright (c) 2003-2015, CKSource - Frederico Knabben. All rights reserved.
+ * For licensing, see LICENSE.md.
+ */
+
+/* bender-include: ../_tools/tools.js */
+
+'use strict';
+
+var createFn3 = function() {};
+var destroyFn3 = function() {};
+
+bender.tools.core.defineEditorCreatorMock( 'test1' );
+bender.tools.core.defineEditorCreatorMock( 'test2', {
+	foo: 1,
+	bar: 2
+} );
+bender.tools.core.defineEditorCreatorMock( 'test3', {
+	create: createFn3,
+	destroy: destroyFn3
+} );
+
+var modules = bender.amd.require( 'creator', 'plugin!creator-test1', 'plugin!creator-test2', 'plugin!creator-test3' );
+
+///////////////////
+
+describe( 'bender.tools.core.defineEditorCreatorMock()', function() {
+	it( 'should register all creators', function() {
+		var Creator = modules.creator;
+		var TestCreator1 = modules[ 'plugin!creator-test1' ];
+		var TestCreator2 = modules[ 'plugin!creator-test2' ];
+		var TestCreator3 = modules[ 'plugin!creator-test3' ];
+
+		expect( TestCreator1.prototype ).to.be.instanceof( Creator );
+		expect( TestCreator2.prototype ).to.be.instanceof( Creator );
+		expect( TestCreator3.prototype ).to.be.instanceof( Creator );
+	} );
+
+	it( 'should copy properties from the second argument', function() {
+		var TestCreator = modules[ 'plugin!creator-test2' ];
+
+		expect( TestCreator.prototype ).to.have.property( 'foo', 1 );
+		expect( TestCreator.prototype ).to.have.property( 'bar', 2 );
+	} );
+
+	it( 'should create spies for create() and destroy() if not defined', function() {
+		var TestCreator1 = modules[ 'plugin!creator-test1' ];
+		var TestCreator2 = modules[ 'plugin!creator-test2' ];
+		var TestCreator3 = modules[ 'plugin!creator-test3' ];
+
+		expect( TestCreator1.prototype.create ).to.have.property( 'called', false, 'test1.create' );
+		expect( TestCreator1.prototype.destroy ).to.have.property( 'called', false, 'test1.destroy' );
+		expect( TestCreator2.prototype.create ).to.have.property( 'called', false, 'test2.create' );
+		expect( TestCreator2.prototype.destroy ).to.have.property( 'called', false, 'test2.destroy' );
+
+		// Not spies:
+		expect( TestCreator3.prototype ).to.have.property( 'create', createFn3 );
+		expect( TestCreator3.prototype ).to.have.property( 'destroy', destroyFn3 );
+	} );
+} );