8
0
Quellcode durchsuchen

Introduced basic mechanism for detecting and firing creators.

Piotrek Koszuliński vor 10 Jahren
Ursprung
Commit
9a932bf1ae

+ 54 - 0
packages/ckeditor5-utils/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-utils/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 );
+	} );
+} );

+ 13 - 7
packages/ckeditor5-utils/tests/ckeditor/ckeditor.js

@@ -4,12 +4,18 @@
  */
 
 /* globals document */
+/* bender-include: ../_tools/tools.js */
 
 'use strict';
 
 var modules = bender.amd.require( 'ckeditor', 'editor', 'promise', 'config' );
 
 var content = document.getElementById( 'content' );
+var editorConfig = { plugins: 'creator-test' };
+
+before( function() {
+	bender.tools.core.defineEditorCreatorMock( 'test' );
+} );
 
 beforeEach( function() {
 	var CKEDITOR = modules.ckeditor;
@@ -25,14 +31,14 @@ describe( 'create', function() {
 		var CKEDITOR = modules.ckeditor;
 		var Promise = modules.promise;
 
-		expect( CKEDITOR.create( content ) ).to.be.instanceof( Promise );
+		expect( CKEDITOR.create( content, editorConfig ) ).to.be.instanceof( Promise );
 	} );
 
 	it( 'should create a new editor instance', function() {
 		var CKEDITOR = modules.ckeditor;
 		var Editor = modules.editor;
 
-		return CKEDITOR.create( content ).then( function( editor ) {
+		return CKEDITOR.create( content, editorConfig ).then( function( editor ) {
 			expect( editor ).to.be.instanceof( Editor );
 			expect( editor.element ).to.equal( content );
 		} );
@@ -42,7 +48,7 @@ describe( 'create', function() {
 		var CKEDITOR = modules.ckeditor;
 		var Editor = modules.editor;
 
-		return CKEDITOR.create( '.editor' ).then( function( editor ) {
+		return CKEDITOR.create( '.editor', editorConfig ).then( function( editor ) {
 			expect( editor ).to.be.instanceof( Editor );
 			expect( editor.element ).to.equal( document.querySelector( '.editor' ) );
 		} );
@@ -51,7 +57,7 @@ describe( 'create', function() {
 	it( 'should set configurations on the new editor', function() {
 		var CKEDITOR = modules.ckeditor;
 
-		return CKEDITOR.create( content, { test: 1 } ).then( function( editor ) {
+		return CKEDITOR.create( content, { test: 1, plugins: 'creator-test' } ).then( function( editor ) {
 			expect( editor.config.test ).to.equal( 1 );
 		} );
 	} );
@@ -59,7 +65,7 @@ describe( 'create', function() {
 	it( 'should add the editor to the `instances` collection', function() {
 		var CKEDITOR = modules.ckeditor;
 
-		return CKEDITOR.create( content ).then( function( editor ) {
+		return CKEDITOR.create( content, editorConfig ).then( function( editor ) {
 			expect( CKEDITOR.instances ).to.have.length( 1 );
 			expect( CKEDITOR.instances.get( 0 ) ).to.equal( editor );
 		} );
@@ -70,11 +76,11 @@ describe( 'create', function() {
 		var editor1, editor2;
 
 		// Create the first editor.
-		return CKEDITOR.create( content ).then( function( editor ) {
+		return CKEDITOR.create( content, editorConfig ).then( function( editor ) {
 			editor1 = editor;
 
 			// Create the second editor.
-			return CKEDITOR.create( '.editor' ).then( function( editor ) {
+			return CKEDITOR.create( '.editor', editorConfig ).then( function( editor ) {
 				editor2 = editor;
 
 				// It should have 2 editors.