collection.js 35 KB

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