collection.js 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240
  1. /**
  2. * @license Copyright (c) 2003-2019, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
  4. */
  5. import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
  6. import Collection from '../src/collection';
  7. import { expectToThrowCKEditorError } from '../tests/_utils/utils';
  8. function getItem( id, idProperty ) {
  9. idProperty = idProperty || 'id';
  10. return {
  11. [ idProperty ]: id
  12. };
  13. }
  14. describe( 'Collection', () => {
  15. let collection;
  16. testUtils.createSinonSandbox();
  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( 'length', () => {
  33. it( 'should return collection length', () => {
  34. expect( collection.length ).to.equal( 0 );
  35. collection.add( { foo: 'bar' } );
  36. expect( collection.length ).to.equal( 1 );
  37. } );
  38. } );
  39. describe( 'first', () => {
  40. it( 'should return the first item from the collection', () => {
  41. const item1 = { foo: 'bar' };
  42. const item2 = { bar: 'biz' };
  43. collection.add( item1 );
  44. collection.add( item2 );
  45. expect( collection.first ).to.equal( item1 );
  46. } );
  47. it( 'should return null when collection is empty', () => {
  48. expect( collection.first ).to.null;
  49. } );
  50. } );
  51. describe( 'last', () => {
  52. it( 'should return the last item from the collection', () => {
  53. const item1 = { foo: 'bar' };
  54. const item2 = { bar: 'biz' };
  55. collection.add( item1 );
  56. collection.add( item2 );
  57. expect( collection.last ).to.equal( item2 );
  58. } );
  59. it( 'should return null when collection is empty', () => {
  60. expect( collection.last ).to.null;
  61. } );
  62. } );
  63. describe( 'add()', () => {
  64. it( 'should be chainable', () => {
  65. expect( collection.add( {} ) ).to.equal( collection );
  66. } );
  67. it( 'should change the length', () => {
  68. expect( collection ).to.have.length( 0 );
  69. collection.add( {} );
  70. expect( collection ).to.have.length( 1 );
  71. collection.add( {} );
  72. expect( collection ).to.have.length( 2 );
  73. } );
  74. it( 'should enable get( index )', () => {
  75. const item1 = {};
  76. const item2 = {};
  77. collection.add( item1 );
  78. expect( collection.get( 0 ) ).to.equal( item1 );
  79. collection.add( item2 );
  80. expect( collection.get( 0 ) ).to.equal( item1 );
  81. expect( collection.get( 1 ) ).to.equal( item2 );
  82. } );
  83. it( 'should enable get( id )', () => {
  84. const item1 = getItem( 'foo' );
  85. const item2 = getItem( 'bar' );
  86. collection.add( item1 );
  87. collection.add( item2 );
  88. expect( collection.get( 'foo' ) ).to.equal( item1 );
  89. expect( collection.get( 'bar' ) ).to.equal( item2 );
  90. } );
  91. it( 'should enable get( id ) - custom id property', () => {
  92. const collection = new Collection( { idProperty: 'name' } );
  93. const item1 = getItem( 'foo', 'name' );
  94. const item2 = getItem( 'bar', 'name' );
  95. collection.add( item1 );
  96. collection.add( item2 );
  97. expect( collection.get( 'foo' ) ).to.equal( item1 );
  98. expect( collection.get( 'bar' ) ).to.equal( item2 );
  99. } );
  100. it( 'should generate an id when not defined', () => {
  101. const item = {};
  102. collection.add( item );
  103. expect( item.id ).to.be.a( 'string' );
  104. expect( collection.get( item.id ) ).to.equal( item );
  105. } );
  106. it( 'should generate an id when not defined - custom id property', () => {
  107. const collection = new Collection( { idProperty: 'name' } );
  108. const item = {};
  109. collection.add( item );
  110. expect( item.name ).to.be.a( 'string' );
  111. expect( collection.get( item.name ) ).to.equal( item );
  112. } );
  113. it( 'should not change an existing id of an item', () => {
  114. const item = getItem( 'foo' );
  115. collection.add( item );
  116. expect( item.id ).to.equal( 'foo' );
  117. } );
  118. it( 'should throw when item with this id already exists', () => {
  119. const item1 = getItem( 'foo' );
  120. const item2 = getItem( 'foo' );
  121. collection.add( item1 );
  122. expectToThrowCKEditorError( () => {
  123. collection.add( item2 );
  124. }, /^collection-add-item-already-exists/ );
  125. } );
  126. it( 'should throw when item\'s id is not a string', () => {
  127. const item = { id: 1 };
  128. expectToThrowCKEditorError( () => {
  129. collection.add( item );
  130. }, /^collection-add-invalid-id/ );
  131. } );
  132. it(
  133. 'should generate an id when not defined, which is globally unique ' +
  134. 'so it is possible to move items between collections and avoid id collisions',
  135. () => {
  136. const collectionA = new Collection();
  137. const collectionB = new Collection();
  138. const itemA = {};
  139. const itemB = {};
  140. collectionA.add( itemA );
  141. collectionB.add( itemB );
  142. collectionB.add( collectionA.remove( itemA ) );
  143. expect( collectionA.length ).to.equal( 0 );
  144. expect( collectionB.length ).to.equal( 2 );
  145. expect( collectionB.get( 0 ) ).to.equal( itemB );
  146. expect( collectionB.get( 1 ) ).to.equal( itemA );
  147. expect( itemA.id ).to.not.equal( itemB.id );
  148. }
  149. );
  150. it(
  151. 'should generate an id when not defined, which is globally unique ' +
  152. 'so it is possible to move items between collections and avoid id collisions ' +
  153. '– custom id property',
  154. () => {
  155. const collectionA = new Collection( { idProperty: 'foo' } );
  156. const collectionB = new Collection( { idProperty: 'foo' } );
  157. const itemA = {};
  158. const itemB = {};
  159. collectionA.add( itemA );
  160. collectionB.add( itemB );
  161. collectionB.add( collectionA.remove( itemA ) );
  162. expect( collectionA.length ).to.equal( 0 );
  163. expect( collectionB.length ).to.equal( 2 );
  164. expect( collectionB.get( 0 ) ).to.equal( itemB );
  165. expect( collectionB.get( 1 ) ).to.equal( itemA );
  166. expect( itemA.foo ).to.not.equal( itemB.foo );
  167. }
  168. );
  169. it( 'should allow an item which is already in some other collection', () => {
  170. const collectionA = new Collection();
  171. const collectionB = new Collection();
  172. const item = {};
  173. collectionA.add( item );
  174. collectionB.add( item );
  175. expect( collectionA.length ).to.equal( 1 );
  176. expect( collectionB.length ).to.equal( 1 );
  177. expect( collectionA.get( item.id ) ).to.equal( collectionB.get( 0 ) );
  178. } );
  179. it( 'should fire the "add" event', () => {
  180. const spy = sinon.spy();
  181. const item = {};
  182. collection.on( 'add', spy );
  183. collection.add( item );
  184. sinon.assert.calledWithExactly( spy, sinon.match.has( 'source', collection ), item, 0 );
  185. } );
  186. it( 'should support an optional index argument', () => {
  187. const item1 = getItem( 'foo' );
  188. const item2 = getItem( 'bar' );
  189. const item3 = getItem( 'baz' );
  190. const item4 = getItem( 'abc' );
  191. collection.add( item1 );
  192. collection.add( item2, 0 );
  193. collection.add( item3, 1 );
  194. collection.add( item4, 3 );
  195. expect( collection.get( 0 ) ).to.equal( item2 );
  196. expect( collection.get( 1 ) ).to.equal( item3 );
  197. expect( collection.get( 2 ) ).to.equal( item1 );
  198. expect( collection.get( 3 ) ).to.equal( item4 );
  199. } );
  200. it( 'should throw when index argument is invalid', () => {
  201. const item1 = getItem( 'foo' );
  202. const item2 = getItem( 'bar' );
  203. const item3 = getItem( 'baz' );
  204. collection.add( item1 );
  205. expectToThrowCKEditorError( () => {
  206. collection.add( item2, -1 );
  207. }, /^collection-add-item-invalid-index/ );
  208. expectToThrowCKEditorError( () => {
  209. collection.add( item2, 2 );
  210. }, /^collection-add-item-invalid-index/ );
  211. collection.add( item2, 1 );
  212. collection.add( item3, 0 );
  213. expect( collection.length ).to.be.equal( 3 );
  214. } );
  215. it( 'should fire the "add" event with the index argument', () => {
  216. const spy = sinon.spy();
  217. collection.add( {} );
  218. collection.add( {} );
  219. collection.on( 'add', spy );
  220. const item = {};
  221. collection.add( item, 1 );
  222. sinon.assert.calledWithExactly( spy, sinon.match.has( 'source', collection ), item, 1 );
  223. } );
  224. } );
  225. describe( 'get()', () => {
  226. it( 'should return an item', () => {
  227. const item = getItem( 'foo' );
  228. collection.add( item );
  229. expect( collection.get( 'foo' ) ).to.equal( item );
  230. } );
  231. it( 'should return null if id does not exist', () => {
  232. collection.add( getItem( 'foo' ) );
  233. expect( collection.get( 'bar' ) ).to.be.null;
  234. } );
  235. it( 'should throw if neither string or number given', () => {
  236. expectToThrowCKEditorError( () => {
  237. collection.get( true );
  238. }, /^collection-get-invalid-arg/ );
  239. } );
  240. } );
  241. describe( 'has()', () => {
  242. it( 'should return true if collection contains item with given id', () => {
  243. collection.add( getItem( 'foo' ) );
  244. expect( collection.has( 'foo' ) ).to.equal( true );
  245. } );
  246. it( 'should return false if collection does not contain item with given id', () => {
  247. collection.add( getItem( 'foo' ) );
  248. expect( collection.has( 'bar' ) ).to.equal( false );
  249. } );
  250. it( 'should return true if collection contains item', () => {
  251. const item = getItem( 'foo' );
  252. collection.add( item );
  253. expect( collection.has( item ) ).to.equal( true );
  254. } );
  255. it( 'should return false if collection does not contains item', () => {
  256. collection.add( getItem( 'foo' ) );
  257. expect( collection.has( getItem( 'bar' ) ) ).to.equal( false );
  258. } );
  259. } );
  260. describe( 'getIndex()', () => {
  261. it( 'should return index of given item', () => {
  262. const item1 = { foo: 'bar' };
  263. const item2 = { bar: 'biz' };
  264. const item3 = { foo: 'biz' };
  265. collection.add( item1 );
  266. collection.add( item2 );
  267. collection.add( item3 );
  268. expect( collection.getIndex( item1 ) ).to.equal( 0 );
  269. expect( collection.getIndex( item2 ) ).to.equal( 1 );
  270. expect( collection.getIndex( item3 ) ).to.equal( 2 );
  271. } );
  272. it( 'should return index of item with given id', () => {
  273. collection.add( { id: 'id1' } );
  274. collection.add( { id: 'id2' } );
  275. collection.add( { id: 'id3' } );
  276. expect( collection.getIndex( 'id1' ) ).to.equal( 0 );
  277. expect( collection.getIndex( 'id2' ) ).to.equal( 1 );
  278. expect( collection.getIndex( 'id3' ) ).to.equal( 2 );
  279. } );
  280. it( 'should return index equal to -1 when given item is not defined in the collection', () => {
  281. const item1 = { foo: 'bar' };
  282. expect( collection.getIndex( item1 ) ).to.equal( -1 );
  283. } );
  284. it( 'should return index equal to -1 when item of given id is not defined in the collection', () => {
  285. expect( collection.getIndex( 'id1' ) ).to.equal( -1 );
  286. } );
  287. } );
  288. describe( 'remove()', () => {
  289. it( 'should remove the model by index', () => {
  290. collection.add( getItem( 'bom' ) );
  291. collection.add( getItem( 'foo' ) );
  292. collection.add( getItem( 'bar' ) );
  293. expect( collection ).to.have.length( 3 );
  294. const removedItem = collection.remove( 1 );
  295. expect( collection ).to.have.length( 2 );
  296. expect( collection.get( 'foo' ) ).to.be.null;
  297. expect( collection.get( 1 ) ).to.have.property( 'id', 'bar' );
  298. expect( removedItem ).to.have.property( 'id', 'foo' );
  299. } );
  300. it( 'should remove the model by index - custom id property', () => {
  301. const collection = new Collection( { idProperty: 'name' } );
  302. collection.add( getItem( 'foo', 'name' ) );
  303. const removedItem = collection.remove( 0 );
  304. expect( collection ).to.have.length( 0 );
  305. expect( collection.get( 'foo' ) ).to.be.null;
  306. expect( removedItem ).to.have.property( 'name', 'foo' );
  307. } );
  308. it( 'should remove the model by id', () => {
  309. collection.add( getItem( 'bom' ) );
  310. collection.add( getItem( 'foo' ) );
  311. collection.add( getItem( 'bar' ) );
  312. expect( collection ).to.have.length( 3 );
  313. const removedItem = collection.remove( 'foo' );
  314. expect( collection ).to.have.length( 2 );
  315. expect( collection.get( 'foo' ) ).to.be.null;
  316. expect( collection.get( 1 ) ).to.have.property( 'id', 'bar' );
  317. expect( removedItem ).to.have.property( 'id', 'foo' );
  318. } );
  319. it( 'should remove the model by model', () => {
  320. const item = getItem( 'foo' );
  321. collection.add( getItem( 'bom' ) );
  322. collection.add( item );
  323. collection.add( getItem( 'bar' ) );
  324. expect( collection ).to.have.length( 3 );
  325. const removedItem = collection.remove( item );
  326. expect( collection ).to.have.length( 2 );
  327. expect( collection.get( 'foo' ) ).to.be.null;
  328. expect( collection.get( 1 ) ).to.have.property( 'id', 'bar' );
  329. expect( removedItem ).to.equal( item );
  330. } );
  331. it( 'should remove the model by model - custom id property', () => {
  332. const collection = new Collection( { idProperty: 'name' } );
  333. const item = getItem( 'foo', 'name' );
  334. collection.add( item );
  335. const removedItem = collection.remove( item );
  336. expect( collection ).to.have.length( 0 );
  337. expect( collection.get( 'foo' ) ).to.be.null;
  338. expect( removedItem ).to.have.property( 'name', 'foo' );
  339. } );
  340. it( 'should fire the "remove" event', () => {
  341. const item1 = getItem( 'foo' );
  342. const item2 = getItem( 'bar' );
  343. const item3 = getItem( 'bom' );
  344. collection.add( item1 );
  345. collection.add( item2 );
  346. collection.add( item3 );
  347. const spy = sinon.spy();
  348. collection.on( 'remove', spy );
  349. collection.remove( 1 ); // by index
  350. collection.remove( item1 ); // by model
  351. collection.remove( 'bom' ); // by id
  352. sinon.assert.calledThrice( spy );
  353. sinon.assert.calledWithExactly( spy, sinon.match.has( 'source', collection ), item1, 0 );
  354. sinon.assert.calledWithExactly( spy, sinon.match.has( 'source', collection ), item2, 1 );
  355. sinon.assert.calledWithExactly( spy, sinon.match.has( 'source', collection ), item3, 0 );
  356. } );
  357. it( 'should throw an error on invalid index', () => {
  358. collection.add( getItem( 'foo' ) );
  359. expectToThrowCKEditorError( () => {
  360. collection.remove( 1 );
  361. }, /^collection-remove-404/ );
  362. expect( collection ).to.have.length( 1 );
  363. } );
  364. it( 'should throw an error on invalid id', () => {
  365. collection.add( getItem( 'foo' ) );
  366. expectToThrowCKEditorError( () => {
  367. collection.remove( 'bar' );
  368. }, /^collection-remove-404/ );
  369. expect( collection ).to.have.length( 1 );
  370. } );
  371. it( 'should throw an error on invalid model', () => {
  372. collection.add( getItem( 'foo' ) );
  373. expectToThrowCKEditorError( () => {
  374. collection.remove( getItem( 'bar' ) );
  375. }, /^collection-remove-404/ );
  376. expect( collection ).to.have.length( 1 );
  377. } );
  378. } );
  379. describe( 'map()', () => {
  380. it( 'uses native map', () => {
  381. const spy = testUtils.sinon.stub( Array.prototype, 'map' ).returns( [ 'foo' ] );
  382. const ctx = {};
  383. const ret = collection.map( callback, ctx );
  384. sinon.assert.calledWithExactly( spy, callback, ctx );
  385. expect( ret ).to.deep.equal( [ 'foo' ], 'ret value was forwarded' );
  386. function callback() {}
  387. } );
  388. } );
  389. describe( 'find()', () => {
  390. it( 'uses native find', () => {
  391. const needl = getItem( 'foo' );
  392. const spy = testUtils.sinon.stub( Array.prototype, 'find' ).returns( needl );
  393. const ctx = {};
  394. const ret = collection.find( callback, ctx );
  395. sinon.assert.calledWithExactly( spy, callback, ctx );
  396. expect( ret ).to.equal( needl, 'ret value was forwarded' );
  397. function callback() {}
  398. } );
  399. } );
  400. describe( 'filter()', () => {
  401. it( 'uses native filter', () => {
  402. const needl = getItem( 'foo' );
  403. // See: https://github.com/sinonjs/sinon/issues/1521
  404. const spy = testUtils.sinon.stub( collection._items, 'filter' ).returns( [ needl ] );
  405. const ctx = {};
  406. const ret = collection.filter( callback, ctx );
  407. sinon.assert.calledWithExactly( spy, callback, ctx );
  408. expect( ret ).to.deep.equal( [ needl ], 'ret value was forwarded' );
  409. function callback() {}
  410. } );
  411. } );
  412. describe( 'clear()', () => {
  413. it( 'removes all items', () => {
  414. const items = [ {}, {}, {} ];
  415. const spy = sinon.spy();
  416. collection.on( 'remove', spy );
  417. items.forEach( i => collection.add( i ) );
  418. collection.clear();
  419. expect( spy.callCount ).to.equal( 3 );
  420. expect( collection.length ).to.equal( 0 );
  421. } );
  422. it( 'breaks the binding', () => {
  423. const external = new Collection();
  424. collection.bindTo( external ).using( i => i );
  425. external.add( { foo: 'bar' } );
  426. expect( collection ).to.have.length( 1 );
  427. collection.clear();
  428. external.add( { foo: 'baz' } );
  429. expect( collection ).to.have.length( 0 );
  430. external.remove( 0 );
  431. expect( collection ).to.have.length( 0 );
  432. expect( collection._bindToCollection ).to.be.null;
  433. } );
  434. } );
  435. describe( 'bindTo()', () => {
  436. class FactoryClass {
  437. constructor( data ) {
  438. this.data = data;
  439. }
  440. }
  441. function assertItems( collection, expectedItems ) {
  442. expect( collection.map( i => i.v ) ).to.deep.equal( expectedItems );
  443. }
  444. it( 'throws when binding more than once', () => {
  445. collection.bindTo( {} );
  446. expectToThrowCKEditorError( () => {
  447. collection.bindTo( {} );
  448. }, /^collection-bind-to-rebind/ );
  449. } );
  450. it( 'provides "using()" and "as()" interfaces', () => {
  451. const returned = collection.bindTo( {} );
  452. expect( returned ).to.have.keys( 'using', 'as' );
  453. expect( returned.using ).to.be.a( 'function' );
  454. expect( returned.as ).to.be.a( 'function' );
  455. } );
  456. it( 'stores reference to bound collection', () => {
  457. const collectionB = new Collection();
  458. expect( collection._bindToCollection ).to.be.undefined;
  459. expect( collectionB._bindToCollection ).to.be.undefined;
  460. collection.bindTo( collectionB ).as( FactoryClass );
  461. expect( collection._bindToCollection ).to.equal( collectionB );
  462. expect( collectionB._bindToCollection ).to.be.undefined;
  463. } );
  464. describe( 'as()', () => {
  465. let items;
  466. beforeEach( () => {
  467. items = new Collection();
  468. } );
  469. it( 'does not chain', () => {
  470. const returned = collection.bindTo( new Collection() ).as( FactoryClass );
  471. expect( returned ).to.be.undefined;
  472. } );
  473. it( 'creates a binding (initial content)', () => {
  474. items.add( { id: '1' } );
  475. items.add( { id: '2' } );
  476. collection.bindTo( items ).as( FactoryClass );
  477. expect( collection ).to.have.length( 2 );
  478. expect( collection.get( 0 ) ).to.be.instanceOf( FactoryClass );
  479. expect( collection.get( 1 ) ).to.be.instanceOf( FactoryClass );
  480. expect( collection.get( 1 ).data ).to.equal( items.get( 1 ) );
  481. } );
  482. it( 'creates a binding (new content)', () => {
  483. collection.bindTo( items ).as( FactoryClass );
  484. expect( collection ).to.have.length( 0 );
  485. items.add( { id: '1' } );
  486. items.add( { id: '2' } );
  487. expect( collection ).to.have.length( 2 );
  488. expect( collection.get( 0 ) ).to.be.instanceOf( FactoryClass );
  489. expect( collection.get( 1 ) ).to.be.instanceOf( FactoryClass );
  490. expect( collection.get( 1 ).data ).to.equal( items.get( 1 ) );
  491. } );
  492. it( 'creates a binding (item removal)', () => {
  493. collection.bindTo( items ).as( FactoryClass );
  494. expect( collection ).to.have.length( 0 );
  495. items.add( { id: '1' } );
  496. items.add( { id: '2' } );
  497. expect( collection ).to.have.length( 2 );
  498. expect( collection.get( 0 ) ).to.be.instanceOf( FactoryClass );
  499. expect( collection.get( 1 ) ).to.be.instanceOf( FactoryClass );
  500. expect( collection.get( 1 ).data ).to.equal( items.get( 1 ) );
  501. items.remove( 1 );
  502. expect( collection.get( 0 ).data ).to.equal( items.get( 0 ) );
  503. items.remove( 0 );
  504. expect( collection ).to.have.length( 0 );
  505. } );
  506. } );
  507. describe( 'using()', () => {
  508. let items;
  509. beforeEach( () => {
  510. items = new Collection();
  511. } );
  512. it( 'does not chain', () => {
  513. const returned = collection.bindTo( new Collection() ).using( () => {} );
  514. expect( returned ).to.be.undefined;
  515. } );
  516. describe( 'callback', () => {
  517. it( 'creates a binding (arrow function)', () => {
  518. collection.bindTo( items ).using( item => {
  519. return new FactoryClass( item );
  520. } );
  521. expect( collection ).to.have.length( 0 );
  522. items.add( { id: '1' } );
  523. items.add( { id: '2' } );
  524. expect( collection ).to.have.length( 2 );
  525. expect( collection.get( 0 ) ).to.be.instanceOf( FactoryClass );
  526. expect( collection.get( 1 ) ).to.be.instanceOf( FactoryClass );
  527. expect( collection.get( 1 ).data ).to.equal( items.get( 1 ) );
  528. } );
  529. // https://github.com/ckeditor/ckeditor5-ui/issues/113
  530. it( 'creates a binding (normal function)', () => {
  531. collection.bindTo( items ).using( function( item ) {
  532. return new FactoryClass( item );
  533. } );
  534. items.add( { id: '1' } );
  535. expect( collection ).to.have.length( 1 );
  536. const view = collection.get( 0 );
  537. // Wrong args will be passed to the callback if it's treated as the view constructor.
  538. expect( view ).to.be.instanceOf( FactoryClass );
  539. expect( view.data ).to.equal( items.get( 0 ) );
  540. } );
  541. it( 'creates a 1:1 binding', () => {
  542. collection.bindTo( items ).using( item => item );
  543. expect( collection ).to.have.length( 0 );
  544. const item1 = { id: '100' };
  545. const item2 = { id: '200' };
  546. items.add( item1 );
  547. items.add( item2 );
  548. expect( collection ).to.have.length( 2 );
  549. expect( collection.get( 0 ) ).to.equal( item1 );
  550. expect( collection.get( 1 ) ).to.equal( item2 );
  551. } );
  552. it( 'creates a conditional binding', () => {
  553. class CustomClass {
  554. constructor( data ) {
  555. this.data = data;
  556. }
  557. }
  558. collection.bindTo( items ).using( item => {
  559. if ( item.id == 'FactoryClass' ) {
  560. return new FactoryClass( item );
  561. } else {
  562. return new CustomClass( item );
  563. }
  564. } );
  565. expect( collection ).to.have.length( 0 );
  566. const item1 = { id: 'FactoryClass' };
  567. const item2 = { id: 'CustomClass' };
  568. items.add( item1 );
  569. items.add( item2 );
  570. expect( collection ).to.have.length( 2 );
  571. expect( collection.get( 0 ) ).to.be.instanceOf( FactoryClass );
  572. expect( collection.get( 1 ) ).to.be.instanceOf( CustomClass );
  573. } );
  574. it( 'creates a binding to a property name', () => {
  575. collection.bindTo( items ).using( item => item.prop );
  576. expect( collection ).to.have.length( 0 );
  577. items.add( { prop: { value: 'foo' } } );
  578. items.add( { prop: { value: 'bar' } } );
  579. expect( collection ).to.have.length( 2 );
  580. expect( collection.get( 0 ).value ).to.equal( 'foo' );
  581. expect( collection.get( 1 ).value ).to.equal( 'bar' );
  582. } );
  583. it( 'skips when there is no item', () => {
  584. // Add before collection is bound.
  585. items.add( { value: 1, skip: true } );
  586. expect( collection ).to.have.length( 0 );
  587. collection.bindTo( items ).using( item => {
  588. if ( item.skip ) {
  589. return null;
  590. }
  591. return item;
  592. } );
  593. // Still 0 because initial item was skipped.
  594. expect( collection ).to.have.length( 0 );
  595. items.add( { value: 2, skip: false } );
  596. items.add( { value: 3, skip: true } );
  597. items.add( { value: 4, skip: false } );
  598. expect( Array.from( collection, item => item.value ) ).to.deep.equal( [ 2, 4 ] );
  599. items.add( { value: 5, skip: false }, 2 );
  600. expect( Array.from( collection, item => item.value ) ).to.deep.equal( [ 2, 5, 4 ] );
  601. } );
  602. } );
  603. describe( 'property name', () => {
  604. it( 'creates a binding', () => {
  605. collection.bindTo( items ).using( 'prop' );
  606. expect( collection ).to.have.length( 0 );
  607. items.add( { prop: { value: 'foo' } } );
  608. items.add( { prop: { value: 'bar' } } );
  609. expect( collection ).to.have.length( 2 );
  610. expect( collection.get( 0 ).value ).to.equal( 'foo' );
  611. expect( collection.get( 1 ).value ).to.equal( 'bar' );
  612. } );
  613. it( 'creates a binding (item removal)', () => {
  614. collection.bindTo( items ).using( 'prop' );
  615. expect( collection ).to.have.length( 0 );
  616. items.add( { prop: { value: 'foo' } } );
  617. items.add( { prop: { value: 'bar' } } );
  618. expect( collection ).to.have.length( 2 );
  619. expect( collection.get( 0 ).value ).to.equal( 'foo' );
  620. expect( collection.get( 1 ).value ).to.equal( 'bar' );
  621. items.remove( 1 );
  622. expect( collection ).to.have.length( 1 );
  623. expect( collection.get( 0 ).value ).to.equal( 'foo' );
  624. items.remove( 0 );
  625. expect( collection ).to.have.length( 0 );
  626. } );
  627. it( 'skips when there is no item', () => {
  628. items.add( { prop: null } );
  629. collection.bindTo( items ).using( 'prop' );
  630. // Still 0 because initial item was skipped.
  631. expect( collection ).to.have.length( 0 );
  632. items.add( { prop: { value: 2, skip: false } } );
  633. items.add( { prop: null } );
  634. items.add( { prop: { value: 4, skip: false } } );
  635. expect( Array.from( collection, item => item.value ) ).to.deep.equal( [ 2, 4 ] );
  636. items.add( { prop: { value: 5 } }, 2 );
  637. expect( Array.from( collection, item => item.value ) ).to.deep.equal( [ 2, 5, 4 ] );
  638. } );
  639. } );
  640. } );
  641. describe( 'two–way data binding', () => {
  642. it( 'works with custom factories (1)', () => {
  643. const collectionA = new Collection();
  644. const collectionB = new Collection();
  645. const spyA = sinon.spy();
  646. const spyB = sinon.spy();
  647. collectionA.on( 'add', spyA );
  648. collectionB.on( 'add', spyB );
  649. // A<--->B
  650. collectionA.bindTo( collectionB ).using( i => ( { v: i.v * 2 } ) );
  651. collectionB.bindTo( collectionA ).using( i => ( { v: i.v / 2 } ) );
  652. assertItems( collectionA, [] );
  653. assertItems( collectionB, [] );
  654. collectionA.add( { v: 4 } );
  655. collectionA.add( { v: 6 } );
  656. assertItems( collectionA, [ 4, 6 ] );
  657. assertItems( collectionB, [ 2, 3 ] );
  658. collectionB.add( { v: 4 } );
  659. assertItems( collectionA, [ 4, 6, 8 ] );
  660. assertItems( collectionB, [ 2, 3, 4 ] );
  661. sinon.assert.callCount( spyA, 3 );
  662. sinon.assert.callCount( spyB, 3 );
  663. } );
  664. it( 'works with custom factories (2)', () => {
  665. const collectionA = new Collection();
  666. const collectionB = new Collection();
  667. const spyA = sinon.spy();
  668. const spyB = sinon.spy();
  669. collectionA.on( 'add', spyA );
  670. collectionB.on( 'add', spyB );
  671. // A<--->B
  672. collectionA.bindTo( collectionB ).using( 'data' );
  673. collectionB.bindTo( collectionA ).using( i => new FactoryClass( i ) );
  674. collectionA.add( { v: 4 } );
  675. collectionA.add( { v: 6 } );
  676. expect( [ ...collectionB ].every( i => i instanceof FactoryClass ) ).to.be.true;
  677. expect( [ ...collectionB ].map( i => i.data ) ).to.deep.equal( [ ...collectionA ] );
  678. expect( collectionB.map( i => i.data.v ) ).to.deep.equal( [ 4, 6 ] );
  679. expect( collectionA.map( i => i.v ) ).to.deep.equal( [ 4, 6 ] );
  680. collectionB.add( new FactoryClass( { v: 8 } ) );
  681. expect( [ ...collectionB ].every( i => i instanceof FactoryClass ) ).to.be.true;
  682. expect( [ ...collectionB ].map( i => i.data ) ).to.deep.equal( [ ...collectionA ] );
  683. expect( collectionB.map( i => i.data.v ) ).to.deep.equal( [ 4, 6, 8 ] );
  684. expect( collectionA.map( i => i.v ) ).to.deep.equal( [ 4, 6, 8 ] );
  685. } );
  686. it( 'works with custom factories (custom index)', () => {
  687. const collectionA = new Collection();
  688. const collectionB = new Collection();
  689. const spyA = sinon.spy();
  690. const spyB = sinon.spy();
  691. collectionA.on( 'add', spyA );
  692. collectionB.on( 'add', spyB );
  693. // A<--->B
  694. collectionA.bindTo( collectionB ).using( i => ( { v: i.v * 2 } ) );
  695. collectionB.bindTo( collectionA ).using( i => ( { v: i.v / 2 } ) );
  696. assertItems( collectionA, [] );
  697. assertItems( collectionB, [] );
  698. collectionA.add( { v: 4 } );
  699. collectionA.add( { v: 6 }, 0 );
  700. assertItems( collectionA, [ 6, 4 ] );
  701. assertItems( collectionB, [ 3, 2 ] );
  702. collectionB.add( { v: 4 }, 1 );
  703. assertItems( collectionA, [ 6, 8, 4 ] );
  704. assertItems( collectionB, [ 3, 4, 2 ] );
  705. sinon.assert.callCount( spyA, 3 );
  706. sinon.assert.callCount( spyB, 3 );
  707. } );
  708. it( 'works with 1:1 binding', () => {
  709. const collectionA = new Collection();
  710. const collectionB = new Collection();
  711. const spyA = sinon.spy();
  712. const spyB = sinon.spy();
  713. collectionA.on( 'add', spyA );
  714. collectionB.on( 'add', spyB );
  715. // A<--->B
  716. collectionA.bindTo( collectionB ).using( i => i );
  717. collectionB.bindTo( collectionA ).using( i => i );
  718. assertItems( collectionA, [], [] );
  719. assertItems( collectionB, [], [] );
  720. collectionA.add( { v: 4 } );
  721. collectionA.add( { v: 6 } );
  722. assertItems( collectionA, [ 4, 6 ] );
  723. assertItems( collectionB, [ 4, 6 ] );
  724. collectionB.add( { v: 8 } );
  725. assertItems( collectionA, [ 4, 6, 8 ] );
  726. assertItems( collectionB, [ 4, 6, 8 ] );
  727. expect( [ ...collectionA ] ).to.deep.equal( [ ...collectionB ] );
  728. sinon.assert.callCount( spyA, 3 );
  729. sinon.assert.callCount( spyB, 3 );
  730. } );
  731. it( 'works with double chaining', () => {
  732. const collectionA = new Collection();
  733. const collectionB = new Collection();
  734. const collectionC = new Collection();
  735. const spyA = sinon.spy();
  736. const spyB = sinon.spy();
  737. const spyC = sinon.spy();
  738. collectionA.on( 'add', spyA );
  739. collectionB.on( 'add', spyB );
  740. collectionC.on( 'add', spyC );
  741. // A<--->B--->C
  742. collectionA.bindTo( collectionB ).using( i => ( { v: i.v * 2 } ) );
  743. collectionB.bindTo( collectionA ).using( i => ( { v: i.v / 2 } ) );
  744. collectionC.bindTo( collectionB ).using( i => ( { v: -i.v } ) );
  745. assertItems( collectionA, [] );
  746. assertItems( collectionB, [] );
  747. assertItems( collectionC, [] );
  748. collectionA.add( { v: 4 } );
  749. collectionA.add( { v: 6 } );
  750. assertItems( collectionA, [ 4, 6 ] );
  751. assertItems( collectionB, [ 2, 3 ] );
  752. assertItems( collectionC, [ -2, -3 ] );
  753. collectionB.add( { v: 4 } );
  754. assertItems( collectionA, [ 4, 6, 8 ] );
  755. assertItems( collectionB, [ 2, 3, 4 ] );
  756. assertItems( collectionC, [ -2, -3, -4 ] );
  757. collectionC.add( { v: -1000 } );
  758. assertItems( collectionA, [ 4, 6, 8 ] );
  759. assertItems( collectionB, [ 2, 3, 4 ] );
  760. assertItems( collectionC, [ -2, -3, -4, -1000 ] );
  761. sinon.assert.callCount( spyA, 3 );
  762. sinon.assert.callCount( spyB, 3 );
  763. sinon.assert.callCount( spyC, 4 );
  764. } );
  765. it( 'removes items correctly', () => {
  766. const collectionA = new Collection();
  767. const collectionB = new Collection();
  768. const spyAddA = sinon.spy();
  769. const spyAddB = sinon.spy();
  770. const spyRemoveA = sinon.spy();
  771. const spyRemoveB = sinon.spy();
  772. collectionA.on( 'add', spyAddA );
  773. collectionB.on( 'add', spyAddB );
  774. collectionA.on( 'remove', spyRemoveA );
  775. collectionB.on( 'remove', spyRemoveB );
  776. // A<--->B
  777. collectionA.bindTo( collectionB ).using( i => ( { v: i.v * 2 } ) );
  778. collectionB.bindTo( collectionA ).using( i => ( { v: i.v / 2 } ) );
  779. assertItems( collectionA, [], [] );
  780. assertItems( collectionB, [], [] );
  781. collectionA.add( { v: 4 } );
  782. collectionA.add( { v: 6 } );
  783. assertItems( collectionA, [ 4, 6 ] );
  784. assertItems( collectionB, [ 2, 3 ] );
  785. collectionB.add( { v: 4 } );
  786. assertItems( collectionA, [ 4, 6, 8 ] );
  787. assertItems( collectionB, [ 2, 3, 4 ] );
  788. collectionB.remove( 0 );
  789. assertItems( collectionA, [ 6, 8 ] );
  790. assertItems( collectionB, [ 3, 4 ] );
  791. sinon.assert.callCount( spyAddA, 3 );
  792. sinon.assert.callCount( spyAddB, 3 );
  793. sinon.assert.callCount( spyRemoveA, 1 );
  794. sinon.assert.callCount( spyRemoveB, 1 );
  795. collectionA.remove( 1 );
  796. assertItems( collectionA, [ 6 ] );
  797. assertItems( collectionB, [ 3 ] );
  798. sinon.assert.callCount( spyAddA, 3 );
  799. sinon.assert.callCount( spyAddB, 3 );
  800. sinon.assert.callCount( spyRemoveA, 2 );
  801. sinon.assert.callCount( spyRemoveB, 2 );
  802. } );
  803. describe( 'skipping items', () => {
  804. let collectionA, collectionB;
  805. beforeEach( () => {
  806. collectionA = new Collection();
  807. collectionB = new Collection();
  808. // A<--->B
  809. collectionA.bindTo( collectionB ).using( item => {
  810. if ( item.skip ) {
  811. return null;
  812. }
  813. return item;
  814. } );
  815. collectionB.bindTo( collectionA ).using( item => {
  816. if ( item.skip ) {
  817. return null;
  818. }
  819. return item;
  820. } );
  821. } );
  822. it( 'should add items at the enf of collections when includes skipped items', () => {
  823. collectionA.add( { v: 'A' } );
  824. collectionA.add( { v: 'B', skip: true } );
  825. collectionA.add( { v: 'C', skip: true } );
  826. collectionA.add( { v: 'D' } );
  827. expect( collectionA._skippedIndexesFromExternal ).to.have.members( [] );
  828. expect( collectionB._skippedIndexesFromExternal ).to.have.members( [ 1, 2 ] );
  829. assertItems( collectionA, [ 'A', 'B', 'C', 'D' ] );
  830. assertItems( collectionB, [ 'A', 'D' ] );
  831. collectionB.add( { v: 'E' } );
  832. collectionB.add( { v: 'F', skip: true } );
  833. collectionB.add( { v: 'G' } );
  834. expect( collectionA._skippedIndexesFromExternal ).to.have.members( [ 3 ] );
  835. expect( collectionB._skippedIndexesFromExternal ).to.have.members( [ 1, 2 ] );
  836. assertItems( collectionA, [ 'A', 'B', 'C', 'D', 'E', 'G' ] );
  837. assertItems( collectionB, [ 'A', 'D', 'E', 'F', 'G' ] );
  838. } );
  839. it( 'should add items between skipped items', () => {
  840. collectionA.add( { v: 'A' } );
  841. collectionA.add( { v: 'B', skip: true } );
  842. collectionA.add( { v: 'C', skip: true } );
  843. collectionA.add( { v: 'D' } );
  844. collectionB.add( { v: 'E' } );
  845. collectionB.add( { v: 'F', skip: true } );
  846. collectionB.add( { v: 'G', skip: true } );
  847. collectionB.add( { v: 'H' } );
  848. expect( collectionA._skippedIndexesFromExternal ).to.have.members( [ 3, 4 ] );
  849. expect( collectionB._skippedIndexesFromExternal ).to.have.members( [ 1, 2 ] );
  850. assertItems( collectionA, [ 'A', 'B', 'C', 'D', 'E', 'H' ] );
  851. assertItems( collectionB, [ 'A', 'D', 'E', 'F', 'G', 'H' ] );
  852. collectionA.add( { v: 'I' }, 2 );
  853. expect( collectionA._skippedIndexesFromExternal ).to.have.members( [ 4, 5 ] );
  854. expect( collectionB._skippedIndexesFromExternal ).to.have.members( [ 1, 2 ] );
  855. assertItems( collectionA, [ 'A', 'B', 'I', 'C', 'D', 'E', 'H' ] );
  856. assertItems( collectionB, [ 'A', 'I', 'D', 'E', 'F', 'G', 'H' ] );
  857. collectionB.add( { v: 'J' }, 5 );
  858. expect( collectionA._skippedIndexesFromExternal ).to.have.members( [ 4, 5 ] );
  859. expect( collectionB._skippedIndexesFromExternal ).to.have.members( [ 1, 2 ] );
  860. assertItems( collectionA, [ 'A', 'B', 'I', 'C', 'D', 'E', 'J', 'H' ] );
  861. assertItems( collectionB, [ 'A', 'I', 'D', 'E', 'F', 'J', 'G', 'H' ] );
  862. } );
  863. it( 'should properly remove skipped items and update skipped indexes', () => {
  864. collectionA.add( { v: 'A' } );
  865. collectionA.add( { v: 'B', skip: true } );
  866. collectionA.add( { v: 'C', skip: true } );
  867. collectionA.add( { v: 'D' } );
  868. collectionB.add( { v: 'E' } );
  869. collectionB.add( { v: 'F', skip: true } );
  870. collectionB.add( { v: 'G', skip: true } );
  871. collectionB.add( { v: 'H' } );
  872. expect( collectionA._skippedIndexesFromExternal ).to.have.members( [ 3, 4 ] );
  873. expect( collectionB._skippedIndexesFromExternal ).to.have.members( [ 1, 2 ] );
  874. assertItems( collectionA, [ 'A', 'B', 'C', 'D', 'E', 'H' ] );
  875. assertItems( collectionB, [ 'A', 'D', 'E', 'F', 'G', 'H' ] );
  876. collectionA.remove( 2 );
  877. expect( collectionA._skippedIndexesFromExternal ).to.have.members( [ 3, 4 ] );
  878. expect( collectionB._skippedIndexesFromExternal ).to.have.members( [ 1 ] );
  879. assertItems( collectionA, [ 'A', 'B', 'D', 'E', 'H' ] );
  880. assertItems( collectionB, [ 'A', 'D', 'E', 'F', 'G', 'H' ] );
  881. collectionB.remove( 3 );
  882. expect( collectionA._skippedIndexesFromExternal ).to.have.members( [ 3 ] );
  883. expect( collectionB._skippedIndexesFromExternal ).to.have.members( [ 1 ] );
  884. assertItems( collectionA, [ 'A', 'B', 'D', 'E', 'H' ] );
  885. assertItems( collectionB, [ 'A', 'D', 'E', 'G', 'H' ] );
  886. } );
  887. } );
  888. } );
  889. } );
  890. describe( 'iterator', () => {
  891. it( 'covers the whole collection', () => {
  892. const item1 = getItem( 'foo' );
  893. const item2 = getItem( 'bar' );
  894. const item3 = getItem( 'bom' );
  895. const items = [];
  896. collection.add( item1 );
  897. collection.add( item2 );
  898. collection.add( item3 );
  899. for ( const item of collection ) {
  900. items.push( item.id );
  901. }
  902. expect( items ).to.deep.equal( [ 'foo', 'bar', 'bom' ] );
  903. } );
  904. } );
  905. } );