浏览代码

Added an optional index argument to Collection#add with tests.

Aleksander Nowodzinski 10 年之前
父节点
当前提交
a233c2be58
共有 2 个文件被更改,包括 72 次插入6 次删除
  1. 13 3
      packages/ckeditor5-ui/src/collection.js
  2. 59 3
      packages/ckeditor5-ui/tests/collection/collection.js

+ 13 - 3
packages/ckeditor5-ui/src/collection.js

@@ -112,12 +112,22 @@ CKEDITOR.define( [ 'emittermixin', 'ckeditorerror', 'utils' ], ( EmitterMixin, C
 			}
 
 			// TODO: Use ES6 default function argument.
-			this._items.splice(
-				index === undefined ? this._items.length : index, 0, item );
+			if ( index === undefined ) {
+				index = this._items.length;
+			} else if ( index > this._items.length || index < 0 ) {
+				/**
+				 * The index number has invalid value.
+				 *
+				 * @error collection-add-item-bad-index
+				 */
+				throw new CKEditorError( 'collection-add-item-invalid-index' );
+			}
+
+			this._items.splice( index, 0, item );
 
 			this._itemMap.set( itemId, item );
 
-			this.fire( 'add', item );
+			this.fire( 'add', item, index );
 
 			return this;
 		}

+ 59 - 3
packages/ckeditor5-ui/tests/collection/collection.js

@@ -10,8 +10,10 @@ const modules = bender.amd.require( 'collection', 'ckeditorerror' );
 bender.tools.createSinonSandbox();
 
 function getItem( id, idProperty ) {
+	idProperty = idProperty || 'id';
+
 	return {
-		[ idProperty || 'id' ]: id
+		[ idProperty ]: id
 	};
 }
 
@@ -165,7 +167,61 @@ describe( 'Collection', () => {
 
 			collection.add( item );
 
-			sinon.assert.calledWithExactly( spy, sinon.match.has( 'source', collection ), item );
+			sinon.assert.calledWithExactly( spy, sinon.match.has( 'source', collection ), item, 0 );
+		} );
+
+		it( 'should support an optional index argument', () => {
+			let collection = new Collection();
+			let item1 = getItem( 'foo' );
+			let item2 = getItem( 'bar' );
+			let item3 = getItem( 'baz' );
+			let item4 = getItem( 'abc' );
+
+			collection.add( item1 );
+			collection.add( item2, 0 );
+			collection.add( item3, 1 );
+			collection.add( item4, 3 );
+
+			expect( collection.get( 0 ) ).to.equal( item2 );
+			expect( collection.get( 1 ) ).to.equal( item3 );
+			expect( collection.get( 2 ) ).to.equal( item1 );
+			expect( collection.get( 3 ) ).to.equal( item4 );
+		} );
+
+		it( 'should throw when index argument is invalid', () => {
+			let collection = new Collection();
+			let item1 = getItem( 'foo' );
+			let item2 = getItem( 'bar' );
+			let item3 = getItem( 'baz' );
+
+			collection.add( item1 );
+
+			expect( () => {
+				collection.add( item2, -1 );
+			} ).to.throw( /^collection-add-item-invalid-index/ );
+
+			expect( () => {
+				collection.add( item2, 2 );
+			} ).to.throw( /^collection-add-item-invalid-index/ );
+
+			collection.add( item2, 1 );
+			collection.add( item3, 0 );
+
+			expect( collection.length ).to.be.equal( 3 );
+		} );
+
+		it( 'should fire the "add" event with the index argument', () => {
+			let spy = sinon.spy();
+
+			collection.add( {} );
+			collection.add( {} );
+
+			collection.on( 'add', spy );
+
+			const item = {};
+			collection.add( item, 1 );
+
+			sinon.assert.calledWithExactly( spy, sinon.match.has( 'source', collection ), item, 1 );
 		} );
 	} );
 
@@ -387,4 +443,4 @@ describe( 'Collection', () => {
 			expect( items ).to.deep.equal( [ 'foo', 'bar', 'bom' ] );
 		} );
 	} );
-} );
+} );