collection.js 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397
  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. expect( spy.callCount, 'call count' ).to.equal( 1 );
  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 throw an error on invalid index', () => {
  474. collection.add( getItem( 'foo' ) );
  475. expectToThrowCKEditorError( () => {
  476. collection.remove( 1 );
  477. }, /^collection-remove-404/ );
  478. expect( collection ).to.have.length( 1 );
  479. } );
  480. it( 'should throw an error on invalid id', () => {
  481. collection.add( getItem( 'foo' ) );
  482. expectToThrowCKEditorError( () => {
  483. collection.remove( 'bar' );
  484. }, /^collection-remove-404/ );
  485. expect( collection ).to.have.length( 1 );
  486. } );
  487. it( 'should throw an error on invalid model', () => {
  488. collection.add( getItem( 'foo' ) );
  489. expectToThrowCKEditorError( () => {
  490. collection.remove( getItem( 'bar' ) );
  491. }, /^collection-remove-404/ );
  492. expect( collection ).to.have.length( 1 );
  493. } );
  494. } );
  495. describe( 'map()', () => {
  496. it( 'uses native map', () => {
  497. const spy = testUtils.sinon.stub( Array.prototype, 'map' ).returns( [ 'foo' ] );
  498. const ctx = {};
  499. const ret = collection.map( callback, ctx );
  500. sinon.assert.calledWithExactly( spy, callback, ctx );
  501. expect( ret ).to.deep.equal( [ 'foo' ], 'ret value was forwarded' );
  502. function callback() {}
  503. } );
  504. } );
  505. describe( 'find()', () => {
  506. it( 'uses native find', () => {
  507. const needl = getItem( 'foo' );
  508. const spy = testUtils.sinon.stub( Array.prototype, 'find' ).returns( needl );
  509. const ctx = {};
  510. const ret = collection.find( callback, ctx );
  511. sinon.assert.calledWithExactly( spy, callback, ctx );
  512. expect( ret ).to.equal( needl, 'ret value was forwarded' );
  513. function callback() {}
  514. } );
  515. } );
  516. describe( 'filter()', () => {
  517. it( 'uses native filter', () => {
  518. const needl = getItem( 'foo' );
  519. // See: https://github.com/sinonjs/sinon/issues/1521
  520. const spy = testUtils.sinon.stub( collection._items, 'filter' ).returns( [ needl ] );
  521. const ctx = {};
  522. const ret = collection.filter( callback, ctx );
  523. sinon.assert.calledWithExactly( spy, callback, ctx );
  524. expect( ret ).to.deep.equal( [ needl ], 'ret value was forwarded' );
  525. function callback() {}
  526. } );
  527. } );
  528. describe( 'clear()', () => {
  529. it( 'removes all items', () => {
  530. const items = [ {}, {}, {} ];
  531. const spy = sinon.spy();
  532. collection.on( 'remove', spy );
  533. items.forEach( i => collection.add( i ) );
  534. collection.clear();
  535. expect( spy.callCount ).to.equal( 3 );
  536. expect( collection.length ).to.equal( 0 );
  537. } );
  538. it( 'breaks the binding', () => {
  539. const external = new Collection();
  540. collection.bindTo( external ).using( i => i );
  541. external.add( { foo: 'bar' } );
  542. expect( collection ).to.have.length( 1 );
  543. collection.clear();
  544. external.add( { foo: 'baz' } );
  545. expect( collection ).to.have.length( 0 );
  546. external.remove( 0 );
  547. expect( collection ).to.have.length( 0 );
  548. expect( collection._bindToCollection ).to.be.null;
  549. } );
  550. } );
  551. describe( 'bindTo()', () => {
  552. class FactoryClass {
  553. constructor( data ) {
  554. this.data = data;
  555. }
  556. }
  557. function assertItems( collection, expectedItems ) {
  558. expect( collection.map( i => i.v ) ).to.deep.equal( expectedItems );
  559. }
  560. it( 'throws when binding more than once', () => {
  561. collection.bindTo( {} );
  562. expectToThrowCKEditorError( () => {
  563. collection.bindTo( {} );
  564. }, /^collection-bind-to-rebind/ );
  565. } );
  566. it( 'provides "using()" and "as()" interfaces', () => {
  567. const returned = collection.bindTo( {} );
  568. expect( returned ).to.have.keys( 'using', 'as' );
  569. expect( returned.using ).to.be.a( 'function' );
  570. expect( returned.as ).to.be.a( 'function' );
  571. } );
  572. it( 'stores reference to bound collection', () => {
  573. const collectionB = new Collection();
  574. expect( collection._bindToCollection ).to.be.undefined;
  575. expect( collectionB._bindToCollection ).to.be.undefined;
  576. collection.bindTo( collectionB ).as( FactoryClass );
  577. expect( collection._bindToCollection ).to.equal( collectionB );
  578. expect( collectionB._bindToCollection ).to.be.undefined;
  579. } );
  580. describe( 'as()', () => {
  581. let items;
  582. beforeEach( () => {
  583. items = new Collection();
  584. } );
  585. it( 'does not chain', () => {
  586. const returned = collection.bindTo( new Collection() ).as( FactoryClass );
  587. expect( returned ).to.be.undefined;
  588. } );
  589. it( 'creates a binding (initial content)', () => {
  590. items.add( { id: '1' } );
  591. items.add( { id: '2' } );
  592. collection.bindTo( items ).as( FactoryClass );
  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 (new content)', () => {
  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. } );
  608. it( 'creates a binding (item removal)', () => {
  609. collection.bindTo( items ).as( FactoryClass );
  610. expect( collection ).to.have.length( 0 );
  611. items.add( { id: '1' } );
  612. items.add( { id: '2' } );
  613. expect( collection ).to.have.length( 2 );
  614. expect( collection.get( 0 ) ).to.be.instanceOf( FactoryClass );
  615. expect( collection.get( 1 ) ).to.be.instanceOf( FactoryClass );
  616. expect( collection.get( 1 ).data ).to.equal( items.get( 1 ) );
  617. items.remove( 1 );
  618. expect( collection.get( 0 ).data ).to.equal( items.get( 0 ) );
  619. items.remove( 0 );
  620. expect( collection ).to.have.length( 0 );
  621. } );
  622. } );
  623. describe( 'using()', () => {
  624. let items;
  625. beforeEach( () => {
  626. items = new Collection();
  627. } );
  628. it( 'does not chain', () => {
  629. const returned = collection.bindTo( new Collection() ).using( () => {} );
  630. expect( returned ).to.be.undefined;
  631. } );
  632. describe( 'callback', () => {
  633. it( 'creates a binding (arrow function)', () => {
  634. collection.bindTo( items ).using( item => {
  635. return new FactoryClass( item );
  636. } );
  637. expect( collection ).to.have.length( 0 );
  638. items.add( { id: '1' } );
  639. items.add( { id: '2' } );
  640. expect( collection ).to.have.length( 2 );
  641. expect( collection.get( 0 ) ).to.be.instanceOf( FactoryClass );
  642. expect( collection.get( 1 ) ).to.be.instanceOf( FactoryClass );
  643. expect( collection.get( 1 ).data ).to.equal( items.get( 1 ) );
  644. } );
  645. // https://github.com/ckeditor/ckeditor5-ui/issues/113
  646. it( 'creates a binding (normal function)', () => {
  647. collection.bindTo( items ).using( function( item ) {
  648. return new FactoryClass( item );
  649. } );
  650. items.add( { id: '1' } );
  651. expect( collection ).to.have.length( 1 );
  652. const view = collection.get( 0 );
  653. // Wrong args will be passed to the callback if it's treated as the view constructor.
  654. expect( view ).to.be.instanceOf( FactoryClass );
  655. expect( view.data ).to.equal( items.get( 0 ) );
  656. } );
  657. it( 'creates a 1:1 binding', () => {
  658. collection.bindTo( items ).using( item => item );
  659. expect( collection ).to.have.length( 0 );
  660. const item1 = { id: '100' };
  661. const item2 = { id: '200' };
  662. items.add( item1 );
  663. items.add( item2 );
  664. expect( collection ).to.have.length( 2 );
  665. expect( collection.get( 0 ) ).to.equal( item1 );
  666. expect( collection.get( 1 ) ).to.equal( item2 );
  667. } );
  668. it( 'creates a conditional binding', () => {
  669. class CustomClass {
  670. constructor( data ) {
  671. this.data = data;
  672. }
  673. }
  674. collection.bindTo( items ).using( item => {
  675. if ( item.id == 'FactoryClass' ) {
  676. return new FactoryClass( item );
  677. } else {
  678. return new CustomClass( item );
  679. }
  680. } );
  681. expect( collection ).to.have.length( 0 );
  682. const item1 = { id: 'FactoryClass' };
  683. const item2 = { id: 'CustomClass' };
  684. items.add( item1 );
  685. items.add( item2 );
  686. expect( collection ).to.have.length( 2 );
  687. expect( collection.get( 0 ) ).to.be.instanceOf( FactoryClass );
  688. expect( collection.get( 1 ) ).to.be.instanceOf( CustomClass );
  689. } );
  690. it( 'creates a binding to a property name', () => {
  691. collection.bindTo( items ).using( item => item.prop );
  692. expect( collection ).to.have.length( 0 );
  693. items.add( { prop: { value: 'foo' } } );
  694. items.add( { prop: { value: 'bar' } } );
  695. expect( collection ).to.have.length( 2 );
  696. expect( collection.get( 0 ).value ).to.equal( 'foo' );
  697. expect( collection.get( 1 ).value ).to.equal( 'bar' );
  698. } );
  699. it( 'skips when there is no item', () => {
  700. // Add before collection is bound.
  701. items.add( { value: 1, skip: true } );
  702. expect( collection ).to.have.length( 0 );
  703. collection.bindTo( items ).using( item => {
  704. if ( item.skip ) {
  705. return null;
  706. }
  707. return item;
  708. } );
  709. // Still 0 because initial item was skipped.
  710. expect( collection ).to.have.length( 0 );
  711. items.add( { value: 2, skip: false } );
  712. items.add( { value: 3, skip: true } );
  713. items.add( { value: 4, skip: false } );
  714. expect( Array.from( collection, item => item.value ) ).to.deep.equal( [ 2, 4 ] );
  715. items.add( { value: 5, skip: false }, 2 );
  716. expect( Array.from( collection, item => item.value ) ).to.deep.equal( [ 2, 5, 4 ] );
  717. } );
  718. } );
  719. describe( 'property name', () => {
  720. it( 'creates a binding', () => {
  721. collection.bindTo( items ).using( 'prop' );
  722. expect( collection ).to.have.length( 0 );
  723. items.add( { prop: { value: 'foo' } } );
  724. items.add( { prop: { value: 'bar' } } );
  725. expect( collection ).to.have.length( 2 );
  726. expect( collection.get( 0 ).value ).to.equal( 'foo' );
  727. expect( collection.get( 1 ).value ).to.equal( 'bar' );
  728. } );
  729. it( 'creates a binding (item removal)', () => {
  730. collection.bindTo( items ).using( 'prop' );
  731. expect( collection ).to.have.length( 0 );
  732. items.add( { prop: { value: 'foo' } } );
  733. items.add( { prop: { value: 'bar' } } );
  734. expect( collection ).to.have.length( 2 );
  735. expect( collection.get( 0 ).value ).to.equal( 'foo' );
  736. expect( collection.get( 1 ).value ).to.equal( 'bar' );
  737. items.remove( 1 );
  738. expect( collection ).to.have.length( 1 );
  739. expect( collection.get( 0 ).value ).to.equal( 'foo' );
  740. items.remove( 0 );
  741. expect( collection ).to.have.length( 0 );
  742. } );
  743. it( 'skips when there is no item', () => {
  744. items.add( { prop: null } );
  745. collection.bindTo( items ).using( 'prop' );
  746. // Still 0 because initial item was skipped.
  747. expect( collection ).to.have.length( 0 );
  748. items.add( { prop: { value: 2, skip: false } } );
  749. items.add( { prop: null } );
  750. items.add( { prop: { value: 4, skip: false } } );
  751. expect( Array.from( collection, item => item.value ) ).to.deep.equal( [ 2, 4 ] );
  752. items.add( { prop: { value: 5 } }, 2 );
  753. expect( Array.from( collection, item => item.value ) ).to.deep.equal( [ 2, 5, 4 ] );
  754. } );
  755. } );
  756. } );
  757. describe( 'two–way data binding', () => {
  758. it( 'works with custom factories (1)', () => {
  759. const collectionA = new Collection();
  760. const collectionB = new Collection();
  761. const spyA = sinon.spy();
  762. const spyB = sinon.spy();
  763. collectionA.on( 'add', spyA );
  764. collectionB.on( 'add', spyB );
  765. // A<--->B
  766. collectionA.bindTo( collectionB ).using( i => ( { v: i.v * 2 } ) );
  767. collectionB.bindTo( collectionA ).using( i => ( { v: i.v / 2 } ) );
  768. assertItems( collectionA, [] );
  769. assertItems( collectionB, [] );
  770. collectionA.add( { v: 4 } );
  771. collectionA.add( { v: 6 } );
  772. assertItems( collectionA, [ 4, 6 ] );
  773. assertItems( collectionB, [ 2, 3 ] );
  774. collectionB.add( { v: 4 } );
  775. assertItems( collectionA, [ 4, 6, 8 ] );
  776. assertItems( collectionB, [ 2, 3, 4 ] );
  777. sinon.assert.callCount( spyA, 3 );
  778. sinon.assert.callCount( spyB, 3 );
  779. } );
  780. it( 'works with custom factories (2)', () => {
  781. const collectionA = new Collection();
  782. const collectionB = new Collection();
  783. const spyA = sinon.spy();
  784. const spyB = sinon.spy();
  785. collectionA.on( 'add', spyA );
  786. collectionB.on( 'add', spyB );
  787. // A<--->B
  788. collectionA.bindTo( collectionB ).using( 'data' );
  789. collectionB.bindTo( collectionA ).using( i => new FactoryClass( i ) );
  790. collectionA.add( { v: 4 } );
  791. collectionA.add( { v: 6 } );
  792. expect( [ ...collectionB ].every( i => i instanceof FactoryClass ) ).to.be.true;
  793. expect( [ ...collectionB ].map( i => i.data ) ).to.deep.equal( [ ...collectionA ] );
  794. expect( collectionB.map( i => i.data.v ) ).to.deep.equal( [ 4, 6 ] );
  795. expect( collectionA.map( i => i.v ) ).to.deep.equal( [ 4, 6 ] );
  796. collectionB.add( new FactoryClass( { v: 8 } ) );
  797. expect( [ ...collectionB ].every( i => i instanceof FactoryClass ) ).to.be.true;
  798. expect( [ ...collectionB ].map( i => i.data ) ).to.deep.equal( [ ...collectionA ] );
  799. expect( collectionB.map( i => i.data.v ) ).to.deep.equal( [ 4, 6, 8 ] );
  800. expect( collectionA.map( i => i.v ) ).to.deep.equal( [ 4, 6, 8 ] );
  801. } );
  802. it( 'works with custom factories (custom index)', () => {
  803. const collectionA = new Collection();
  804. const collectionB = new Collection();
  805. const spyA = sinon.spy();
  806. const spyB = sinon.spy();
  807. collectionA.on( 'add', spyA );
  808. collectionB.on( 'add', spyB );
  809. // A<--->B
  810. collectionA.bindTo( collectionB ).using( i => ( { v: i.v * 2 } ) );
  811. collectionB.bindTo( collectionA ).using( i => ( { v: i.v / 2 } ) );
  812. assertItems( collectionA, [] );
  813. assertItems( collectionB, [] );
  814. collectionA.add( { v: 4 } );
  815. collectionA.add( { v: 6 }, 0 );
  816. assertItems( collectionA, [ 6, 4 ] );
  817. assertItems( collectionB, [ 3, 2 ] );
  818. collectionB.add( { v: 4 }, 1 );
  819. assertItems( collectionA, [ 6, 8, 4 ] );
  820. assertItems( collectionB, [ 3, 4, 2 ] );
  821. sinon.assert.callCount( spyA, 3 );
  822. sinon.assert.callCount( spyB, 3 );
  823. } );
  824. it( 'works with 1:1 binding', () => {
  825. const collectionA = new Collection();
  826. const collectionB = new Collection();
  827. const spyA = sinon.spy();
  828. const spyB = sinon.spy();
  829. collectionA.on( 'add', spyA );
  830. collectionB.on( 'add', spyB );
  831. // A<--->B
  832. collectionA.bindTo( collectionB ).using( i => i );
  833. collectionB.bindTo( collectionA ).using( i => i );
  834. assertItems( collectionA, [], [] );
  835. assertItems( collectionB, [], [] );
  836. collectionA.add( { v: 4 } );
  837. collectionA.add( { v: 6 } );
  838. assertItems( collectionA, [ 4, 6 ] );
  839. assertItems( collectionB, [ 4, 6 ] );
  840. collectionB.add( { v: 8 } );
  841. assertItems( collectionA, [ 4, 6, 8 ] );
  842. assertItems( collectionB, [ 4, 6, 8 ] );
  843. expect( [ ...collectionA ] ).to.deep.equal( [ ...collectionB ] );
  844. sinon.assert.callCount( spyA, 3 );
  845. sinon.assert.callCount( spyB, 3 );
  846. } );
  847. it( 'works with double chaining', () => {
  848. const collectionA = new Collection();
  849. const collectionB = new Collection();
  850. const collectionC = new Collection();
  851. const spyA = sinon.spy();
  852. const spyB = sinon.spy();
  853. const spyC = sinon.spy();
  854. collectionA.on( 'add', spyA );
  855. collectionB.on( 'add', spyB );
  856. collectionC.on( 'add', spyC );
  857. // A<--->B--->C
  858. collectionA.bindTo( collectionB ).using( i => ( { v: i.v * 2 } ) );
  859. collectionB.bindTo( collectionA ).using( i => ( { v: i.v / 2 } ) );
  860. collectionC.bindTo( collectionB ).using( i => ( { v: -i.v } ) );
  861. assertItems( collectionA, [] );
  862. assertItems( collectionB, [] );
  863. assertItems( collectionC, [] );
  864. collectionA.add( { v: 4 } );
  865. collectionA.add( { v: 6 } );
  866. assertItems( collectionA, [ 4, 6 ] );
  867. assertItems( collectionB, [ 2, 3 ] );
  868. assertItems( collectionC, [ -2, -3 ] );
  869. collectionB.add( { v: 4 } );
  870. assertItems( collectionA, [ 4, 6, 8 ] );
  871. assertItems( collectionB, [ 2, 3, 4 ] );
  872. assertItems( collectionC, [ -2, -3, -4 ] );
  873. collectionC.add( { v: -1000 } );
  874. assertItems( collectionA, [ 4, 6, 8 ] );
  875. assertItems( collectionB, [ 2, 3, 4 ] );
  876. assertItems( collectionC, [ -2, -3, -4, -1000 ] );
  877. sinon.assert.callCount( spyA, 3 );
  878. sinon.assert.callCount( spyB, 3 );
  879. sinon.assert.callCount( spyC, 4 );
  880. } );
  881. it( 'removes items correctly', () => {
  882. const collectionA = new Collection();
  883. const collectionB = new Collection();
  884. const spyAddA = sinon.spy();
  885. const spyAddB = sinon.spy();
  886. const spyRemoveA = sinon.spy();
  887. const spyRemoveB = sinon.spy();
  888. collectionA.on( 'add', spyAddA );
  889. collectionB.on( 'add', spyAddB );
  890. collectionA.on( 'remove', spyRemoveA );
  891. collectionB.on( 'remove', spyRemoveB );
  892. // A<--->B
  893. collectionA.bindTo( collectionB ).using( i => ( { v: i.v * 2 } ) );
  894. collectionB.bindTo( collectionA ).using( i => ( { v: i.v / 2 } ) );
  895. assertItems( collectionA, [], [] );
  896. assertItems( collectionB, [], [] );
  897. collectionA.add( { v: 4 } );
  898. collectionA.add( { v: 6 } );
  899. assertItems( collectionA, [ 4, 6 ] );
  900. assertItems( collectionB, [ 2, 3 ] );
  901. collectionB.add( { v: 4 } );
  902. assertItems( collectionA, [ 4, 6, 8 ] );
  903. assertItems( collectionB, [ 2, 3, 4 ] );
  904. collectionB.remove( 0 );
  905. assertItems( collectionA, [ 6, 8 ] );
  906. assertItems( collectionB, [ 3, 4 ] );
  907. sinon.assert.callCount( spyAddA, 3 );
  908. sinon.assert.callCount( spyAddB, 3 );
  909. sinon.assert.callCount( spyRemoveA, 1 );
  910. sinon.assert.callCount( spyRemoveB, 1 );
  911. collectionA.remove( 1 );
  912. assertItems( collectionA, [ 6 ] );
  913. assertItems( collectionB, [ 3 ] );
  914. sinon.assert.callCount( spyAddA, 3 );
  915. sinon.assert.callCount( spyAddB, 3 );
  916. sinon.assert.callCount( spyRemoveA, 2 );
  917. sinon.assert.callCount( spyRemoveB, 2 );
  918. } );
  919. describe( 'skipping items', () => {
  920. let collectionA, collectionB;
  921. beforeEach( () => {
  922. collectionA = new Collection();
  923. collectionB = new Collection();
  924. // A<--->B
  925. collectionA.bindTo( collectionB ).using( item => {
  926. if ( item.skip ) {
  927. return null;
  928. }
  929. return item;
  930. } );
  931. collectionB.bindTo( collectionA ).using( item => {
  932. if ( item.skip ) {
  933. return null;
  934. }
  935. return item;
  936. } );
  937. } );
  938. it( 'should add items at the enf of collections when includes skipped items', () => {
  939. collectionA.add( { v: 'A' } );
  940. collectionA.add( { v: 'B', skip: true } );
  941. collectionA.add( { v: 'C', skip: true } );
  942. collectionA.add( { v: 'D' } );
  943. expect( collectionA._skippedIndexesFromExternal ).to.have.members( [] );
  944. expect( collectionB._skippedIndexesFromExternal ).to.have.members( [ 1, 2 ] );
  945. assertItems( collectionA, [ 'A', 'B', 'C', 'D' ] );
  946. assertItems( collectionB, [ 'A', 'D' ] );
  947. collectionB.add( { v: 'E' } );
  948. collectionB.add( { v: 'F', skip: true } );
  949. collectionB.add( { v: 'G' } );
  950. expect( collectionA._skippedIndexesFromExternal ).to.have.members( [ 3 ] );
  951. expect( collectionB._skippedIndexesFromExternal ).to.have.members( [ 1, 2 ] );
  952. assertItems( collectionA, [ 'A', 'B', 'C', 'D', 'E', 'G' ] );
  953. assertItems( collectionB, [ 'A', 'D', 'E', 'F', 'G' ] );
  954. } );
  955. it( 'should add items between skipped items', () => {
  956. collectionA.add( { v: 'A' } );
  957. collectionA.add( { v: 'B', skip: true } );
  958. collectionA.add( { v: 'C', skip: true } );
  959. collectionA.add( { v: 'D' } );
  960. collectionB.add( { v: 'E' } );
  961. collectionB.add( { v: 'F', skip: true } );
  962. collectionB.add( { v: 'G', skip: true } );
  963. collectionB.add( { v: 'H' } );
  964. expect( collectionA._skippedIndexesFromExternal ).to.have.members( [ 3, 4 ] );
  965. expect( collectionB._skippedIndexesFromExternal ).to.have.members( [ 1, 2 ] );
  966. assertItems( collectionA, [ 'A', 'B', 'C', 'D', 'E', 'H' ] );
  967. assertItems( collectionB, [ 'A', 'D', 'E', 'F', 'G', 'H' ] );
  968. collectionA.add( { v: 'I' }, 2 );
  969. expect( collectionA._skippedIndexesFromExternal ).to.have.members( [ 4, 5 ] );
  970. expect( collectionB._skippedIndexesFromExternal ).to.have.members( [ 1, 2 ] );
  971. assertItems( collectionA, [ 'A', 'B', 'I', 'C', 'D', 'E', 'H' ] );
  972. assertItems( collectionB, [ 'A', 'I', 'D', 'E', 'F', 'G', 'H' ] );
  973. collectionB.add( { v: 'J' }, 5 );
  974. expect( collectionA._skippedIndexesFromExternal ).to.have.members( [ 4, 5 ] );
  975. expect( collectionB._skippedIndexesFromExternal ).to.have.members( [ 1, 2 ] );
  976. assertItems( collectionA, [ 'A', 'B', 'I', 'C', 'D', 'E', 'J', 'H' ] );
  977. assertItems( collectionB, [ 'A', 'I', 'D', 'E', 'F', 'J', 'G', 'H' ] );
  978. } );
  979. it( 'should properly remove skipped items and update skipped indexes', () => {
  980. collectionA.add( { v: 'A' } );
  981. collectionA.add( { v: 'B', skip: true } );
  982. collectionA.add( { v: 'C', skip: true } );
  983. collectionA.add( { v: 'D' } );
  984. collectionB.add( { v: 'E' } );
  985. collectionB.add( { v: 'F', skip: true } );
  986. collectionB.add( { v: 'G', skip: true } );
  987. collectionB.add( { v: 'H' } );
  988. expect( collectionA._skippedIndexesFromExternal ).to.have.members( [ 3, 4 ] );
  989. expect( collectionB._skippedIndexesFromExternal ).to.have.members( [ 1, 2 ] );
  990. assertItems( collectionA, [ 'A', 'B', 'C', 'D', 'E', 'H' ] );
  991. assertItems( collectionB, [ 'A', 'D', 'E', 'F', 'G', 'H' ] );
  992. collectionA.remove( 2 );
  993. expect( collectionA._skippedIndexesFromExternal ).to.have.members( [ 3, 4 ] );
  994. expect( collectionB._skippedIndexesFromExternal ).to.have.members( [ 1 ] );
  995. assertItems( collectionA, [ 'A', 'B', 'D', 'E', 'H' ] );
  996. assertItems( collectionB, [ 'A', 'D', 'E', 'F', 'G', 'H' ] );
  997. collectionB.remove( 3 );
  998. expect( collectionA._skippedIndexesFromExternal ).to.have.members( [ 3 ] );
  999. expect( collectionB._skippedIndexesFromExternal ).to.have.members( [ 1 ] );
  1000. assertItems( collectionA, [ 'A', 'B', 'D', 'E', 'H' ] );
  1001. assertItems( collectionB, [ 'A', 'D', 'E', 'G', 'H' ] );
  1002. } );
  1003. } );
  1004. } );
  1005. } );
  1006. describe( 'iterator', () => {
  1007. it( 'covers the whole collection', () => {
  1008. const item1 = getItem( 'foo' );
  1009. const item2 = getItem( 'bar' );
  1010. const item3 = getItem( 'bom' );
  1011. const items = [];
  1012. collection.add( item1 );
  1013. collection.add( item2 );
  1014. collection.add( item3 );
  1015. for ( const item of collection ) {
  1016. items.push( item.id );
  1017. }
  1018. expect( items ).to.deep.equal( [ 'foo', 'bar', 'bom' ] );
  1019. } );
  1020. } );
  1021. } );