8
0
Quellcode durchsuchen

Internal (utils): Renamed addBatch to change event.

Marek Lewandowski vor 5 Jahren
Ursprung
Commit
c9994a81f0

+ 16 - 3
packages/ckeditor5-utils/src/collection.js

@@ -179,7 +179,7 @@ export default class Collection {
 	 * @param {Number} [index] The position of the item in the collection. The item
 	 * is pushed to the collection when `index` not specified.
 	 * @fires add
-	 * @fires addBatch
+	 * @fires change
 	 */
 	add( item, index ) {
 		return this.addMany( [ item ], index );
@@ -194,7 +194,7 @@ export default class Collection {
 	 * @param {Iterable.<Object>} item
 	 * @param {Number} [index] The position of the insertion. Items will be appended if no `index` is specified.
 	 * @fires add
-	 * @fires addBatch
+	 * @fires change
 	 */
 	addMany( items, index ) {
 		if ( index === undefined ) {
@@ -221,7 +221,11 @@ export default class Collection {
 			this.fire( 'add', item, currentItemIndex );
 		}
 
-		this.fire( 'addBatch', items, index );
+		this.fire( 'change', {
+			added: items,
+			removed: [],
+			index
+		} );
 
 		return this;
 	}
@@ -703,6 +707,15 @@ export default class Collection {
 	 */
 
 	/**
+	 * Fired when the collection was changed due to adding or removing items.
+	 *
+	 * @event change
+	 * @param {Iterable.<Object>} added List of added items.
+	 * @param {Iterable.<Object>} removed List of removed items.
+	 * @param {Number} index Index where the addition or removal occurred.
+	 */
+
+	/**
 	 * Fired when an item is removed from the collection.
 	 *
 	 * @event remove

+ 16 - 6
packages/ckeditor5-utils/tests/collection.js

@@ -383,29 +383,39 @@ describe( 'Collection', () => {
 			sinon.assert.calledWithExactly( spy, sinon.match.has( 'source', collection ), item, 1 );
 		} );
 
-		it( 'should fire the "addBatch" event', () => {
+		it( 'should fire the "change" event', () => {
 			const spy = sinon.spy();
 			const items = [ {}, {} ];
 
-			collection.on( 'addBatch', spy );
+			collection.on( 'change', spy );
 
 			collection.addMany( items );
 
-			sinon.assert.calledWithExactly( spy, sinon.match.has( 'source', collection ), items, 0 );
+			expect( spy.callCount, 'call count' ).to.equal( 1 );
+			expect( spy.args[ 0 ][ 1 ] ).to.deep.eql( {
+				added: items,
+				removed: [],
+				index: 0
+			} );
 		} );
 
-		it( 'should fire the "addBatch" event with the index argument', () => {
+		it( 'should fire the "change" event with the index argument', () => {
 			const spy = sinon.spy();
 			const firstBatch = [ {}, {} ];
 			const secondBatch = [ {}, {} ];
 
 			collection.addMany( firstBatch );
 
-			collection.on( 'addBatch', spy );
+			collection.on( 'change', spy );
 
 			collection.addMany( secondBatch, 1 );
 
-			sinon.assert.calledWithExactly( spy, sinon.match.has( 'source', collection ), secondBatch, 1 );
+			expect( spy.callCount, 'call count' ).to.equal( 1 );
+			expect( spy.args[ 0 ][ 1 ] ).to.deep.eql( {
+				added: secondBatch,
+				removed: [],
+				index: 1
+			} );
 		} );
 
 		it( 'should support an optional index argument', () => {