collection.js 36 KB

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