collection.js 36 KB

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