collection.js 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611
  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 be chainable', () => {
  115. expect( collection.add( {} ) ).to.equal( collection );
  116. } );
  117. it( 'should change the length', () => {
  118. expect( collection ).to.have.length( 0 );
  119. collection.add( {} );
  120. expect( collection ).to.have.length( 1 );
  121. collection.add( {} );
  122. expect( collection ).to.have.length( 2 );
  123. } );
  124. it( 'should enable get( index )', () => {
  125. const item1 = {};
  126. const item2 = {};
  127. collection.add( item1 );
  128. expect( collection.get( 0 ) ).to.equal( item1 );
  129. collection.add( item2 );
  130. expect( collection.get( 0 ) ).to.equal( item1 );
  131. expect( collection.get( 1 ) ).to.equal( item2 );
  132. } );
  133. it( 'should enable get( id )', () => {
  134. const item1 = getItem( 'foo' );
  135. const item2 = getItem( 'bar' );
  136. collection.add( item1 );
  137. collection.add( item2 );
  138. expect( collection.get( 'foo' ) ).to.equal( item1 );
  139. expect( collection.get( 'bar' ) ).to.equal( item2 );
  140. } );
  141. it( 'should enable get( id ) - custom id property', () => {
  142. const collection = new Collection( { idProperty: 'name' } );
  143. const item1 = getItem( 'foo', 'name' );
  144. const item2 = getItem( 'bar', 'name' );
  145. collection.add( item1 );
  146. collection.add( item2 );
  147. expect( collection.get( 'foo' ) ).to.equal( item1 );
  148. expect( collection.get( 'bar' ) ).to.equal( item2 );
  149. } );
  150. it( 'should generate an id when not defined', () => {
  151. const item = {};
  152. collection.add( item );
  153. expect( item.id ).to.be.a( 'string' );
  154. expect( collection.get( item.id ) ).to.equal( item );
  155. } );
  156. it( 'should generate an id when not defined - custom id property', () => {
  157. const collection = new Collection( { idProperty: 'name' } );
  158. const item = {};
  159. collection.add( item );
  160. expect( item.name ).to.be.a( 'string' );
  161. expect( collection.get( item.name ) ).to.equal( item );
  162. } );
  163. it( 'should not change an existing id of an item', () => {
  164. const item = getItem( 'foo' );
  165. collection.add( item );
  166. expect( item.id ).to.equal( 'foo' );
  167. } );
  168. it( 'should throw when item with this id already exists', () => {
  169. const item1 = getItem( 'foo' );
  170. const item2 = getItem( 'foo' );
  171. collection.add( item1 );
  172. expectToThrowCKEditorError( () => {
  173. collection.add( item2 );
  174. }, /^collection-add-item-already-exists/ );
  175. } );
  176. it( 'should throw when item\'s id is not a string', () => {
  177. const item = { id: 1 };
  178. expectToThrowCKEditorError( () => {
  179. collection.add( item );
  180. }, /^collection-add-invalid-id/ );
  181. } );
  182. it(
  183. 'should generate an id when not defined, which is globally unique ' +
  184. 'so it is possible to move items between collections and avoid id collisions',
  185. () => {
  186. const collectionA = new Collection();
  187. const collectionB = new Collection();
  188. const itemA = {};
  189. const itemB = {};
  190. collectionA.add( itemA );
  191. collectionB.add( itemB );
  192. collectionB.add( collectionA.remove( itemA ) );
  193. expect( collectionA.length ).to.equal( 0 );
  194. expect( collectionB.length ).to.equal( 2 );
  195. expect( collectionB.get( 0 ) ).to.equal( itemB );
  196. expect( collectionB.get( 1 ) ).to.equal( itemA );
  197. expect( itemA.id ).to.not.equal( itemB.id );
  198. }
  199. );
  200. it(
  201. 'should generate an id when not defined, which is globally unique ' +
  202. 'so it is possible to move items between collections and avoid id collisions ' +
  203. '– custom id property',
  204. () => {
  205. const collectionA = new Collection( { idProperty: 'foo' } );
  206. const collectionB = new Collection( { idProperty: 'foo' } );
  207. const itemA = {};
  208. const itemB = {};
  209. collectionA.add( itemA );
  210. collectionB.add( itemB );
  211. collectionB.add( collectionA.remove( itemA ) );
  212. expect( collectionA.length ).to.equal( 0 );
  213. expect( collectionB.length ).to.equal( 2 );
  214. expect( collectionB.get( 0 ) ).to.equal( itemB );
  215. expect( collectionB.get( 1 ) ).to.equal( itemA );
  216. expect( itemA.foo ).to.not.equal( itemB.foo );
  217. }
  218. );
  219. it( 'should allow an item which is already in some other collection', () => {
  220. const collectionA = new Collection();
  221. const collectionB = new Collection();
  222. const item = {};
  223. collectionA.add( item );
  224. collectionB.add( item );
  225. expect( collectionA.length ).to.equal( 1 );
  226. expect( collectionB.length ).to.equal( 1 );
  227. expect( collectionA.get( item.id ) ).to.equal( collectionB.get( 0 ) );
  228. } );
  229. it( 'should fire the "add" event', () => {
  230. const spy = sinon.spy();
  231. const item = {};
  232. collection.on( 'add', spy );
  233. collection.add( item );
  234. sinon.assert.calledWithExactly( spy, sinon.match.has( 'source', collection ), item, 0 );
  235. } );
  236. it( 'should support an optional index argument', () => {
  237. const item1 = getItem( 'foo' );
  238. const item2 = getItem( 'bar' );
  239. const item3 = getItem( 'baz' );
  240. const item4 = getItem( 'abc' );
  241. collection.add( item1 );
  242. collection.add( item2, 0 );
  243. collection.add( item3, 1 );
  244. collection.add( item4, 3 );
  245. expect( collection.get( 0 ) ).to.equal( item2 );
  246. expect( collection.get( 1 ) ).to.equal( item3 );
  247. expect( collection.get( 2 ) ).to.equal( item1 );
  248. expect( collection.get( 3 ) ).to.equal( item4 );
  249. } );
  250. it( 'should throw when index argument is invalid', () => {
  251. const item1 = getItem( 'foo' );
  252. const item2 = getItem( 'bar' );
  253. const item3 = getItem( 'baz' );
  254. collection.add( item1 );
  255. expectToThrowCKEditorError( () => {
  256. collection.add( item2, -1 );
  257. }, /^collection-add-item-invalid-index/ );
  258. expectToThrowCKEditorError( () => {
  259. collection.add( item2, 2 );
  260. }, /^collection-add-item-invalid-index/ );
  261. collection.add( item2, 1 );
  262. collection.add( item3, 0 );
  263. expect( collection.length ).to.equal( 3 );
  264. } );
  265. it( 'should fire the "add" event with the index argument', () => {
  266. const spy = sinon.spy();
  267. collection.add( {} );
  268. collection.add( {} );
  269. collection.on( 'add', spy );
  270. const item = {};
  271. collection.add( item, 1 );
  272. sinon.assert.calledWithExactly( spy, sinon.match.has( 'source', collection ), item, 1 );
  273. } );
  274. } );
  275. describe( 'addMany()', () => {
  276. it( 'should be chainable', () => {
  277. expect( collection.addMany( [ {} ] ) ).to.equal( collection );
  278. } );
  279. it( 'should change the length', () => {
  280. expect( collection ).to.have.length( 0 );
  281. collection.addMany( [ {}, {} ] );
  282. expect( collection ).to.have.length( 2 );
  283. collection.addMany( [ {} ] );
  284. expect( collection ).to.have.length( 3 );
  285. } );
  286. it( 'should enable get( index )', () => {
  287. const item1 = {};
  288. const item2 = {};
  289. collection.addMany( [ item1, item2 ] );
  290. expect( collection.get( 0 ) ).to.equal( item1 );
  291. expect( collection.get( 1 ) ).to.equal( item2 );
  292. } );
  293. it( 'should enable get( id )', () => {
  294. const item1 = getItem( 'foo' );
  295. const item2 = getItem( 'bar' );
  296. collection.addMany( [ item1, item2 ] );
  297. expect( collection.get( 'foo' ) ).to.equal( item1 );
  298. expect( collection.get( 'bar' ) ).to.equal( item2 );
  299. } );
  300. it( 'should enable get( id ) - custom id property', () => {
  301. const collection = new Collection( { idProperty: 'name' } );
  302. const item1 = getItem( 'foo', 'name' );
  303. const item2 = getItem( 'bar', 'name' );
  304. collection.add( item1 );
  305. collection.add( item2 );
  306. expect( collection.get( 'foo' ) ).to.equal( item1 );
  307. expect( collection.get( 'bar' ) ).to.equal( item2 );
  308. } );
  309. it( 'should generate an id when not defined', () => {
  310. const item = {};
  311. collection.addMany( [ item ] );
  312. expect( item.id ).to.be.a( 'string' );
  313. expect( collection.get( item.id ) ).to.equal( item );
  314. } );
  315. it( 'should generate an id when not defined - custom id property', () => {
  316. const collection = new Collection( { idProperty: 'name' } );
  317. const item = {};
  318. collection.addMany( [ item ] );
  319. expect( item.name ).to.be.a( 'string' );
  320. expect( collection.get( item.name ) ).to.equal( item );
  321. } );
  322. it( 'should not change an existing id of an item', () => {
  323. const item = getItem( 'foo' );
  324. collection.addMany( [ item ] );
  325. expect( item.id ).to.equal( 'foo' );
  326. } );
  327. it( 'should throw when item with this id already exists - single call', () => {
  328. const item1 = getItem( 'foo' );
  329. expectToThrowCKEditorError( () => {
  330. collection.addMany( [ item1, item1 ] );
  331. }, /^collection-add-item-already-exists/ );
  332. } );
  333. it( 'should throw when item with this id already exists - multiple calls', () => {
  334. const item1 = getItem( 'foo' );
  335. const item2 = getItem( 'foo' );
  336. collection.addMany( [ item1 ] );
  337. expectToThrowCKEditorError( () => {
  338. collection.addMany( [ item2 ] );
  339. }, /^collection-add-item-already-exists/ );
  340. } );
  341. it( 'should throw when item\'s id is not a string', () => {
  342. const item = { id: 1 };
  343. expectToThrowCKEditorError( () => {
  344. collection.addMany( [ item ] );
  345. }, /^collection-add-invalid-id/ );
  346. } );
  347. it(
  348. 'should generate an id when not defined, which is globally unique ' +
  349. 'so it is possible to move items between collections and avoid id collisions',
  350. () => {
  351. const collectionA = new Collection();
  352. const collectionB = new Collection();
  353. const itemA = {};
  354. const itemB = {};
  355. collectionA.addMany( [ itemA ] );
  356. collectionB.addMany( [ itemB ] );
  357. collectionB.addMany( [ collectionA.remove( itemA ) ] );
  358. expect( collectionA.length ).to.equal( 0 );
  359. expect( collectionB.length ).to.equal( 2 );
  360. expect( collectionB.get( 0 ) ).to.equal( itemB );
  361. expect( collectionB.get( 1 ) ).to.equal( itemA );
  362. expect( itemA.id ).to.not.equal( itemB.id );
  363. }
  364. );
  365. it(
  366. 'should generate an id when not defined, which is globally unique ' +
  367. 'so it is possible to move items between collections and avoid id collisions ' +
  368. '– custom id property',
  369. () => {
  370. const collectionA = new Collection( { idProperty: 'foo' } );
  371. const collectionB = new Collection( { idProperty: 'foo' } );
  372. const itemA = {};
  373. const itemB = {};
  374. collectionA.addMany( [ itemA ] );
  375. collectionB.addMany( [ itemB ] );
  376. collectionB.addMany( [ collectionA.remove( itemA ) ] );
  377. expect( collectionA.length ).to.equal( 0 );
  378. expect( collectionB.length ).to.equal( 2 );
  379. expect( collectionB.get( 0 ) ).to.equal( itemB );
  380. expect( collectionB.get( 1 ) ).to.equal( itemA );
  381. expect( itemA.foo ).to.not.equal( itemB.foo );
  382. }
  383. );
  384. it( 'should allow an item which is already in some other collection', () => {
  385. const collectionA = new Collection();
  386. const collectionB = new Collection();
  387. const item = {};
  388. collectionA.addMany( [ item ] );
  389. collectionB.addMany( [ item ] );
  390. expect( collectionA.length ).to.equal( 1 );
  391. expect( collectionB.length ).to.equal( 1 );
  392. expect( collectionA.get( item.id ) ).to.equal( collectionB.get( 0 ) );
  393. } );
  394. it( 'should fire the "add" event', () => {
  395. const spy = sinon.spy();
  396. const item = {};
  397. collection.on( 'add', spy );
  398. collection.addMany( [ item ] );
  399. sinon.assert.calledWithExactly( spy, sinon.match.has( 'source', collection ), item, 0 );
  400. } );
  401. it( 'should fire the "add" event for each item', () => {
  402. const spy = sinon.spy();
  403. const items = [ {}, {} ];
  404. collection.on( 'add', spy );
  405. collection.addMany( items );
  406. sinon.assert.calledWithExactly( spy, sinon.match.has( 'source', collection ), items[ 0 ], 0 );
  407. sinon.assert.calledWithExactly( spy, sinon.match.has( 'source', collection ), items[ 1 ], 1 );
  408. expect( spy.callCount ).to.equal( 2 );
  409. } );
  410. it( 'should fire the "add" event with the index argument', () => {
  411. const spy = sinon.spy();
  412. collection.addMany( [ {} ] );
  413. collection.addMany( [ {} ] );
  414. collection.on( 'add', spy );
  415. const item = {};
  416. collection.addMany( [ item ], 1 );
  417. sinon.assert.calledWithExactly( spy, sinon.match.has( 'source', collection ), item, 1 );
  418. } );
  419. it( 'should fire the "change" event', () => {
  420. const spy = sinon.spy();
  421. const items = [ {}, {} ];
  422. collection.on( 'change', spy );
  423. collection.addMany( items );
  424. sinon.assert.calledOnce( spy );
  425. expect( spy.args[ 0 ][ 1 ] ).to.deep.eql( {
  426. added: items,
  427. removed: [],
  428. index: 0
  429. } );
  430. } );
  431. it( 'should fire the "change" event with the index argument', () => {
  432. const spy = sinon.spy();
  433. const firstBatch = [ {}, {} ];
  434. const secondBatch = [ {}, {} ];
  435. collection.addMany( firstBatch );
  436. collection.on( 'change', spy );
  437. collection.addMany( secondBatch, 1 );
  438. expect( spy.callCount, 'call count' ).to.equal( 1 );
  439. expect( spy.args[ 0 ][ 1 ] ).to.deep.eql( {
  440. added: secondBatch,
  441. removed: [],
  442. index: 1
  443. } );
  444. } );
  445. it( 'should support an optional index argument', () => {
  446. const item1 = getItem( 'foo' );
  447. const item2 = getItem( 'bar' );
  448. const item3 = getItem( 'baz' );
  449. const item4 = getItem( 'abc' );
  450. collection.addMany( [ item1 ] );
  451. collection.addMany( [ item2 ], 0 );
  452. collection.addMany( [ item3 ], 1 );
  453. collection.addMany( [ item4 ], 3 );
  454. expect( collection.get( 0 ) ).to.equal( item2 );
  455. expect( collection.get( 1 ) ).to.equal( item3 );
  456. expect( collection.get( 2 ) ).to.equal( item1 );
  457. expect( collection.get( 3 ) ).to.equal( item4 );
  458. } );
  459. it( 'should throw when index argument is invalid', () => {
  460. const item1 = getItem( 'foo' );
  461. const item2 = getItem( 'bar' );
  462. const item3 = getItem( 'baz' );
  463. collection.addMany( [ item1 ] );
  464. expectToThrowCKEditorError( () => {
  465. collection.addMany( [ item2 ], -1 );
  466. }, /^collection-add-item-invalid-index/ );
  467. expectToThrowCKEditorError( () => {
  468. collection.addMany( [ item2 ], 2 );
  469. }, /^collection-add-item-invalid-index/ );
  470. collection.addMany( [ item2 ], 1 );
  471. collection.addMany( [ item3 ], 0 );
  472. expect( collection.length ).to.equal( 3 );
  473. } );
  474. } );
  475. describe( 'get()', () => {
  476. it( 'should return an item', () => {
  477. const item = getItem( 'foo' );
  478. collection.add( item );
  479. expect( collection.get( 'foo' ) ).to.equal( item );
  480. } );
  481. it( 'should return null if id does not exist', () => {
  482. collection.add( getItem( 'foo' ) );
  483. expect( collection.get( 'bar' ) ).to.be.null;
  484. } );
  485. it( 'should throw if neither string or number given', () => {
  486. expectToThrowCKEditorError( () => {
  487. collection.get( true );
  488. }, /^collection-get-invalid-arg/ );
  489. } );
  490. } );
  491. describe( 'has()', () => {
  492. it( 'should return true if collection contains item with given id', () => {
  493. collection.add( getItem( 'foo' ) );
  494. expect( collection.has( 'foo' ) ).to.equal( true );
  495. } );
  496. it( 'should return false if collection does not contain item with given id', () => {
  497. collection.add( getItem( 'foo' ) );
  498. expect( collection.has( 'bar' ) ).to.equal( false );
  499. } );
  500. it( 'should return true if collection contains item', () => {
  501. const item = getItem( 'foo' );
  502. collection.add( item );
  503. expect( collection.has( item ) ).to.equal( true );
  504. } );
  505. it( 'should return false if collection does not contains item', () => {
  506. collection.add( getItem( 'foo' ) );
  507. expect( collection.has( getItem( 'bar' ) ) ).to.equal( false );
  508. } );
  509. } );
  510. describe( 'getIndex()', () => {
  511. it( 'should return index of given item', () => {
  512. const item1 = { foo: 'bar' };
  513. const item2 = { bar: 'biz' };
  514. const item3 = { foo: 'biz' };
  515. collection.add( item1 );
  516. collection.add( item2 );
  517. collection.add( item3 );
  518. expect( collection.getIndex( item1 ) ).to.equal( 0 );
  519. expect( collection.getIndex( item2 ) ).to.equal( 1 );
  520. expect( collection.getIndex( item3 ) ).to.equal( 2 );
  521. } );
  522. it( 'should return index of item with given id', () => {
  523. collection.add( { id: 'id1' } );
  524. collection.add( { id: 'id2' } );
  525. collection.add( { id: 'id3' } );
  526. expect( collection.getIndex( 'id1' ) ).to.equal( 0 );
  527. expect( collection.getIndex( 'id2' ) ).to.equal( 1 );
  528. expect( collection.getIndex( 'id3' ) ).to.equal( 2 );
  529. } );
  530. it( 'should return index equal to -1 when given item is not defined in the collection', () => {
  531. const item1 = { foo: 'bar' };
  532. expect( collection.getIndex( item1 ) ).to.equal( -1 );
  533. } );
  534. it( 'should return index equal to -1 when item of given id is not defined in the collection', () => {
  535. expect( collection.getIndex( 'id1' ) ).to.equal( -1 );
  536. } );
  537. } );
  538. describe( 'remove()', () => {
  539. it( 'should remove the model by index', () => {
  540. collection.add( getItem( 'bom' ) );
  541. collection.add( getItem( 'foo' ) );
  542. collection.add( getItem( 'bar' ) );
  543. expect( collection ).to.have.length( 3 );
  544. const removedItem = collection.remove( 1 );
  545. expect( collection ).to.have.length( 2 );
  546. expect( collection.get( 'foo' ) ).to.be.null;
  547. expect( collection.get( 1 ) ).to.have.property( 'id', 'bar' );
  548. expect( removedItem ).to.have.property( 'id', 'foo' );
  549. } );
  550. it( 'should remove the model by index - custom id property', () => {
  551. const collection = new Collection( { idProperty: 'name' } );
  552. collection.add( getItem( 'foo', 'name' ) );
  553. const removedItem = collection.remove( 0 );
  554. expect( collection ).to.have.length( 0 );
  555. expect( collection.get( 'foo' ) ).to.be.null;
  556. expect( removedItem ).to.have.property( 'name', 'foo' );
  557. } );
  558. it( 'should remove the model by id', () => {
  559. collection.add( getItem( 'bom' ) );
  560. collection.add( getItem( 'foo' ) );
  561. collection.add( getItem( 'bar' ) );
  562. expect( collection ).to.have.length( 3 );
  563. const removedItem = collection.remove( 'foo' );
  564. expect( collection ).to.have.length( 2 );
  565. expect( collection.get( 'foo' ) ).to.be.null;
  566. expect( collection.get( 1 ) ).to.have.property( 'id', 'bar' );
  567. expect( removedItem ).to.have.property( 'id', 'foo' );
  568. } );
  569. it( 'should remove the model by model', () => {
  570. const item = getItem( 'foo' );
  571. collection.add( getItem( 'bom' ) );
  572. collection.add( item );
  573. collection.add( getItem( 'bar' ) );
  574. expect( collection ).to.have.length( 3 );
  575. const removedItem = collection.remove( item );
  576. expect( collection ).to.have.length( 2 );
  577. expect( collection.get( 'foo' ) ).to.be.null;
  578. expect( collection.get( 1 ) ).to.have.property( 'id', 'bar' );
  579. expect( removedItem ).to.equal( item );
  580. } );
  581. it( 'should remove the model by model - custom id property', () => {
  582. const collection = new Collection( { idProperty: 'name' } );
  583. const item = getItem( 'foo', 'name' );
  584. collection.add( item );
  585. const removedItem = collection.remove( item );
  586. expect( collection ).to.have.length( 0 );
  587. expect( collection.get( 'foo' ) ).to.be.null;
  588. expect( removedItem ).to.have.property( 'name', 'foo' );
  589. } );
  590. it( 'should fire the "remove" event', () => {
  591. const item1 = getItem( 'foo' );
  592. const item2 = getItem( 'bar' );
  593. const item3 = getItem( 'bom' );
  594. collection.add( item1 );
  595. collection.add( item2 );
  596. collection.add( item3 );
  597. const spy = sinon.spy();
  598. collection.on( 'remove', spy );
  599. collection.remove( 1 ); // by index
  600. collection.remove( item1 ); // by model
  601. collection.remove( 'bom' ); // by id
  602. sinon.assert.calledThrice( spy );
  603. sinon.assert.calledWithExactly( spy, sinon.match.has( 'source', collection ), item1, 0 );
  604. sinon.assert.calledWithExactly( spy, sinon.match.has( 'source', collection ), item2, 1 );
  605. sinon.assert.calledWithExactly( spy, sinon.match.has( 'source', collection ), item3, 0 );
  606. } );
  607. it( 'should fire the "change" event', () => {
  608. const item = getItem( 'foo' );
  609. const spy = sinon.spy();
  610. collection.add( item );
  611. collection.on( 'change', spy );
  612. collection.remove( item );
  613. sinon.assert.calledOnce( spy );
  614. expect( spy.args[ 0 ][ 1 ] ).to.deep.eql( {
  615. added: [],
  616. removed: [ item ],
  617. index: 0
  618. } );
  619. } );
  620. it( 'should throw an error on invalid index', () => {
  621. collection.add( getItem( 'foo' ) );
  622. expectToThrowCKEditorError( () => {
  623. collection.remove( 1 );
  624. }, /^collection-remove-404/ );
  625. expect( collection ).to.have.length( 1 );
  626. } );
  627. it( 'should throw an error on invalid id', () => {
  628. collection.add( getItem( 'foo' ) );
  629. expectToThrowCKEditorError( () => {
  630. collection.remove( 'bar' );
  631. }, /^collection-remove-404/ );
  632. expect( collection ).to.have.length( 1 );
  633. } );
  634. it( 'should throw an error on invalid model', () => {
  635. collection.add( getItem( 'foo' ) );
  636. expectToThrowCKEditorError( () => {
  637. collection.remove( getItem( 'bar' ) );
  638. }, /^collection-remove-404/ );
  639. expect( collection ).to.have.length( 1 );
  640. } );
  641. } );
  642. describe( 'map()', () => {
  643. it( 'uses native map', () => {
  644. const spy = testUtils.sinon.stub( Array.prototype, 'map' ).returns( [ 'foo' ] );
  645. const ctx = {};
  646. const ret = collection.map( callback, ctx );
  647. sinon.assert.calledWithExactly( spy, callback, ctx );
  648. expect( ret ).to.deep.equal( [ 'foo' ], 'ret value was forwarded' );
  649. function callback() {}
  650. } );
  651. } );
  652. describe( 'find()', () => {
  653. it( 'uses native find', () => {
  654. const needl = getItem( 'foo' );
  655. const spy = testUtils.sinon.stub( Array.prototype, 'find' ).returns( needl );
  656. const ctx = {};
  657. const ret = collection.find( callback, ctx );
  658. sinon.assert.calledWithExactly( spy, callback, ctx );
  659. expect( ret ).to.equal( needl, 'ret value was forwarded' );
  660. function callback() {}
  661. } );
  662. } );
  663. describe( 'filter()', () => {
  664. it( 'uses native filter', () => {
  665. const needl = getItem( 'foo' );
  666. // See: https://github.com/sinonjs/sinon/issues/1521
  667. const spy = testUtils.sinon.stub( collection._items, 'filter' ).returns( [ needl ] );
  668. const ctx = {};
  669. const ret = collection.filter( callback, ctx );
  670. sinon.assert.calledWithExactly( spy, callback, ctx );
  671. expect( ret ).to.deep.equal( [ needl ], 'ret value was forwarded' );
  672. function callback() {}
  673. } );
  674. } );
  675. describe( 'clear()', () => {
  676. it( 'removes all items', () => {
  677. const items = [ {}, {}, {} ];
  678. const spy = sinon.spy();
  679. collection.on( 'remove', spy );
  680. items.forEach( i => collection.add( i ) );
  681. collection.clear();
  682. expect( spy.callCount ).to.equal( 3 );
  683. expect( collection.length ).to.equal( 0 );
  684. } );
  685. it( 'breaks the binding', () => {
  686. const external = new Collection();
  687. collection.bindTo( external ).using( i => i );
  688. external.add( { foo: 'bar' } );
  689. expect( collection ).to.have.length( 1 );
  690. collection.clear();
  691. external.add( { foo: 'baz' } );
  692. expect( collection ).to.have.length( 0 );
  693. external.remove( 0 );
  694. expect( collection ).to.have.length( 0 );
  695. expect( collection._bindToCollection ).to.be.null;
  696. } );
  697. it( 'should fire the "change" event', () => {
  698. const items = [ {}, {}, {} ];
  699. const spy = sinon.spy();
  700. collection.addMany( items );
  701. collection.on( 'change', spy );
  702. collection.clear();
  703. sinon.assert.calledOnce( spy );
  704. expect( spy.args[ 0 ][ 1 ] ).to.deep.eql( {
  705. added: [],
  706. removed: items,
  707. index: 0
  708. } );
  709. } );
  710. } );
  711. describe( 'bindTo()', () => {
  712. class FactoryClass {
  713. constructor( data ) {
  714. this.data = data;
  715. }
  716. }
  717. function assertItems( collection, expectedItems ) {
  718. expect( collection.map( i => i.v ) ).to.deep.equal( expectedItems );
  719. }
  720. it( 'throws when binding more than once', () => {
  721. collection.bindTo( {} );
  722. expectToThrowCKEditorError( () => {
  723. collection.bindTo( {} );
  724. }, /^collection-bind-to-rebind/ );
  725. } );
  726. it( 'provides "using()" and "as()" interfaces', () => {
  727. const returned = collection.bindTo( {} );
  728. expect( returned ).to.have.keys( 'using', 'as' );
  729. expect( returned.using ).to.be.a( 'function' );
  730. expect( returned.as ).to.be.a( 'function' );
  731. } );
  732. it( 'stores reference to bound collection', () => {
  733. const collectionB = new Collection();
  734. expect( collection._bindToCollection ).to.be.undefined;
  735. expect( collectionB._bindToCollection ).to.be.undefined;
  736. collection.bindTo( collectionB ).as( FactoryClass );
  737. expect( collection._bindToCollection ).to.equal( collectionB );
  738. expect( collectionB._bindToCollection ).to.be.undefined;
  739. } );
  740. describe( 'as()', () => {
  741. let items;
  742. beforeEach( () => {
  743. items = new Collection();
  744. } );
  745. it( 'does not chain', () => {
  746. const returned = collection.bindTo( new Collection() ).as( FactoryClass );
  747. expect( returned ).to.be.undefined;
  748. } );
  749. it( 'creates a binding (initial content)', () => {
  750. items.add( { id: '1' } );
  751. items.add( { id: '2' } );
  752. collection.bindTo( items ).as( FactoryClass );
  753. expect( collection ).to.have.length( 2 );
  754. expect( collection.get( 0 ) ).to.be.instanceOf( FactoryClass );
  755. expect( collection.get( 1 ) ).to.be.instanceOf( FactoryClass );
  756. expect( collection.get( 1 ).data ).to.equal( items.get( 1 ) );
  757. } );
  758. it( 'creates a binding (new content)', () => {
  759. collection.bindTo( items ).as( FactoryClass );
  760. expect( collection ).to.have.length( 0 );
  761. items.add( { id: '1' } );
  762. items.add( { id: '2' } );
  763. expect( collection ).to.have.length( 2 );
  764. expect( collection.get( 0 ) ).to.be.instanceOf( FactoryClass );
  765. expect( collection.get( 1 ) ).to.be.instanceOf( FactoryClass );
  766. expect( collection.get( 1 ).data ).to.equal( items.get( 1 ) );
  767. } );
  768. it( 'creates a binding (item removal)', () => {
  769. collection.bindTo( items ).as( FactoryClass );
  770. expect( collection ).to.have.length( 0 );
  771. items.add( { id: '1' } );
  772. items.add( { id: '2' } );
  773. expect( collection ).to.have.length( 2 );
  774. expect( collection.get( 0 ) ).to.be.instanceOf( FactoryClass );
  775. expect( collection.get( 1 ) ).to.be.instanceOf( FactoryClass );
  776. expect( collection.get( 1 ).data ).to.equal( items.get( 1 ) );
  777. items.remove( 1 );
  778. expect( collection.get( 0 ).data ).to.equal( items.get( 0 ) );
  779. items.remove( 0 );
  780. expect( collection ).to.have.length( 0 );
  781. } );
  782. } );
  783. describe( 'using()', () => {
  784. let items;
  785. beforeEach( () => {
  786. items = new Collection();
  787. } );
  788. it( 'does not chain', () => {
  789. const returned = collection.bindTo( new Collection() ).using( () => {} );
  790. expect( returned ).to.be.undefined;
  791. } );
  792. describe( 'callback', () => {
  793. it( 'creates a binding (arrow function)', () => {
  794. collection.bindTo( items ).using( item => {
  795. return new FactoryClass( item );
  796. } );
  797. expect( collection ).to.have.length( 0 );
  798. items.add( { id: '1' } );
  799. items.add( { id: '2' } );
  800. expect( collection ).to.have.length( 2 );
  801. expect( collection.get( 0 ) ).to.be.instanceOf( FactoryClass );
  802. expect( collection.get( 1 ) ).to.be.instanceOf( FactoryClass );
  803. expect( collection.get( 1 ).data ).to.equal( items.get( 1 ) );
  804. } );
  805. // https://github.com/ckeditor/ckeditor5-ui/issues/113
  806. it( 'creates a binding (normal function)', () => {
  807. collection.bindTo( items ).using( function( item ) {
  808. return new FactoryClass( item );
  809. } );
  810. items.add( { id: '1' } );
  811. expect( collection ).to.have.length( 1 );
  812. const view = collection.get( 0 );
  813. // Wrong args will be passed to the callback if it's treated as the view constructor.
  814. expect( view ).to.be.instanceOf( FactoryClass );
  815. expect( view.data ).to.equal( items.get( 0 ) );
  816. } );
  817. it( 'creates a 1:1 binding', () => {
  818. collection.bindTo( items ).using( item => item );
  819. expect( collection ).to.have.length( 0 );
  820. const item1 = { id: '100' };
  821. const item2 = { id: '200' };
  822. items.add( item1 );
  823. items.add( item2 );
  824. expect( collection ).to.have.length( 2 );
  825. expect( collection.get( 0 ) ).to.equal( item1 );
  826. expect( collection.get( 1 ) ).to.equal( item2 );
  827. } );
  828. it( 'creates a conditional binding', () => {
  829. class CustomClass {
  830. constructor( data ) {
  831. this.data = data;
  832. }
  833. }
  834. collection.bindTo( items ).using( item => {
  835. if ( item.id == 'FactoryClass' ) {
  836. return new FactoryClass( item );
  837. } else {
  838. return new CustomClass( item );
  839. }
  840. } );
  841. expect( collection ).to.have.length( 0 );
  842. const item1 = { id: 'FactoryClass' };
  843. const item2 = { id: 'CustomClass' };
  844. items.add( item1 );
  845. items.add( item2 );
  846. expect( collection ).to.have.length( 2 );
  847. expect( collection.get( 0 ) ).to.be.instanceOf( FactoryClass );
  848. expect( collection.get( 1 ) ).to.be.instanceOf( CustomClass );
  849. } );
  850. it( 'creates a binding to a property name', () => {
  851. collection.bindTo( items ).using( item => item.prop );
  852. expect( collection ).to.have.length( 0 );
  853. items.add( { prop: { value: 'foo' } } );
  854. items.add( { prop: { value: 'bar' } } );
  855. expect( collection ).to.have.length( 2 );
  856. expect( collection.get( 0 ).value ).to.equal( 'foo' );
  857. expect( collection.get( 1 ).value ).to.equal( 'bar' );
  858. } );
  859. it( 'skips when there is no item', () => {
  860. // Add before collection is bound.
  861. items.add( { value: 1, skip: true } );
  862. expect( collection ).to.have.length( 0 );
  863. collection.bindTo( items ).using( item => {
  864. if ( item.skip ) {
  865. return null;
  866. }
  867. return item;
  868. } );
  869. // Still 0 because initial item was skipped.
  870. expect( collection ).to.have.length( 0 );
  871. items.add( { value: 2, skip: false } );
  872. items.add( { value: 3, skip: true } );
  873. items.add( { value: 4, skip: false } );
  874. expect( Array.from( collection, item => item.value ) ).to.deep.equal( [ 2, 4 ] );
  875. items.add( { value: 5, skip: false }, 2 );
  876. expect( Array.from( collection, item => item.value ) ).to.deep.equal( [ 2, 5, 4 ] );
  877. } );
  878. } );
  879. describe( 'property name', () => {
  880. it( 'creates a binding', () => {
  881. collection.bindTo( items ).using( 'prop' );
  882. expect( collection ).to.have.length( 0 );
  883. items.add( { prop: { value: 'foo' } } );
  884. items.add( { prop: { value: 'bar' } } );
  885. expect( collection ).to.have.length( 2 );
  886. expect( collection.get( 0 ).value ).to.equal( 'foo' );
  887. expect( collection.get( 1 ).value ).to.equal( 'bar' );
  888. } );
  889. it( 'creates a binding (item removal)', () => {
  890. collection.bindTo( items ).using( 'prop' );
  891. expect( collection ).to.have.length( 0 );
  892. items.add( { prop: { value: 'foo' } } );
  893. items.add( { prop: { value: 'bar' } } );
  894. expect( collection ).to.have.length( 2 );
  895. expect( collection.get( 0 ).value ).to.equal( 'foo' );
  896. expect( collection.get( 1 ).value ).to.equal( 'bar' );
  897. items.remove( 1 );
  898. expect( collection ).to.have.length( 1 );
  899. expect( collection.get( 0 ).value ).to.equal( 'foo' );
  900. items.remove( 0 );
  901. expect( collection ).to.have.length( 0 );
  902. } );
  903. it( 'skips when there is no item', () => {
  904. items.add( { prop: null } );
  905. collection.bindTo( items ).using( 'prop' );
  906. // Still 0 because initial item was skipped.
  907. expect( collection ).to.have.length( 0 );
  908. items.add( { prop: { value: 2, skip: false } } );
  909. items.add( { prop: null } );
  910. items.add( { prop: { value: 4, skip: false } } );
  911. expect( Array.from( collection, item => item.value ) ).to.deep.equal( [ 2, 4 ] );
  912. items.add( { prop: { value: 5 } }, 2 );
  913. expect( Array.from( collection, item => item.value ) ).to.deep.equal( [ 2, 5, 4 ] );
  914. } );
  915. } );
  916. } );
  917. describe( 'two–way data binding', () => {
  918. it( 'works with custom factories (1)', () => {
  919. const collectionA = new Collection();
  920. const collectionB = new Collection();
  921. const spyA = sinon.spy();
  922. const spyB = sinon.spy();
  923. collectionA.on( 'add', spyA );
  924. collectionB.on( 'add', spyB );
  925. // A<--->B
  926. collectionA.bindTo( collectionB ).using( i => ( { v: i.v * 2 } ) );
  927. collectionB.bindTo( collectionA ).using( i => ( { v: i.v / 2 } ) );
  928. assertItems( collectionA, [] );
  929. assertItems( collectionB, [] );
  930. collectionA.add( { v: 4 } );
  931. collectionA.add( { v: 6 } );
  932. assertItems( collectionA, [ 4, 6 ] );
  933. assertItems( collectionB, [ 2, 3 ] );
  934. collectionB.add( { v: 4 } );
  935. assertItems( collectionA, [ 4, 6, 8 ] );
  936. assertItems( collectionB, [ 2, 3, 4 ] );
  937. sinon.assert.callCount( spyA, 3 );
  938. sinon.assert.callCount( spyB, 3 );
  939. } );
  940. it( 'works with custom factories (2)', () => {
  941. const collectionA = new Collection();
  942. const collectionB = new Collection();
  943. const spyA = sinon.spy();
  944. const spyB = sinon.spy();
  945. collectionA.on( 'add', spyA );
  946. collectionB.on( 'add', spyB );
  947. // A<--->B
  948. collectionA.bindTo( collectionB ).using( 'data' );
  949. collectionB.bindTo( collectionA ).using( i => new FactoryClass( i ) );
  950. collectionA.add( { v: 4 } );
  951. collectionA.add( { v: 6 } );
  952. expect( [ ...collectionB ].every( i => i instanceof FactoryClass ) ).to.be.true;
  953. expect( [ ...collectionB ].map( i => i.data ) ).to.deep.equal( [ ...collectionA ] );
  954. expect( collectionB.map( i => i.data.v ) ).to.deep.equal( [ 4, 6 ] );
  955. expect( collectionA.map( i => i.v ) ).to.deep.equal( [ 4, 6 ] );
  956. collectionB.add( new FactoryClass( { v: 8 } ) );
  957. expect( [ ...collectionB ].every( i => i instanceof FactoryClass ) ).to.be.true;
  958. expect( [ ...collectionB ].map( i => i.data ) ).to.deep.equal( [ ...collectionA ] );
  959. expect( collectionB.map( i => i.data.v ) ).to.deep.equal( [ 4, 6, 8 ] );
  960. expect( collectionA.map( i => i.v ) ).to.deep.equal( [ 4, 6, 8 ] );
  961. } );
  962. it( 'works with custom factories (custom index)', () => {
  963. const collectionA = new Collection();
  964. const collectionB = new Collection();
  965. const spyA = sinon.spy();
  966. const spyB = sinon.spy();
  967. collectionA.on( 'add', spyA );
  968. collectionB.on( 'add', spyB );
  969. // A<--->B
  970. collectionA.bindTo( collectionB ).using( i => ( { v: i.v * 2 } ) );
  971. collectionB.bindTo( collectionA ).using( i => ( { v: i.v / 2 } ) );
  972. assertItems( collectionA, [] );
  973. assertItems( collectionB, [] );
  974. collectionA.add( { v: 4 } );
  975. collectionA.add( { v: 6 }, 0 );
  976. assertItems( collectionA, [ 6, 4 ] );
  977. assertItems( collectionB, [ 3, 2 ] );
  978. collectionB.add( { v: 4 }, 1 );
  979. assertItems( collectionA, [ 6, 8, 4 ] );
  980. assertItems( collectionB, [ 3, 4, 2 ] );
  981. sinon.assert.callCount( spyA, 3 );
  982. sinon.assert.callCount( spyB, 3 );
  983. } );
  984. it( 'works with 1:1 binding', () => {
  985. const collectionA = new Collection();
  986. const collectionB = new Collection();
  987. const spyA = sinon.spy();
  988. const spyB = sinon.spy();
  989. collectionA.on( 'add', spyA );
  990. collectionB.on( 'add', spyB );
  991. // A<--->B
  992. collectionA.bindTo( collectionB ).using( i => i );
  993. collectionB.bindTo( collectionA ).using( i => i );
  994. assertItems( collectionA, [], [] );
  995. assertItems( collectionB, [], [] );
  996. collectionA.add( { v: 4 } );
  997. collectionA.add( { v: 6 } );
  998. assertItems( collectionA, [ 4, 6 ] );
  999. assertItems( collectionB, [ 4, 6 ] );
  1000. collectionB.add( { v: 8 } );
  1001. assertItems( collectionA, [ 4, 6, 8 ] );
  1002. assertItems( collectionB, [ 4, 6, 8 ] );
  1003. expect( [ ...collectionA ] ).to.deep.equal( [ ...collectionB ] );
  1004. sinon.assert.callCount( spyA, 3 );
  1005. sinon.assert.callCount( spyB, 3 );
  1006. } );
  1007. it( 'works with double chaining', () => {
  1008. const collectionA = new Collection();
  1009. const collectionB = new Collection();
  1010. const collectionC = new Collection();
  1011. const spyA = sinon.spy();
  1012. const spyB = sinon.spy();
  1013. const spyC = sinon.spy();
  1014. collectionA.on( 'add', spyA );
  1015. collectionB.on( 'add', spyB );
  1016. collectionC.on( 'add', spyC );
  1017. // A<--->B--->C
  1018. collectionA.bindTo( collectionB ).using( i => ( { v: i.v * 2 } ) );
  1019. collectionB.bindTo( collectionA ).using( i => ( { v: i.v / 2 } ) );
  1020. collectionC.bindTo( collectionB ).using( i => ( { v: -i.v } ) );
  1021. assertItems( collectionA, [] );
  1022. assertItems( collectionB, [] );
  1023. assertItems( collectionC, [] );
  1024. collectionA.add( { v: 4 } );
  1025. collectionA.add( { v: 6 } );
  1026. assertItems( collectionA, [ 4, 6 ] );
  1027. assertItems( collectionB, [ 2, 3 ] );
  1028. assertItems( collectionC, [ -2, -3 ] );
  1029. collectionB.add( { v: 4 } );
  1030. assertItems( collectionA, [ 4, 6, 8 ] );
  1031. assertItems( collectionB, [ 2, 3, 4 ] );
  1032. assertItems( collectionC, [ -2, -3, -4 ] );
  1033. collectionC.add( { v: -1000 } );
  1034. assertItems( collectionA, [ 4, 6, 8 ] );
  1035. assertItems( collectionB, [ 2, 3, 4 ] );
  1036. assertItems( collectionC, [ -2, -3, -4, -1000 ] );
  1037. sinon.assert.callCount( spyA, 3 );
  1038. sinon.assert.callCount( spyB, 3 );
  1039. sinon.assert.callCount( spyC, 4 );
  1040. } );
  1041. it( 'removes items correctly', () => {
  1042. const collectionA = new Collection();
  1043. const collectionB = new Collection();
  1044. const spyAddA = sinon.spy();
  1045. const spyAddB = sinon.spy();
  1046. const spyRemoveA = sinon.spy();
  1047. const spyRemoveB = sinon.spy();
  1048. collectionA.on( 'add', spyAddA );
  1049. collectionB.on( 'add', spyAddB );
  1050. collectionA.on( 'remove', spyRemoveA );
  1051. collectionB.on( 'remove', spyRemoveB );
  1052. // A<--->B
  1053. collectionA.bindTo( collectionB ).using( i => ( { v: i.v * 2 } ) );
  1054. collectionB.bindTo( collectionA ).using( i => ( { v: i.v / 2 } ) );
  1055. assertItems( collectionA, [], [] );
  1056. assertItems( collectionB, [], [] );
  1057. collectionA.add( { v: 4 } );
  1058. collectionA.add( { v: 6 } );
  1059. assertItems( collectionA, [ 4, 6 ] );
  1060. assertItems( collectionB, [ 2, 3 ] );
  1061. collectionB.add( { v: 4 } );
  1062. assertItems( collectionA, [ 4, 6, 8 ] );
  1063. assertItems( collectionB, [ 2, 3, 4 ] );
  1064. collectionB.remove( 0 );
  1065. assertItems( collectionA, [ 6, 8 ] );
  1066. assertItems( collectionB, [ 3, 4 ] );
  1067. sinon.assert.callCount( spyAddA, 3 );
  1068. sinon.assert.callCount( spyAddB, 3 );
  1069. sinon.assert.callCount( spyRemoveA, 1 );
  1070. sinon.assert.callCount( spyRemoveB, 1 );
  1071. collectionA.remove( 1 );
  1072. assertItems( collectionA, [ 6 ] );
  1073. assertItems( collectionB, [ 3 ] );
  1074. sinon.assert.callCount( spyAddA, 3 );
  1075. sinon.assert.callCount( spyAddB, 3 );
  1076. sinon.assert.callCount( spyRemoveA, 2 );
  1077. sinon.assert.callCount( spyRemoveB, 2 );
  1078. } );
  1079. describe( 'skipping items', () => {
  1080. let collectionA, collectionB;
  1081. beforeEach( () => {
  1082. collectionA = new Collection();
  1083. collectionB = new Collection();
  1084. // A<--->B
  1085. collectionA.bindTo( collectionB ).using( item => {
  1086. if ( item.skip ) {
  1087. return null;
  1088. }
  1089. return item;
  1090. } );
  1091. collectionB.bindTo( collectionA ).using( item => {
  1092. if ( item.skip ) {
  1093. return null;
  1094. }
  1095. return item;
  1096. } );
  1097. } );
  1098. it( 'should add items at the enf of collections when includes skipped items', () => {
  1099. collectionA.add( { v: 'A' } );
  1100. collectionA.add( { v: 'B', skip: true } );
  1101. collectionA.add( { v: 'C', skip: true } );
  1102. collectionA.add( { v: 'D' } );
  1103. expect( collectionA._skippedIndexesFromExternal ).to.have.members( [] );
  1104. expect( collectionB._skippedIndexesFromExternal ).to.have.members( [ 1, 2 ] );
  1105. assertItems( collectionA, [ 'A', 'B', 'C', 'D' ] );
  1106. assertItems( collectionB, [ 'A', 'D' ] );
  1107. collectionB.add( { v: 'E' } );
  1108. collectionB.add( { v: 'F', skip: true } );
  1109. collectionB.add( { v: 'G' } );
  1110. expect( collectionA._skippedIndexesFromExternal ).to.have.members( [ 3 ] );
  1111. expect( collectionB._skippedIndexesFromExternal ).to.have.members( [ 1, 2 ] );
  1112. assertItems( collectionA, [ 'A', 'B', 'C', 'D', 'E', 'G' ] );
  1113. assertItems( collectionB, [ 'A', 'D', 'E', 'F', 'G' ] );
  1114. } );
  1115. it( 'should add items between skipped items', () => {
  1116. collectionA.add( { v: 'A' } );
  1117. collectionA.add( { v: 'B', skip: true } );
  1118. collectionA.add( { v: 'C', skip: true } );
  1119. collectionA.add( { v: 'D' } );
  1120. collectionB.add( { v: 'E' } );
  1121. collectionB.add( { v: 'F', skip: true } );
  1122. collectionB.add( { v: 'G', skip: true } );
  1123. collectionB.add( { v: 'H' } );
  1124. expect( collectionA._skippedIndexesFromExternal ).to.have.members( [ 3, 4 ] );
  1125. expect( collectionB._skippedIndexesFromExternal ).to.have.members( [ 1, 2 ] );
  1126. assertItems( collectionA, [ 'A', 'B', 'C', 'D', 'E', 'H' ] );
  1127. assertItems( collectionB, [ 'A', 'D', 'E', 'F', 'G', 'H' ] );
  1128. collectionA.add( { v: 'I' }, 2 );
  1129. expect( collectionA._skippedIndexesFromExternal ).to.have.members( [ 4, 5 ] );
  1130. expect( collectionB._skippedIndexesFromExternal ).to.have.members( [ 1, 2 ] );
  1131. assertItems( collectionA, [ 'A', 'B', 'I', 'C', 'D', 'E', 'H' ] );
  1132. assertItems( collectionB, [ 'A', 'I', 'D', 'E', 'F', 'G', 'H' ] );
  1133. collectionB.add( { v: 'J' }, 5 );
  1134. expect( collectionA._skippedIndexesFromExternal ).to.have.members( [ 4, 5 ] );
  1135. expect( collectionB._skippedIndexesFromExternal ).to.have.members( [ 1, 2 ] );
  1136. assertItems( collectionA, [ 'A', 'B', 'I', 'C', 'D', 'E', 'J', 'H' ] );
  1137. assertItems( collectionB, [ 'A', 'I', 'D', 'E', 'F', 'J', 'G', 'H' ] );
  1138. } );
  1139. it( 'should properly remove skipped items and update skipped indexes', () => {
  1140. collectionA.add( { v: 'A' } );
  1141. collectionA.add( { v: 'B', skip: true } );
  1142. collectionA.add( { v: 'C', skip: true } );
  1143. collectionA.add( { v: 'D' } );
  1144. collectionB.add( { v: 'E' } );
  1145. collectionB.add( { v: 'F', skip: true } );
  1146. collectionB.add( { v: 'G', skip: true } );
  1147. collectionB.add( { v: 'H' } );
  1148. expect( collectionA._skippedIndexesFromExternal ).to.have.members( [ 3, 4 ] );
  1149. expect( collectionB._skippedIndexesFromExternal ).to.have.members( [ 1, 2 ] );
  1150. assertItems( collectionA, [ 'A', 'B', 'C', 'D', 'E', 'H' ] );
  1151. assertItems( collectionB, [ 'A', 'D', 'E', 'F', 'G', 'H' ] );
  1152. collectionA.remove( 2 );
  1153. expect( collectionA._skippedIndexesFromExternal ).to.have.members( [ 3, 4 ] );
  1154. expect( collectionB._skippedIndexesFromExternal ).to.have.members( [ 1 ] );
  1155. assertItems( collectionA, [ 'A', 'B', 'D', 'E', 'H' ] );
  1156. assertItems( collectionB, [ 'A', 'D', 'E', 'F', 'G', 'H' ] );
  1157. collectionB.remove( 3 );
  1158. expect( collectionA._skippedIndexesFromExternal ).to.have.members( [ 3 ] );
  1159. expect( collectionB._skippedIndexesFromExternal ).to.have.members( [ 1 ] );
  1160. assertItems( collectionA, [ 'A', 'B', 'D', 'E', 'H' ] );
  1161. assertItems( collectionB, [ 'A', 'D', 'E', 'G', 'H' ] );
  1162. } );
  1163. } );
  1164. } );
  1165. } );
  1166. describe( 'iterator', () => {
  1167. it( 'covers the whole collection', () => {
  1168. const item1 = getItem( 'foo' );
  1169. const item2 = getItem( 'bar' );
  1170. const item3 = getItem( 'bom' );
  1171. const items = [];
  1172. collection.add( item1 );
  1173. collection.add( item2 );
  1174. collection.add( item3 );
  1175. for ( const item of collection ) {
  1176. items.push( item.id );
  1177. }
  1178. expect( items ).to.deep.equal( [ 'foo', 'bar', 'bom' ] );
  1179. } );
  1180. } );
  1181. } );