collection.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751
  1. /**
  2. * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
  6. import Collection from '../src/collection';
  7. import CKEditorError from '../src/ckeditorerror';
  8. testUtils.createSinonSandbox();
  9. function getItem( id, idProperty ) {
  10. idProperty = idProperty || 'id';
  11. return {
  12. [ idProperty ]: id
  13. };
  14. }
  15. describe( 'Collection', () => {
  16. let collection;
  17. beforeEach( () => {
  18. collection = new Collection();
  19. } );
  20. describe( 'constructor()', () => {
  21. it( 'allows to change the id property used by the collection', () => {
  22. let item1 = { id: 'foo', name: 'xx' };
  23. let item2 = { id: 'foo', name: 'yy' };
  24. let collection = new Collection( { idProperty: 'name' } );
  25. collection.add( item1 );
  26. collection.add( item2 );
  27. expect( collection ).to.have.length( 2 );
  28. expect( collection.get( 'xx' ) ).to.equal( item1 );
  29. expect( collection.remove( 'yy' ) ).to.equal( item2 );
  30. } );
  31. } );
  32. describe( 'add()', () => {
  33. it( 'should be chainable', () => {
  34. expect( collection.add( {} ) ).to.equal( collection );
  35. } );
  36. it( 'should change the length', () => {
  37. expect( collection ).to.have.length( 0 );
  38. collection.add( {} );
  39. expect( collection ).to.have.length( 1 );
  40. collection.add( {} );
  41. expect( collection ).to.have.length( 2 );
  42. } );
  43. it( 'should enable get( index )', () => {
  44. let item1 = {};
  45. let item2 = {};
  46. collection.add( item1 );
  47. expect( collection.get( 0 ) ).to.equal( item1 );
  48. collection.add( item2 );
  49. expect( collection.get( 0 ) ).to.equal( item1 );
  50. expect( collection.get( 1 ) ).to.equal( item2 );
  51. } );
  52. it( 'should enable get( id )', () => {
  53. let item1 = getItem( 'foo' );
  54. let item2 = getItem( 'bar' );
  55. collection.add( item1 );
  56. collection.add( item2 );
  57. expect( collection.get( 'foo' ) ).to.equal( item1 );
  58. expect( collection.get( 'bar' ) ).to.equal( item2 );
  59. } );
  60. it( 'should enable get( id ) - custom id property', () => {
  61. let collection = new Collection( { idProperty: 'name' } );
  62. let item1 = getItem( 'foo', 'name' );
  63. let item2 = getItem( 'bar', 'name' );
  64. collection.add( item1 );
  65. collection.add( item2 );
  66. expect( collection.get( 'foo' ) ).to.equal( item1 );
  67. expect( collection.get( 'bar' ) ).to.equal( item2 );
  68. } );
  69. it( 'should generate an id when not defined', () => {
  70. let item = {};
  71. collection.add( item );
  72. expect( item.id ).to.be.a( 'string' );
  73. expect( collection.get( item.id ) ).to.equal( item );
  74. } );
  75. it( 'should generate an id when not defined - custom id property', () => {
  76. let collection = new Collection( { idProperty: 'name' } );
  77. let item = {};
  78. collection.add( item );
  79. expect( item.name ).to.be.a( 'string' );
  80. expect( collection.get( item.name ) ).to.equal( item );
  81. } );
  82. it( 'should not change an existing id of an item', () => {
  83. let item = getItem( 'foo' );
  84. collection.add( item );
  85. expect( item.id ).to.equal( 'foo' );
  86. } );
  87. it( 'should throw when item with this id already exists', () => {
  88. let item1 = getItem( 'foo' );
  89. let item2 = getItem( 'foo' );
  90. collection.add( item1 );
  91. expect( () => {
  92. collection.add( item2 );
  93. } ).to.throw( CKEditorError, /^collection-add-item-already-exists/ );
  94. } );
  95. it( 'should throw when item\'s id is not a string', () => {
  96. let item = { id: 1 };
  97. expect( () => {
  98. collection.add( item );
  99. } ).to.throw( CKEditorError, /^collection-add-invalid-id/ );
  100. } );
  101. it(
  102. 'should generate an id when not defined, which is globally unique ' +
  103. 'so it is possible to move items between collections and avoid id collisions',
  104. () => {
  105. const collectionA = new Collection();
  106. const collectionB = new Collection();
  107. const itemA = {};
  108. const itemB = {};
  109. collectionA.add( itemA );
  110. collectionB.add( itemB );
  111. collectionB.add( collectionA.remove( itemA ) );
  112. expect( collectionA.length ).to.equal( 0 );
  113. expect( collectionB.length ).to.equal( 2 );
  114. expect( collectionB.get( 0 ) ).to.equal( itemB );
  115. expect( collectionB.get( 1 ) ).to.equal( itemA );
  116. expect( itemA.id ).to.not.equal( itemB.id );
  117. }
  118. );
  119. it(
  120. 'should generate an id when not defined, which is globally unique ' +
  121. 'so it is possible to move items between collections and avoid id collisions ' +
  122. '– custom id property',
  123. () => {
  124. const collectionA = new Collection( { idProperty: 'foo' } );
  125. const collectionB = new Collection( { idProperty: 'foo' } );
  126. const itemA = {};
  127. const itemB = {};
  128. collectionA.add( itemA );
  129. collectionB.add( itemB );
  130. collectionB.add( collectionA.remove( itemA ) );
  131. expect( collectionA.length ).to.equal( 0 );
  132. expect( collectionB.length ).to.equal( 2 );
  133. expect( collectionB.get( 0 ) ).to.equal( itemB );
  134. expect( collectionB.get( 1 ) ).to.equal( itemA );
  135. expect( itemA.foo ).to.not.equal( itemB.foo );
  136. }
  137. );
  138. it( 'should allow an item which is already in some other collection', () => {
  139. const collectionA = new Collection();
  140. const collectionB = new Collection();
  141. const item = {};
  142. collectionA.add( item );
  143. collectionB.add( item );
  144. expect( collectionA.length ).to.equal( 1 );
  145. expect( collectionB.length ).to.equal( 1 );
  146. expect( collectionA.get( item.id ) ).to.equal( collectionB.get( 0 ) );
  147. } );
  148. it( 'should fire the "add" event', () => {
  149. let spy = sinon.spy();
  150. let item = {};
  151. collection.on( 'add', spy );
  152. collection.add( item );
  153. sinon.assert.calledWithExactly( spy, sinon.match.has( 'source', collection ), item, 0 );
  154. } );
  155. it( 'should support an optional index argument', () => {
  156. let collection = new Collection();
  157. let item1 = getItem( 'foo' );
  158. let item2 = getItem( 'bar' );
  159. let item3 = getItem( 'baz' );
  160. let item4 = getItem( 'abc' );
  161. collection.add( item1 );
  162. collection.add( item2, 0 );
  163. collection.add( item3, 1 );
  164. collection.add( item4, 3 );
  165. expect( collection.get( 0 ) ).to.equal( item2 );
  166. expect( collection.get( 1 ) ).to.equal( item3 );
  167. expect( collection.get( 2 ) ).to.equal( item1 );
  168. expect( collection.get( 3 ) ).to.equal( item4 );
  169. } );
  170. it( 'should throw when index argument is invalid', () => {
  171. let collection = new Collection();
  172. let item1 = getItem( 'foo' );
  173. let item2 = getItem( 'bar' );
  174. let item3 = getItem( 'baz' );
  175. collection.add( item1 );
  176. expect( () => {
  177. collection.add( item2, -1 );
  178. } ).to.throw( /^collection-add-item-invalid-index/ );
  179. expect( () => {
  180. collection.add( item2, 2 );
  181. } ).to.throw( /^collection-add-item-invalid-index/ );
  182. collection.add( item2, 1 );
  183. collection.add( item3, 0 );
  184. expect( collection.length ).to.be.equal( 3 );
  185. } );
  186. it( 'should fire the "add" event with the index argument', () => {
  187. let spy = sinon.spy();
  188. collection.add( {} );
  189. collection.add( {} );
  190. collection.on( 'add', spy );
  191. const item = {};
  192. collection.add( item, 1 );
  193. sinon.assert.calledWithExactly( spy, sinon.match.has( 'source', collection ), item, 1 );
  194. } );
  195. } );
  196. describe( 'get()', () => {
  197. it( 'should return an item', () => {
  198. let item = getItem( 'foo' );
  199. collection.add( item );
  200. expect( collection.get( 'foo' ) ).to.equal( item );
  201. } );
  202. it( 'should return null if id does not exist', () => {
  203. collection.add( getItem( 'foo' ) );
  204. expect( collection.get( 'bar' ) ).to.be.null;
  205. } );
  206. it( 'should throw if neither string or number given', () => {
  207. expect( () => {
  208. collection.get( true );
  209. } ).to.throw( CKEditorError, /^collection-get-invalid-arg/ );
  210. } );
  211. } );
  212. describe( 'getIndex()', () => {
  213. it( 'should return index of given item', () => {
  214. const item1 = { foo: 'bar' };
  215. const item2 = { bar: 'biz' };
  216. const item3 = { foo: 'biz' };
  217. collection.add( item1 );
  218. collection.add( item2 );
  219. collection.add( item3 );
  220. expect( collection.getIndex( item1 ) ).to.equal( 0 );
  221. expect( collection.getIndex( item2 ) ).to.equal( 1 );
  222. expect( collection.getIndex( item3 ) ).to.equal( 2 );
  223. } );
  224. it( 'should return index of item with given id', () => {
  225. collection.add( { id: 'id1' } );
  226. collection.add( { id: 'id2' } );
  227. collection.add( { id: 'id3' } );
  228. expect( collection.getIndex( 'id1' ) ).to.equal( 0 );
  229. expect( collection.getIndex( 'id2' ) ).to.equal( 1 );
  230. expect( collection.getIndex( 'id3' ) ).to.equal( 2 );
  231. } );
  232. it( 'should return index equal to -1 when given item is not defined in the collection', () => {
  233. const item1 = { foo: 'bar' };
  234. expect( collection.getIndex( item1 ) ).to.equal( -1 );
  235. } );
  236. it( 'should return index equal to -1 when item of given id is not defined in the collection', () => {
  237. expect( collection.getIndex( 'id1' ) ).to.equal( -1 );
  238. } );
  239. } );
  240. describe( 'remove()', () => {
  241. it( 'should remove the model by index', () => {
  242. collection.add( getItem( 'bom' ) );
  243. collection.add( getItem( 'foo' ) );
  244. collection.add( getItem( 'bar' ) );
  245. expect( collection ).to.have.length( 3 );
  246. let removedItem = collection.remove( 1 );
  247. expect( collection ).to.have.length( 2 );
  248. expect( collection.get( 'foo' ) ).to.be.null;
  249. expect( collection.get( 1 ) ).to.have.property( 'id', 'bar' );
  250. expect( removedItem ).to.have.property( 'id', 'foo' );
  251. } );
  252. it( 'should remove the model by index - custom id property', () => {
  253. let collection = new Collection( { idProperty: 'name' } );
  254. collection.add( getItem( 'foo', 'name' ) );
  255. let removedItem = collection.remove( 0 );
  256. expect( collection ).to.have.length( 0 );
  257. expect( collection.get( 'foo' ) ).to.be.null;
  258. expect( removedItem ).to.have.property( 'name', 'foo' );
  259. } );
  260. it( 'should remove the model by id', () => {
  261. collection.add( getItem( 'bom' ) );
  262. collection.add( getItem( 'foo' ) );
  263. collection.add( getItem( 'bar' ) );
  264. expect( collection ).to.have.length( 3 );
  265. let removedItem = collection.remove( 'foo' );
  266. expect( collection ).to.have.length( 2 );
  267. expect( collection.get( 'foo' ) ).to.be.null;
  268. expect( collection.get( 1 ) ).to.have.property( 'id', 'bar' );
  269. expect( removedItem ).to.have.property( 'id', 'foo' );
  270. } );
  271. it( 'should remove the model by model', () => {
  272. let item = getItem( 'foo' );
  273. collection.add( getItem( 'bom' ) );
  274. collection.add( item );
  275. collection.add( getItem( 'bar' ) );
  276. expect( collection ).to.have.length( 3 );
  277. let removedItem = collection.remove( item );
  278. expect( collection ).to.have.length( 2 );
  279. expect( collection.get( 'foo' ) ).to.be.null;
  280. expect( collection.get( 1 ) ).to.have.property( 'id', 'bar' );
  281. expect( removedItem ).to.equal( item );
  282. } );
  283. it( 'should remove the model by model - custom id property', () => {
  284. let collection = new Collection( null, 'name' );
  285. let item = getItem( 'foo', 'name' );
  286. collection.add( item );
  287. let removedItem = collection.remove( item );
  288. expect( collection ).to.have.length( 0 );
  289. expect( collection.get( 'foo' ) ).to.be.null;
  290. expect( removedItem ).to.have.property( 'name', 'foo' );
  291. } );
  292. it( 'should fire the "remove" event', () => {
  293. let item1 = getItem( 'foo' );
  294. let item2 = getItem( 'bar' );
  295. let item3 = getItem( 'bom' );
  296. collection.add( item1 );
  297. collection.add( item2 );
  298. collection.add( item3 );
  299. let spy = sinon.spy();
  300. collection.on( 'remove', spy );
  301. collection.remove( 1 ); // by index
  302. collection.remove( item1 ); // by model
  303. collection.remove( 'bom' ); // by id
  304. sinon.assert.calledThrice( spy );
  305. sinon.assert.calledWithExactly( spy, sinon.match.has( 'source', collection ), item1 );
  306. sinon.assert.calledWithExactly( spy, sinon.match.has( 'source', collection ), item2 );
  307. sinon.assert.calledWithExactly( spy, sinon.match.has( 'source', collection ), item3 );
  308. } );
  309. it( 'should throw an error on invalid index', () => {
  310. collection.add( getItem( 'foo' ) );
  311. expect( () => {
  312. collection.remove( 1 );
  313. } ).to.throw( CKEditorError, /^collection-remove-404/ );
  314. expect( collection ).to.have.length( 1 );
  315. } );
  316. it( 'should throw an error on invalid id', () => {
  317. collection.add( getItem( 'foo' ) );
  318. expect( () => {
  319. collection.remove( 'bar' );
  320. } ).to.throw( CKEditorError, /^collection-remove-404/ );
  321. expect( collection ).to.have.length( 1 );
  322. } );
  323. it( 'should throw an error on invalid model', () => {
  324. collection.add( getItem( 'foo' ) );
  325. expect( () => {
  326. collection.remove( getItem( 'bar' ) );
  327. } ).to.throw( CKEditorError, /^collection-remove-404/ );
  328. expect( collection ).to.have.length( 1 );
  329. } );
  330. } );
  331. describe( 'map()', () => {
  332. it( 'uses native map', () => {
  333. let spy = testUtils.sinon.stub( Array.prototype, 'map', () => {
  334. return [ 'foo' ];
  335. } );
  336. let ctx = {};
  337. let ret = collection.map( callback, ctx );
  338. sinon.assert.calledWithExactly( spy, callback, ctx );
  339. expect( ret ).to.deep.equal( [ 'foo' ], 'ret value was forwarded' );
  340. function callback() {}
  341. } );
  342. } );
  343. describe( 'find()', () => {
  344. it( 'uses native find', () => {
  345. let needl = getItem( 'foo' );
  346. let spy = testUtils.sinon.stub( Array.prototype, 'find', () => {
  347. return needl;
  348. } );
  349. let ctx = {};
  350. let ret = collection.find( callback, ctx );
  351. sinon.assert.calledWithExactly( spy, callback, ctx );
  352. expect( ret ).to.equal( needl, 'ret value was forwarded' );
  353. function callback() {}
  354. } );
  355. } );
  356. describe( 'filter()', () => {
  357. it( 'uses native filter', () => {
  358. let needl = getItem( 'foo' );
  359. let spy = testUtils.sinon.stub( Array.prototype, 'filter', () => {
  360. return [ needl ];
  361. } );
  362. let ctx = {};
  363. let ret = collection.filter( callback, ctx );
  364. sinon.assert.calledWithExactly( spy, callback, ctx );
  365. expect( ret ).to.deep.equal( [ needl ], 'ret value was forwarded' );
  366. function callback() {}
  367. } );
  368. } );
  369. describe( 'clear()', () => {
  370. it( 'removes all items', () => {
  371. const items = [ {}, {}, {} ];
  372. const spy = sinon.spy();
  373. collection.on( 'remove', spy );
  374. items.forEach( i => collection.add( i ) );
  375. collection.clear();
  376. expect( spy.callCount ).to.equal( 3 );
  377. expect( collection.length ).to.equal( 0 );
  378. } );
  379. } );
  380. describe( 'bindTo()', () => {
  381. class FactoryClass {
  382. constructor( data ) {
  383. this.data = data;
  384. }
  385. }
  386. it( 'provides "using()" and "as()" interfaces', () => {
  387. const returned = collection.bindTo( {} );
  388. expect( returned ).to.have.keys( 'using', 'as' );
  389. expect( returned.using ).to.be.a( 'function' );
  390. expect( returned.as ).to.be.a( 'function' );
  391. } );
  392. describe( 'as()', () => {
  393. let items;
  394. beforeEach( () => {
  395. items = new Collection();
  396. } );
  397. it( 'does not chain', () => {
  398. const returned = collection.bindTo( new Collection() ).as( FactoryClass );
  399. expect( returned ).to.be.undefined;
  400. } );
  401. it( 'creates a binding (initial content)', () => {
  402. items.add( { id: '1' } );
  403. items.add( { id: '2' } );
  404. collection.bindTo( items ).as( FactoryClass );
  405. expect( collection ).to.have.length( 2 );
  406. expect( collection.get( 0 ) ).to.be.instanceOf( FactoryClass );
  407. expect( collection.get( 1 ) ).to.be.instanceOf( FactoryClass );
  408. expect( collection.get( 1 ).data ).to.equal( items.get( 1 ) );
  409. } );
  410. it( 'creates a binding (new content)', () => {
  411. collection.bindTo( items ).as( FactoryClass );
  412. expect( collection ).to.have.length( 0 );
  413. items.add( { id: '1' } );
  414. items.add( { id: '2' } );
  415. expect( collection ).to.have.length( 2 );
  416. expect( collection.get( 0 ) ).to.be.instanceOf( FactoryClass );
  417. expect( collection.get( 1 ) ).to.be.instanceOf( FactoryClass );
  418. expect( collection.get( 1 ).data ).to.equal( items.get( 1 ) );
  419. } );
  420. it( 'creates a binding (item removal)', () => {
  421. collection.bindTo( items ).as( FactoryClass );
  422. expect( collection ).to.have.length( 0 );
  423. items.add( { id: '1' } );
  424. items.add( { id: '2' } );
  425. expect( collection ).to.have.length( 2 );
  426. expect( collection.get( 0 ) ).to.be.instanceOf( FactoryClass );
  427. expect( collection.get( 1 ) ).to.be.instanceOf( FactoryClass );
  428. expect( collection.get( 1 ).data ).to.equal( items.get( 1 ) );
  429. items.remove( 1 );
  430. expect( collection.get( 0 ).data ).to.equal( items.get( 0 ) );
  431. items.remove( 0 );
  432. expect( collection ).to.have.length( 0 );
  433. } );
  434. } );
  435. describe( 'using()', () => {
  436. let items;
  437. beforeEach( () => {
  438. items = new Collection();
  439. } );
  440. it( 'does not chain', () => {
  441. const returned = collection.bindTo( new Collection() ).using( () => {} );
  442. expect( returned ).to.be.undefined;
  443. } );
  444. describe( 'callback', () => {
  445. it( 'creates a binding (arrow function)', () => {
  446. collection = new Collection();
  447. collection.bindTo( items ).using( ( item ) => {
  448. return new FactoryClass( item );
  449. } );
  450. expect( collection ).to.have.length( 0 );
  451. items.add( { id: '1' } );
  452. items.add( { id: '2' } );
  453. expect( collection ).to.have.length( 2 );
  454. expect( collection.get( 0 ) ).to.be.instanceOf( FactoryClass );
  455. expect( collection.get( 1 ) ).to.be.instanceOf( FactoryClass );
  456. expect( collection.get( 1 ).data ).to.equal( items.get( 1 ) );
  457. } );
  458. // https://github.com/ckeditor/ckeditor5-ui/issues/113
  459. it( 'creates a binding (normal function)', () => {
  460. collection.bindTo( items ).using( function( item ) {
  461. return new FactoryClass( item );
  462. } );
  463. items.add( { id: '1' } );
  464. expect( collection ).to.have.length( 1 );
  465. const view = collection.get( 0 );
  466. // Wrong args will be passed to the callback if it's treated as the view constructor.
  467. expect( view ).to.be.instanceOf( FactoryClass );
  468. expect( view.data ).to.equal( items.get( 0 ) );
  469. } );
  470. it( 'creates a 1:1 binding', () => {
  471. collection.bindTo( items ).using( item => item );
  472. expect( collection ).to.have.length( 0 );
  473. const item1 = { id: '100' };
  474. const item2 = { id: '200' };
  475. items.add( item1 );
  476. items.add( item2 );
  477. expect( collection ).to.have.length( 2 );
  478. expect( collection.get( 0 ) ).to.equal( item1 );
  479. expect( collection.get( 1 ) ).to.equal( item2 );
  480. } );
  481. it( 'creates a conditional binding', () => {
  482. class CustomClass {
  483. constructor( data ) {
  484. this.data = data;
  485. }
  486. }
  487. collection.bindTo( items ).using( item => {
  488. if ( item.id == 'FactoryClass' ) {
  489. return new FactoryClass( item );
  490. } else {
  491. return new CustomClass( item );
  492. }
  493. } );
  494. expect( collection ).to.have.length( 0 );
  495. const item1 = { id: 'FactoryClass' };
  496. const item2 = { id: 'CustomClass' };
  497. items.add( item1 );
  498. items.add( item2 );
  499. expect( collection ).to.have.length( 2 );
  500. expect( collection.get( 0 ) ).to.be.instanceOf( FactoryClass );
  501. expect( collection.get( 1 ) ).to.be.instanceOf( CustomClass );
  502. } );
  503. it( 'creates a binding to a property name', () => {
  504. collection.bindTo( items ).using( item => item.prop );
  505. expect( collection ).to.have.length( 0 );
  506. items.add( { prop: { value: 'foo' } } );
  507. items.add( { prop: { value: 'bar' } } );
  508. expect( collection ).to.have.length( 2 );
  509. expect( collection.get( 0 ).value ).to.equal( 'foo' );
  510. expect( collection.get( 1 ).value ).to.equal( 'bar' );
  511. } );
  512. } );
  513. describe( 'property name', () => {
  514. it( 'creates a binding', () => {
  515. collection.bindTo( items ).using( 'prop' );
  516. expect( collection ).to.have.length( 0 );
  517. items.add( { prop: { value: 'foo' } } );
  518. items.add( { prop: { value: 'bar' } } );
  519. expect( collection ).to.have.length( 2 );
  520. expect( collection.get( 0 ).value ).to.equal( 'foo' );
  521. expect( collection.get( 1 ).value ).to.equal( 'bar' );
  522. } );
  523. it( 'creates a binding (item removal)', () => {
  524. collection.bindTo( items ).using( 'prop' );
  525. expect( collection ).to.have.length( 0 );
  526. items.add( { prop: { value: 'foo' } } );
  527. items.add( { prop: { value: 'bar' } } );
  528. expect( collection ).to.have.length( 2 );
  529. expect( collection.get( 0 ).value ).to.equal( 'foo' );
  530. expect( collection.get( 1 ).value ).to.equal( 'bar' );
  531. items.remove( 1 );
  532. expect( collection ).to.have.length( 1 );
  533. expect( collection.get( 0 ).value ).to.equal( 'foo' );
  534. items.remove( 0 );
  535. expect( collection ).to.have.length( 0 );
  536. } );
  537. } );
  538. } );
  539. } );
  540. describe( 'iterator', () => {
  541. it( 'covers the whole collection', () => {
  542. let item1 = getItem( 'foo' );
  543. let item2 = getItem( 'bar' );
  544. let item3 = getItem( 'bom' );
  545. let items = [];
  546. collection.add( item1 );
  547. collection.add( item2 );
  548. collection.add( item3 );
  549. for ( let item of collection ) {
  550. items.push( item.id );
  551. }
  552. expect( items ).to.deep.equal( [ 'foo', 'bar', 'bom' ] );
  553. } );
  554. } );
  555. } );