collection.js 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881
  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 item1 = getItem( 'foo' );
  157. let item2 = getItem( 'bar' );
  158. let item3 = getItem( 'baz' );
  159. let item4 = getItem( 'abc' );
  160. collection.add( item1 );
  161. collection.add( item2, 0 );
  162. collection.add( item3, 1 );
  163. collection.add( item4, 3 );
  164. expect( collection.get( 0 ) ).to.equal( item2 );
  165. expect( collection.get( 1 ) ).to.equal( item3 );
  166. expect( collection.get( 2 ) ).to.equal( item1 );
  167. expect( collection.get( 3 ) ).to.equal( item4 );
  168. } );
  169. it( 'should throw when index argument is invalid', () => {
  170. let item1 = getItem( 'foo' );
  171. let item2 = getItem( 'bar' );
  172. let item3 = getItem( 'baz' );
  173. collection.add( item1 );
  174. expect( () => {
  175. collection.add( item2, -1 );
  176. } ).to.throw( /^collection-add-item-invalid-index/ );
  177. expect( () => {
  178. collection.add( item2, 2 );
  179. } ).to.throw( /^collection-add-item-invalid-index/ );
  180. collection.add( item2, 1 );
  181. collection.add( item3, 0 );
  182. expect( collection.length ).to.be.equal( 3 );
  183. } );
  184. it( 'should fire the "add" event with the index argument', () => {
  185. let spy = sinon.spy();
  186. collection.add( {} );
  187. collection.add( {} );
  188. collection.on( 'add', spy );
  189. const item = {};
  190. collection.add( item, 1 );
  191. sinon.assert.calledWithExactly( spy, sinon.match.has( 'source', collection ), item, 1 );
  192. } );
  193. } );
  194. describe( 'get()', () => {
  195. it( 'should return an item', () => {
  196. let item = getItem( 'foo' );
  197. collection.add( item );
  198. expect( collection.get( 'foo' ) ).to.equal( item );
  199. } );
  200. it( 'should return null if id does not exist', () => {
  201. collection.add( getItem( 'foo' ) );
  202. expect( collection.get( 'bar' ) ).to.be.null;
  203. } );
  204. it( 'should throw if neither string or number given', () => {
  205. expect( () => {
  206. collection.get( true );
  207. } ).to.throw( CKEditorError, /^collection-get-invalid-arg/ );
  208. } );
  209. } );
  210. describe( 'getIndex()', () => {
  211. it( 'should return index of given item', () => {
  212. const item1 = { foo: 'bar' };
  213. const item2 = { bar: 'biz' };
  214. const item3 = { foo: 'biz' };
  215. collection.add( item1 );
  216. collection.add( item2 );
  217. collection.add( item3 );
  218. expect( collection.getIndex( item1 ) ).to.equal( 0 );
  219. expect( collection.getIndex( item2 ) ).to.equal( 1 );
  220. expect( collection.getIndex( item3 ) ).to.equal( 2 );
  221. } );
  222. it( 'should return index of item with given id', () => {
  223. collection.add( { id: 'id1' } );
  224. collection.add( { id: 'id2' } );
  225. collection.add( { id: 'id3' } );
  226. expect( collection.getIndex( 'id1' ) ).to.equal( 0 );
  227. expect( collection.getIndex( 'id2' ) ).to.equal( 1 );
  228. expect( collection.getIndex( 'id3' ) ).to.equal( 2 );
  229. } );
  230. it( 'should return index equal to -1 when given item is not defined in the collection', () => {
  231. const item1 = { foo: 'bar' };
  232. expect( collection.getIndex( item1 ) ).to.equal( -1 );
  233. } );
  234. it( 'should return index equal to -1 when item of given id is not defined in the collection', () => {
  235. expect( collection.getIndex( 'id1' ) ).to.equal( -1 );
  236. } );
  237. } );
  238. describe( 'remove()', () => {
  239. it( 'should remove the model by index', () => {
  240. collection.add( getItem( 'bom' ) );
  241. collection.add( getItem( 'foo' ) );
  242. collection.add( getItem( 'bar' ) );
  243. expect( collection ).to.have.length( 3 );
  244. let removedItem = collection.remove( 1 );
  245. expect( collection ).to.have.length( 2 );
  246. expect( collection.get( 'foo' ) ).to.be.null;
  247. expect( collection.get( 1 ) ).to.have.property( 'id', 'bar' );
  248. expect( removedItem ).to.have.property( 'id', 'foo' );
  249. } );
  250. it( 'should remove the model by index - custom id property', () => {
  251. let collection = new Collection( { idProperty: 'name' } );
  252. collection.add( getItem( 'foo', 'name' ) );
  253. let removedItem = collection.remove( 0 );
  254. expect( collection ).to.have.length( 0 );
  255. expect( collection.get( 'foo' ) ).to.be.null;
  256. expect( removedItem ).to.have.property( 'name', 'foo' );
  257. } );
  258. it( 'should remove the model by id', () => {
  259. collection.add( getItem( 'bom' ) );
  260. collection.add( getItem( 'foo' ) );
  261. collection.add( getItem( 'bar' ) );
  262. expect( collection ).to.have.length( 3 );
  263. let removedItem = collection.remove( 'foo' );
  264. expect( collection ).to.have.length( 2 );
  265. expect( collection.get( 'foo' ) ).to.be.null;
  266. expect( collection.get( 1 ) ).to.have.property( 'id', 'bar' );
  267. expect( removedItem ).to.have.property( 'id', 'foo' );
  268. } );
  269. it( 'should remove the model by model', () => {
  270. let item = getItem( 'foo' );
  271. collection.add( getItem( 'bom' ) );
  272. collection.add( item );
  273. collection.add( getItem( 'bar' ) );
  274. expect( collection ).to.have.length( 3 );
  275. let removedItem = collection.remove( item );
  276. expect( collection ).to.have.length( 2 );
  277. expect( collection.get( 'foo' ) ).to.be.null;
  278. expect( collection.get( 1 ) ).to.have.property( 'id', 'bar' );
  279. expect( removedItem ).to.equal( item );
  280. } );
  281. it( 'should remove the model by model - custom id property', () => {
  282. let collection = new Collection( null, 'name' );
  283. let item = getItem( 'foo', 'name' );
  284. collection.add( item );
  285. let removedItem = collection.remove( item );
  286. expect( collection ).to.have.length( 0 );
  287. expect( collection.get( 'foo' ) ).to.be.null;
  288. expect( removedItem ).to.have.property( 'name', 'foo' );
  289. } );
  290. it( 'should fire the "remove" event', () => {
  291. let item1 = getItem( 'foo' );
  292. let item2 = getItem( 'bar' );
  293. let item3 = getItem( 'bom' );
  294. collection.add( item1 );
  295. collection.add( item2 );
  296. collection.add( item3 );
  297. let spy = sinon.spy();
  298. collection.on( 'remove', spy );
  299. collection.remove( 1 ); // by index
  300. collection.remove( item1 ); // by model
  301. collection.remove( 'bom' ); // by id
  302. sinon.assert.calledThrice( spy );
  303. sinon.assert.calledWithExactly( spy, sinon.match.has( 'source', collection ), item1 );
  304. sinon.assert.calledWithExactly( spy, sinon.match.has( 'source', collection ), item2 );
  305. sinon.assert.calledWithExactly( spy, sinon.match.has( 'source', collection ), item3 );
  306. } );
  307. it( 'should throw an error on invalid index', () => {
  308. collection.add( getItem( 'foo' ) );
  309. expect( () => {
  310. collection.remove( 1 );
  311. } ).to.throw( CKEditorError, /^collection-remove-404/ );
  312. expect( collection ).to.have.length( 1 );
  313. } );
  314. it( 'should throw an error on invalid id', () => {
  315. collection.add( getItem( 'foo' ) );
  316. expect( () => {
  317. collection.remove( 'bar' );
  318. } ).to.throw( CKEditorError, /^collection-remove-404/ );
  319. expect( collection ).to.have.length( 1 );
  320. } );
  321. it( 'should throw an error on invalid model', () => {
  322. collection.add( getItem( 'foo' ) );
  323. expect( () => {
  324. collection.remove( getItem( 'bar' ) );
  325. } ).to.throw( CKEditorError, /^collection-remove-404/ );
  326. expect( collection ).to.have.length( 1 );
  327. } );
  328. } );
  329. describe( 'map()', () => {
  330. it( 'uses native map', () => {
  331. let spy = testUtils.sinon.stub( Array.prototype, 'map', () => {
  332. return [ 'foo' ];
  333. } );
  334. let ctx = {};
  335. let ret = collection.map( callback, ctx );
  336. sinon.assert.calledWithExactly( spy, callback, ctx );
  337. expect( ret ).to.deep.equal( [ 'foo' ], 'ret value was forwarded' );
  338. function callback() {}
  339. } );
  340. } );
  341. describe( 'find()', () => {
  342. it( 'uses native find', () => {
  343. let needl = getItem( 'foo' );
  344. let spy = testUtils.sinon.stub( Array.prototype, 'find', () => {
  345. return needl;
  346. } );
  347. let ctx = {};
  348. let ret = collection.find( callback, ctx );
  349. sinon.assert.calledWithExactly( spy, callback, ctx );
  350. expect( ret ).to.equal( needl, 'ret value was forwarded' );
  351. function callback() {}
  352. } );
  353. } );
  354. describe( 'filter()', () => {
  355. it( 'uses native filter', () => {
  356. let needl = getItem( 'foo' );
  357. let spy = testUtils.sinon.stub( Array.prototype, 'filter', () => {
  358. return [ needl ];
  359. } );
  360. let ctx = {};
  361. let ret = collection.filter( callback, ctx );
  362. sinon.assert.calledWithExactly( spy, callback, ctx );
  363. expect( ret ).to.deep.equal( [ needl ], 'ret value was forwarded' );
  364. function callback() {}
  365. } );
  366. } );
  367. describe( 'clear()', () => {
  368. it( 'removes all items', () => {
  369. const items = [ {}, {}, {} ];
  370. const spy = sinon.spy();
  371. collection.on( 'remove', spy );
  372. items.forEach( i => collection.add( i ) );
  373. collection.clear();
  374. expect( spy.callCount ).to.equal( 3 );
  375. expect( collection.length ).to.equal( 0 );
  376. } );
  377. } );
  378. describe( 'bindTo()', () => {
  379. class FactoryClass {
  380. constructor( data ) {
  381. this.data = data;
  382. }
  383. }
  384. function isFactoryClass( item ) {
  385. return item instanceof FactoryClass;
  386. }
  387. it( 'throws when binding more than once', () => {
  388. collection.bindTo( {} );
  389. expect( () => {
  390. collection.bindTo( {} );
  391. } ).to.throw( CKEditorError, /^collection-bind-to-rebind/ );
  392. } );
  393. it( 'provides "using()" and "as()" interfaces', () => {
  394. const returned = collection.bindTo( {} );
  395. expect( returned ).to.have.keys( 'using', 'as' );
  396. expect( returned.using ).to.be.a( 'function' );
  397. expect( returned.as ).to.be.a( 'function' );
  398. } );
  399. it( 'stores reference to bound collection', () => {
  400. const collectionB = new Collection();
  401. expect( collection._bindToCollection ).to.be.undefined;
  402. expect( collectionB._bindToCollection ).to.be.undefined;
  403. collection.bindTo( collectionB ).as( FactoryClass );
  404. expect( collection._bindToCollection ).to.equal( collectionB );
  405. expect( collectionB._bindToCollection ).to.be.undefined;
  406. } );
  407. describe( 'as()', () => {
  408. let items;
  409. beforeEach( () => {
  410. items = new Collection();
  411. } );
  412. it( 'does not chain', () => {
  413. const returned = collection.bindTo( new Collection() ).as( FactoryClass );
  414. expect( returned ).to.be.undefined;
  415. } );
  416. it( 'creates a binding (initial content)', () => {
  417. items.add( { id: '1' } );
  418. items.add( { id: '2' } );
  419. collection.bindTo( items ).as( FactoryClass );
  420. expect( collection ).to.have.length( 2 );
  421. expect( collection.get( 0 ) ).to.be.instanceOf( FactoryClass );
  422. expect( collection.get( 1 ) ).to.be.instanceOf( FactoryClass );
  423. expect( collection.get( 1 ).data ).to.equal( items.get( 1 ) );
  424. } );
  425. it( 'creates a binding (new content)', () => {
  426. collection.bindTo( items ).as( FactoryClass );
  427. expect( collection ).to.have.length( 0 );
  428. items.add( { id: '1' } );
  429. items.add( { id: '2' } );
  430. expect( collection ).to.have.length( 2 );
  431. expect( collection.get( 0 ) ).to.be.instanceOf( FactoryClass );
  432. expect( collection.get( 1 ) ).to.be.instanceOf( FactoryClass );
  433. expect( collection.get( 1 ).data ).to.equal( items.get( 1 ) );
  434. } );
  435. it( 'creates a binding (item removal)', () => {
  436. collection.bindTo( items ).as( FactoryClass );
  437. expect( collection ).to.have.length( 0 );
  438. items.add( { id: '1' } );
  439. items.add( { id: '2' } );
  440. expect( collection ).to.have.length( 2 );
  441. expect( collection.get( 0 ) ).to.be.instanceOf( FactoryClass );
  442. expect( collection.get( 1 ) ).to.be.instanceOf( FactoryClass );
  443. expect( collection.get( 1 ).data ).to.equal( items.get( 1 ) );
  444. items.remove( 1 );
  445. expect( collection.get( 0 ).data ).to.equal( items.get( 0 ) );
  446. items.remove( 0 );
  447. expect( collection ).to.have.length( 0 );
  448. } );
  449. } );
  450. describe( 'using()', () => {
  451. let items;
  452. beforeEach( () => {
  453. items = new Collection();
  454. } );
  455. it( 'does not chain', () => {
  456. const returned = collection.bindTo( new Collection() ).using( () => {} );
  457. expect( returned ).to.be.undefined;
  458. } );
  459. describe( 'callback', () => {
  460. it( 'creates a binding (arrow function)', () => {
  461. collection.bindTo( items ).using( ( item ) => {
  462. return new FactoryClass( item );
  463. } );
  464. expect( collection ).to.have.length( 0 );
  465. items.add( { id: '1' } );
  466. items.add( { id: '2' } );
  467. expect( collection ).to.have.length( 2 );
  468. expect( collection.get( 0 ) ).to.be.instanceOf( FactoryClass );
  469. expect( collection.get( 1 ) ).to.be.instanceOf( FactoryClass );
  470. expect( collection.get( 1 ).data ).to.equal( items.get( 1 ) );
  471. } );
  472. // https://github.com/ckeditor/ckeditor5-ui/issues/113
  473. it( 'creates a binding (normal function)', () => {
  474. collection.bindTo( items ).using( function( item ) {
  475. return new FactoryClass( item );
  476. } );
  477. items.add( { id: '1' } );
  478. expect( collection ).to.have.length( 1 );
  479. const view = collection.get( 0 );
  480. // Wrong args will be passed to the callback if it's treated as the view constructor.
  481. expect( view ).to.be.instanceOf( FactoryClass );
  482. expect( view.data ).to.equal( items.get( 0 ) );
  483. } );
  484. it( 'creates a 1:1 binding', () => {
  485. collection.bindTo( items ).using( item => item );
  486. expect( collection ).to.have.length( 0 );
  487. const item1 = { id: '100' };
  488. const item2 = { id: '200' };
  489. items.add( item1 );
  490. items.add( item2 );
  491. expect( collection ).to.have.length( 2 );
  492. expect( collection.get( 0 ) ).to.equal( item1 );
  493. expect( collection.get( 1 ) ).to.equal( item2 );
  494. } );
  495. it( 'creates a conditional binding', () => {
  496. class CustomClass {
  497. constructor( data ) {
  498. this.data = data;
  499. }
  500. }
  501. collection.bindTo( items ).using( item => {
  502. if ( item.id == 'FactoryClass' ) {
  503. return new FactoryClass( item );
  504. } else {
  505. return new CustomClass( item );
  506. }
  507. } );
  508. expect( collection ).to.have.length( 0 );
  509. const item1 = { id: 'FactoryClass' };
  510. const item2 = { id: 'CustomClass' };
  511. items.add( item1 );
  512. items.add( item2 );
  513. expect( collection ).to.have.length( 2 );
  514. expect( collection.get( 0 ) ).to.be.instanceOf( FactoryClass );
  515. expect( collection.get( 1 ) ).to.be.instanceOf( CustomClass );
  516. } );
  517. it( 'creates a binding to a property name', () => {
  518. collection.bindTo( items ).using( item => item.prop );
  519. expect( collection ).to.have.length( 0 );
  520. items.add( { prop: { value: 'foo' } } );
  521. items.add( { prop: { value: 'bar' } } );
  522. expect( collection ).to.have.length( 2 );
  523. expect( collection.get( 0 ).value ).to.equal( 'foo' );
  524. expect( collection.get( 1 ).value ).to.equal( 'bar' );
  525. } );
  526. } );
  527. describe( 'property name', () => {
  528. it( 'creates a binding', () => {
  529. collection.bindTo( items ).using( 'prop' );
  530. expect( collection ).to.have.length( 0 );
  531. items.add( { prop: { value: 'foo' } } );
  532. items.add( { prop: { value: 'bar' } } );
  533. expect( collection ).to.have.length( 2 );
  534. expect( collection.get( 0 ).value ).to.equal( 'foo' );
  535. expect( collection.get( 1 ).value ).to.equal( 'bar' );
  536. } );
  537. it( 'creates a binding (item removal)', () => {
  538. collection.bindTo( items ).using( 'prop' );
  539. expect( collection ).to.have.length( 0 );
  540. items.add( { prop: { value: 'foo' } } );
  541. items.add( { prop: { value: 'bar' } } );
  542. expect( collection ).to.have.length( 2 );
  543. expect( collection.get( 0 ).value ).to.equal( 'foo' );
  544. expect( collection.get( 1 ).value ).to.equal( 'bar' );
  545. items.remove( 1 );
  546. expect( collection ).to.have.length( 1 );
  547. expect( collection.get( 0 ).value ).to.equal( 'foo' );
  548. items.remove( 0 );
  549. expect( collection ).to.have.length( 0 );
  550. } );
  551. } );
  552. } );
  553. describe( 'two–way data binding', () => {
  554. it( 'works with custom factories', () => {
  555. const collectionA = new Collection();
  556. const collectionB = new Collection();
  557. const spyA = sinon.spy();
  558. const spyB = sinon.spy();
  559. collectionA.on( 'add', spyA );
  560. collectionB.on( 'add', spyB );
  561. // A<--->B
  562. collectionA.bindTo( collectionB ).using( 'data' );
  563. collectionB.bindTo( collectionA ).as( FactoryClass );
  564. collectionA.add( { value: 'foo' } );
  565. collectionA.add( { value: 'bar' } );
  566. expect( collectionA.map( i => i.value ) ).to.deep.equal( [ 'foo', 'bar' ], 'CollectionA' );
  567. expect( collectionB.map( i => i ).every( isFactoryClass ) ).to.be.true;
  568. expect( collectionB.map( i => i.data.value ) ).to.deep.equal( [ 'foo', 'bar' ], 'CollectionB' );
  569. collectionB.add( new FactoryClass( { value: 'baz' } ) );
  570. expect( collectionA.map( i => i.value ) ).to.deep.equal( [ 'foo', 'bar', 'baz' ], 'CollectionA' );
  571. expect( collectionB.map( i => i ).every( isFactoryClass ) ).to.be.true;
  572. expect( collectionB.map( i => i.data.value ) ).to.deep.equal( [ 'foo', 'bar', 'baz' ], 'CollectionB' );
  573. sinon.assert.callCount( spyA, 3 );
  574. sinon.assert.callCount( spyB, 3 );
  575. } );
  576. it( 'works with 1:1 binding', () => {
  577. const collectionA = new Collection();
  578. const collectionB = new Collection();
  579. const spyA = sinon.spy();
  580. const spyB = sinon.spy();
  581. collectionA.on( 'add', spyA );
  582. collectionB.on( 'add', spyB );
  583. // A<--->B
  584. collectionA.bindTo( collectionB ).using( i => i );
  585. collectionB.bindTo( collectionA ).using( i => i );
  586. collectionA.add( new FactoryClass( 'foo' ) );
  587. collectionA.add( new FactoryClass( 'bar' ) );
  588. expect( collectionA.map( i => i.data ) ).to.deep.equal( [ 'foo', 'bar' ], 'CollectionA' );
  589. expect( collectionB.map( i => i ).every( isFactoryClass ) ).to.be.true;
  590. expect( collectionB.map( i => i.data ) ).to.deep.equal( [ 'foo', 'bar' ], 'CollectionB' );
  591. collectionB.add( new FactoryClass( 'baz' ) );
  592. expect( collectionA.map( i => i.data ) ).to.deep.equal( [ 'foo', 'bar', 'baz' ], 'CollectionA' );
  593. expect( collectionB.map( i => i ).every( isFactoryClass ) ).to.be.true;
  594. expect( collectionB.map( i => i.data ) ).to.deep.equal( [ 'foo', 'bar', 'baz' ], 'CollectionB' );
  595. sinon.assert.callCount( spyA, 3 );
  596. sinon.assert.callCount( spyB, 3 );
  597. } );
  598. it( 'works with double chaining', () => {
  599. const collectionA = new Collection();
  600. const collectionB = new Collection();
  601. const collectionC = new Collection();
  602. const spyA = sinon.spy();
  603. const spyB = sinon.spy();
  604. const spyC = sinon.spy();
  605. collectionA.on( 'add', spyA );
  606. collectionB.on( 'add', spyB );
  607. collectionC.on( 'add', spyC );
  608. // A<--->B--->C
  609. collectionA.bindTo( collectionB ).using( i => i );
  610. collectionB.bindTo( collectionA ).using( i => i );
  611. collectionC.bindTo( collectionB ).using( i => i );
  612. collectionA.add( new FactoryClass( 'foo' ) );
  613. collectionA.add( new FactoryClass( 'bar' ) );
  614. expect( collectionA.map( i => i.data ) ).to.deep.equal( [ 'foo', 'bar' ], 'CollectionA' );
  615. expect( collectionB.map( i => i ).every( isFactoryClass ) ).to.be.true;
  616. expect( collectionB.map( i => i.data ) ).to.deep.equal( [ 'foo', 'bar' ], 'CollectionB' );
  617. expect( collectionC.map( i => i.data ) ).to.deep.equal( [ 'foo', 'bar' ], 'CollectionC' );
  618. collectionB.add( new FactoryClass( 'baz' ) );
  619. expect( collectionA.map( i => i.data ) ).to.deep.equal( [ 'foo', 'bar', 'baz' ], 'CollectionA' );
  620. expect( collectionB.map( i => i ).every( isFactoryClass ) ).to.be.true;
  621. expect( collectionB.map( i => i.data ) ).to.deep.equal( [ 'foo', 'bar', 'baz' ], 'CollectionB' );
  622. expect( collectionC.map( i => i.data ) ).to.deep.equal( [ 'foo', 'bar', 'baz' ], 'CollectionC' );
  623. collectionC.add( new FactoryClass( 'qux' ) );
  624. expect( collectionA.map( i => i.data ) ).to.deep.equal( [ 'foo', 'bar', 'baz' ], 'CollectionA' );
  625. expect( collectionB.map( i => i ).every( isFactoryClass ) ).to.be.true;
  626. expect( collectionB.map( i => i.data ) ).to.deep.equal( [ 'foo', 'bar', 'baz' ], 'CollectionB' );
  627. expect( collectionC.map( i => i.data ) ).to.deep.equal( [ 'foo', 'bar', 'baz', 'qux' ], 'CollectionC' );
  628. sinon.assert.callCount( spyA, 3 );
  629. sinon.assert.callCount( spyB, 3 );
  630. sinon.assert.callCount( spyC, 4 );
  631. } );
  632. } );
  633. } );
  634. describe( 'iterator', () => {
  635. it( 'covers the whole collection', () => {
  636. let item1 = getItem( 'foo' );
  637. let item2 = getItem( 'bar' );
  638. let item3 = getItem( 'bom' );
  639. let items = [];
  640. collection.add( item1 );
  641. collection.add( item2 );
  642. collection.add( item3 );
  643. for ( let item of collection ) {
  644. items.push( item.id );
  645. }
  646. expect( items ).to.deep.equal( [ 'foo', 'bar', 'bom' ] );
  647. } );
  648. } );
  649. } );