collection.js 38 KB

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