8
0

collection.js 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056
  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 );
  335. sinon.assert.calledWithExactly( spy, sinon.match.has( 'source', collection ), item2 );
  336. sinon.assert.calledWithExactly( spy, sinon.match.has( 'source', collection ), item3 );
  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. } );
  565. describe( 'property name', () => {
  566. it( 'creates a binding', () => {
  567. collection.bindTo( items ).using( 'prop' );
  568. expect( collection ).to.have.length( 0 );
  569. items.add( { prop: { value: 'foo' } } );
  570. items.add( { prop: { value: 'bar' } } );
  571. expect( collection ).to.have.length( 2 );
  572. expect( collection.get( 0 ).value ).to.equal( 'foo' );
  573. expect( collection.get( 1 ).value ).to.equal( 'bar' );
  574. } );
  575. it( 'creates a binding (item removal)', () => {
  576. collection.bindTo( items ).using( 'prop' );
  577. expect( collection ).to.have.length( 0 );
  578. items.add( { prop: { value: 'foo' } } );
  579. items.add( { prop: { value: 'bar' } } );
  580. expect( collection ).to.have.length( 2 );
  581. expect( collection.get( 0 ).value ).to.equal( 'foo' );
  582. expect( collection.get( 1 ).value ).to.equal( 'bar' );
  583. items.remove( 1 );
  584. expect( collection ).to.have.length( 1 );
  585. expect( collection.get( 0 ).value ).to.equal( 'foo' );
  586. items.remove( 0 );
  587. expect( collection ).to.have.length( 0 );
  588. } );
  589. } );
  590. } );
  591. describe( 'two–way data binding', () => {
  592. it( 'works with custom factories (1)', () => {
  593. const collectionA = new Collection();
  594. const collectionB = new Collection();
  595. const spyA = sinon.spy();
  596. const spyB = sinon.spy();
  597. collectionA.on( 'add', spyA );
  598. collectionB.on( 'add', spyB );
  599. // A<--->B
  600. collectionA.bindTo( collectionB ).using( i => ( { v: i.v * 2 } ) );
  601. collectionB.bindTo( collectionA ).using( i => ( { v: i.v / 2 } ) );
  602. assertItems( collectionA, [] );
  603. assertItems( collectionB, [] );
  604. collectionA.add( { v: 4 } );
  605. collectionA.add( { v: 6 } );
  606. assertItems( collectionA, [ 4, 6 ] );
  607. assertItems( collectionB, [ 2, 3 ] );
  608. collectionB.add( { v: 4 } );
  609. assertItems( collectionA, [ 4, 6, 8 ] );
  610. assertItems( collectionB, [ 2, 3, 4 ] );
  611. sinon.assert.callCount( spyA, 3 );
  612. sinon.assert.callCount( spyB, 3 );
  613. } );
  614. it( 'works with custom factories (2)', () => {
  615. const collectionA = new Collection();
  616. const collectionB = new Collection();
  617. const spyA = sinon.spy();
  618. const spyB = sinon.spy();
  619. collectionA.on( 'add', spyA );
  620. collectionB.on( 'add', spyB );
  621. // A<--->B
  622. collectionA.bindTo( collectionB ).using( 'data' );
  623. collectionB.bindTo( collectionA ).using( i => new FactoryClass( i ) );
  624. collectionA.add( { v: 4 } );
  625. collectionA.add( { v: 6 } );
  626. expect( [ ...collectionB ].every( i => i instanceof FactoryClass ) ).to.be.true;
  627. expect( [ ...collectionB ].map( i => i.data ) ).to.deep.equal( [ ...collectionA ] );
  628. expect( collectionB.map( i => i.data.v ) ).to.deep.equal( [ 4, 6 ] );
  629. expect( collectionA.map( i => i.v ) ).to.deep.equal( [ 4, 6 ] );
  630. collectionB.add( new FactoryClass( { v: 8 } ) );
  631. expect( [ ...collectionB ].every( i => i instanceof FactoryClass ) ).to.be.true;
  632. expect( [ ...collectionB ].map( i => i.data ) ).to.deep.equal( [ ...collectionA ] );
  633. expect( collectionB.map( i => i.data.v ) ).to.deep.equal( [ 4, 6, 8 ] );
  634. expect( collectionA.map( i => i.v ) ).to.deep.equal( [ 4, 6, 8 ] );
  635. } );
  636. it( 'works with custom factories (custom index)', () => {
  637. const collectionA = new Collection();
  638. const collectionB = new Collection();
  639. const spyA = sinon.spy();
  640. const spyB = sinon.spy();
  641. collectionA.on( 'add', spyA );
  642. collectionB.on( 'add', spyB );
  643. // A<--->B
  644. collectionA.bindTo( collectionB ).using( i => ( { v: i.v * 2 } ) );
  645. collectionB.bindTo( collectionA ).using( i => ( { v: i.v / 2 } ) );
  646. assertItems( collectionA, [] );
  647. assertItems( collectionB, [] );
  648. collectionA.add( { v: 4 } );
  649. collectionA.add( { v: 6 }, 0 );
  650. assertItems( collectionA, [ 6, 4 ] );
  651. assertItems( collectionB, [ 3, 2 ] );
  652. collectionB.add( { v: 4 }, 1 );
  653. assertItems( collectionA, [ 6, 8, 4 ] );
  654. assertItems( collectionB, [ 3, 4, 2 ] );
  655. sinon.assert.callCount( spyA, 3 );
  656. sinon.assert.callCount( spyB, 3 );
  657. } );
  658. it( 'works with 1:1 binding', () => {
  659. const collectionA = new Collection();
  660. const collectionB = new Collection();
  661. const spyA = sinon.spy();
  662. const spyB = sinon.spy();
  663. collectionA.on( 'add', spyA );
  664. collectionB.on( 'add', spyB );
  665. // A<--->B
  666. collectionA.bindTo( collectionB ).using( i => i );
  667. collectionB.bindTo( collectionA ).using( i => i );
  668. assertItems( collectionA, [], [] );
  669. assertItems( collectionB, [], [] );
  670. collectionA.add( { v: 4 } );
  671. collectionA.add( { v: 6 } );
  672. assertItems( collectionA, [ 4, 6 ] );
  673. assertItems( collectionB, [ 4, 6 ] );
  674. collectionB.add( { v: 8 } );
  675. assertItems( collectionA, [ 4, 6, 8 ] );
  676. assertItems( collectionB, [ 4, 6, 8 ] );
  677. expect( [ ...collectionA ] ).to.deep.equal( [ ...collectionB ] );
  678. sinon.assert.callCount( spyA, 3 );
  679. sinon.assert.callCount( spyB, 3 );
  680. } );
  681. it( 'works with double chaining', () => {
  682. const collectionA = new Collection();
  683. const collectionB = new Collection();
  684. const collectionC = new Collection();
  685. const spyA = sinon.spy();
  686. const spyB = sinon.spy();
  687. const spyC = sinon.spy();
  688. collectionA.on( 'add', spyA );
  689. collectionB.on( 'add', spyB );
  690. collectionC.on( 'add', spyC );
  691. // A<--->B--->C
  692. collectionA.bindTo( collectionB ).using( i => ( { v: i.v * 2 } ) );
  693. collectionB.bindTo( collectionA ).using( i => ( { v: i.v / 2 } ) );
  694. collectionC.bindTo( collectionB ).using( i => ( { v: -i.v } ) );
  695. assertItems( collectionA, [] );
  696. assertItems( collectionB, [] );
  697. assertItems( collectionC, [] );
  698. collectionA.add( { v: 4 } );
  699. collectionA.add( { v: 6 } );
  700. assertItems( collectionA, [ 4, 6 ] );
  701. assertItems( collectionB, [ 2, 3 ] );
  702. assertItems( collectionC, [ -2, -3 ] );
  703. collectionB.add( { v: 4 } );
  704. assertItems( collectionA, [ 4, 6, 8 ] );
  705. assertItems( collectionB, [ 2, 3, 4 ] );
  706. assertItems( collectionC, [ -2, -3, -4 ] );
  707. collectionC.add( { v: -1000 } );
  708. assertItems( collectionA, [ 4, 6, 8 ] );
  709. assertItems( collectionB, [ 2, 3, 4 ] );
  710. assertItems( collectionC, [ -2, -3, -4, -1000 ] );
  711. sinon.assert.callCount( spyA, 3 );
  712. sinon.assert.callCount( spyB, 3 );
  713. sinon.assert.callCount( spyC, 4 );
  714. } );
  715. it( 'removes items correctly', () => {
  716. const collectionA = new Collection();
  717. const collectionB = new Collection();
  718. const spyAddA = sinon.spy();
  719. const spyAddB = sinon.spy();
  720. const spyRemoveA = sinon.spy();
  721. const spyRemoveB = sinon.spy();
  722. collectionA.on( 'add', spyAddA );
  723. collectionB.on( 'add', spyAddB );
  724. collectionA.on( 'remove', spyRemoveA );
  725. collectionB.on( 'remove', spyRemoveB );
  726. // A<--->B
  727. collectionA.bindTo( collectionB ).using( i => ( { v: i.v * 2 } ) );
  728. collectionB.bindTo( collectionA ).using( i => ( { v: i.v / 2 } ) );
  729. assertItems( collectionA, [], [] );
  730. assertItems( collectionB, [], [] );
  731. collectionA.add( { v: 4 } );
  732. collectionA.add( { v: 6 } );
  733. assertItems( collectionA, [ 4, 6 ] );
  734. assertItems( collectionB, [ 2, 3 ] );
  735. collectionB.add( { v: 4 } );
  736. assertItems( collectionA, [ 4, 6, 8 ] );
  737. assertItems( collectionB, [ 2, 3, 4 ] );
  738. collectionB.remove( 0 );
  739. assertItems( collectionA, [ 6, 8 ] );
  740. assertItems( collectionB, [ 3, 4 ] );
  741. sinon.assert.callCount( spyAddA, 3 );
  742. sinon.assert.callCount( spyAddB, 3 );
  743. sinon.assert.callCount( spyRemoveA, 1 );
  744. sinon.assert.callCount( spyRemoveB, 1 );
  745. collectionA.remove( 1 );
  746. assertItems( collectionA, [ 6 ] );
  747. assertItems( collectionB, [ 3 ] );
  748. sinon.assert.callCount( spyAddA, 3 );
  749. sinon.assert.callCount( spyAddB, 3 );
  750. sinon.assert.callCount( spyRemoveA, 2 );
  751. sinon.assert.callCount( spyRemoveB, 2 );
  752. } );
  753. } );
  754. } );
  755. describe( 'iterator', () => {
  756. it( 'covers the whole collection', () => {
  757. const item1 = getItem( 'foo' );
  758. const item2 = getItem( 'bar' );
  759. const item3 = getItem( 'bom' );
  760. const items = [];
  761. collection.add( item1 );
  762. collection.add( item2 );
  763. collection.add( item3 );
  764. for ( const item of collection ) {
  765. items.push( item.id );
  766. }
  767. expect( items ).to.deep.equal( [ 'foo', 'bar', 'bom' ] );
  768. } );
  769. } );
  770. } );