collection.js 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432
  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 "change" event', () => {
  286. const spy = sinon.spy();
  287. const items = [ {}, {} ];
  288. collection.on( 'change', spy );
  289. collection.addMany( items );
  290. sinon.assert.calledOnce( spy );
  291. expect( spy.args[ 0 ][ 1 ] ).to.deep.eql( {
  292. added: items,
  293. removed: [],
  294. index: 0
  295. } );
  296. } );
  297. it( 'should fire the "change" event with the index argument', () => {
  298. const spy = sinon.spy();
  299. const firstBatch = [ {}, {} ];
  300. const secondBatch = [ {}, {} ];
  301. collection.addMany( firstBatch );
  302. collection.on( 'change', spy );
  303. collection.addMany( secondBatch, 1 );
  304. expect( spy.callCount, 'call count' ).to.equal( 1 );
  305. expect( spy.args[ 0 ][ 1 ] ).to.deep.eql( {
  306. added: secondBatch,
  307. removed: [],
  308. index: 1
  309. } );
  310. } );
  311. it( 'should support an optional index argument', () => {
  312. const item1 = getItem( 'foo' );
  313. const item2 = getItem( 'bar' );
  314. const item3 = getItem( 'baz' );
  315. const item4 = getItem( 'abc' );
  316. collection.addMany( [ item1 ] );
  317. collection.addMany( [ item2 ], 0 );
  318. collection.addMany( [ item3 ], 1 );
  319. collection.addMany( [ item4 ], 3 );
  320. expect( collection.get( 0 ) ).to.equal( item2 );
  321. expect( collection.get( 1 ) ).to.equal( item3 );
  322. expect( collection.get( 2 ) ).to.equal( item1 );
  323. expect( collection.get( 3 ) ).to.equal( item4 );
  324. } );
  325. it( 'should throw when index argument is invalid', () => {
  326. const item1 = getItem( 'foo' );
  327. const item2 = getItem( 'bar' );
  328. const item3 = getItem( 'baz' );
  329. collection.addMany( [ item1 ] );
  330. expectToThrowCKEditorError( () => {
  331. collection.addMany( [ item2 ], -1 );
  332. }, /^collection-add-item-invalid-index/ );
  333. expectToThrowCKEditorError( () => {
  334. collection.addMany( [ item2 ], 2 );
  335. }, /^collection-add-item-invalid-index/ );
  336. collection.addMany( [ item2 ], 1 );
  337. collection.addMany( [ item3 ], 0 );
  338. expect( collection.length ).to.equal( 3 );
  339. } );
  340. } );
  341. describe( 'get()', () => {
  342. it( 'should return an item', () => {
  343. const item = getItem( 'foo' );
  344. collection.add( item );
  345. expect( collection.get( 'foo' ) ).to.equal( item );
  346. } );
  347. it( 'should return null if id does not exist', () => {
  348. collection.add( getItem( 'foo' ) );
  349. expect( collection.get( 'bar' ) ).to.be.null;
  350. } );
  351. it( 'should throw if neither string or number given', () => {
  352. expectToThrowCKEditorError( () => {
  353. collection.get( true );
  354. }, /^collection-get-invalid-arg/ );
  355. } );
  356. } );
  357. describe( 'has()', () => {
  358. it( 'should return true if collection contains item with given id', () => {
  359. collection.add( getItem( 'foo' ) );
  360. expect( collection.has( 'foo' ) ).to.equal( true );
  361. } );
  362. it( 'should return false if collection does not contain item with given id', () => {
  363. collection.add( getItem( 'foo' ) );
  364. expect( collection.has( 'bar' ) ).to.equal( false );
  365. } );
  366. it( 'should return true if collection contains item', () => {
  367. const item = getItem( 'foo' );
  368. collection.add( item );
  369. expect( collection.has( item ) ).to.equal( true );
  370. } );
  371. it( 'should return false if collection does not contains item', () => {
  372. collection.add( getItem( 'foo' ) );
  373. expect( collection.has( getItem( 'bar' ) ) ).to.equal( false );
  374. } );
  375. } );
  376. describe( 'getIndex()', () => {
  377. it( 'should return index of given item', () => {
  378. const item1 = { foo: 'bar' };
  379. const item2 = { bar: 'biz' };
  380. const item3 = { foo: 'biz' };
  381. collection.add( item1 );
  382. collection.add( item2 );
  383. collection.add( item3 );
  384. expect( collection.getIndex( item1 ) ).to.equal( 0 );
  385. expect( collection.getIndex( item2 ) ).to.equal( 1 );
  386. expect( collection.getIndex( item3 ) ).to.equal( 2 );
  387. } );
  388. it( 'should return index of item with given id', () => {
  389. collection.add( { id: 'id1' } );
  390. collection.add( { id: 'id2' } );
  391. collection.add( { id: 'id3' } );
  392. expect( collection.getIndex( 'id1' ) ).to.equal( 0 );
  393. expect( collection.getIndex( 'id2' ) ).to.equal( 1 );
  394. expect( collection.getIndex( 'id3' ) ).to.equal( 2 );
  395. } );
  396. it( 'should return index equal to -1 when given item is not defined in the collection', () => {
  397. const item1 = { foo: 'bar' };
  398. expect( collection.getIndex( item1 ) ).to.equal( -1 );
  399. } );
  400. it( 'should return index equal to -1 when item of given id is not defined in the collection', () => {
  401. expect( collection.getIndex( 'id1' ) ).to.equal( -1 );
  402. } );
  403. } );
  404. describe( 'remove()', () => {
  405. it( 'should remove the model by index', () => {
  406. collection.add( getItem( 'bom' ) );
  407. collection.add( getItem( 'foo' ) );
  408. collection.add( getItem( 'bar' ) );
  409. expect( collection ).to.have.length( 3 );
  410. const removedItem = collection.remove( 1 );
  411. expect( collection ).to.have.length( 2 );
  412. expect( collection.get( 'foo' ) ).to.be.null;
  413. expect( collection.get( 1 ) ).to.have.property( 'id', 'bar' );
  414. expect( removedItem ).to.have.property( 'id', 'foo' );
  415. } );
  416. it( 'should remove the model by index - custom id property', () => {
  417. const collection = new Collection( { idProperty: 'name' } );
  418. collection.add( getItem( 'foo', 'name' ) );
  419. const removedItem = collection.remove( 0 );
  420. expect( collection ).to.have.length( 0 );
  421. expect( collection.get( 'foo' ) ).to.be.null;
  422. expect( removedItem ).to.have.property( 'name', 'foo' );
  423. } );
  424. it( 'should remove the model by id', () => {
  425. collection.add( getItem( 'bom' ) );
  426. collection.add( getItem( 'foo' ) );
  427. collection.add( getItem( 'bar' ) );
  428. expect( collection ).to.have.length( 3 );
  429. const removedItem = collection.remove( 'foo' );
  430. expect( collection ).to.have.length( 2 );
  431. expect( collection.get( 'foo' ) ).to.be.null;
  432. expect( collection.get( 1 ) ).to.have.property( 'id', 'bar' );
  433. expect( removedItem ).to.have.property( 'id', 'foo' );
  434. } );
  435. it( 'should remove the model by model', () => {
  436. const item = getItem( 'foo' );
  437. collection.add( getItem( 'bom' ) );
  438. collection.add( item );
  439. collection.add( getItem( 'bar' ) );
  440. expect( collection ).to.have.length( 3 );
  441. const removedItem = collection.remove( item );
  442. expect( collection ).to.have.length( 2 );
  443. expect( collection.get( 'foo' ) ).to.be.null;
  444. expect( collection.get( 1 ) ).to.have.property( 'id', 'bar' );
  445. expect( removedItem ).to.equal( item );
  446. } );
  447. it( 'should remove the model by model - custom id property', () => {
  448. const collection = new Collection( { idProperty: 'name' } );
  449. const item = getItem( 'foo', 'name' );
  450. collection.add( item );
  451. const removedItem = collection.remove( item );
  452. expect( collection ).to.have.length( 0 );
  453. expect( collection.get( 'foo' ) ).to.be.null;
  454. expect( removedItem ).to.have.property( 'name', 'foo' );
  455. } );
  456. it( 'should fire the "remove" event', () => {
  457. const item1 = getItem( 'foo' );
  458. const item2 = getItem( 'bar' );
  459. const item3 = getItem( 'bom' );
  460. collection.add( item1 );
  461. collection.add( item2 );
  462. collection.add( item3 );
  463. const spy = sinon.spy();
  464. collection.on( 'remove', spy );
  465. collection.remove( 1 ); // by index
  466. collection.remove( item1 ); // by model
  467. collection.remove( 'bom' ); // by id
  468. sinon.assert.calledThrice( spy );
  469. sinon.assert.calledWithExactly( spy, sinon.match.has( 'source', collection ), item1, 0 );
  470. sinon.assert.calledWithExactly( spy, sinon.match.has( 'source', collection ), item2, 1 );
  471. sinon.assert.calledWithExactly( spy, sinon.match.has( 'source', collection ), item3, 0 );
  472. } );
  473. it( 'should fire the "change" event', () => {
  474. const item = getItem( 'foo' );
  475. const spy = sinon.spy();
  476. collection.add( item );
  477. collection.on( 'change', spy );
  478. collection.remove( item );
  479. sinon.assert.calledOnce( spy );
  480. expect( spy.args[ 0 ][ 1 ] ).to.deep.eql( {
  481. added: [],
  482. removed: [ item ],
  483. index: 0
  484. } );
  485. } );
  486. it( 'should throw an error on invalid index', () => {
  487. collection.add( getItem( 'foo' ) );
  488. expectToThrowCKEditorError( () => {
  489. collection.remove( 1 );
  490. }, /^collection-remove-404/ );
  491. expect( collection ).to.have.length( 1 );
  492. } );
  493. it( 'should throw an error on invalid id', () => {
  494. collection.add( getItem( 'foo' ) );
  495. expectToThrowCKEditorError( () => {
  496. collection.remove( 'bar' );
  497. }, /^collection-remove-404/ );
  498. expect( collection ).to.have.length( 1 );
  499. } );
  500. it( 'should throw an error on invalid model', () => {
  501. collection.add( getItem( 'foo' ) );
  502. expectToThrowCKEditorError( () => {
  503. collection.remove( getItem( 'bar' ) );
  504. }, /^collection-remove-404/ );
  505. expect( collection ).to.have.length( 1 );
  506. } );
  507. } );
  508. describe( 'map()', () => {
  509. it( 'uses native map', () => {
  510. const spy = testUtils.sinon.stub( Array.prototype, 'map' ).returns( [ 'foo' ] );
  511. const ctx = {};
  512. const ret = collection.map( callback, ctx );
  513. sinon.assert.calledWithExactly( spy, callback, ctx );
  514. expect( ret ).to.deep.equal( [ 'foo' ], 'ret value was forwarded' );
  515. function callback() {}
  516. } );
  517. } );
  518. describe( 'find()', () => {
  519. it( 'uses native find', () => {
  520. const needl = getItem( 'foo' );
  521. const spy = testUtils.sinon.stub( Array.prototype, 'find' ).returns( needl );
  522. const ctx = {};
  523. const ret = collection.find( callback, ctx );
  524. sinon.assert.calledWithExactly( spy, callback, ctx );
  525. expect( ret ).to.equal( needl, 'ret value was forwarded' );
  526. function callback() {}
  527. } );
  528. } );
  529. describe( 'filter()', () => {
  530. it( 'uses native filter', () => {
  531. const needl = getItem( 'foo' );
  532. // See: https://github.com/sinonjs/sinon/issues/1521
  533. const spy = testUtils.sinon.stub( collection._items, 'filter' ).returns( [ needl ] );
  534. const ctx = {};
  535. const ret = collection.filter( callback, ctx );
  536. sinon.assert.calledWithExactly( spy, callback, ctx );
  537. expect( ret ).to.deep.equal( [ needl ], 'ret value was forwarded' );
  538. function callback() {}
  539. } );
  540. } );
  541. describe( 'clear()', () => {
  542. it( 'removes all items', () => {
  543. const items = [ {}, {}, {} ];
  544. const spy = sinon.spy();
  545. collection.on( 'remove', spy );
  546. items.forEach( i => collection.add( i ) );
  547. collection.clear();
  548. expect( spy.callCount ).to.equal( 3 );
  549. expect( collection.length ).to.equal( 0 );
  550. } );
  551. it( 'breaks the binding', () => {
  552. const external = new Collection();
  553. collection.bindTo( external ).using( i => i );
  554. external.add( { foo: 'bar' } );
  555. expect( collection ).to.have.length( 1 );
  556. collection.clear();
  557. external.add( { foo: 'baz' } );
  558. expect( collection ).to.have.length( 0 );
  559. external.remove( 0 );
  560. expect( collection ).to.have.length( 0 );
  561. expect( collection._bindToCollection ).to.be.null;
  562. } );
  563. it( 'should fire the "change" event', () => {
  564. const items = [ {}, {}, {} ];
  565. const spy = sinon.spy();
  566. collection.addMany( items );
  567. collection.on( 'change', spy );
  568. collection.clear();
  569. sinon.assert.calledOnce( spy );
  570. expect( spy.args[ 0 ][ 1 ] ).to.deep.eql( {
  571. added: [],
  572. removed: items,
  573. index: 0
  574. } );
  575. } );
  576. } );
  577. describe( 'bindTo()', () => {
  578. class FactoryClass {
  579. constructor( data ) {
  580. this.data = data;
  581. }
  582. }
  583. function assertItems( collection, expectedItems ) {
  584. expect( collection.map( i => i.v ) ).to.deep.equal( expectedItems );
  585. }
  586. it( 'throws when binding more than once', () => {
  587. collection.bindTo( {} );
  588. expectToThrowCKEditorError( () => {
  589. collection.bindTo( {} );
  590. }, /^collection-bind-to-rebind/ );
  591. } );
  592. it( 'provides "using()" and "as()" interfaces', () => {
  593. const returned = collection.bindTo( {} );
  594. expect( returned ).to.have.keys( 'using', 'as' );
  595. expect( returned.using ).to.be.a( 'function' );
  596. expect( returned.as ).to.be.a( 'function' );
  597. } );
  598. it( 'stores reference to bound collection', () => {
  599. const collectionB = new Collection();
  600. expect( collection._bindToCollection ).to.be.undefined;
  601. expect( collectionB._bindToCollection ).to.be.undefined;
  602. collection.bindTo( collectionB ).as( FactoryClass );
  603. expect( collection._bindToCollection ).to.equal( collectionB );
  604. expect( collectionB._bindToCollection ).to.be.undefined;
  605. } );
  606. describe( 'as()', () => {
  607. let items;
  608. beforeEach( () => {
  609. items = new Collection();
  610. } );
  611. it( 'does not chain', () => {
  612. const returned = collection.bindTo( new Collection() ).as( FactoryClass );
  613. expect( returned ).to.be.undefined;
  614. } );
  615. it( 'creates a binding (initial content)', () => {
  616. items.add( { id: '1' } );
  617. items.add( { id: '2' } );
  618. collection.bindTo( items ).as( FactoryClass );
  619. expect( collection ).to.have.length( 2 );
  620. expect( collection.get( 0 ) ).to.be.instanceOf( FactoryClass );
  621. expect( collection.get( 1 ) ).to.be.instanceOf( FactoryClass );
  622. expect( collection.get( 1 ).data ).to.equal( items.get( 1 ) );
  623. } );
  624. it( 'creates a binding (new content)', () => {
  625. collection.bindTo( items ).as( FactoryClass );
  626. expect( collection ).to.have.length( 0 );
  627. items.add( { id: '1' } );
  628. items.add( { id: '2' } );
  629. expect( collection ).to.have.length( 2 );
  630. expect( collection.get( 0 ) ).to.be.instanceOf( FactoryClass );
  631. expect( collection.get( 1 ) ).to.be.instanceOf( FactoryClass );
  632. expect( collection.get( 1 ).data ).to.equal( items.get( 1 ) );
  633. } );
  634. it( 'creates a binding (item removal)', () => {
  635. collection.bindTo( items ).as( FactoryClass );
  636. expect( collection ).to.have.length( 0 );
  637. items.add( { id: '1' } );
  638. items.add( { id: '2' } );
  639. expect( collection ).to.have.length( 2 );
  640. expect( collection.get( 0 ) ).to.be.instanceOf( FactoryClass );
  641. expect( collection.get( 1 ) ).to.be.instanceOf( FactoryClass );
  642. expect( collection.get( 1 ).data ).to.equal( items.get( 1 ) );
  643. items.remove( 1 );
  644. expect( collection.get( 0 ).data ).to.equal( items.get( 0 ) );
  645. items.remove( 0 );
  646. expect( collection ).to.have.length( 0 );
  647. } );
  648. } );
  649. describe( 'using()', () => {
  650. let items;
  651. beforeEach( () => {
  652. items = new Collection();
  653. } );
  654. it( 'does not chain', () => {
  655. const returned = collection.bindTo( new Collection() ).using( () => {} );
  656. expect( returned ).to.be.undefined;
  657. } );
  658. describe( 'callback', () => {
  659. it( 'creates a binding (arrow function)', () => {
  660. collection.bindTo( items ).using( item => {
  661. return new FactoryClass( item );
  662. } );
  663. expect( collection ).to.have.length( 0 );
  664. items.add( { id: '1' } );
  665. items.add( { id: '2' } );
  666. expect( collection ).to.have.length( 2 );
  667. expect( collection.get( 0 ) ).to.be.instanceOf( FactoryClass );
  668. expect( collection.get( 1 ) ).to.be.instanceOf( FactoryClass );
  669. expect( collection.get( 1 ).data ).to.equal( items.get( 1 ) );
  670. } );
  671. // https://github.com/ckeditor/ckeditor5-ui/issues/113
  672. it( 'creates a binding (normal function)', () => {
  673. collection.bindTo( items ).using( function( item ) {
  674. return new FactoryClass( item );
  675. } );
  676. items.add( { id: '1' } );
  677. expect( collection ).to.have.length( 1 );
  678. const view = collection.get( 0 );
  679. // Wrong args will be passed to the callback if it's treated as the view constructor.
  680. expect( view ).to.be.instanceOf( FactoryClass );
  681. expect( view.data ).to.equal( items.get( 0 ) );
  682. } );
  683. it( 'creates a 1:1 binding', () => {
  684. collection.bindTo( items ).using( item => item );
  685. expect( collection ).to.have.length( 0 );
  686. const item1 = { id: '100' };
  687. const item2 = { id: '200' };
  688. items.add( item1 );
  689. items.add( item2 );
  690. expect( collection ).to.have.length( 2 );
  691. expect( collection.get( 0 ) ).to.equal( item1 );
  692. expect( collection.get( 1 ) ).to.equal( item2 );
  693. } );
  694. it( 'creates a conditional binding', () => {
  695. class CustomClass {
  696. constructor( data ) {
  697. this.data = data;
  698. }
  699. }
  700. collection.bindTo( items ).using( item => {
  701. if ( item.id == 'FactoryClass' ) {
  702. return new FactoryClass( item );
  703. } else {
  704. return new CustomClass( item );
  705. }
  706. } );
  707. expect( collection ).to.have.length( 0 );
  708. const item1 = { id: 'FactoryClass' };
  709. const item2 = { id: 'CustomClass' };
  710. items.add( item1 );
  711. items.add( item2 );
  712. expect( collection ).to.have.length( 2 );
  713. expect( collection.get( 0 ) ).to.be.instanceOf( FactoryClass );
  714. expect( collection.get( 1 ) ).to.be.instanceOf( CustomClass );
  715. } );
  716. it( 'creates a binding to a property name', () => {
  717. collection.bindTo( items ).using( item => item.prop );
  718. expect( collection ).to.have.length( 0 );
  719. items.add( { prop: { value: 'foo' } } );
  720. items.add( { prop: { value: 'bar' } } );
  721. expect( collection ).to.have.length( 2 );
  722. expect( collection.get( 0 ).value ).to.equal( 'foo' );
  723. expect( collection.get( 1 ).value ).to.equal( 'bar' );
  724. } );
  725. it( 'skips when there is no item', () => {
  726. // Add before collection is bound.
  727. items.add( { value: 1, skip: true } );
  728. expect( collection ).to.have.length( 0 );
  729. collection.bindTo( items ).using( item => {
  730. if ( item.skip ) {
  731. return null;
  732. }
  733. return item;
  734. } );
  735. // Still 0 because initial item was skipped.
  736. expect( collection ).to.have.length( 0 );
  737. items.add( { value: 2, skip: false } );
  738. items.add( { value: 3, skip: true } );
  739. items.add( { value: 4, skip: false } );
  740. expect( Array.from( collection, item => item.value ) ).to.deep.equal( [ 2, 4 ] );
  741. items.add( { value: 5, skip: false }, 2 );
  742. expect( Array.from( collection, item => item.value ) ).to.deep.equal( [ 2, 5, 4 ] );
  743. } );
  744. } );
  745. describe( 'property name', () => {
  746. it( 'creates a binding', () => {
  747. collection.bindTo( items ).using( 'prop' );
  748. expect( collection ).to.have.length( 0 );
  749. items.add( { prop: { value: 'foo' } } );
  750. items.add( { prop: { value: 'bar' } } );
  751. expect( collection ).to.have.length( 2 );
  752. expect( collection.get( 0 ).value ).to.equal( 'foo' );
  753. expect( collection.get( 1 ).value ).to.equal( 'bar' );
  754. } );
  755. it( 'creates a binding (item removal)', () => {
  756. collection.bindTo( items ).using( 'prop' );
  757. expect( collection ).to.have.length( 0 );
  758. items.add( { prop: { value: 'foo' } } );
  759. items.add( { prop: { value: 'bar' } } );
  760. expect( collection ).to.have.length( 2 );
  761. expect( collection.get( 0 ).value ).to.equal( 'foo' );
  762. expect( collection.get( 1 ).value ).to.equal( 'bar' );
  763. items.remove( 1 );
  764. expect( collection ).to.have.length( 1 );
  765. expect( collection.get( 0 ).value ).to.equal( 'foo' );
  766. items.remove( 0 );
  767. expect( collection ).to.have.length( 0 );
  768. } );
  769. it( 'skips when there is no item', () => {
  770. items.add( { prop: null } );
  771. collection.bindTo( items ).using( 'prop' );
  772. // Still 0 because initial item was skipped.
  773. expect( collection ).to.have.length( 0 );
  774. items.add( { prop: { value: 2, skip: false } } );
  775. items.add( { prop: null } );
  776. items.add( { prop: { value: 4, skip: false } } );
  777. expect( Array.from( collection, item => item.value ) ).to.deep.equal( [ 2, 4 ] );
  778. items.add( { prop: { value: 5 } }, 2 );
  779. expect( Array.from( collection, item => item.value ) ).to.deep.equal( [ 2, 5, 4 ] );
  780. } );
  781. } );
  782. } );
  783. describe( 'two–way data binding', () => {
  784. it( 'works with custom factories (1)', () => {
  785. const collectionA = new Collection();
  786. const collectionB = new Collection();
  787. const spyA = sinon.spy();
  788. const spyB = sinon.spy();
  789. collectionA.on( 'add', spyA );
  790. collectionB.on( 'add', spyB );
  791. // A<--->B
  792. collectionA.bindTo( collectionB ).using( i => ( { v: i.v * 2 } ) );
  793. collectionB.bindTo( collectionA ).using( i => ( { v: i.v / 2 } ) );
  794. assertItems( collectionA, [] );
  795. assertItems( collectionB, [] );
  796. collectionA.add( { v: 4 } );
  797. collectionA.add( { v: 6 } );
  798. assertItems( collectionA, [ 4, 6 ] );
  799. assertItems( collectionB, [ 2, 3 ] );
  800. collectionB.add( { v: 4 } );
  801. assertItems( collectionA, [ 4, 6, 8 ] );
  802. assertItems( collectionB, [ 2, 3, 4 ] );
  803. sinon.assert.callCount( spyA, 3 );
  804. sinon.assert.callCount( spyB, 3 );
  805. } );
  806. it( 'works with custom factories (2)', () => {
  807. const collectionA = new Collection();
  808. const collectionB = new Collection();
  809. const spyA = sinon.spy();
  810. const spyB = sinon.spy();
  811. collectionA.on( 'add', spyA );
  812. collectionB.on( 'add', spyB );
  813. // A<--->B
  814. collectionA.bindTo( collectionB ).using( 'data' );
  815. collectionB.bindTo( collectionA ).using( i => new FactoryClass( i ) );
  816. collectionA.add( { v: 4 } );
  817. collectionA.add( { v: 6 } );
  818. expect( [ ...collectionB ].every( i => i instanceof FactoryClass ) ).to.be.true;
  819. expect( [ ...collectionB ].map( i => i.data ) ).to.deep.equal( [ ...collectionA ] );
  820. expect( collectionB.map( i => i.data.v ) ).to.deep.equal( [ 4, 6 ] );
  821. expect( collectionA.map( i => i.v ) ).to.deep.equal( [ 4, 6 ] );
  822. collectionB.add( new FactoryClass( { v: 8 } ) );
  823. expect( [ ...collectionB ].every( i => i instanceof FactoryClass ) ).to.be.true;
  824. expect( [ ...collectionB ].map( i => i.data ) ).to.deep.equal( [ ...collectionA ] );
  825. expect( collectionB.map( i => i.data.v ) ).to.deep.equal( [ 4, 6, 8 ] );
  826. expect( collectionA.map( i => i.v ) ).to.deep.equal( [ 4, 6, 8 ] );
  827. } );
  828. it( 'works with custom factories (custom index)', () => {
  829. const collectionA = new Collection();
  830. const collectionB = new Collection();
  831. const spyA = sinon.spy();
  832. const spyB = sinon.spy();
  833. collectionA.on( 'add', spyA );
  834. collectionB.on( 'add', spyB );
  835. // A<--->B
  836. collectionA.bindTo( collectionB ).using( i => ( { v: i.v * 2 } ) );
  837. collectionB.bindTo( collectionA ).using( i => ( { v: i.v / 2 } ) );
  838. assertItems( collectionA, [] );
  839. assertItems( collectionB, [] );
  840. collectionA.add( { v: 4 } );
  841. collectionA.add( { v: 6 }, 0 );
  842. assertItems( collectionA, [ 6, 4 ] );
  843. assertItems( collectionB, [ 3, 2 ] );
  844. collectionB.add( { v: 4 }, 1 );
  845. assertItems( collectionA, [ 6, 8, 4 ] );
  846. assertItems( collectionB, [ 3, 4, 2 ] );
  847. sinon.assert.callCount( spyA, 3 );
  848. sinon.assert.callCount( spyB, 3 );
  849. } );
  850. it( 'works with 1:1 binding', () => {
  851. const collectionA = new Collection();
  852. const collectionB = new Collection();
  853. const spyA = sinon.spy();
  854. const spyB = sinon.spy();
  855. collectionA.on( 'add', spyA );
  856. collectionB.on( 'add', spyB );
  857. // A<--->B
  858. collectionA.bindTo( collectionB ).using( i => i );
  859. collectionB.bindTo( collectionA ).using( i => i );
  860. assertItems( collectionA, [], [] );
  861. assertItems( collectionB, [], [] );
  862. collectionA.add( { v: 4 } );
  863. collectionA.add( { v: 6 } );
  864. assertItems( collectionA, [ 4, 6 ] );
  865. assertItems( collectionB, [ 4, 6 ] );
  866. collectionB.add( { v: 8 } );
  867. assertItems( collectionA, [ 4, 6, 8 ] );
  868. assertItems( collectionB, [ 4, 6, 8 ] );
  869. expect( [ ...collectionA ] ).to.deep.equal( [ ...collectionB ] );
  870. sinon.assert.callCount( spyA, 3 );
  871. sinon.assert.callCount( spyB, 3 );
  872. } );
  873. it( 'works with double chaining', () => {
  874. const collectionA = new Collection();
  875. const collectionB = new Collection();
  876. const collectionC = new Collection();
  877. const spyA = sinon.spy();
  878. const spyB = sinon.spy();
  879. const spyC = sinon.spy();
  880. collectionA.on( 'add', spyA );
  881. collectionB.on( 'add', spyB );
  882. collectionC.on( 'add', spyC );
  883. // A<--->B--->C
  884. collectionA.bindTo( collectionB ).using( i => ( { v: i.v * 2 } ) );
  885. collectionB.bindTo( collectionA ).using( i => ( { v: i.v / 2 } ) );
  886. collectionC.bindTo( collectionB ).using( i => ( { v: -i.v } ) );
  887. assertItems( collectionA, [] );
  888. assertItems( collectionB, [] );
  889. assertItems( collectionC, [] );
  890. collectionA.add( { v: 4 } );
  891. collectionA.add( { v: 6 } );
  892. assertItems( collectionA, [ 4, 6 ] );
  893. assertItems( collectionB, [ 2, 3 ] );
  894. assertItems( collectionC, [ -2, -3 ] );
  895. collectionB.add( { v: 4 } );
  896. assertItems( collectionA, [ 4, 6, 8 ] );
  897. assertItems( collectionB, [ 2, 3, 4 ] );
  898. assertItems( collectionC, [ -2, -3, -4 ] );
  899. collectionC.add( { v: -1000 } );
  900. assertItems( collectionA, [ 4, 6, 8 ] );
  901. assertItems( collectionB, [ 2, 3, 4 ] );
  902. assertItems( collectionC, [ -2, -3, -4, -1000 ] );
  903. sinon.assert.callCount( spyA, 3 );
  904. sinon.assert.callCount( spyB, 3 );
  905. sinon.assert.callCount( spyC, 4 );
  906. } );
  907. it( 'removes items correctly', () => {
  908. const collectionA = new Collection();
  909. const collectionB = new Collection();
  910. const spyAddA = sinon.spy();
  911. const spyAddB = sinon.spy();
  912. const spyRemoveA = sinon.spy();
  913. const spyRemoveB = sinon.spy();
  914. collectionA.on( 'add', spyAddA );
  915. collectionB.on( 'add', spyAddB );
  916. collectionA.on( 'remove', spyRemoveA );
  917. collectionB.on( 'remove', spyRemoveB );
  918. // A<--->B
  919. collectionA.bindTo( collectionB ).using( i => ( { v: i.v * 2 } ) );
  920. collectionB.bindTo( collectionA ).using( i => ( { v: i.v / 2 } ) );
  921. assertItems( collectionA, [], [] );
  922. assertItems( collectionB, [], [] );
  923. collectionA.add( { v: 4 } );
  924. collectionA.add( { v: 6 } );
  925. assertItems( collectionA, [ 4, 6 ] );
  926. assertItems( collectionB, [ 2, 3 ] );
  927. collectionB.add( { v: 4 } );
  928. assertItems( collectionA, [ 4, 6, 8 ] );
  929. assertItems( collectionB, [ 2, 3, 4 ] );
  930. collectionB.remove( 0 );
  931. assertItems( collectionA, [ 6, 8 ] );
  932. assertItems( collectionB, [ 3, 4 ] );
  933. sinon.assert.callCount( spyAddA, 3 );
  934. sinon.assert.callCount( spyAddB, 3 );
  935. sinon.assert.callCount( spyRemoveA, 1 );
  936. sinon.assert.callCount( spyRemoveB, 1 );
  937. collectionA.remove( 1 );
  938. assertItems( collectionA, [ 6 ] );
  939. assertItems( collectionB, [ 3 ] );
  940. sinon.assert.callCount( spyAddA, 3 );
  941. sinon.assert.callCount( spyAddB, 3 );
  942. sinon.assert.callCount( spyRemoveA, 2 );
  943. sinon.assert.callCount( spyRemoveB, 2 );
  944. } );
  945. describe( 'skipping items', () => {
  946. let collectionA, collectionB;
  947. beforeEach( () => {
  948. collectionA = new Collection();
  949. collectionB = new Collection();
  950. // A<--->B
  951. collectionA.bindTo( collectionB ).using( item => {
  952. if ( item.skip ) {
  953. return null;
  954. }
  955. return item;
  956. } );
  957. collectionB.bindTo( collectionA ).using( item => {
  958. if ( item.skip ) {
  959. return null;
  960. }
  961. return item;
  962. } );
  963. } );
  964. it( 'should add items at the enf of collections when includes skipped items', () => {
  965. collectionA.add( { v: 'A' } );
  966. collectionA.add( { v: 'B', skip: true } );
  967. collectionA.add( { v: 'C', skip: true } );
  968. collectionA.add( { v: 'D' } );
  969. expect( collectionA._skippedIndexesFromExternal ).to.have.members( [] );
  970. expect( collectionB._skippedIndexesFromExternal ).to.have.members( [ 1, 2 ] );
  971. assertItems( collectionA, [ 'A', 'B', 'C', 'D' ] );
  972. assertItems( collectionB, [ 'A', 'D' ] );
  973. collectionB.add( { v: 'E' } );
  974. collectionB.add( { v: 'F', skip: true } );
  975. collectionB.add( { v: 'G' } );
  976. expect( collectionA._skippedIndexesFromExternal ).to.have.members( [ 3 ] );
  977. expect( collectionB._skippedIndexesFromExternal ).to.have.members( [ 1, 2 ] );
  978. assertItems( collectionA, [ 'A', 'B', 'C', 'D', 'E', 'G' ] );
  979. assertItems( collectionB, [ 'A', 'D', 'E', 'F', 'G' ] );
  980. } );
  981. it( 'should add items between skipped items', () => {
  982. collectionA.add( { v: 'A' } );
  983. collectionA.add( { v: 'B', skip: true } );
  984. collectionA.add( { v: 'C', skip: true } );
  985. collectionA.add( { v: 'D' } );
  986. collectionB.add( { v: 'E' } );
  987. collectionB.add( { v: 'F', skip: true } );
  988. collectionB.add( { v: 'G', skip: true } );
  989. collectionB.add( { v: 'H' } );
  990. expect( collectionA._skippedIndexesFromExternal ).to.have.members( [ 3, 4 ] );
  991. expect( collectionB._skippedIndexesFromExternal ).to.have.members( [ 1, 2 ] );
  992. assertItems( collectionA, [ 'A', 'B', 'C', 'D', 'E', 'H' ] );
  993. assertItems( collectionB, [ 'A', 'D', 'E', 'F', 'G', 'H' ] );
  994. collectionA.add( { v: 'I' }, 2 );
  995. expect( collectionA._skippedIndexesFromExternal ).to.have.members( [ 4, 5 ] );
  996. expect( collectionB._skippedIndexesFromExternal ).to.have.members( [ 1, 2 ] );
  997. assertItems( collectionA, [ 'A', 'B', 'I', 'C', 'D', 'E', 'H' ] );
  998. assertItems( collectionB, [ 'A', 'I', 'D', 'E', 'F', 'G', 'H' ] );
  999. collectionB.add( { v: 'J' }, 5 );
  1000. expect( collectionA._skippedIndexesFromExternal ).to.have.members( [ 4, 5 ] );
  1001. expect( collectionB._skippedIndexesFromExternal ).to.have.members( [ 1, 2 ] );
  1002. assertItems( collectionA, [ 'A', 'B', 'I', 'C', 'D', 'E', 'J', 'H' ] );
  1003. assertItems( collectionB, [ 'A', 'I', 'D', 'E', 'F', 'J', 'G', 'H' ] );
  1004. } );
  1005. it( 'should properly remove skipped items and update skipped indexes', () => {
  1006. collectionA.add( { v: 'A' } );
  1007. collectionA.add( { v: 'B', skip: true } );
  1008. collectionA.add( { v: 'C', skip: true } );
  1009. collectionA.add( { v: 'D' } );
  1010. collectionB.add( { v: 'E' } );
  1011. collectionB.add( { v: 'F', skip: true } );
  1012. collectionB.add( { v: 'G', skip: true } );
  1013. collectionB.add( { v: 'H' } );
  1014. expect( collectionA._skippedIndexesFromExternal ).to.have.members( [ 3, 4 ] );
  1015. expect( collectionB._skippedIndexesFromExternal ).to.have.members( [ 1, 2 ] );
  1016. assertItems( collectionA, [ 'A', 'B', 'C', 'D', 'E', 'H' ] );
  1017. assertItems( collectionB, [ 'A', 'D', 'E', 'F', 'G', 'H' ] );
  1018. collectionA.remove( 2 );
  1019. expect( collectionA._skippedIndexesFromExternal ).to.have.members( [ 3, 4 ] );
  1020. expect( collectionB._skippedIndexesFromExternal ).to.have.members( [ 1 ] );
  1021. assertItems( collectionA, [ 'A', 'B', 'D', 'E', 'H' ] );
  1022. assertItems( collectionB, [ 'A', 'D', 'E', 'F', 'G', 'H' ] );
  1023. collectionB.remove( 3 );
  1024. expect( collectionA._skippedIndexesFromExternal ).to.have.members( [ 3 ] );
  1025. expect( collectionB._skippedIndexesFromExternal ).to.have.members( [ 1 ] );
  1026. assertItems( collectionA, [ 'A', 'B', 'D', 'E', 'H' ] );
  1027. assertItems( collectionB, [ 'A', 'D', 'E', 'G', 'H' ] );
  1028. } );
  1029. } );
  1030. } );
  1031. } );
  1032. describe( 'iterator', () => {
  1033. it( 'covers the whole collection', () => {
  1034. const item1 = getItem( 'foo' );
  1035. const item2 = getItem( 'bar' );
  1036. const item3 = getItem( 'bom' );
  1037. const items = [];
  1038. collection.add( item1 );
  1039. collection.add( item2 );
  1040. collection.add( item3 );
  1041. for ( const item of collection ) {
  1042. items.push( item.id );
  1043. }
  1044. expect( items ).to.deep.equal( [ 'foo', 'bar', 'bom' ] );
  1045. } );
  1046. } );
  1047. } );