Browse Source

Merge pull request #144 from ckeditor/t/140

T/140: Introduced ES6 modules in the tests.
Piotrek Koszuliński 10 years ago
parent
commit
2aa9822def

+ 4 - 5
packages/ckeditor5-utils/tests/.jshintrc

@@ -15,14 +15,13 @@
 	"globalstrict": true,
 
 	"globals": {
-		"bender": false,
-		"describe": false,
-		"it": false,
-		"before": false,
-		"beforeEach": false,
 		"after": false,
 		"afterEach": false,
+		"before": false,
+		"beforeEach": false,
+		"describe": false,
 		"expect": false,
+		"it": false,
 		"sinon": false
 	}
 }

+ 0 - 94
packages/ckeditor5-utils/tests/_tools/tools.js

@@ -1,94 +0,0 @@
-/**
- * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
- * For licensing, see LICENSE.md.
- */
-
-'use strict';
-
-( () => {
-	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( creatorName, proto ) {
-			bender.amd.define( 'creator-' + creatorName, [ 'core/creator' ], ( Creator ) => {
-				class TestCreator extends Creator {}
-
-				if ( proto ) {
-					for ( let 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;
-			} );
-		},
-
-		/**
-		 * Returns the number of elements return by the iterator.
-		 *
-		 *		bender.tools.core.getIteratorCount( [ 1, 2, 3, 4, 5 ] ); // 5;
-		 *
-		 * @param {Iterable.<*>} iterator Any iterator.
-		 * @returns {Number} Number of elements returned by that iterator.
-		 */
-		getIteratorCount( iterator ) {
-			let count = 0;
-
-			for ( let _ of iterator ) { // jshint ignore:line
-				count++;
-			}
-
-			return count;
-		}
-	};
-
-	bender.tools.treemodel = {
-		/**
-		 * Returns tree structure as a simplified string. Elements are uppercase and characters are lowercase.
-		 * Start and end of an element is marked the same way, by the element's name (in uppercase).
-		 *
-		 *		let element = new Element( 'div', [], [ 'abc', new Element( 'p', [], 'foo' ), 'xyz' ] );
-		 *		bender.tools.treemodel.getNodesAndText( element ); // abcPfooPxyz
-		 *
-		 * @param {treeModel.Range} range Range to stringify.
-		 * @returns {String} String representing element inner structure.
-		 */
-		getNodesAndText: ( range ) => {
-			let txt = '';
-
-			for ( let step of range ) {
-				let node = step.node;
-
-				if ( node.character ) {
-					txt += node.character.toLowerCase();
-				} else if ( node.name ) {
-					txt += node.name.toUpperCase();
-				}
-			}
-
-			return txt;
-		}
-	};
-} )();

+ 68 - 0
packages/ckeditor5-utils/tests/_utils/utils.js

@@ -0,0 +1,68 @@
+/**
+ * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
+ * For licensing, see LICENSE.md.
+ */
+
+'use strict';
+
+import amdUtils from '/tests/_utils/amd.js';
+
+const utils = {
+	/**
+	 * 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:
+	 *
+	 *		testUtils.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( creatorName, proto ) {
+		amdUtils.define( 'creator-' + creatorName, [ 'core/creator' ], ( Creator ) => {
+			class TestCreator extends Creator {}
+
+			if ( proto ) {
+				for ( let 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;
+		} );
+	},
+
+	/**
+	 * Returns the number of elements return by the iterator.
+	 *
+	 *		testUtils.getIteratorCount( [ 1, 2, 3, 4, 5 ] ); // 5;
+	 *
+	 * @param {Iterable.<*>} iterator Any iterator.
+	 * @returns {Number} Number of elements returned by that iterator.
+	 */
+	getIteratorCount( iterator ) {
+		let count = 0;
+
+		for ( let _ of iterator ) { // jshint ignore:line
+			count++;
+		}
+
+		return count;
+	}
+};
+
+export default utils;

+ 9 - 19
packages/ckeditor5-utils/tests/ckeditor/ckeditor.js

@@ -3,21 +3,22 @@
  * For licensing, see LICENSE.md.
  */
 
-/* bender-include: ../_tools/tools.js */
-
 'use strict';
 
-const modules = bender.amd.require( 'ckeditor', 'core/editor', 'core/config' );
+import testUtils from '/tests/_utils/utils.js';
+import coreTestUtils from '/tests/core/_utils/utils.js';
+
+import CKEDITOR from '/ckeditor.js';
+import Editor from '/ckeditor5/core/editor.js';
+import Config from '/ckeditor5/core/config.js';
 
 let content = document.getElementById( 'content' );
 let editorConfig = { creator: 'creator-test' };
 
-bender.tools.createSinonSandbox();
-bender.tools.core.defineEditorCreatorMock( 'test' );
+testUtils.createSinonSandbox();
+coreTestUtils.defineEditorCreatorMock( 'test' );
 
 beforeEach( () => {
-	const CKEDITOR = modules.ckeditor;
-
 	// Destroy all editor instances.
 	while ( CKEDITOR.instances.length ) {
 		CKEDITOR.instances.get( 0 ).destroy();
@@ -25,14 +26,6 @@ beforeEach( () => {
 } );
 
 describe( 'create', () => {
-	let CKEDITOR, Editor, Config;
-
-	before( () => {
-		CKEDITOR = modules.ckeditor;
-		Editor = modules[ 'core/editor' ];
-		Config = modules[ 'core/config' ];
-	} );
-
 	it( 'should return a promise', () => {
 		expect( CKEDITOR.create( content, editorConfig ) ).to.be.instanceof( Promise );
 	} );
@@ -91,7 +84,7 @@ describe( 'create', () => {
 	} );
 
 	it( 'should be rejected on element not found', () => {
-		let addSpy = bender.sinon.spy( CKEDITOR.instances, 'add' );
+		let addSpy = testUtils.sinon.spy( CKEDITOR.instances, 'add' );
 
 		return CKEDITOR.create( '.undefined' ).then( () => {
 			throw new Error( 'It should not enter this function' );
@@ -107,9 +100,6 @@ describe( 'create', () => {
 
 describe( 'config', () => {
 	it( 'should be an instance of Config', () => {
-		const CKEDITOR = modules.ckeditor;
-		const Config = modules[ 'core/config' ];
-
 		expect( CKEDITOR.config ).to.be.an.instanceof( Config );
 	} );
 } );

+ 1 - 7
packages/ckeditor5-utils/tests/ckeditorerror.js

@@ -5,15 +5,9 @@
 
 'use strict';
 
-const modules = bender.amd.require( 'core/ckeditorerror' );
+import CKEditorError from '/ckeditor5/core/ckeditorerror.js';
 
 describe( 'CKEditorError', () => {
-	let CKEditorError;
-
-	before( () => {
-		CKEditorError = modules[ 'core/ckeditorerror' ];
-	} );
-
 	it( 'inherits from Error', () => {
 		let error = new CKEditorError( 'foo' );
 

+ 9 - 14
packages/ckeditor5-utils/tests/collection.js

@@ -5,9 +5,12 @@
 
 'use strict';
 
-const modules = bender.amd.require( 'core/collection', 'core/ckeditorerror', 'core/utils' );
+import testUtils from '/tests/_utils/utils.js';
+import Collection from '/ckeditor5/core/collection.js';
+import CKEditorError from '/ckeditor5/core/ckeditorerror.js';
+import utils from '/ckeditor5/core/utils.js';
 
-bender.tools.createSinonSandbox();
+testUtils.createSinonSandbox();
 
 function getItem( id, idProperty ) {
 	idProperty = idProperty || 'id';
@@ -18,14 +21,6 @@ function getItem( id, idProperty ) {
 }
 
 describe( 'Collection', () => {
-	let Collection, CKEditorError, utils;
-
-	before( () => {
-		Collection = modules[ 'core/collection' ];
-		CKEditorError = modules[ 'core/ckeditorerror' ];
-		utils = modules[ 'core/utils' ];
-	} );
-
 	let collection;
 
 	beforeEach( () => {
@@ -150,7 +145,7 @@ describe( 'Collection', () => {
 			() => {
 				let nextUid = 0;
 
-				bender.sinon.stub( utils, 'uid', () => {
+				testUtils.sinon.stub( utils, 'uid', () => {
 					return nextUid++;
 				} );
 
@@ -440,7 +435,7 @@ describe( 'Collection', () => {
 
 	describe( 'map', () => {
 		it( 'uses native map', () => {
-			let spy = bender.sinon.stub( Array.prototype, 'map', () => {
+			let spy = testUtils.sinon.stub( Array.prototype, 'map', () => {
 				return [ 'foo' ];
 			} );
 			let ctx = {};
@@ -458,7 +453,7 @@ describe( 'Collection', () => {
 		it( 'uses native find', () => {
 			let needl = getItem( 'foo' );
 
-			let spy = bender.sinon.stub( Array.prototype, 'find', () => {
+			let spy = testUtils.sinon.stub( Array.prototype, 'find', () => {
 				return needl;
 			} );
 			let ctx = {};
@@ -476,7 +471,7 @@ describe( 'Collection', () => {
 		it( 'uses native filter', () => {
 			let needl = getItem( 'foo' );
 
-			let spy = bender.sinon.stub( Array.prototype, 'filter', () => {
+			let spy = testUtils.sinon.stub( Array.prototype, 'filter', () => {
 				return [ needl ];
 			} );
 			let ctx = {};

+ 2 - 6
packages/ckeditor5-utils/tests/config.js

@@ -5,13 +5,9 @@
 
 'use strict';
 
-const modules = bender.amd.require( 'core/config' );
+import Config from '/ckeditor5/core/config.js';
 
-let Config, config;
-
-before( () => {
-	Config = modules[ 'core/config' ];
-} );
+let config;
 
 beforeEach( () => {
 	config = new Config( {

+ 4 - 8
packages/ckeditor5-utils/tests/emittermixin.js

@@ -5,15 +5,11 @@
 
 'use strict';
 
-const modules = bender.amd.require( 'core/emittermixin', 'core/eventinfo', 'core/lib/lodash/object' );
-let EmitterMixin, EventInfo, utilsObject;
-let emitter, listener;
+import EmitterMixin from '/ckeditor5/core/emittermixin.js';
+import EventInfo from '/ckeditor5/core/eventinfo.js';
+import utilsObject from '/ckeditor5/core/lib/lodash/object.js';
 
-before( () => {
-	EmitterMixin = modules[ 'core/emittermixin' ];
-	EventInfo = modules[ 'core/eventinfo' ];
-	utilsObject = modules[ 'core/lib/lodash/object' ];
-} );
+let emitter, listener;
 
 beforeEach( refreshEmitter );
 

+ 1 - 7
packages/ckeditor5-utils/tests/eventinfo.js

@@ -5,15 +5,9 @@
 
 'use strict';
 
-const modules = bender.amd.require( 'core/eventinfo' );
+import EventInfo from '/ckeditor5/core/eventinfo.js';
 
 describe( 'EventInfo', () => {
-	let EventInfo;
-
-	before( () => {
-		EventInfo = modules[ 'core/eventinfo' ];
-	} );
-
 	it( 'should be created properly', () => {
 		let event = new EventInfo( this, 'test' );
 

+ 1 - 7
packages/ckeditor5-utils/tests/lodash.js

@@ -5,15 +5,9 @@
 
 'use strict';
 
-const modules = bender.amd.require( 'core/lib/lodash/object' );
+import objectUtils from '/ckeditor5/core/lib/lodash/object.js';
 
 describe( 'utils', () => {
-	let objectUtils;
-
-	before( () => {
-		objectUtils = modules[ 'core/lib/lodash/object' ];
-	} );
-
 	describe( 'extend()', () => {
 		// Properties of the subsequent objects should override properties of the preceding objects. This is critical for
 		// CKEditor so we keep this test to ensure that Lo-Dash (or whatever) implements it in the way we need it.

+ 2 - 4
packages/ckeditor5-utils/tests/log.js

@@ -7,13 +7,11 @@
 
 'use strict';
 
-const modules = bender.amd.require( 'core/log' );
-let log;
+import log from '/ckeditor5/core/log.js';
+
 let spy;
 
 beforeEach( () => {
-	log = modules[ 'core/log' ];
-
 	if ( spy ) {
 		spy.restore();
 	}

+ 5 - 10
packages/ckeditor5-utils/tests/model.js

@@ -5,21 +5,16 @@
 
 'use strict';
 
-const modules = bender.amd.require( 'core/model', 'core/eventinfo', 'core/ckeditorerror' );
+import testUtils from '/tests/_utils/utils.js';
+import Model from '/ckeditor5/core/model.js';
+import EventInfo from '/ckeditor5/core/eventinfo.js';
+import CKEditorError from '/ckeditor5/core/ckeditorerror.js';
 
 let Car, car;
 
-bender.tools.createSinonSandbox();
+testUtils.createSinonSandbox();
 
 describe( 'Model', () => {
-	let Model, EventInfo, CKEditorError;
-
-	before( () => {
-		Model = modules[ 'core/model' ];
-		EventInfo = modules[ 'core/eventinfo' ];
-		CKEditorError = modules[ 'core/ckeditorerror' ];
-	} );
-
 	beforeEach( 'Create a test model instance', () => {
 		Car = class extends Model {};
 

+ 23 - 24
packages/ckeditor5-utils/tests/bender/tools.js

@@ -3,55 +3,54 @@
  * For licensing, see LICENSE.md.
  */
 
-/* bender-include: ../_tools/tools.js */
-
 'use strict';
 
+import amdTestUtils from '/tests/_utils/amd.js';
+import coreTestUtils from '/tests/core/_utils/utils.js';
+import Creator from '/ckeditor5/core/creator.js';
+
 let createFn3 = () => {};
 let destroyFn3 = () => {};
 
-bender.tools.core.defineEditorCreatorMock( 'test1' );
-bender.tools.core.defineEditorCreatorMock( 'test2', {
+coreTestUtils.defineEditorCreatorMock( 'test1' );
+coreTestUtils.defineEditorCreatorMock( 'test2', {
 	foo: 1,
 	bar: 2
 } );
-bender.tools.core.defineEditorCreatorMock( 'test3', {
+coreTestUtils.defineEditorCreatorMock( 'test3', {
 	create: createFn3,
 	destroy: destroyFn3
 } );
 
-const modules = bender.amd.require( 'core/creator', 'creator-test1', 'creator-test2', 'creator-test3' );
-let Creator;
+const modules = amdTestUtils.require( {
+	testCreator1: 'creator-test1/creator-test1',
+	testCreator2: 'creator-test2/creator-test2',
+	testCreator3: 'creator-test3/creator-test3'
+} );
 
 ///////////////////
 
+let TestCreator1, TestCreator2, TestCreator3;
+
 before( () => {
-	Creator = modules[ 'core/creator' ];
+	TestCreator1 = modules.testCreator1;
+	TestCreator2 = modules.testCreator2;
+	TestCreator3 = modules.testCreator3;
 } );
 
-describe( 'bender.tools.core.defineEditorCreatorMock()', () => {
+describe( 'coreTestUtils.defineEditorCreatorMock()', () => {
 	it( 'should register all creators', () => {
-		const TestCreator1 = modules[ 'creator-test1' ];
-		const TestCreator2 = modules[ 'creator-test2' ];
-		const TestCreator3 = modules[ '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', () => {
-		const TestCreator = modules[ 'creator-test2' ];
-
-		expect( TestCreator.prototype ).to.have.property( 'foo', 1 );
-		expect( TestCreator.prototype ).to.have.property( 'bar', 2 );
+		expect( TestCreator2.prototype ).to.have.property( 'foo', 1 );
+		expect( TestCreator2.prototype ).to.have.property( 'bar', 2 );
 	} );
 
 	it( 'should create spies for create() and destroy() if not defined', () => {
-		const TestCreator1 = modules[ 'creator-test1' ];
-		const TestCreator2 = modules[ 'creator-test2' ];
-		const TestCreator3 = modules[ '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' );
@@ -63,9 +62,9 @@ describe( 'bender.tools.core.defineEditorCreatorMock()', () => {
 	} );
 } );
 
-describe( 'bender.tools.core.getIteratorCount()', () => {
+describe( 'coreTestUtils.getIteratorCount()', () => {
 	it( 'should returns number of editable items ', () => {
-		const count = bender.tools.core.getIteratorCount( [ 1, 2, 3, 4, 5 ] );
+		const count = coreTestUtils.getIteratorCount( [ 1, 2, 3, 4, 5 ] );
 		expect( count ).to.equal( 5 );
 	} );
-} );
+} );

+ 1 - 7
packages/ckeditor5-utils/tests/utils.js

@@ -5,15 +5,9 @@
 
 'use strict';
 
-const modules = bender.amd.require( 'core/utils' );
+import utils from '/ckeditor5/core/utils.js';
 
 describe( 'utils', () => {
-	let utils;
-
-	before( () => {
-		utils = modules[ 'core/utils' ];
-	} );
-
 	describe( 'spy', () => {
 		it( 'should not have `called` after creation', () => {
 			let spy = utils.spy();