Skip to content
This repository has been archived by the owner on Jun 26, 2020. It is now read-only.

Commit

Permalink
Added missing index for initial items in collecton binging.
Browse files Browse the repository at this point in the history
  • Loading branch information
oskarwrobel committed Jan 9, 2018
1 parent 19c9905 commit 24db40a
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 13 deletions.
2 changes: 1 addition & 1 deletion src/collection.js
Original file line number Diff line number Diff line change
Expand Up @@ -572,7 +572,7 @@ export default class Collection {

// Load the initial content of the collection.
for ( const externalItem of externalCollection ) {
addItem( null, externalItem );
addItem( null, externalItem, externalCollection.getIndex( externalItem ) );
}

// Synchronize the with collection as new items are added.
Expand Down
33 changes: 21 additions & 12 deletions tests/collection.js
Original file line number Diff line number Diff line change
Expand Up @@ -769,6 +769,11 @@ describe( 'Collection', () => {
} );

it( 'skips when there is no item', () => {
// Add before collection is bound.
items.add( { value: 1, skip: true } );

expect( collection ).to.have.length( 0 );

collection.bindTo( items ).using( item => {
if ( item.skip ) {
return null;
Expand All @@ -777,17 +782,18 @@ describe( 'Collection', () => {
return item;
} );

// Still 0 because initial item was skipped.
expect( collection ).to.have.length( 0 );

items.add( { value: 1, skip: false } );
items.add( { value: 2, skip: true } );
items.add( { value: 3, skip: false } );
items.add( { value: 2, skip: false } );
items.add( { value: 3, skip: true } );
items.add( { value: 4, skip: false } );

expect( Array.from( collection, item => item.value ) ).to.deep.equal( [ 1, 3 ] );
expect( Array.from( collection, item => item.value ) ).to.deep.equal( [ 2, 4 ] );

items.add( { value: 4, skip: false }, 1 );
items.add( { value: 5, skip: false }, 2 );

expect( Array.from( collection, item => item.value ) ).to.deep.equal( [ 1, 4, 3 ] );
expect( Array.from( collection, item => item.value ) ).to.deep.equal( [ 2, 5, 4 ] );
} );
} );

Expand Down Expand Up @@ -826,19 +832,22 @@ describe( 'Collection', () => {
} );

it( 'skips when there is no item', () => {
items.add( { prop: null } );

collection.bindTo( items ).using( 'prop' );

// Still 0 because initial item was skipped.
expect( collection ).to.have.length( 0 );

items.add( { prop: { value: 'foo' } } );
items.add( { value: null } );
items.add( { prop: { value: 'biz' } } );
items.add( { prop: { value: 2, skip: false } } );
items.add( { prop: null } );
items.add( { prop: { value: 4, skip: false } } );

expect( Array.from( collection, item => item.value ) ).to.deep.equal( [ 'foo', 'biz' ] );
expect( Array.from( collection, item => item.value ) ).to.deep.equal( [ 2, 4 ] );

items.add( { prop: { value: 'bar' } }, 1 );
items.add( { prop: { value: 5 } }, 2 );

expect( Array.from( collection, item => item.value ) ).to.deep.equal( [ 'foo', 'bar', 'biz' ] );
expect( Array.from( collection, item => item.value ) ).to.deep.equal( [ 2, 5, 4 ] );
} );
} );
} );
Expand Down

0 comments on commit 24db40a

Please sign in to comment.