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

Commit

Permalink
Tests: Extended Collection#bindTo tests. Minor refactoring.
Browse files Browse the repository at this point in the history
  • Loading branch information
oleq committed Mar 14, 2017
1 parent 8996c3d commit 9f1d6c9
Showing 1 changed file with 48 additions and 8 deletions.
56 changes: 48 additions & 8 deletions tests/collection.js
Original file line number Diff line number Diff line change
Expand Up @@ -534,11 +534,16 @@ describe( 'Collection', () => {
items = new Collection();
} );

it( 'does not chain', () => {
const returned = collection.bindTo( new Collection() ).as( FactoryClass );

expect( returned ).to.be.undefined;
} );

it( 'creates a binding (initial content)', () => {
items.add( { id: '1' } );
items.add( { id: '2' } );

collection = new Collection();
collection.bindTo( items ).as( FactoryClass );

expect( collection ).to.have.length( 2 );
Expand All @@ -548,7 +553,6 @@ describe( 'Collection', () => {
} );

it( 'creates a binding (new content)', () => {
collection = new Collection();
collection.bindTo( items ).as( FactoryClass );

expect( collection ).to.have.length( 0 );
Expand All @@ -563,7 +567,6 @@ describe( 'Collection', () => {
} );

it( 'creates a binding (item removal)', () => {
collection = new Collection();
collection.bindTo( items ).as( FactoryClass );

expect( collection ).to.have.length( 0 );
Expand Down Expand Up @@ -592,7 +595,7 @@ describe( 'Collection', () => {
} );

it( 'does not chain', () => {
const returned = collection.bindTo( new Collection() ).using( FactoryClass );
const returned = collection.bindTo( new Collection() ).using( () => {} );

expect( returned ).to.be.undefined;
} );
Expand All @@ -617,7 +620,6 @@ describe( 'Collection', () => {

// https://github.com/ckeditor/ckeditor5-ui/issues/113
it( 'creates a binding (normal function)', () => {
collection = new Collection();
collection.bindTo( items ).using( function( item ) {
return new FactoryClass( item );
} );
Expand All @@ -634,7 +636,6 @@ describe( 'Collection', () => {
} );

it( 'creates a 1:1 binding', () => {
collection = new Collection();
collection.bindTo( items ).using( item => item );

expect( collection ).to.have.length( 0 );
Expand All @@ -649,11 +650,51 @@ describe( 'Collection', () => {
expect( collection.get( 0 ) ).to.equal( item1 );
expect( collection.get( 1 ) ).to.equal( item2 );
} );

it( 'creates a conditional binding', () => {
class CustomClass {
constructor( data ) {
this.data = data;
}
}

collection.bindTo( items ).using( item => {
if ( item.id == 'FactoryClass' ) {
return new FactoryClass( item );
} else {
return new CustomClass( item );
}
} );

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

const item1 = { id: 'FactoryClass' };
const item2 = { id: 'CustomClass' };

items.add( item1 );
items.add( item2 );

expect( collection ).to.have.length( 2 );
expect( collection.get( 0 ) ).to.be.instanceOf( FactoryClass );
expect( collection.get( 1 ) ).to.be.instanceOf( CustomClass );
} );

it( 'creates a binding to a property name', () => {
collection.bindTo( items ).using( item => item.prop );

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

items.add( { prop: { value: 'foo' } } );
items.add( { prop: { value: 'bar' } } );

expect( collection ).to.have.length( 2 );
expect( collection.get( 0 ).value ).to.equal( 'foo' );
expect( collection.get( 1 ).value ).to.equal( 'bar' );
} );
} );

describe( 'property name', () => {
it( 'creates a binding', () => {
collection = new Collection();
collection.bindTo( items ).using( 'prop' );

expect( collection ).to.have.length( 0 );
Expand All @@ -667,7 +708,6 @@ describe( 'Collection', () => {
} );

it( 'creates a binding (item removal)', () => {
collection = new Collection();
collection.bindTo( items ).using( 'prop' );

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

0 comments on commit 9f1d6c9

Please sign in to comment.