| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397 |
- /**
- * @license Copyright (c) 2003-2020, CKSource - Frederico Knabben. All rights reserved.
- * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
- */
- import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
- import Collection from '../src/collection';
- import { expectToThrowCKEditorError } from '../tests/_utils/utils';
- function getItem( id, idProperty ) {
- idProperty = idProperty || 'id';
- return {
- [ idProperty ]: id
- };
- }
- describe( 'Collection', () => {
- let collection;
- testUtils.createSinonSandbox();
- beforeEach( () => {
- collection = new Collection();
- } );
- describe( 'constructor()', () => {
- describe( 'setting initial collection items', () => {
- it( 'should work using an array', () => {
- const item1 = getItem( 'foo' );
- const item2 = getItem( 'bar' );
- const collection = new Collection( [ item1, item2 ] );
- expect( collection ).to.have.length( 2 );
- expect( collection.get( 0 ) ).to.equal( item1 );
- expect( collection.get( 1 ) ).to.equal( item2 );
- expect( collection.get( 'foo' ) ).to.equal( item1 );
- expect( collection.get( 'bar' ) ).to.equal( item2 );
- } );
- it( 'should work using an iterable', () => {
- const item1 = getItem( 'foo' );
- const item2 = getItem( 'bar' );
- const itemsSet = new Set( [ item1, item2 ] );
- const collection = new Collection( itemsSet );
- expect( collection ).to.have.length( 2 );
- expect( collection.get( 0 ) ).to.equal( item1 );
- expect( collection.get( 1 ) ).to.equal( item2 );
- } );
- it( 'should generate ids for items that doesn\'t have it', () => {
- const item = {};
- const collection = new Collection( [ item ] );
- expect( collection.get( 0 ).id ).to.be.a( 'string' );
- expect( collection.get( 0 ).id ).not.to.be.empty;
- } );
- it( 'should throw an error when an invalid item key is provided', () => {
- const badIdItem = getItem( 1 ); // Number id is not supported.
- expectToThrowCKEditorError( () => {
- return new Collection( [ badIdItem ] );
- }, /^collection-add-invalid-id/ );
- } );
- it( 'should throw an error when two items have the same key', () => {
- const item1 = getItem( 'foo' );
- const item2 = getItem( 'foo' );
- expectToThrowCKEditorError( () => {
- return new Collection( [ item1, item2 ] );
- }, /^collection-add-item-already-exists/ );
- } );
- } );
- describe( 'options', () => {
- it( 'should allow to change the id property used by the collection', () => {
- const item1 = { id: 'foo', name: 'xx' };
- const item2 = { id: 'foo', name: 'yy' };
- const collection = new Collection( { idProperty: 'name' } );
- collection.add( item1 );
- collection.add( item2 );
- expect( collection ).to.have.length( 2 );
- expect( collection.get( 'xx' ) ).to.equal( item1 );
- expect( collection.remove( 'yy' ) ).to.equal( item2 );
- } );
- it( 'should allow to change the id property used by the collection (initial items passed to the constructor)', () => {
- const item1 = { id: 'foo', name: 'xx' };
- const item2 = { id: 'foo', name: 'yy' };
- const collection = new Collection( [ item1, item2 ], { idProperty: 'name' } );
- expect( collection ).to.have.length( 2 );
- expect( collection.get( 'xx' ) ).to.equal( item1 );
- expect( collection.remove( 'yy' ) ).to.equal( item2 );
- } );
- } );
- } );
- describe( 'length', () => {
- it( 'should return collection length', () => {
- expect( collection.length ).to.equal( 0 );
- collection.add( { foo: 'bar' } );
- expect( collection.length ).to.equal( 1 );
- } );
- } );
- describe( 'first', () => {
- it( 'should return the first item from the collection', () => {
- const item1 = { foo: 'bar' };
- const item2 = { bar: 'biz' };
- collection.add( item1 );
- collection.add( item2 );
- expect( collection.first ).to.equal( item1 );
- } );
- it( 'should return null when collection is empty', () => {
- expect( collection.first ).to.null;
- } );
- } );
- describe( 'last', () => {
- it( 'should return the last item from the collection', () => {
- const item1 = { foo: 'bar' };
- const item2 = { bar: 'biz' };
- collection.add( item1 );
- collection.add( item2 );
- expect( collection.last ).to.equal( item2 );
- } );
- it( 'should return null when collection is empty', () => {
- expect( collection.last ).to.null;
- } );
- } );
- describe( 'add()', () => {
- it( 'should proxy its calls to addMany', () => {
- const spy = sinon.spy( collection, 'addMany' );
- const item = {};
- collection.add( item );
- sinon.assert.calledOnce( spy );
- sinon.assert.calledWithExactly( spy, sinon.match.array.contains( [ item ] ), undefined );
- } );
- it( 'should proxy custom index', () => {
- const stub = sinon.stub( collection, 'addMany' );
- const item = {};
- collection.add( item, 5 );
- sinon.assert.calledOnce( stub );
- sinon.assert.calledWithExactly( stub, sinon.match.any, 5 );
- } );
- it( 'should proxy returned value', () => {
- const expectedReturn = {};
- sinon.stub( collection, 'addMany' ).returns( expectedReturn );
- expect( collection.add( 1 ) ).to.equal( expectedReturn );
- } );
- it( 'should change the length', () => {
- expect( collection ).to.have.length( 0 );
- collection.add( {} );
- expect( collection ).to.have.length( 1 );
- collection.add( {} );
- expect( collection ).to.have.length( 2 );
- } );
- } );
- describe( 'addMany()', () => {
- it( 'should be chainable', () => {
- expect( collection.addMany( [ {} ] ) ).to.equal( collection );
- } );
- it( 'should change the length', () => {
- expect( collection ).to.have.length( 0 );
- collection.addMany( [ {}, {} ] );
- expect( collection ).to.have.length( 2 );
- collection.addMany( [ {} ] );
- expect( collection ).to.have.length( 3 );
- } );
- it( 'should enable get( index )', () => {
- const item1 = {};
- const item2 = {};
- collection.addMany( [ item1, item2 ] );
- expect( collection.get( 0 ) ).to.equal( item1 );
- expect( collection.get( 1 ) ).to.equal( item2 );
- } );
- it( 'should enable get( id )', () => {
- const item1 = getItem( 'foo' );
- const item2 = getItem( 'bar' );
- collection.addMany( [ item1, item2 ] );
- expect( collection.get( 'foo' ) ).to.equal( item1 );
- expect( collection.get( 'bar' ) ).to.equal( item2 );
- } );
- it( 'should enable get( id ) - custom id property', () => {
- const collection = new Collection( { idProperty: 'name' } );
- const item1 = getItem( 'foo', 'name' );
- const item2 = getItem( 'bar', 'name' );
- collection.add( item1 );
- collection.add( item2 );
- expect( collection.get( 'foo' ) ).to.equal( item1 );
- expect( collection.get( 'bar' ) ).to.equal( item2 );
- } );
- it( 'should generate an id when not defined', () => {
- const item = {};
- collection.addMany( [ item ] );
- expect( item.id ).to.be.a( 'string' );
- expect( collection.get( item.id ) ).to.equal( item );
- } );
- it( 'should generate an id when not defined - custom id property', () => {
- const collection = new Collection( { idProperty: 'name' } );
- const item = {};
- collection.addMany( [ item ] );
- expect( item.name ).to.be.a( 'string' );
- expect( collection.get( item.name ) ).to.equal( item );
- } );
- it( 'should not change an existing id of an item', () => {
- const item = getItem( 'foo' );
- collection.addMany( [ item ] );
- expect( item.id ).to.equal( 'foo' );
- } );
- it( 'should throw when item with this id already exists - single call', () => {
- const item1 = getItem( 'foo' );
- expectToThrowCKEditorError( () => {
- collection.addMany( [ item1, item1 ] );
- }, /^collection-add-item-already-exists/ );
- } );
- it( 'should throw when item with this id already exists - multiple calls', () => {
- const item1 = getItem( 'foo' );
- const item2 = getItem( 'foo' );
- collection.addMany( [ item1 ] );
- expectToThrowCKEditorError( () => {
- collection.addMany( [ item2 ] );
- }, /^collection-add-item-already-exists/ );
- } );
- it( 'should throw when item\'s id is not a string', () => {
- const item = { id: 1 };
- expectToThrowCKEditorError( () => {
- collection.addMany( [ item ] );
- }, /^collection-add-invalid-id/ );
- } );
- it(
- 'should generate an id when not defined, which is globally unique ' +
- 'so it is possible to move items between collections and avoid id collisions',
- () => {
- const collectionA = new Collection();
- const collectionB = new Collection();
- const itemA = {};
- const itemB = {};
- collectionA.addMany( [ itemA ] );
- collectionB.addMany( [ itemB ] );
- collectionB.addMany( [ collectionA.remove( itemA ) ] );
- expect( collectionA.length ).to.equal( 0 );
- expect( collectionB.length ).to.equal( 2 );
- expect( collectionB.get( 0 ) ).to.equal( itemB );
- expect( collectionB.get( 1 ) ).to.equal( itemA );
- expect( itemA.id ).to.not.equal( itemB.id );
- }
- );
- it(
- 'should generate an id when not defined, which is globally unique ' +
- 'so it is possible to move items between collections and avoid id collisions ' +
- '– custom id property',
- () => {
- const collectionA = new Collection( { idProperty: 'foo' } );
- const collectionB = new Collection( { idProperty: 'foo' } );
- const itemA = {};
- const itemB = {};
- collectionA.addMany( [ itemA ] );
- collectionB.addMany( [ itemB ] );
- collectionB.addMany( [ collectionA.remove( itemA ) ] );
- expect( collectionA.length ).to.equal( 0 );
- expect( collectionB.length ).to.equal( 2 );
- expect( collectionB.get( 0 ) ).to.equal( itemB );
- expect( collectionB.get( 1 ) ).to.equal( itemA );
- expect( itemA.foo ).to.not.equal( itemB.foo );
- }
- );
- it( 'should allow an item which is already in some other collection', () => {
- const collectionA = new Collection();
- const collectionB = new Collection();
- const item = {};
- collectionA.addMany( [ item ] );
- collectionB.addMany( [ item ] );
- expect( collectionA.length ).to.equal( 1 );
- expect( collectionB.length ).to.equal( 1 );
- expect( collectionA.get( item.id ) ).to.equal( collectionB.get( 0 ) );
- } );
- it( 'should fire the "add" event', () => {
- const spy = sinon.spy();
- const item = {};
- collection.on( 'add', spy );
- collection.addMany( [ item ] );
- sinon.assert.calledWithExactly( spy, sinon.match.has( 'source', collection ), item, 0 );
- } );
- it( 'should fire the "add" event for each item', () => {
- const spy = sinon.spy();
- const items = [ {}, {} ];
- collection.on( 'add', spy );
- collection.addMany( items );
- sinon.assert.calledWithExactly( spy, sinon.match.has( 'source', collection ), items[ 0 ], 0 );
- sinon.assert.calledWithExactly( spy, sinon.match.has( 'source', collection ), items[ 1 ], 1 );
- expect( spy.callCount ).to.equal( 2 );
- } );
- it( 'should fire the "add" event with the index argument', () => {
- const spy = sinon.spy();
- collection.addMany( [ {} ] );
- collection.addMany( [ {} ] );
- collection.on( 'add', spy );
- const item = {};
- collection.addMany( [ item ], 1 );
- sinon.assert.calledWithExactly( spy, sinon.match.has( 'source', collection ), item, 1 );
- } );
- it( 'should fire the "change" event', () => {
- const spy = sinon.spy();
- const items = [ {}, {} ];
- collection.on( 'change', spy );
- collection.addMany( items );
- 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 "change" event with the index argument', () => {
- const spy = sinon.spy();
- const firstBatch = [ {}, {} ];
- const secondBatch = [ {}, {} ];
- collection.addMany( firstBatch );
- collection.on( 'change', spy );
- collection.addMany( 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', () => {
- const item1 = getItem( 'foo' );
- const item2 = getItem( 'bar' );
- const item3 = getItem( 'baz' );
- const item4 = getItem( 'abc' );
- collection.addMany( [ item1 ] );
- collection.addMany( [ item2 ], 0 );
- collection.addMany( [ item3 ], 1 );
- collection.addMany( [ 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', () => {
- const item1 = getItem( 'foo' );
- const item2 = getItem( 'bar' );
- const item3 = getItem( 'baz' );
- collection.addMany( [ item1 ] );
- expectToThrowCKEditorError( () => {
- collection.addMany( [ item2 ], -1 );
- }, /^collection-add-item-invalid-index/ );
- expectToThrowCKEditorError( () => {
- collection.addMany( [ item2 ], 2 );
- }, /^collection-add-item-invalid-index/ );
- collection.addMany( [ item2 ], 1 );
- collection.addMany( [ item3 ], 0 );
- expect( collection.length ).to.equal( 3 );
- } );
- } );
- describe( 'get()', () => {
- it( 'should return an item', () => {
- const item = getItem( 'foo' );
- collection.add( item );
- expect( collection.get( 'foo' ) ).to.equal( item );
- } );
- it( 'should return null if id does not exist', () => {
- collection.add( getItem( 'foo' ) );
- expect( collection.get( 'bar' ) ).to.be.null;
- } );
- it( 'should throw if neither string or number given', () => {
- expectToThrowCKEditorError( () => {
- collection.get( true );
- }, /^collection-get-invalid-arg/ );
- } );
- } );
- describe( 'has()', () => {
- it( 'should return true if collection contains item with given id', () => {
- collection.add( getItem( 'foo' ) );
- expect( collection.has( 'foo' ) ).to.equal( true );
- } );
- it( 'should return false if collection does not contain item with given id', () => {
- collection.add( getItem( 'foo' ) );
- expect( collection.has( 'bar' ) ).to.equal( false );
- } );
- it( 'should return true if collection contains item', () => {
- const item = getItem( 'foo' );
- collection.add( item );
- expect( collection.has( item ) ).to.equal( true );
- } );
- it( 'should return false if collection does not contains item', () => {
- collection.add( getItem( 'foo' ) );
- expect( collection.has( getItem( 'bar' ) ) ).to.equal( false );
- } );
- } );
- describe( 'getIndex()', () => {
- it( 'should return index of given item', () => {
- const item1 = { foo: 'bar' };
- const item2 = { bar: 'biz' };
- const item3 = { foo: 'biz' };
- collection.add( item1 );
- collection.add( item2 );
- collection.add( item3 );
- expect( collection.getIndex( item1 ) ).to.equal( 0 );
- expect( collection.getIndex( item2 ) ).to.equal( 1 );
- expect( collection.getIndex( item3 ) ).to.equal( 2 );
- } );
- it( 'should return index of item with given id', () => {
- collection.add( { id: 'id1' } );
- collection.add( { id: 'id2' } );
- collection.add( { id: 'id3' } );
- expect( collection.getIndex( 'id1' ) ).to.equal( 0 );
- expect( collection.getIndex( 'id2' ) ).to.equal( 1 );
- expect( collection.getIndex( 'id3' ) ).to.equal( 2 );
- } );
- it( 'should return index equal to -1 when given item is not defined in the collection', () => {
- const item1 = { foo: 'bar' };
- expect( collection.getIndex( item1 ) ).to.equal( -1 );
- } );
- it( 'should return index equal to -1 when item of given id is not defined in the collection', () => {
- expect( collection.getIndex( 'id1' ) ).to.equal( -1 );
- } );
- } );
- describe( 'remove()', () => {
- it( 'should remove the model by index', () => {
- collection.add( getItem( 'bom' ) );
- collection.add( getItem( 'foo' ) );
- collection.add( getItem( 'bar' ) );
- expect( collection ).to.have.length( 3 );
- const removedItem = collection.remove( 1 );
- 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 index - custom id property', () => {
- const collection = new Collection( { idProperty: 'name' } );
- collection.add( getItem( 'foo', 'name' ) );
- const removedItem = collection.remove( 0 );
- expect( collection ).to.have.length( 0 );
- expect( collection.get( 'foo' ) ).to.be.null;
- expect( removedItem ).to.have.property( 'name', 'foo' );
- } );
- it( 'should remove the model by id', () => {
- collection.add( getItem( 'bom' ) );
- collection.add( getItem( 'foo' ) );
- collection.add( getItem( 'bar' ) );
- expect( collection ).to.have.length( 3 );
- const removedItem = collection.remove( '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 model', () => {
- const item = getItem( 'foo' );
- collection.add( getItem( 'bom' ) );
- collection.add( item );
- collection.add( getItem( 'bar' ) );
- expect( collection ).to.have.length( 3 );
- const removedItem = collection.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 );
- } );
- it( 'should remove the model by model - custom id property', () => {
- const collection = new Collection( { idProperty: 'name' } );
- const item = getItem( 'foo', 'name' );
- collection.add( item );
- const removedItem = collection.remove( item );
- expect( collection ).to.have.length( 0 );
- expect( collection.get( 'foo' ) ).to.be.null;
- expect( removedItem ).to.have.property( 'name', 'foo' );
- } );
- it( 'should fire the "remove" event', () => {
- const item1 = getItem( 'foo' );
- const item2 = getItem( 'bar' );
- const item3 = getItem( 'bom' );
- collection.add( item1 );
- collection.add( item2 );
- collection.add( item3 );
- const spy = sinon.spy();
- collection.on( 'remove', spy );
- collection.remove( 1 ); // by index
- collection.remove( item1 ); // by model
- collection.remove( 'bom' ); // by id
- sinon.assert.calledThrice( spy );
- sinon.assert.calledWithExactly( spy, sinon.match.has( 'source', collection ), item1, 0 );
- sinon.assert.calledWithExactly( spy, sinon.match.has( 'source', collection ), item2, 1 );
- sinon.assert.calledWithExactly( spy, sinon.match.has( 'source', collection ), item3, 0 );
- } );
- it( 'should throw an error on invalid index', () => {
- collection.add( getItem( 'foo' ) );
- expectToThrowCKEditorError( () => {
- collection.remove( 1 );
- }, /^collection-remove-404/ );
- expect( collection ).to.have.length( 1 );
- } );
- it( 'should throw an error on invalid id', () => {
- collection.add( getItem( 'foo' ) );
- expectToThrowCKEditorError( () => {
- collection.remove( 'bar' );
- }, /^collection-remove-404/ );
- expect( collection ).to.have.length( 1 );
- } );
- it( 'should throw an error on invalid model', () => {
- collection.add( getItem( 'foo' ) );
- expectToThrowCKEditorError( () => {
- collection.remove( getItem( 'bar' ) );
- }, /^collection-remove-404/ );
- expect( collection ).to.have.length( 1 );
- } );
- } );
- describe( 'map()', () => {
- it( 'uses native map', () => {
- const spy = testUtils.sinon.stub( Array.prototype, 'map' ).returns( [ 'foo' ] );
- const ctx = {};
- const ret = collection.map( callback, ctx );
- sinon.assert.calledWithExactly( spy, callback, ctx );
- expect( ret ).to.deep.equal( [ 'foo' ], 'ret value was forwarded' );
- function callback() {}
- } );
- } );
- describe( 'find()', () => {
- it( 'uses native find', () => {
- const needl = getItem( 'foo' );
- const spy = testUtils.sinon.stub( Array.prototype, 'find' ).returns( needl );
- const ctx = {};
- const ret = collection.find( callback, ctx );
- sinon.assert.calledWithExactly( spy, callback, ctx );
- expect( ret ).to.equal( needl, 'ret value was forwarded' );
- function callback() {}
- } );
- } );
- describe( 'filter()', () => {
- it( 'uses native filter', () => {
- const needl = getItem( 'foo' );
- // See: https://github.com/sinonjs/sinon/issues/1521
- const spy = testUtils.sinon.stub( collection._items, 'filter' ).returns( [ needl ] );
- const ctx = {};
- const ret = collection.filter( callback, ctx );
- sinon.assert.calledWithExactly( spy, callback, ctx );
- expect( ret ).to.deep.equal( [ needl ], 'ret value was forwarded' );
- function callback() {}
- } );
- } );
- describe( 'clear()', () => {
- it( 'removes all items', () => {
- const items = [ {}, {}, {} ];
- const spy = sinon.spy();
- collection.on( 'remove', spy );
- items.forEach( i => collection.add( i ) );
- collection.clear();
- expect( spy.callCount ).to.equal( 3 );
- expect( collection.length ).to.equal( 0 );
- } );
- it( 'breaks the binding', () => {
- const external = new Collection();
- collection.bindTo( external ).using( i => i );
- external.add( { foo: 'bar' } );
- expect( collection ).to.have.length( 1 );
- collection.clear();
- external.add( { foo: 'baz' } );
- expect( collection ).to.have.length( 0 );
- external.remove( 0 );
- expect( collection ).to.have.length( 0 );
- expect( collection._bindToCollection ).to.be.null;
- } );
- } );
- describe( 'bindTo()', () => {
- class FactoryClass {
- constructor( data ) {
- this.data = data;
- }
- }
- function assertItems( collection, expectedItems ) {
- expect( collection.map( i => i.v ) ).to.deep.equal( expectedItems );
- }
- it( 'throws when binding more than once', () => {
- collection.bindTo( {} );
- expectToThrowCKEditorError( () => {
- collection.bindTo( {} );
- }, /^collection-bind-to-rebind/ );
- } );
- it( 'provides "using()" and "as()" interfaces', () => {
- const returned = collection.bindTo( {} );
- expect( returned ).to.have.keys( 'using', 'as' );
- expect( returned.using ).to.be.a( 'function' );
- expect( returned.as ).to.be.a( 'function' );
- } );
- it( 'stores reference to bound collection', () => {
- const collectionB = new Collection();
- expect( collection._bindToCollection ).to.be.undefined;
- expect( collectionB._bindToCollection ).to.be.undefined;
- collection.bindTo( collectionB ).as( FactoryClass );
- expect( collection._bindToCollection ).to.equal( collectionB );
- expect( collectionB._bindToCollection ).to.be.undefined;
- } );
- describe( 'as()', () => {
- let items;
- beforeEach( () => {
- 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.bindTo( items ).as( FactoryClass );
- expect( collection ).to.have.length( 2 );
- expect( collection.get( 0 ) ).to.be.instanceOf( FactoryClass );
- expect( collection.get( 1 ) ).to.be.instanceOf( FactoryClass );
- expect( collection.get( 1 ).data ).to.equal( items.get( 1 ) );
- } );
- it( 'creates a binding (new content)', () => {
- collection.bindTo( items ).as( FactoryClass );
- expect( collection ).to.have.length( 0 );
- items.add( { id: '1' } );
- items.add( { id: '2' } );
- expect( collection ).to.have.length( 2 );
- expect( collection.get( 0 ) ).to.be.instanceOf( FactoryClass );
- expect( collection.get( 1 ) ).to.be.instanceOf( FactoryClass );
- expect( collection.get( 1 ).data ).to.equal( items.get( 1 ) );
- } );
- it( 'creates a binding (item removal)', () => {
- collection.bindTo( items ).as( FactoryClass );
- expect( collection ).to.have.length( 0 );
- items.add( { id: '1' } );
- items.add( { id: '2' } );
- expect( collection ).to.have.length( 2 );
- expect( collection.get( 0 ) ).to.be.instanceOf( FactoryClass );
- expect( collection.get( 1 ) ).to.be.instanceOf( FactoryClass );
- expect( collection.get( 1 ).data ).to.equal( items.get( 1 ) );
- items.remove( 1 );
- expect( collection.get( 0 ).data ).to.equal( items.get( 0 ) );
- items.remove( 0 );
- expect( collection ).to.have.length( 0 );
- } );
- } );
- describe( 'using()', () => {
- let items;
- beforeEach( () => {
- items = new Collection();
- } );
- it( 'does not chain', () => {
- const returned = collection.bindTo( new Collection() ).using( () => {} );
- expect( returned ).to.be.undefined;
- } );
- describe( 'callback', () => {
- it( 'creates a binding (arrow function)', () => {
- collection.bindTo( items ).using( item => {
- return new FactoryClass( item );
- } );
- expect( collection ).to.have.length( 0 );
- items.add( { id: '1' } );
- items.add( { id: '2' } );
- expect( collection ).to.have.length( 2 );
- expect( collection.get( 0 ) ).to.be.instanceOf( FactoryClass );
- expect( collection.get( 1 ) ).to.be.instanceOf( FactoryClass );
- expect( collection.get( 1 ).data ).to.equal( items.get( 1 ) );
- } );
- // https://github.com/ckeditor/ckeditor5-ui/issues/113
- it( 'creates a binding (normal function)', () => {
- collection.bindTo( items ).using( function( item ) {
- return new FactoryClass( item );
- } );
- items.add( { id: '1' } );
- expect( collection ).to.have.length( 1 );
- const view = collection.get( 0 );
- // Wrong args will be passed to the callback if it's treated as the view constructor.
- expect( view ).to.be.instanceOf( FactoryClass );
- expect( view.data ).to.equal( items.get( 0 ) );
- } );
- it( 'creates a 1:1 binding', () => {
- collection.bindTo( items ).using( item => item );
- expect( collection ).to.have.length( 0 );
- const item1 = { id: '100' };
- const item2 = { id: '200' };
- items.add( item1 );
- items.add( item2 );
- expect( collection ).to.have.length( 2 );
- 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' );
- } );
- 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;
- }
- return item;
- } );
- // Still 0 because initial item was skipped.
- expect( collection ).to.have.length( 0 );
- 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( [ 2, 4 ] );
- items.add( { value: 5, skip: false }, 2 );
- expect( Array.from( collection, item => item.value ) ).to.deep.equal( [ 2, 5, 4 ] );
- } );
- } );
- describe( 'property name', () => {
- it( 'creates a binding', () => {
- collection.bindTo( items ).using( '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' );
- } );
- it( 'creates a binding (item removal)', () => {
- collection.bindTo( items ).using( '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' );
- items.remove( 1 );
- expect( collection ).to.have.length( 1 );
- expect( collection.get( 0 ).value ).to.equal( 'foo' );
- items.remove( 0 );
- expect( collection ).to.have.length( 0 );
- } );
- 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: 2, skip: false } } );
- items.add( { prop: null } );
- items.add( { prop: { value: 4, skip: false } } );
- expect( Array.from( collection, item => item.value ) ).to.deep.equal( [ 2, 4 ] );
- items.add( { prop: { value: 5 } }, 2 );
- expect( Array.from( collection, item => item.value ) ).to.deep.equal( [ 2, 5, 4 ] );
- } );
- } );
- } );
- describe( 'two–way data binding', () => {
- it( 'works with custom factories (1)', () => {
- const collectionA = new Collection();
- const collectionB = new Collection();
- const spyA = sinon.spy();
- const spyB = sinon.spy();
- collectionA.on( 'add', spyA );
- collectionB.on( 'add', spyB );
- // A<--->B
- collectionA.bindTo( collectionB ).using( i => ( { v: i.v * 2 } ) );
- collectionB.bindTo( collectionA ).using( i => ( { v: i.v / 2 } ) );
- assertItems( collectionA, [] );
- assertItems( collectionB, [] );
- collectionA.add( { v: 4 } );
- collectionA.add( { v: 6 } );
- assertItems( collectionA, [ 4, 6 ] );
- assertItems( collectionB, [ 2, 3 ] );
- collectionB.add( { v: 4 } );
- assertItems( collectionA, [ 4, 6, 8 ] );
- assertItems( collectionB, [ 2, 3, 4 ] );
- sinon.assert.callCount( spyA, 3 );
- sinon.assert.callCount( spyB, 3 );
- } );
- it( 'works with custom factories (2)', () => {
- const collectionA = new Collection();
- const collectionB = new Collection();
- const spyA = sinon.spy();
- const spyB = sinon.spy();
- collectionA.on( 'add', spyA );
- collectionB.on( 'add', spyB );
- // A<--->B
- collectionA.bindTo( collectionB ).using( 'data' );
- collectionB.bindTo( collectionA ).using( i => new FactoryClass( i ) );
- collectionA.add( { v: 4 } );
- collectionA.add( { v: 6 } );
- expect( [ ...collectionB ].every( i => i instanceof FactoryClass ) ).to.be.true;
- expect( [ ...collectionB ].map( i => i.data ) ).to.deep.equal( [ ...collectionA ] );
- expect( collectionB.map( i => i.data.v ) ).to.deep.equal( [ 4, 6 ] );
- expect( collectionA.map( i => i.v ) ).to.deep.equal( [ 4, 6 ] );
- collectionB.add( new FactoryClass( { v: 8 } ) );
- expect( [ ...collectionB ].every( i => i instanceof FactoryClass ) ).to.be.true;
- expect( [ ...collectionB ].map( i => i.data ) ).to.deep.equal( [ ...collectionA ] );
- expect( collectionB.map( i => i.data.v ) ).to.deep.equal( [ 4, 6, 8 ] );
- expect( collectionA.map( i => i.v ) ).to.deep.equal( [ 4, 6, 8 ] );
- } );
- it( 'works with custom factories (custom index)', () => {
- const collectionA = new Collection();
- const collectionB = new Collection();
- const spyA = sinon.spy();
- const spyB = sinon.spy();
- collectionA.on( 'add', spyA );
- collectionB.on( 'add', spyB );
- // A<--->B
- collectionA.bindTo( collectionB ).using( i => ( { v: i.v * 2 } ) );
- collectionB.bindTo( collectionA ).using( i => ( { v: i.v / 2 } ) );
- assertItems( collectionA, [] );
- assertItems( collectionB, [] );
- collectionA.add( { v: 4 } );
- collectionA.add( { v: 6 }, 0 );
- assertItems( collectionA, [ 6, 4 ] );
- assertItems( collectionB, [ 3, 2 ] );
- collectionB.add( { v: 4 }, 1 );
- assertItems( collectionA, [ 6, 8, 4 ] );
- assertItems( collectionB, [ 3, 4, 2 ] );
- sinon.assert.callCount( spyA, 3 );
- sinon.assert.callCount( spyB, 3 );
- } );
- it( 'works with 1:1 binding', () => {
- const collectionA = new Collection();
- const collectionB = new Collection();
- const spyA = sinon.spy();
- const spyB = sinon.spy();
- collectionA.on( 'add', spyA );
- collectionB.on( 'add', spyB );
- // A<--->B
- collectionA.bindTo( collectionB ).using( i => i );
- collectionB.bindTo( collectionA ).using( i => i );
- assertItems( collectionA, [], [] );
- assertItems( collectionB, [], [] );
- collectionA.add( { v: 4 } );
- collectionA.add( { v: 6 } );
- assertItems( collectionA, [ 4, 6 ] );
- assertItems( collectionB, [ 4, 6 ] );
- collectionB.add( { v: 8 } );
- assertItems( collectionA, [ 4, 6, 8 ] );
- assertItems( collectionB, [ 4, 6, 8 ] );
- expect( [ ...collectionA ] ).to.deep.equal( [ ...collectionB ] );
- sinon.assert.callCount( spyA, 3 );
- sinon.assert.callCount( spyB, 3 );
- } );
- it( 'works with double chaining', () => {
- const collectionA = new Collection();
- const collectionB = new Collection();
- const collectionC = new Collection();
- const spyA = sinon.spy();
- const spyB = sinon.spy();
- const spyC = sinon.spy();
- collectionA.on( 'add', spyA );
- collectionB.on( 'add', spyB );
- collectionC.on( 'add', spyC );
- // A<--->B--->C
- collectionA.bindTo( collectionB ).using( i => ( { v: i.v * 2 } ) );
- collectionB.bindTo( collectionA ).using( i => ( { v: i.v / 2 } ) );
- collectionC.bindTo( collectionB ).using( i => ( { v: -i.v } ) );
- assertItems( collectionA, [] );
- assertItems( collectionB, [] );
- assertItems( collectionC, [] );
- collectionA.add( { v: 4 } );
- collectionA.add( { v: 6 } );
- assertItems( collectionA, [ 4, 6 ] );
- assertItems( collectionB, [ 2, 3 ] );
- assertItems( collectionC, [ -2, -3 ] );
- collectionB.add( { v: 4 } );
- assertItems( collectionA, [ 4, 6, 8 ] );
- assertItems( collectionB, [ 2, 3, 4 ] );
- assertItems( collectionC, [ -2, -3, -4 ] );
- collectionC.add( { v: -1000 } );
- assertItems( collectionA, [ 4, 6, 8 ] );
- assertItems( collectionB, [ 2, 3, 4 ] );
- assertItems( collectionC, [ -2, -3, -4, -1000 ] );
- sinon.assert.callCount( spyA, 3 );
- sinon.assert.callCount( spyB, 3 );
- sinon.assert.callCount( spyC, 4 );
- } );
- it( 'removes items correctly', () => {
- const collectionA = new Collection();
- const collectionB = new Collection();
- const spyAddA = sinon.spy();
- const spyAddB = sinon.spy();
- const spyRemoveA = sinon.spy();
- const spyRemoveB = sinon.spy();
- collectionA.on( 'add', spyAddA );
- collectionB.on( 'add', spyAddB );
- collectionA.on( 'remove', spyRemoveA );
- collectionB.on( 'remove', spyRemoveB );
- // A<--->B
- collectionA.bindTo( collectionB ).using( i => ( { v: i.v * 2 } ) );
- collectionB.bindTo( collectionA ).using( i => ( { v: i.v / 2 } ) );
- assertItems( collectionA, [], [] );
- assertItems( collectionB, [], [] );
- collectionA.add( { v: 4 } );
- collectionA.add( { v: 6 } );
- assertItems( collectionA, [ 4, 6 ] );
- assertItems( collectionB, [ 2, 3 ] );
- collectionB.add( { v: 4 } );
- assertItems( collectionA, [ 4, 6, 8 ] );
- assertItems( collectionB, [ 2, 3, 4 ] );
- collectionB.remove( 0 );
- assertItems( collectionA, [ 6, 8 ] );
- assertItems( collectionB, [ 3, 4 ] );
- sinon.assert.callCount( spyAddA, 3 );
- sinon.assert.callCount( spyAddB, 3 );
- sinon.assert.callCount( spyRemoveA, 1 );
- sinon.assert.callCount( spyRemoveB, 1 );
- collectionA.remove( 1 );
- assertItems( collectionA, [ 6 ] );
- assertItems( collectionB, [ 3 ] );
- sinon.assert.callCount( spyAddA, 3 );
- sinon.assert.callCount( spyAddB, 3 );
- sinon.assert.callCount( spyRemoveA, 2 );
- sinon.assert.callCount( spyRemoveB, 2 );
- } );
- describe( 'skipping items', () => {
- let collectionA, collectionB;
- beforeEach( () => {
- collectionA = new Collection();
- collectionB = new Collection();
- // A<--->B
- collectionA.bindTo( collectionB ).using( item => {
- if ( item.skip ) {
- return null;
- }
- return item;
- } );
- collectionB.bindTo( collectionA ).using( item => {
- if ( item.skip ) {
- return null;
- }
- return item;
- } );
- } );
- it( 'should add items at the enf of collections when includes skipped items', () => {
- collectionA.add( { v: 'A' } );
- collectionA.add( { v: 'B', skip: true } );
- collectionA.add( { v: 'C', skip: true } );
- collectionA.add( { v: 'D' } );
- expect( collectionA._skippedIndexesFromExternal ).to.have.members( [] );
- expect( collectionB._skippedIndexesFromExternal ).to.have.members( [ 1, 2 ] );
- assertItems( collectionA, [ 'A', 'B', 'C', 'D' ] );
- assertItems( collectionB, [ 'A', 'D' ] );
- collectionB.add( { v: 'E' } );
- collectionB.add( { v: 'F', skip: true } );
- collectionB.add( { v: 'G' } );
- expect( collectionA._skippedIndexesFromExternal ).to.have.members( [ 3 ] );
- expect( collectionB._skippedIndexesFromExternal ).to.have.members( [ 1, 2 ] );
- assertItems( collectionA, [ 'A', 'B', 'C', 'D', 'E', 'G' ] );
- assertItems( collectionB, [ 'A', 'D', 'E', 'F', 'G' ] );
- } );
- it( 'should add items between skipped items', () => {
- collectionA.add( { v: 'A' } );
- collectionA.add( { v: 'B', skip: true } );
- collectionA.add( { v: 'C', skip: true } );
- collectionA.add( { v: 'D' } );
- collectionB.add( { v: 'E' } );
- collectionB.add( { v: 'F', skip: true } );
- collectionB.add( { v: 'G', skip: true } );
- collectionB.add( { v: 'H' } );
- expect( collectionA._skippedIndexesFromExternal ).to.have.members( [ 3, 4 ] );
- expect( collectionB._skippedIndexesFromExternal ).to.have.members( [ 1, 2 ] );
- assertItems( collectionA, [ 'A', 'B', 'C', 'D', 'E', 'H' ] );
- assertItems( collectionB, [ 'A', 'D', 'E', 'F', 'G', 'H' ] );
- collectionA.add( { v: 'I' }, 2 );
- expect( collectionA._skippedIndexesFromExternal ).to.have.members( [ 4, 5 ] );
- expect( collectionB._skippedIndexesFromExternal ).to.have.members( [ 1, 2 ] );
- assertItems( collectionA, [ 'A', 'B', 'I', 'C', 'D', 'E', 'H' ] );
- assertItems( collectionB, [ 'A', 'I', 'D', 'E', 'F', 'G', 'H' ] );
- collectionB.add( { v: 'J' }, 5 );
- expect( collectionA._skippedIndexesFromExternal ).to.have.members( [ 4, 5 ] );
- expect( collectionB._skippedIndexesFromExternal ).to.have.members( [ 1, 2 ] );
- assertItems( collectionA, [ 'A', 'B', 'I', 'C', 'D', 'E', 'J', 'H' ] );
- assertItems( collectionB, [ 'A', 'I', 'D', 'E', 'F', 'J', 'G', 'H' ] );
- } );
- it( 'should properly remove skipped items and update skipped indexes', () => {
- collectionA.add( { v: 'A' } );
- collectionA.add( { v: 'B', skip: true } );
- collectionA.add( { v: 'C', skip: true } );
- collectionA.add( { v: 'D' } );
- collectionB.add( { v: 'E' } );
- collectionB.add( { v: 'F', skip: true } );
- collectionB.add( { v: 'G', skip: true } );
- collectionB.add( { v: 'H' } );
- expect( collectionA._skippedIndexesFromExternal ).to.have.members( [ 3, 4 ] );
- expect( collectionB._skippedIndexesFromExternal ).to.have.members( [ 1, 2 ] );
- assertItems( collectionA, [ 'A', 'B', 'C', 'D', 'E', 'H' ] );
- assertItems( collectionB, [ 'A', 'D', 'E', 'F', 'G', 'H' ] );
- collectionA.remove( 2 );
- expect( collectionA._skippedIndexesFromExternal ).to.have.members( [ 3, 4 ] );
- expect( collectionB._skippedIndexesFromExternal ).to.have.members( [ 1 ] );
- assertItems( collectionA, [ 'A', 'B', 'D', 'E', 'H' ] );
- assertItems( collectionB, [ 'A', 'D', 'E', 'F', 'G', 'H' ] );
- collectionB.remove( 3 );
- expect( collectionA._skippedIndexesFromExternal ).to.have.members( [ 3 ] );
- expect( collectionB._skippedIndexesFromExternal ).to.have.members( [ 1 ] );
- assertItems( collectionA, [ 'A', 'B', 'D', 'E', 'H' ] );
- assertItems( collectionB, [ 'A', 'D', 'E', 'G', 'H' ] );
- } );
- } );
- } );
- } );
- describe( 'iterator', () => {
- it( 'covers the whole collection', () => {
- const item1 = getItem( 'foo' );
- const item2 = getItem( 'bar' );
- const item3 = getItem( 'bom' );
- const items = [];
- collection.add( item1 );
- collection.add( item2 );
- collection.add( item3 );
- for ( const item of collection ) {
- items.push( item.id );
- }
- expect( items ).to.deep.equal( [ 'foo', 'bar', 'bom' ] );
- } );
- } );
- } );
|