collection.js 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985
  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 assertItems( collection, expectedItems, expectedMaps ) {
  385. expect( collection.map( i => i.v ) ).to.deep.equal( expectedItems );
  386. assertMaps( collection, expectedMaps );
  387. }
  388. function assertMaps( collection, expected ) {
  389. const ext2Int = collection._bindToExternalToInternalMap;
  390. const int2Ext = collection._bindToInternalToExternalMap;
  391. expect( [ ...ext2Int ] ).to.have.length( expected.length );
  392. expect( [ ...int2Ext ] ).to.have.length( expected.length );
  393. expect( [ ...ext2Int.entries() ]
  394. .map( ( [ key, val ] ) => [ key.v, val.v ] ) )
  395. .to.deep.equal( expected );
  396. expect( [ ...int2Ext.entries() ]
  397. .map( ( [ key, val ] ) => [ val.v, key.v ] ) )
  398. .to.deep.equal( expected );
  399. }
  400. it( 'throws when binding more than once', () => {
  401. collection.bindTo( {} );
  402. expect( () => {
  403. collection.bindTo( {} );
  404. } ).to.throw( CKEditorError, /^collection-bind-to-rebind/ );
  405. } );
  406. it( 'provides "using()" and "as()" interfaces', () => {
  407. const returned = collection.bindTo( {} );
  408. expect( returned ).to.have.keys( 'using', 'as' );
  409. expect( returned.using ).to.be.a( 'function' );
  410. expect( returned.as ).to.be.a( 'function' );
  411. } );
  412. it( 'stores reference to bound collection', () => {
  413. const collectionB = new Collection();
  414. expect( collection._bindToCollection ).to.be.undefined;
  415. expect( collectionB._bindToCollection ).to.be.undefined;
  416. collection.bindTo( collectionB ).as( FactoryClass );
  417. expect( collection._bindToCollection ).to.equal( collectionB );
  418. expect( collectionB._bindToCollection ).to.be.undefined;
  419. } );
  420. describe( 'as()', () => {
  421. let items;
  422. beforeEach( () => {
  423. items = new Collection();
  424. } );
  425. it( 'does not chain', () => {
  426. const returned = collection.bindTo( new Collection() ).as( FactoryClass );
  427. expect( returned ).to.be.undefined;
  428. } );
  429. it( 'creates a binding (initial content)', () => {
  430. items.add( { id: '1' } );
  431. items.add( { id: '2' } );
  432. collection.bindTo( items ).as( FactoryClass );
  433. expect( collection ).to.have.length( 2 );
  434. expect( collection.get( 0 ) ).to.be.instanceOf( FactoryClass );
  435. expect( collection.get( 1 ) ).to.be.instanceOf( FactoryClass );
  436. expect( collection.get( 1 ).data ).to.equal( items.get( 1 ) );
  437. } );
  438. it( 'creates a binding (new content)', () => {
  439. collection.bindTo( items ).as( FactoryClass );
  440. expect( collection ).to.have.length( 0 );
  441. items.add( { id: '1' } );
  442. items.add( { id: '2' } );
  443. expect( collection ).to.have.length( 2 );
  444. expect( collection.get( 0 ) ).to.be.instanceOf( FactoryClass );
  445. expect( collection.get( 1 ) ).to.be.instanceOf( FactoryClass );
  446. expect( collection.get( 1 ).data ).to.equal( items.get( 1 ) );
  447. } );
  448. it( 'creates a binding (item removal)', () => {
  449. collection.bindTo( items ).as( FactoryClass );
  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. items.remove( 1 );
  458. expect( collection.get( 0 ).data ).to.equal( items.get( 0 ) );
  459. items.remove( 0 );
  460. expect( collection ).to.have.length( 0 );
  461. } );
  462. } );
  463. describe( 'using()', () => {
  464. let items;
  465. beforeEach( () => {
  466. items = new Collection();
  467. } );
  468. it( 'does not chain', () => {
  469. const returned = collection.bindTo( new Collection() ).using( () => {} );
  470. expect( returned ).to.be.undefined;
  471. } );
  472. describe( 'callback', () => {
  473. it( 'creates a binding (arrow function)', () => {
  474. collection.bindTo( items ).using( ( item ) => {
  475. return new FactoryClass( item );
  476. } );
  477. expect( collection ).to.have.length( 0 );
  478. items.add( { id: '1' } );
  479. items.add( { id: '2' } );
  480. expect( collection ).to.have.length( 2 );
  481. expect( collection.get( 0 ) ).to.be.instanceOf( FactoryClass );
  482. expect( collection.get( 1 ) ).to.be.instanceOf( FactoryClass );
  483. expect( collection.get( 1 ).data ).to.equal( items.get( 1 ) );
  484. } );
  485. // https://github.com/ckeditor/ckeditor5-ui/issues/113
  486. it( 'creates a binding (normal function)', () => {
  487. collection.bindTo( items ).using( function( item ) {
  488. return new FactoryClass( item );
  489. } );
  490. items.add( { id: '1' } );
  491. expect( collection ).to.have.length( 1 );
  492. const view = collection.get( 0 );
  493. // Wrong args will be passed to the callback if it's treated as the view constructor.
  494. expect( view ).to.be.instanceOf( FactoryClass );
  495. expect( view.data ).to.equal( items.get( 0 ) );
  496. } );
  497. it( 'creates a 1:1 binding', () => {
  498. collection.bindTo( items ).using( item => item );
  499. expect( collection ).to.have.length( 0 );
  500. const item1 = { id: '100' };
  501. const item2 = { id: '200' };
  502. items.add( item1 );
  503. items.add( item2 );
  504. expect( collection ).to.have.length( 2 );
  505. expect( collection.get( 0 ) ).to.equal( item1 );
  506. expect( collection.get( 1 ) ).to.equal( item2 );
  507. } );
  508. it( 'creates a conditional binding', () => {
  509. class CustomClass {
  510. constructor( data ) {
  511. this.data = data;
  512. }
  513. }
  514. collection.bindTo( items ).using( item => {
  515. if ( item.id == 'FactoryClass' ) {
  516. return new FactoryClass( item );
  517. } else {
  518. return new CustomClass( item );
  519. }
  520. } );
  521. expect( collection ).to.have.length( 0 );
  522. const item1 = { id: 'FactoryClass' };
  523. const item2 = { id: 'CustomClass' };
  524. items.add( item1 );
  525. items.add( item2 );
  526. expect( collection ).to.have.length( 2 );
  527. expect( collection.get( 0 ) ).to.be.instanceOf( FactoryClass );
  528. expect( collection.get( 1 ) ).to.be.instanceOf( CustomClass );
  529. } );
  530. it( 'creates a binding to a property name', () => {
  531. collection.bindTo( items ).using( item => item.prop );
  532. expect( collection ).to.have.length( 0 );
  533. items.add( { prop: { value: 'foo' } } );
  534. items.add( { prop: { value: 'bar' } } );
  535. expect( collection ).to.have.length( 2 );
  536. expect( collection.get( 0 ).value ).to.equal( 'foo' );
  537. expect( collection.get( 1 ).value ).to.equal( 'bar' );
  538. } );
  539. } );
  540. describe( 'property name', () => {
  541. it( 'creates a binding', () => {
  542. collection.bindTo( items ).using( 'prop' );
  543. expect( collection ).to.have.length( 0 );
  544. items.add( { prop: { value: 'foo' } } );
  545. items.add( { prop: { value: 'bar' } } );
  546. expect( collection ).to.have.length( 2 );
  547. expect( collection.get( 0 ).value ).to.equal( 'foo' );
  548. expect( collection.get( 1 ).value ).to.equal( 'bar' );
  549. } );
  550. it( 'creates a binding (item removal)', () => {
  551. collection.bindTo( items ).using( 'prop' );
  552. expect( collection ).to.have.length( 0 );
  553. items.add( { prop: { value: 'foo' } } );
  554. items.add( { prop: { value: 'bar' } } );
  555. expect( collection ).to.have.length( 2 );
  556. expect( collection.get( 0 ).value ).to.equal( 'foo' );
  557. expect( collection.get( 1 ).value ).to.equal( 'bar' );
  558. items.remove( 1 );
  559. expect( collection ).to.have.length( 1 );
  560. expect( collection.get( 0 ).value ).to.equal( 'foo' );
  561. items.remove( 0 );
  562. expect( collection ).to.have.length( 0 );
  563. } );
  564. } );
  565. } );
  566. describe( 'two–way data binding', () => {
  567. it( 'works with custom factories (1)', () => {
  568. const collectionA = new Collection();
  569. const collectionB = new Collection();
  570. const spyA = sinon.spy();
  571. const spyB = sinon.spy();
  572. collectionA.on( 'add', spyA );
  573. collectionB.on( 'add', spyB );
  574. // A<--->B
  575. collectionA.bindTo( collectionB ).using( i => ( { v: i.v * 2 } ) );
  576. collectionB.bindTo( collectionA ).using( i => ( { v: i.v / 2 } ) );
  577. assertItems( collectionA, [], [] );
  578. assertItems( collectionB, [], [] );
  579. collectionA.add( { v: 4 } );
  580. collectionA.add( { v: 6 } );
  581. assertItems( collectionA, [ 4, 6 ], [ [ 2, 4 ], [ 3, 6 ] ] );
  582. assertItems( collectionB, [ 2, 3 ], [ [ 4, 2 ], [ 6, 3 ] ] );
  583. collectionB.add( { v: 4 } );
  584. assertItems( collectionA, [ 4, 6, 8 ], [ [ 2, 4 ], [ 3, 6 ], [ 4, 8 ] ] );
  585. assertItems( collectionB, [ 2, 3, 4 ], [ [ 4, 2 ], [ 6, 3 ], [ 8, 4 ] ] );
  586. sinon.assert.callCount( spyA, 3 );
  587. sinon.assert.callCount( spyB, 3 );
  588. } );
  589. it( 'works with custom factories (2)', () => {
  590. const collectionA = new Collection();
  591. const collectionB = new Collection();
  592. const spyA = sinon.spy();
  593. const spyB = sinon.spy();
  594. collectionA.on( 'add', spyA );
  595. collectionB.on( 'add', spyB );
  596. // A<--->B
  597. collectionA.bindTo( collectionB ).using( i => i.data );
  598. collectionB.bindTo( collectionA ).using( i => new FactoryClass( i ) );
  599. collectionA.add( { v: 4 } );
  600. collectionA.add( { v: 6 } );
  601. expect( [ ...collectionB ].every( i => i instanceof FactoryClass ) ).to.be.true;
  602. expect( [ ...collectionB ].map( i => i.data ) ).to.deep.equal( [ ...collectionA ] );
  603. expect( collectionB.map( i => i.data.v ) ).to.deep.equal( [ 4, 6 ] );
  604. expect( collectionA.map( i => i.v ) ).to.deep.equal( [ 4, 6 ] );
  605. collectionB.add( new FactoryClass( { v: 8 } ) );
  606. expect( [ ...collectionB ].every( i => i instanceof FactoryClass ) ).to.be.true;
  607. expect( [ ...collectionB ].map( i => i.data ) ).to.deep.equal( [ ...collectionA ] );
  608. expect( collectionB.map( i => i.data.v ) ).to.deep.equal( [ 4, 6, 8 ] );
  609. expect( collectionA.map( i => i.v ) ).to.deep.equal( [ 4, 6, 8 ] );
  610. } );
  611. it( 'works with 1:1 binding', () => {
  612. const collectionA = new Collection();
  613. const collectionB = new Collection();
  614. const spyA = sinon.spy();
  615. const spyB = sinon.spy();
  616. collectionA.on( 'add', spyA );
  617. collectionB.on( 'add', spyB );
  618. // A<--->B
  619. collectionA.bindTo( collectionB ).using( i => i );
  620. collectionB.bindTo( collectionA ).using( i => i );
  621. assertItems( collectionA, [], [] );
  622. assertItems( collectionB, [], [] );
  623. collectionA.add( { v: 4 } );
  624. collectionA.add( { v: 6 } );
  625. assertItems( collectionA, [ 4, 6 ], [ [ 4, 4 ], [ 6, 6 ] ] );
  626. assertItems( collectionB, [ 4, 6 ], [ [ 4, 4 ], [ 6, 6 ] ] );
  627. collectionB.add( { v: 8 } );
  628. assertItems( collectionA, [ 4, 6, 8 ], [ [ 4, 4 ], [ 6, 6 ], [ 8, 8 ] ] );
  629. assertItems( collectionB, [ 4, 6, 8 ], [ [ 4, 4 ], [ 6, 6 ], [ 8, 8 ] ] );
  630. expect( collectionA ).to.deep.equal( collectionB );
  631. sinon.assert.callCount( spyA, 3 );
  632. sinon.assert.callCount( spyB, 3 );
  633. } );
  634. it( 'works with double chaining', () => {
  635. const collectionA = new Collection();
  636. const collectionB = new Collection();
  637. const collectionC = new Collection();
  638. const spyA = sinon.spy();
  639. const spyB = sinon.spy();
  640. const spyC = sinon.spy();
  641. collectionA.on( 'add', spyA );
  642. collectionB.on( 'add', spyB );
  643. collectionC.on( 'add', spyC );
  644. // A<--->B--->C
  645. collectionA.bindTo( collectionB ).using( i => ( { v: i.v * 2 } ) );
  646. collectionB.bindTo( collectionA ).using( i => ( { v: i.v / 2 } ) );
  647. collectionC.bindTo( collectionB ).using( i => ( { v: -i.v } ) );
  648. assertItems( collectionA, [], [] );
  649. assertItems( collectionB, [], [] );
  650. assertItems( collectionC, [], [] );
  651. collectionA.add( { v: 4 } );
  652. collectionA.add( { v: 6 } );
  653. assertItems( collectionA, [ 4, 6 ], [ [ 2, 4 ], [ 3, 6 ] ] );
  654. assertItems( collectionB, [ 2, 3 ], [ [ 4, 2 ], [ 6, 3 ] ] );
  655. assertItems( collectionC, [ -2, -3 ], [ [ 2, -2 ], [ 3, -3 ] ] );
  656. collectionB.add( { v: 4 } );
  657. assertItems( collectionA, [ 4, 6, 8 ], [ [ 2, 4 ], [ 3, 6 ], [ 4, 8 ] ] );
  658. assertItems( collectionB, [ 2, 3, 4 ], [ [ 4, 2 ], [ 6, 3 ], [ 8, 4 ] ] );
  659. assertItems( collectionC, [ -2, -3, -4 ], [ [ 2, -2 ], [ 3, -3 ], [ 4, -4 ] ] );
  660. collectionC.add( { v: -1000 } );
  661. assertItems( collectionA, [ 4, 6, 8 ], [ [ 2, 4 ], [ 3, 6 ], [ 4, 8 ] ] );
  662. assertItems( collectionB, [ 2, 3, 4 ], [ [ 4, 2 ], [ 6, 3 ], [ 8, 4 ] ] );
  663. assertItems( collectionC, [ -2, -3, -4, -1000 ], [ [ 2, -2 ], [ 3, -3 ], [ 4, -4 ] ] );
  664. sinon.assert.callCount( spyA, 3 );
  665. sinon.assert.callCount( spyB, 3 );
  666. sinon.assert.callCount( spyC, 4 );
  667. } );
  668. it( 'removes items correctly', () => {
  669. const collectionA = new Collection();
  670. const collectionB = new Collection();
  671. const spyAddA = sinon.spy();
  672. const spyAddB = sinon.spy();
  673. const spyRemoveA = sinon.spy();
  674. const spyRemoveB = sinon.spy();
  675. collectionA.on( 'add', spyAddA );
  676. collectionB.on( 'add', spyAddB );
  677. collectionA.on( 'remove', spyRemoveA );
  678. collectionB.on( 'remove', spyRemoveB );
  679. // A<--->B
  680. collectionA.bindTo( collectionB ).using( i => ( { v: i.v * 2 } ) );
  681. collectionB.bindTo( collectionA ).using( i => ( { v: i.v / 2 } ) );
  682. assertItems( collectionA, [], [] );
  683. assertItems( collectionB, [], [] );
  684. collectionA.add( { v: 4 } );
  685. collectionA.add( { v: 6 } );
  686. assertItems( collectionA, [ 4, 6 ], [ [ 2, 4 ], [ 3, 6 ] ] );
  687. assertItems( collectionB, [ 2, 3 ], [ [ 4, 2 ], [ 6, 3 ] ] );
  688. collectionB.add( { v: 4 } );
  689. assertItems( collectionA, [ 4, 6, 8 ], [ [ 2, 4 ], [ 3, 6 ], [ 4, 8 ] ] );
  690. assertItems( collectionB, [ 2, 3, 4 ], [ [ 4, 2 ], [ 6, 3 ], [ 8, 4 ] ] );
  691. collectionB.remove( 0 );
  692. assertItems( collectionA, [ 6, 8 ], [ [ 3, 6 ], [ 4, 8 ] ] );
  693. assertItems( collectionB, [ 3, 4 ], [ [ 6, 3 ], [ 8, 4 ] ] );
  694. sinon.assert.callCount( spyAddA, 3 );
  695. sinon.assert.callCount( spyAddB, 3 );
  696. sinon.assert.callCount( spyRemoveA, 1 );
  697. sinon.assert.callCount( spyRemoveB, 1 );
  698. collectionA.remove( 1 );
  699. assertItems( collectionA, [ 6 ], [ [ 3, 6 ] ] );
  700. assertItems( collectionB, [ 3 ], [ [ 6, 3 ] ] );
  701. sinon.assert.callCount( spyAddA, 3 );
  702. sinon.assert.callCount( spyAddB, 3 );
  703. sinon.assert.callCount( spyRemoveA, 2 );
  704. sinon.assert.callCount( spyRemoveB, 2 );
  705. } );
  706. } );
  707. } );
  708. describe( 'iterator', () => {
  709. it( 'covers the whole collection', () => {
  710. let item1 = getItem( 'foo' );
  711. let item2 = getItem( 'bar' );
  712. let item3 = getItem( 'bom' );
  713. let items = [];
  714. collection.add( item1 );
  715. collection.add( item2 );
  716. collection.add( item3 );
  717. for ( let item of collection ) {
  718. items.push( item.id );
  719. }
  720. expect( items ).to.deep.equal( [ 'foo', 'bar', 'bom' ] );
  721. } );
  722. } );
  723. } );