瀏覽代碼

Tests: Update tests according to style introduced in the tree model.

Piotr Jasiun 10 年之前
父節點
當前提交
7fbe5deebb
共有 1 個文件被更改,包括 261 次插入297 次删除
  1. 261 297
      packages/ckeditor5-utils/tests/collection/collection.js

+ 261 - 297
packages/ckeditor5-utils/tests/collection/collection.js

@@ -9,418 +9,382 @@ var modules = bender.amd.require( 'collection', 'ckeditorerror' );
 
 
 bender.tools.createSinonSandbox();
 bender.tools.createSinonSandbox();
 
 
-function getCollection( items, idProperty ) {
-	var Collection = modules.collection;
-
-	return new Collection( items, idProperty );
-}
-
 function getItem( id, idProperty ) {
 function getItem( id, idProperty ) {
 	return {
 	return {
 		[ idProperty || 'id' ]: id
 		[ idProperty || 'id' ]: id
 	};
 	};
 }
 }
 
 
-///////////////////////////////////////
-
-describe( 'constructor', () => {
-	it( 'allows to change the id property used by the collection', () => {
-		var Collection = modules.collection;
-		var item1 = { id: 'foo', name: 'xx' };
-		var item2 = { id: 'foo', name: 'yy' };
-		var box = new Collection( { idProperty: 'name' } );
-
-		box.add( item1 );
-		box.add( item2 );
+describe( 'Collection', () => {
+	var Collection, CKEditorError;
 
 
-		expect( box ).to.have.length( 2 );
-
-		expect( box.get( 'xx' ) ).to.equal( item1 );
-		expect( box.remove( 'yy' ) ).to.equal( item2 );
+	before( () => {
+		Collection = modules.collection;
+		CKEditorError = modules.CKEditorError;
 	} );
 	} );
-} );
 
 
-describe( 'add', () => {
-	it( 'should be chainable', () => {
-		var box = getCollection();
+	var collection;
 
 
-		expect( box.add( {} ) ).to.equal( box );
+	beforeEach( () => {
+		collection = new Collection();
 	} );
 	} );
 
 
-	it( 'should change the length', () => {
-		var box = getCollection();
-
-		expect( box ).to.have.length( 0 );
+	describe( 'constructor', () => {
+		it( 'allows to change the id property used by the collection', () => {
+			var item1 = { id: 'foo', name: 'xx' };
+			var item2 = { id: 'foo', name: 'yy' };
+			var collection = new Collection( { idProperty: 'name' } );
 
 
-		box.add( {} );
-		expect( box ).to.have.length( 1 );
-
-		box.add( {} );
-		expect( box ).to.have.length( 2 );
-	} );
+			collection.add( item1 );
+			collection.add( item2 );
 
 
-	it( 'should enable get( index )', () => {
-		var box = getCollection();
-		var item1 = {};
-		var item2 = {};
+			expect( collection ).to.have.length( 2 );
 
 
-		box.add( item1 );
-		expect( box.get( 0 ) ).to.equal( item1 );
-
-		box.add( item2 );
-		expect( box.get( 0 ) ).to.equal( item1 );
-		expect( box.get( 1 ) ).to.equal( item2 );
+			expect( collection.get( 'xx' ) ).to.equal( item1 );
+			expect( collection.remove( 'yy' ) ).to.equal( item2 );
+		} );
 	} );
 	} );
 
 
-	it( 'should enable get( id )', () => {
-		var box = getCollection();
-		var item1 = getItem( 'foo' );
-		var item2 = getItem( 'bar' );
+	describe( 'add', () => {
+		it( 'should be chainable', () => {
+			expect( collection.add( {} ) ).to.equal( collection );
+		} );
 
 
-		box.add( item1 );
-		box.add( item2 );
+		it( 'should change the length', () => {
+			expect( collection ).to.have.length( 0 );
 
 
-		expect( box.get( 'foo' ) ).to.equal( item1 );
-		expect( box.get( 'bar' ) ).to.equal( item2 );
-	} );
+			collection.add( {} );
+			expect( collection ).to.have.length( 1 );
 
 
-	it( 'should enable get( id ) - custom id property', () => {
-		var box = getCollection( { idProperty: 'name' } );
-		var item1 = getItem( 'foo', 'name' );
-		var item2 = getItem( 'bar', 'name' );
+			collection.add( {} );
+			expect( collection ).to.have.length( 2 );
+		} );
 
 
-		box.add( item1 );
-		box.add( item2 );
+		it( 'should enable get( index )', () => {
+			var item1 = {};
+			var item2 = {};
 
 
-		expect( box.get( 'foo' ) ).to.equal( item1 );
-		expect( box.get( 'bar' ) ).to.equal( item2 );
-	} );
+			collection.add( item1 );
+			expect( collection.get( 0 ) ).to.equal( item1 );
 
 
-	it( 'should generate an id when not defined', () => {
-		var box = getCollection();
-		var item = {};
+			collection.add( item2 );
+			expect( collection.get( 0 ) ).to.equal( item1 );
+			expect( collection.get( 1 ) ).to.equal( item2 );
+		} );
 
 
-		box.add( item );
+		it( 'should enable get( id )', () => {
+			var item1 = getItem( 'foo' );
+			var item2 = getItem( 'bar' );
 
 
-		expect( item.id ).to.be.a( 'string' );
-		expect( box.get( item.id ) ).to.equal( item );
-	} );
+			collection.add( item1 );
+			collection.add( item2 );
 
 
-	it( 'should generate an id when not defined - custom id property', () => {
-		var box = getCollection( { idProperty: 'name' } );
-		var item = {};
-
-		box.add( item );
+			expect( collection.get( 'foo' ) ).to.equal( item1 );
+			expect( collection.get( 'bar' ) ).to.equal( item2 );
+		} );
 
 
-		expect( item.name ).to.be.a( 'string' );
-		expect( box.get( item.name ) ).to.equal( item );
-	} );
+		it( 'should enable get( id ) - custom id property', () => {
+			var collection = new Collection( { idProperty: 'name' } );
+			var item1 = getItem( 'foo', 'name' );
+			var item2 = getItem( 'bar', 'name' );
 
 
-	it( 'should not change an existing id of an item', () => {
-		var box = getCollection();
-		var item = getItem( 'foo' );
+			collection.add( item1 );
+			collection.add( item2 );
 
 
-		box.add( item );
+			expect( collection.get( 'foo' ) ).to.equal( item1 );
+			expect( collection.get( 'bar' ) ).to.equal( item2 );
+		} );
 
 
-		expect( item.id ).to.equal( 'foo' );
-	} );
+		it( 'should generate an id when not defined', () => {
+			var item = {};
 
 
-	it( 'should throw when item with this id already exists', () => {
-		var CKEditorError = modules.CKEditorError;
+			collection.add( item );
 
 
-		var box = getCollection();
-		var item1 = getItem( 'foo' );
-		var item2 = getItem( 'foo' );
+			expect( item.id ).to.be.a( 'string' );
+			expect( collection.get( item.id ) ).to.equal( item );
+		} );
 
 
-		box.add( item1 );
+		it( 'should generate an id when not defined - custom id property', () => {
+			var collection = new Collection( { idProperty: 'name' } );
+			var item = {};
 
 
-		expect( () => {
-			box.add( item2 );
-		} ).to.throw( CKEditorError, /^collection-add-item-already-exists/ );
-	} );
+			collection.add( item );
 
 
-	it( 'should throw when item\'s id is not a string', () => {
-		var CKEditorError = modules.CKEditorError;
+			expect( item.name ).to.be.a( 'string' );
+			expect( collection.get( item.name ) ).to.equal( item );
+		} );
 
 
-		var box = getCollection();
-		var item = { id: 1 };
+		it( 'should not change an existing id of an item', () => {
+			var item = getItem( 'foo' );
 
 
-		expect( () => {
-			box.add( item );
-		} ).to.throw( CKEditorError, /^collection-add-invalid-id/ );
-	} );
+			collection.add( item );
 
 
-	it(
-		'should not override item under an existing id in case of a collision ' +
-		'between existing items and one with an automatically generated id',
-		() => {
-			var box = getCollection();
+			expect( item.id ).to.equal( 'foo' );
+		} );
 
 
-			box.add( getItem( '0' ) );
-			box.add( getItem( '1' ) );
-			box.add( getItem( '2' ) );
+		it( 'should throw when item with this id already exists', () => {
+			var item1 = getItem( 'foo' );
+			var item2 = getItem( 'foo' );
 
 
-			var item = {};
+			collection.add( item1 );
 
 
-			box.add( item );
+			expect( () => {
+				collection.add( item2 );
+			} ).to.throw( CKEditorError, /^collection-add-item-already-exists/ );
+		} );
 
 
-			expect( item ).to.have.property( 'id', '3' );
-		}
-	);
+		it( 'should throw when item\'s id is not a string', () => {
+			var item = { id: 1 };
 
 
-	it( 'should fire the "add" event', () => {
-		var box = getCollection();
-		var spy = sinon.spy();
-		var item = {};
+			expect( () => {
+				collection.add( item );
+			} ).to.throw( CKEditorError, /^collection-add-invalid-id/ );
+		} );
 
 
-		box.on( 'add', spy );
+		it(
+			'should not override item under an existing id in case of a collision ' +
+			'between existing items and one with an automatically generated id',
+			() => {
+				collection.add( getItem( '0' ) );
+				collection.add( getItem( '1' ) );
+				collection.add( getItem( '2' ) );
 
 
-		box.add( item );
+				var item = {};
 
 
-		sinon.assert.calledWithExactly( spy, sinon.match.has( 'source', box ), item );
-	} );
-} );
+				collection.add( item );
 
 
-describe( 'get', () => {
-	it( 'should return an item', () => {
-		var box = getCollection();
-		var item = getItem( 'foo' );
-		box.add( item );
+				expect( item ).to.have.property( 'id', '3' );
+			}
+		);
 
 
-		expect( box.get( 'foo' ) ).to.equal( item );
-	} );
-
-	it( 'should return null if id does not exist', () => {
-		var box = getCollection();
-		box.add( getItem( 'foo' ) );
+		it( 'should fire the "add" event', () => {
+			var spy = sinon.spy();
+			var item = {};
 
 
-		expect( box.get( 'bar' ) ).to.be.null;
-	} );
+			collection.on( 'add', spy );
 
 
-	it( 'should throw if neither string or number given', () => {
-		var CKEditorError = modules.ckeditorerror;
-		var box = getCollection();
+			collection.add( item );
 
 
-		expect( () => {
-			box.get( true );
-		} ).to.throw( CKEditorError, /^collection-get-invalid-arg/ );
+			sinon.assert.calledWithExactly( spy, sinon.match.has( 'source', collection ), item );
+		} );
 	} );
 	} );
-} );
 
 
-describe( 'remove', () => {
-	it( 'should remove the model by index', () => {
-		var box = getCollection();
+	describe( 'get', () => {
+		it( 'should return an item', () => {
+			var item = getItem( 'foo' );
+			collection.add( item );
 
 
-		box.add( getItem( 'bom' ) );
-		box.add( getItem( 'foo' ) );
-		box.add( getItem( 'bar' ) );
+			expect( collection.get( 'foo' ) ).to.equal( item );
+		} );
 
 
-		expect( box ).to.have.length( 3 );
+		it( 'should return null if id does not exist', () => {
+			collection.add( getItem( 'foo' ) );
 
 
-		var removedItem = box.remove( 1 );
+			expect( collection.get( 'bar' ) ).to.be.null;
+		} );
 
 
-		expect( box ).to.have.length( 2 );
-		expect( box.get( 'foo' ) ).to.be.null;
-		expect( box.get( 1 ) ).to.have.property( 'id', 'bar' );
-		expect( removedItem ).to.have.property( 'id', 'foo' );
+		it( 'should throw if neither string or number given', () => {
+			expect( () => {
+				collection.get( true );
+			} ).to.throw( CKEditorError, /^collection-get-invalid-arg/ );
+		} );
 	} );
 	} );
 
 
-	it( 'should remove the model by index - custom id property', () => {
-		var box = getCollection( { idProperty: 'name' } );
+	describe( 'remove', () => {
+		it( 'should remove the model by index', () => {
+			collection.add( getItem( 'bom' ) );
+			collection.add( getItem( 'foo' ) );
+			collection.add( getItem( 'bar' ) );
 
 
-		box.add( getItem( 'foo', 'name' ) );
+			expect( collection ).to.have.length( 3 );
 
 
-		var removedItem = box.remove( 0 );
+			var removedItem = collection.remove( 1 );
 
 
-		expect( box ).to.have.length( 0 );
-		expect( box.get( 'foo' ) ).to.be.null;
-		expect( removedItem ).to.have.property( 'name', 'foo' );
-	} );
+			expect( collection ).to.have.length( 2 );
+			expect( collection.get( 'foo' ) ).to.be.null;
+			expect( collection.get( 1 ) ).to.have.property( 'id', 'bar' );
+			expect( removedItem ).to.have.property( 'id', 'foo' );
+		} );
 
 
-	it( 'should remove the model by id', () => {
-		var box = getCollection();
+		it( 'should remove the model by index - custom id property', () => {
+			var collection = new Collection( { idProperty: 'name' } );
 
 
-		box.add( getItem( 'bom' ) );
-		box.add( getItem( 'foo' ) );
-		box.add( getItem( 'bar' ) );
+			collection.add( getItem( 'foo', 'name' ) );
 
 
-		expect( box ).to.have.length( 3 );
+			var removedItem = collection.remove( 0 );
 
 
-		var removedItem = box.remove( 'foo' );
+			expect( collection ).to.have.length( 0 );
+			expect( collection.get( 'foo' ) ).to.be.null;
+			expect( removedItem ).to.have.property( 'name', 'foo' );
+		} );
 
 
-		expect( box ).to.have.length( 2 );
-		expect( box.get( 'foo' ) ).to.be.null;
-		expect( box.get( 1 ) ).to.have.property( 'id', 'bar' );
-		expect( removedItem ).to.have.property( 'id', 'foo' );
-	} );
+		it( 'should remove the model by id', () => {
+			collection.add( getItem( 'bom' ) );
+			collection.add( getItem( 'foo' ) );
+			collection.add( getItem( 'bar' ) );
 
 
-	it( 'should remove the model by model', () => {
-		var box = getCollection();
-		var item = getItem( 'foo' );
+			expect( collection ).to.have.length( 3 );
 
 
-		box.add( getItem( 'bom' ) );
-		box.add( item );
-		box.add( getItem( 'bar' ) );
+			var removedItem = collection.remove( 'foo' );
 
 
-		expect( box ).to.have.length( 3 );
+			expect( collection ).to.have.length( 2 );
+			expect( collection.get( 'foo' ) ).to.be.null;
+			expect( collection.get( 1 ) ).to.have.property( 'id', 'bar' );
+			expect( removedItem ).to.have.property( 'id', 'foo' );
+		} );
 
 
-		var removedItem = box.remove( item );
+		it( 'should remove the model by model', () => {
+			var item = getItem( 'foo' );
 
 
-		expect( box ).to.have.length( 2 );
-		expect( box.get( 'foo' ) ).to.be.null;
-		expect( box.get( 1 ) ).to.have.property( 'id', 'bar' );
-		expect( removedItem ).to.equal( item );
-	} );
+			collection.add( getItem( 'bom' ) );
+			collection.add( item );
+			collection.add( getItem( 'bar' ) );
 
 
-	it( 'should remove the model by model - custom id property', () => {
-		var box = getCollection( null, 'name' );
-		var item = getItem( 'foo', 'name' );
+			expect( collection ).to.have.length( 3 );
 
 
-		box.add( item );
+			var removedItem = collection.remove( item );
 
 
-		var removedItem = box.remove( item );
+			expect( collection ).to.have.length( 2 );
+			expect( collection.get( 'foo' ) ).to.be.null;
+			expect( collection.get( 1 ) ).to.have.property( 'id', 'bar' );
+			expect( removedItem ).to.equal( item );
+		} );
 
 
-		expect( box ).to.have.length( 0 );
-		expect( box.get( 'foo' ) ).to.be.null;
-		expect( removedItem ).to.have.property( 'name', 'foo' );
-	} );
+		it( 'should remove the model by model - custom id property', () => {
+			var collection = new Collection( null, 'name' );
+			var item = getItem( 'foo', 'name' );
 
 
-	it( 'should fire the "remove" event', () => {
-		var box = getCollection();
-		var item1 = getItem( 'foo' );
-		var item2 = getItem( 'bar' );
-		var item3 = getItem( 'bom' );
+			collection.add( item );
 
 
-		box.add( item1 );
-		box.add( item2 );
-		box.add( item3 );
+			var removedItem = collection.remove( item );
 
 
-		var spy = sinon.spy();
+			expect( collection ).to.have.length( 0 );
+			expect( collection.get( 'foo' ) ).to.be.null;
+			expect( removedItem ).to.have.property( 'name', 'foo' );
+		} );
 
 
-		box.on( 'remove', spy );
+		it( 'should fire the "remove" event', () => {
+			var item1 = getItem( 'foo' );
+			var item2 = getItem( 'bar' );
+			var item3 = getItem( 'bom' );
 
 
-		box.remove( 1 );		// by index
-		box.remove( item1 );	// by model
-		box.remove( 'bom' );	// by id
+			collection.add( item1 );
+			collection.add( item2 );
+			collection.add( item3 );
 
 
-		sinon.assert.calledThrice( spy );
-		sinon.assert.calledWithExactly( spy, sinon.match.has( 'source', box ), item1 );
-		sinon.assert.calledWithExactly( spy, sinon.match.has( 'source', box ), item2 );
-		sinon.assert.calledWithExactly( spy, sinon.match.has( 'source', box ), item3 );
-	} );
+			var spy = sinon.spy();
 
 
-	it( 'should throw an error on invalid index', () => {
-		var CKEditorError = modules.ckeditorerror;
+			collection.on( 'remove', spy );
 
 
-		var box = getCollection();
-		box.add( getItem( 'foo' ) );
+			collection.remove( 1 );		// by index
+			collection.remove( item1 );	// by model
+			collection.remove( 'bom' );	// by id
 
 
-		expect( () => {
-			box.remove( 1 );
-		} ).to.throw( CKEditorError, /^collection-remove-404/ );
+			sinon.assert.calledThrice( spy );
+			sinon.assert.calledWithExactly( spy, sinon.match.has( 'source', collection ), item1 );
+			sinon.assert.calledWithExactly( spy, sinon.match.has( 'source', collection ), item2 );
+			sinon.assert.calledWithExactly( spy, sinon.match.has( 'source', collection ), item3 );
+		} );
 
 
-		expect( box ).to.have.length( 1 );
-	} );
+		it( 'should throw an error on invalid index', () => {
+			collection.add( getItem( 'foo' ) );
 
 
-	it( 'should throw an error on invalid id', () => {
-		var CKEditorError = modules.ckeditorerror;
+			expect( () => {
+				collection.remove( 1 );
+			} ).to.throw( CKEditorError, /^collection-remove-404/ );
 
 
-		var box = getCollection();
-		box.add( getItem( 'foo' ) );
+			expect( collection ).to.have.length( 1 );
+		} );
 
 
-		expect( () => {
-			box.remove( 'bar' );
-		} ).to.throw( CKEditorError, /^collection-remove-404/ );
+		it( 'should throw an error on invalid id', () => {
+			collection.add( getItem( 'foo' ) );
 
 
-		expect( box ).to.have.length( 1 );
-	} );
+			expect( () => {
+				collection.remove( 'bar' );
+			} ).to.throw( CKEditorError, /^collection-remove-404/ );
 
 
-	it( 'should throw an error on invalid model', () => {
-		var CKEditorError = modules.ckeditorerror;
+			expect( collection ).to.have.length( 1 );
+		} );
 
 
-		var box = getCollection();
-		box.add( getItem( 'foo' ) );
+		it( 'should throw an error on invalid model', () => {
+			collection.add( getItem( 'foo' ) );
 
 
-		expect( () => {
-			box.remove( getItem( 'bar' ) );
-		} ).to.throw( CKEditorError, /^collection-remove-404/ );
+			expect( () => {
+				collection.remove( getItem( 'bar' ) );
+			} ).to.throw( CKEditorError, /^collection-remove-404/ );
 
 
-		expect( box ).to.have.length( 1 );
+			expect( collection ).to.have.length( 1 );
+		} );
 	} );
 	} );
-} );
 
 
-describe( 'map', () => {
-	it( 'uses native map', () => {
-		var collection = getCollection();
+	describe( 'map', () => {
+		it( 'uses native map', () => {
+			var spy = bender.sinon.stub( Array.prototype, 'map', () => {
+				return [ 'foo' ];
+			} );
+			var ctx = {};
 
 
-		var spy = bender.sinon.stub( Array.prototype, 'map', () => {
-			return [ 'foo' ];
-		} );
-		var ctx = {};
-
-		var ret = collection.map( callback, ctx );
+			var ret = collection.map( callback, ctx );
 
 
-		sinon.assert.calledWithExactly( spy, callback, ctx );
-		expect( ret ).to.deep.equal( [ 'foo' ], 'ret value was forwarded' );
+			sinon.assert.calledWithExactly( spy, callback, ctx );
+			expect( ret ).to.deep.equal( [ 'foo' ], 'ret value was forwarded' );
 
 
-		function callback() {}
+			function callback() {}
+		} );
 	} );
 	} );
-} );
 
 
-describe( 'find', () => {
-	it( 'uses native find', () => {
-		var collection = getCollection();
-		var needl = getItem( 'foo' );
+	describe( 'find', () => {
+		it( 'uses native find', () => {
+			var needl = getItem( 'foo' );
 
 
-		var spy = bender.sinon.stub( Array.prototype, 'find', () => {
-			return needl;
-		} );
-		var ctx = {};
+			var spy = bender.sinon.stub( Array.prototype, 'find', () => {
+				return needl;
+			} );
+			var ctx = {};
 
 
-		var ret = collection.find( callback, ctx );
+			var ret = collection.find( callback, ctx );
 
 
-		sinon.assert.calledWithExactly( spy, callback, ctx );
-		expect( ret ).to.equal( needl, 'ret value was forwarded' );
+			sinon.assert.calledWithExactly( spy, callback, ctx );
+			expect( ret ).to.equal( needl, 'ret value was forwarded' );
 
 
-		function callback() {}
+			function callback() {}
+		} );
 	} );
 	} );
-} );
 
 
-describe( 'filter', () => {
-	it( 'uses native filter', () => {
-		var collection = getCollection();
-		var needl = getItem( 'foo' );
+	describe( 'filter', () => {
+		it( 'uses native filter', () => {
+			var needl = getItem( 'foo' );
 
 
-		var spy = bender.sinon.stub( Array.prototype, 'filter', () => {
-			return [ needl ];
-		} );
-		var ctx = {};
+			var spy = bender.sinon.stub( Array.prototype, 'filter', () => {
+				return [ needl ];
+			} );
+			var ctx = {};
 
 
-		var ret = collection.filter( callback, ctx );
+			var ret = collection.filter( callback, ctx );
 
 
-		sinon.assert.calledWithExactly( spy, callback, ctx );
-		expect( ret ).to.deep.equal( [ needl ], 'ret value was forwarded' );
+			sinon.assert.calledWithExactly( spy, callback, ctx );
+			expect( ret ).to.deep.equal( [ needl ], 'ret value was forwarded' );
 
 
-		function callback() {}
+			function callback() {}
+		} );
 	} );
 	} );
-} );
 
 
-describe( 'iterator', () => {
-	it( 'covers the whole collection', () => {
-		var collection = getCollection();
-		var item1 = getItem( 'foo' );
-		var item2 = getItem( 'bar' );
-		var item3 = getItem( 'bom' );
-		var items = [];
+	describe( 'iterator', () => {
+		it( 'covers the whole collection', () => {
+			var item1 = getItem( 'foo' );
+			var item2 = getItem( 'bar' );
+			var item3 = getItem( 'bom' );
+			var items = [];
 
 
-		collection.add( item1 );
-		collection.add( item2 );
-		collection.add( item3 );
+			collection.add( item1 );
+			collection.add( item2 );
+			collection.add( item3 );
 
 
-		for ( let item of collection ) {
-			items.push( item.id );
-		}
+			for ( let item of collection ) {
+				items.push( item.id );
+			}
 
 
-		expect( items ).to.deep.equal( [ 'foo', 'bar', 'bom' ] );
+			expect( items ).to.deep.equal( [ 'foo', 'bar', 'bom' ] );
+		} );
 	} );
 	} );
 } );
 } );