collection.js 36 KB

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