collection.js 40 KB

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