collection.js 29 KB

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