collection.js 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014
  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. const item1 = { id: 'foo', name: 'xx' };
  23. const item2 = { id: 'foo', name: 'yy' };
  24. const 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. const item1 = {};
  45. const 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. const item1 = getItem( 'foo' );
  54. const 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. const collection = new Collection( { idProperty: 'name' } );
  62. const item1 = getItem( 'foo', 'name' );
  63. const 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. const 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. const collection = new Collection( { idProperty: 'name' } );
  77. const 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. const 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. const item1 = getItem( 'foo' );
  89. const 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. const 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. const spy = sinon.spy();
  150. const 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. const item1 = getItem( 'foo' );
  157. const item2 = getItem( 'bar' );
  158. const item3 = getItem( 'baz' );
  159. const 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. const item1 = getItem( 'foo' );
  171. const item2 = getItem( 'bar' );
  172. const 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. const 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. const 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. const 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. const collection = new Collection( { idProperty: 'name' } );
  252. collection.add( getItem( 'foo', 'name' ) );
  253. const 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. const 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. const 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. const 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. const collection = new Collection( null, 'name' );
  283. const item = getItem( 'foo', 'name' );
  284. collection.add( item );
  285. const 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. const item1 = getItem( 'foo' );
  292. const item2 = getItem( 'bar' );
  293. const item3 = getItem( 'bom' );
  294. collection.add( item1 );
  295. collection.add( item2 );
  296. collection.add( item3 );
  297. const 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. const spy = testUtils.sinon.stub( Array.prototype, 'map' ).returns( [ 'foo' ] );
  332. const ctx = {};
  333. const ret = collection.map( callback, ctx );
  334. sinon.assert.calledWithExactly( spy, callback, ctx );
  335. expect( ret ).to.deep.equal( [ 'foo' ], 'ret value was forwarded' );
  336. function callback() {}
  337. } );
  338. } );
  339. describe( 'find()', () => {
  340. it( 'uses native find', () => {
  341. const needl = getItem( 'foo' );
  342. const spy = testUtils.sinon.stub( Array.prototype, 'find' ).returns( needl );
  343. const ctx = {};
  344. const ret = collection.find( callback, ctx );
  345. sinon.assert.calledWithExactly( spy, callback, ctx );
  346. expect( ret ).to.equal( needl, 'ret value was forwarded' );
  347. function callback() {}
  348. } );
  349. } );
  350. describe( 'filter()', () => {
  351. it( 'uses native filter', () => {
  352. const needl = getItem( 'foo' );
  353. // See: https://github.com/sinonjs/sinon/issues/1521
  354. const spy = testUtils.sinon.stub( collection._items, 'filter' ).returns( [ needl ] );
  355. const ctx = {};
  356. const ret = collection.filter( callback, ctx );
  357. sinon.assert.calledWithExactly( spy, callback, ctx );
  358. expect( ret ).to.deep.equal( [ needl ], 'ret value was forwarded' );
  359. function callback() {}
  360. } );
  361. } );
  362. describe( 'clear()', () => {
  363. it( 'removes all items', () => {
  364. const items = [ {}, {}, {} ];
  365. const spy = sinon.spy();
  366. collection.on( 'remove', spy );
  367. items.forEach( i => collection.add( i ) );
  368. collection.clear();
  369. expect( spy.callCount ).to.equal( 3 );
  370. expect( collection.length ).to.equal( 0 );
  371. } );
  372. it( 'breaks the binding', () => {
  373. const external = new Collection();
  374. collection.bindTo( external ).using( i => i );
  375. external.add( { foo: 'bar' } );
  376. expect( collection ).to.have.length( 1 );
  377. collection.clear();
  378. external.add( { foo: 'baz' } );
  379. expect( collection ).to.have.length( 0 );
  380. external.remove( 0 );
  381. expect( collection ).to.have.length( 0 );
  382. expect( collection._bindToCollection ).to.be.null;
  383. } );
  384. } );
  385. describe( 'bindTo()', () => {
  386. class FactoryClass {
  387. constructor( data ) {
  388. this.data = data;
  389. }
  390. }
  391. function assertItems( collection, expectedItems ) {
  392. expect( collection.map( i => i.v ) ).to.deep.equal( expectedItems );
  393. }
  394. it( 'throws when binding more than once', () => {
  395. collection.bindTo( {} );
  396. expect( () => {
  397. collection.bindTo( {} );
  398. } ).to.throw( CKEditorError, /^collection-bind-to-rebind/ );
  399. } );
  400. it( 'provides "using()" and "as()" interfaces', () => {
  401. const returned = collection.bindTo( {} );
  402. expect( returned ).to.have.keys( 'using', 'as' );
  403. expect( returned.using ).to.be.a( 'function' );
  404. expect( returned.as ).to.be.a( 'function' );
  405. } );
  406. it( 'stores reference to bound collection', () => {
  407. const collectionB = new Collection();
  408. expect( collection._bindToCollection ).to.be.undefined;
  409. expect( collectionB._bindToCollection ).to.be.undefined;
  410. collection.bindTo( collectionB ).as( FactoryClass );
  411. expect( collection._bindToCollection ).to.equal( collectionB );
  412. expect( collectionB._bindToCollection ).to.be.undefined;
  413. } );
  414. describe( 'as()', () => {
  415. let items;
  416. beforeEach( () => {
  417. items = new Collection();
  418. } );
  419. it( 'does not chain', () => {
  420. const returned = collection.bindTo( new Collection() ).as( FactoryClass );
  421. expect( returned ).to.be.undefined;
  422. } );
  423. it( 'creates a binding (initial content)', () => {
  424. items.add( { id: '1' } );
  425. items.add( { id: '2' } );
  426. collection.bindTo( items ).as( FactoryClass );
  427. expect( collection ).to.have.length( 2 );
  428. expect( collection.get( 0 ) ).to.be.instanceOf( FactoryClass );
  429. expect( collection.get( 1 ) ).to.be.instanceOf( FactoryClass );
  430. expect( collection.get( 1 ).data ).to.equal( items.get( 1 ) );
  431. } );
  432. it( 'creates a binding (new content)', () => {
  433. collection.bindTo( items ).as( FactoryClass );
  434. expect( collection ).to.have.length( 0 );
  435. items.add( { id: '1' } );
  436. items.add( { id: '2' } );
  437. expect( collection ).to.have.length( 2 );
  438. expect( collection.get( 0 ) ).to.be.instanceOf( FactoryClass );
  439. expect( collection.get( 1 ) ).to.be.instanceOf( FactoryClass );
  440. expect( collection.get( 1 ).data ).to.equal( items.get( 1 ) );
  441. } );
  442. it( 'creates a binding (item removal)', () => {
  443. collection.bindTo( items ).as( FactoryClass );
  444. expect( collection ).to.have.length( 0 );
  445. items.add( { id: '1' } );
  446. items.add( { id: '2' } );
  447. expect( collection ).to.have.length( 2 );
  448. expect( collection.get( 0 ) ).to.be.instanceOf( FactoryClass );
  449. expect( collection.get( 1 ) ).to.be.instanceOf( FactoryClass );
  450. expect( collection.get( 1 ).data ).to.equal( items.get( 1 ) );
  451. items.remove( 1 );
  452. expect( collection.get( 0 ).data ).to.equal( items.get( 0 ) );
  453. items.remove( 0 );
  454. expect( collection ).to.have.length( 0 );
  455. } );
  456. } );
  457. describe( 'using()', () => {
  458. let items;
  459. beforeEach( () => {
  460. items = new Collection();
  461. } );
  462. it( 'does not chain', () => {
  463. const returned = collection.bindTo( new Collection() ).using( () => {} );
  464. expect( returned ).to.be.undefined;
  465. } );
  466. describe( 'callback', () => {
  467. it( 'creates a binding (arrow function)', () => {
  468. collection.bindTo( items ).using( item => {
  469. return new FactoryClass( item );
  470. } );
  471. expect( collection ).to.have.length( 0 );
  472. items.add( { id: '1' } );
  473. items.add( { id: '2' } );
  474. expect( collection ).to.have.length( 2 );
  475. expect( collection.get( 0 ) ).to.be.instanceOf( FactoryClass );
  476. expect( collection.get( 1 ) ).to.be.instanceOf( FactoryClass );
  477. expect( collection.get( 1 ).data ).to.equal( items.get( 1 ) );
  478. } );
  479. // https://github.com/ckeditor/ckeditor5-ui/issues/113
  480. it( 'creates a binding (normal function)', () => {
  481. collection.bindTo( items ).using( function( item ) {
  482. return new FactoryClass( item );
  483. } );
  484. items.add( { id: '1' } );
  485. expect( collection ).to.have.length( 1 );
  486. const view = collection.get( 0 );
  487. // Wrong args will be passed to the callback if it's treated as the view constructor.
  488. expect( view ).to.be.instanceOf( FactoryClass );
  489. expect( view.data ).to.equal( items.get( 0 ) );
  490. } );
  491. it( 'creates a 1:1 binding', () => {
  492. collection.bindTo( items ).using( item => item );
  493. expect( collection ).to.have.length( 0 );
  494. const item1 = { id: '100' };
  495. const item2 = { id: '200' };
  496. items.add( item1 );
  497. items.add( item2 );
  498. expect( collection ).to.have.length( 2 );
  499. expect( collection.get( 0 ) ).to.equal( item1 );
  500. expect( collection.get( 1 ) ).to.equal( item2 );
  501. } );
  502. it( 'creates a conditional binding', () => {
  503. class CustomClass {
  504. constructor( data ) {
  505. this.data = data;
  506. }
  507. }
  508. collection.bindTo( items ).using( item => {
  509. if ( item.id == 'FactoryClass' ) {
  510. return new FactoryClass( item );
  511. } else {
  512. return new CustomClass( item );
  513. }
  514. } );
  515. expect( collection ).to.have.length( 0 );
  516. const item1 = { id: 'FactoryClass' };
  517. const item2 = { id: 'CustomClass' };
  518. items.add( item1 );
  519. items.add( item2 );
  520. expect( collection ).to.have.length( 2 );
  521. expect( collection.get( 0 ) ).to.be.instanceOf( FactoryClass );
  522. expect( collection.get( 1 ) ).to.be.instanceOf( CustomClass );
  523. } );
  524. it( 'creates a binding to a property name', () => {
  525. collection.bindTo( items ).using( item => item.prop );
  526. expect( collection ).to.have.length( 0 );
  527. items.add( { prop: { value: 'foo' } } );
  528. items.add( { prop: { value: 'bar' } } );
  529. expect( collection ).to.have.length( 2 );
  530. expect( collection.get( 0 ).value ).to.equal( 'foo' );
  531. expect( collection.get( 1 ).value ).to.equal( 'bar' );
  532. } );
  533. } );
  534. describe( 'property name', () => {
  535. it( 'creates a binding', () => {
  536. collection.bindTo( items ).using( 'prop' );
  537. expect( collection ).to.have.length( 0 );
  538. items.add( { prop: { value: 'foo' } } );
  539. items.add( { prop: { value: 'bar' } } );
  540. expect( collection ).to.have.length( 2 );
  541. expect( collection.get( 0 ).value ).to.equal( 'foo' );
  542. expect( collection.get( 1 ).value ).to.equal( 'bar' );
  543. } );
  544. it( 'creates a binding (item removal)', () => {
  545. collection.bindTo( items ).using( 'prop' );
  546. expect( collection ).to.have.length( 0 );
  547. items.add( { prop: { value: 'foo' } } );
  548. items.add( { prop: { value: 'bar' } } );
  549. expect( collection ).to.have.length( 2 );
  550. expect( collection.get( 0 ).value ).to.equal( 'foo' );
  551. expect( collection.get( 1 ).value ).to.equal( 'bar' );
  552. items.remove( 1 );
  553. expect( collection ).to.have.length( 1 );
  554. expect( collection.get( 0 ).value ).to.equal( 'foo' );
  555. items.remove( 0 );
  556. expect( collection ).to.have.length( 0 );
  557. } );
  558. } );
  559. } );
  560. describe( 'two–way data binding', () => {
  561. it( 'works with custom factories (1)', () => {
  562. const collectionA = new Collection();
  563. const collectionB = new Collection();
  564. const spyA = sinon.spy();
  565. const spyB = sinon.spy();
  566. collectionA.on( 'add', spyA );
  567. collectionB.on( 'add', spyB );
  568. // A<--->B
  569. collectionA.bindTo( collectionB ).using( i => ( { v: i.v * 2 } ) );
  570. collectionB.bindTo( collectionA ).using( i => ( { v: i.v / 2 } ) );
  571. assertItems( collectionA, [] );
  572. assertItems( collectionB, [] );
  573. collectionA.add( { v: 4 } );
  574. collectionA.add( { v: 6 } );
  575. assertItems( collectionA, [ 4, 6 ] );
  576. assertItems( collectionB, [ 2, 3 ] );
  577. collectionB.add( { v: 4 } );
  578. assertItems( collectionA, [ 4, 6, 8 ] );
  579. assertItems( collectionB, [ 2, 3, 4 ] );
  580. sinon.assert.callCount( spyA, 3 );
  581. sinon.assert.callCount( spyB, 3 );
  582. } );
  583. it( 'works with custom factories (2)', () => {
  584. const collectionA = new Collection();
  585. const collectionB = new Collection();
  586. const spyA = sinon.spy();
  587. const spyB = sinon.spy();
  588. collectionA.on( 'add', spyA );
  589. collectionB.on( 'add', spyB );
  590. // A<--->B
  591. collectionA.bindTo( collectionB ).using( 'data' );
  592. collectionB.bindTo( collectionA ).using( i => new FactoryClass( i ) );
  593. collectionA.add( { v: 4 } );
  594. collectionA.add( { v: 6 } );
  595. expect( [ ...collectionB ].every( i => i instanceof FactoryClass ) ).to.be.true;
  596. expect( [ ...collectionB ].map( i => i.data ) ).to.deep.equal( [ ...collectionA ] );
  597. expect( collectionB.map( i => i.data.v ) ).to.deep.equal( [ 4, 6 ] );
  598. expect( collectionA.map( i => i.v ) ).to.deep.equal( [ 4, 6 ] );
  599. collectionB.add( new FactoryClass( { v: 8 } ) );
  600. expect( [ ...collectionB ].every( i => i instanceof FactoryClass ) ).to.be.true;
  601. expect( [ ...collectionB ].map( i => i.data ) ).to.deep.equal( [ ...collectionA ] );
  602. expect( collectionB.map( i => i.data.v ) ).to.deep.equal( [ 4, 6, 8 ] );
  603. expect( collectionA.map( i => i.v ) ).to.deep.equal( [ 4, 6, 8 ] );
  604. } );
  605. it( 'works with custom factories (custom index)', () => {
  606. const collectionA = new Collection();
  607. const collectionB = new Collection();
  608. const spyA = sinon.spy();
  609. const spyB = sinon.spy();
  610. collectionA.on( 'add', spyA );
  611. collectionB.on( 'add', spyB );
  612. // A<--->B
  613. collectionA.bindTo( collectionB ).using( i => ( { v: i.v * 2 } ) );
  614. collectionB.bindTo( collectionA ).using( i => ( { v: i.v / 2 } ) );
  615. assertItems( collectionA, [] );
  616. assertItems( collectionB, [] );
  617. collectionA.add( { v: 4 } );
  618. collectionA.add( { v: 6 }, 0 );
  619. assertItems( collectionA, [ 6, 4 ] );
  620. assertItems( collectionB, [ 3, 2 ] );
  621. collectionB.add( { v: 4 }, 1 );
  622. assertItems( collectionA, [ 6, 8, 4 ] );
  623. assertItems( collectionB, [ 3, 4, 2 ] );
  624. sinon.assert.callCount( spyA, 3 );
  625. sinon.assert.callCount( spyB, 3 );
  626. } );
  627. it( 'works with 1:1 binding', () => {
  628. const collectionA = new Collection();
  629. const collectionB = new Collection();
  630. const spyA = sinon.spy();
  631. const spyB = sinon.spy();
  632. collectionA.on( 'add', spyA );
  633. collectionB.on( 'add', spyB );
  634. // A<--->B
  635. collectionA.bindTo( collectionB ).using( i => i );
  636. collectionB.bindTo( collectionA ).using( i => i );
  637. assertItems( collectionA, [], [] );
  638. assertItems( collectionB, [], [] );
  639. collectionA.add( { v: 4 } );
  640. collectionA.add( { v: 6 } );
  641. assertItems( collectionA, [ 4, 6 ] );
  642. assertItems( collectionB, [ 4, 6 ] );
  643. collectionB.add( { v: 8 } );
  644. assertItems( collectionA, [ 4, 6, 8 ] );
  645. assertItems( collectionB, [ 4, 6, 8 ] );
  646. expect( [ ...collectionA ] ).to.deep.equal( [ ...collectionB ] );
  647. sinon.assert.callCount( spyA, 3 );
  648. sinon.assert.callCount( spyB, 3 );
  649. } );
  650. it( 'works with double chaining', () => {
  651. const collectionA = new Collection();
  652. const collectionB = new Collection();
  653. const collectionC = new Collection();
  654. const spyA = sinon.spy();
  655. const spyB = sinon.spy();
  656. const spyC = sinon.spy();
  657. collectionA.on( 'add', spyA );
  658. collectionB.on( 'add', spyB );
  659. collectionC.on( 'add', spyC );
  660. // A<--->B--->C
  661. collectionA.bindTo( collectionB ).using( i => ( { v: i.v * 2 } ) );
  662. collectionB.bindTo( collectionA ).using( i => ( { v: i.v / 2 } ) );
  663. collectionC.bindTo( collectionB ).using( i => ( { v: -i.v } ) );
  664. assertItems( collectionA, [] );
  665. assertItems( collectionB, [] );
  666. assertItems( collectionC, [] );
  667. collectionA.add( { v: 4 } );
  668. collectionA.add( { v: 6 } );
  669. assertItems( collectionA, [ 4, 6 ] );
  670. assertItems( collectionB, [ 2, 3 ] );
  671. assertItems( collectionC, [ -2, -3 ] );
  672. collectionB.add( { v: 4 } );
  673. assertItems( collectionA, [ 4, 6, 8 ] );
  674. assertItems( collectionB, [ 2, 3, 4 ] );
  675. assertItems( collectionC, [ -2, -3, -4 ] );
  676. collectionC.add( { v: -1000 } );
  677. assertItems( collectionA, [ 4, 6, 8 ] );
  678. assertItems( collectionB, [ 2, 3, 4 ] );
  679. assertItems( collectionC, [ -2, -3, -4, -1000 ] );
  680. sinon.assert.callCount( spyA, 3 );
  681. sinon.assert.callCount( spyB, 3 );
  682. sinon.assert.callCount( spyC, 4 );
  683. } );
  684. it( 'removes items correctly', () => {
  685. const collectionA = new Collection();
  686. const collectionB = new Collection();
  687. const spyAddA = sinon.spy();
  688. const spyAddB = sinon.spy();
  689. const spyRemoveA = sinon.spy();
  690. const spyRemoveB = sinon.spy();
  691. collectionA.on( 'add', spyAddA );
  692. collectionB.on( 'add', spyAddB );
  693. collectionA.on( 'remove', spyRemoveA );
  694. collectionB.on( 'remove', spyRemoveB );
  695. // A<--->B
  696. collectionA.bindTo( collectionB ).using( i => ( { v: i.v * 2 } ) );
  697. collectionB.bindTo( collectionA ).using( i => ( { v: i.v / 2 } ) );
  698. assertItems( collectionA, [], [] );
  699. assertItems( collectionB, [], [] );
  700. collectionA.add( { v: 4 } );
  701. collectionA.add( { v: 6 } );
  702. assertItems( collectionA, [ 4, 6 ] );
  703. assertItems( collectionB, [ 2, 3 ] );
  704. collectionB.add( { v: 4 } );
  705. assertItems( collectionA, [ 4, 6, 8 ] );
  706. assertItems( collectionB, [ 2, 3, 4 ] );
  707. collectionB.remove( 0 );
  708. assertItems( collectionA, [ 6, 8 ] );
  709. assertItems( collectionB, [ 3, 4 ] );
  710. sinon.assert.callCount( spyAddA, 3 );
  711. sinon.assert.callCount( spyAddB, 3 );
  712. sinon.assert.callCount( spyRemoveA, 1 );
  713. sinon.assert.callCount( spyRemoveB, 1 );
  714. collectionA.remove( 1 );
  715. assertItems( collectionA, [ 6 ] );
  716. assertItems( collectionB, [ 3 ] );
  717. sinon.assert.callCount( spyAddA, 3 );
  718. sinon.assert.callCount( spyAddB, 3 );
  719. sinon.assert.callCount( spyRemoveA, 2 );
  720. sinon.assert.callCount( spyRemoveB, 2 );
  721. } );
  722. } );
  723. } );
  724. describe( 'iterator', () => {
  725. it( 'covers the whole collection', () => {
  726. const item1 = getItem( 'foo' );
  727. const item2 = getItem( 'bar' );
  728. const item3 = getItem( 'bom' );
  729. const items = [];
  730. collection.add( item1 );
  731. collection.add( item2 );
  732. collection.add( item3 );
  733. for ( const item of collection ) {
  734. items.push( item.id );
  735. }
  736. expect( items ).to.deep.equal( [ 'foo', 'bar', 'bom' ] );
  737. } );
  738. } );
  739. } );