collection.js 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019
  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( 'add()', () => {
  33. it( 'should be chainable', () => {
  34. expect( collection.add( {} ) ).to.equal( collection );
  35. } );
  36. it( 'should change the length', () => {
  37. expect( collection ).to.have.length( 0 );
  38. collection.add( {} );
  39. expect( collection ).to.have.length( 1 );
  40. collection.add( {} );
  41. expect( collection ).to.have.length( 2 );
  42. } );
  43. it( 'should enable get( index )', () => {
  44. const item1 = {};
  45. const item2 = {};
  46. collection.add( item1 );
  47. expect( collection.get( 0 ) ).to.equal( item1 );
  48. collection.add( item2 );
  49. expect( collection.get( 0 ) ).to.equal( item1 );
  50. expect( collection.get( 1 ) ).to.equal( item2 );
  51. } );
  52. it( 'should enable get( id )', () => {
  53. const item1 = getItem( 'foo' );
  54. const item2 = getItem( 'bar' );
  55. collection.add( item1 );
  56. collection.add( item2 );
  57. expect( collection.get( 'foo' ) ).to.equal( item1 );
  58. expect( collection.get( 'bar' ) ).to.equal( item2 );
  59. } );
  60. it( 'should enable get( id ) - custom id property', () => {
  61. const collection = new Collection( { idProperty: 'name' } );
  62. const item1 = getItem( 'foo', 'name' );
  63. const item2 = getItem( 'bar', 'name' );
  64. collection.add( item1 );
  65. collection.add( item2 );
  66. expect( collection.get( 'foo' ) ).to.equal( item1 );
  67. expect( collection.get( 'bar' ) ).to.equal( item2 );
  68. } );
  69. it( 'should generate an id when not defined', () => {
  70. const item = {};
  71. collection.add( item );
  72. expect( item.id ).to.be.a( 'string' );
  73. expect( collection.get( item.id ) ).to.equal( item );
  74. } );
  75. it( 'should generate an id when not defined - custom id property', () => {
  76. const collection = new Collection( { idProperty: 'name' } );
  77. const item = {};
  78. collection.add( item );
  79. expect( item.name ).to.be.a( 'string' );
  80. expect( collection.get( item.name ) ).to.equal( item );
  81. } );
  82. it( 'should not change an existing id of an item', () => {
  83. const item = getItem( 'foo' );
  84. collection.add( item );
  85. expect( item.id ).to.equal( 'foo' );
  86. } );
  87. it( 'should throw when item with this id already exists', () => {
  88. const item1 = getItem( 'foo' );
  89. const item2 = getItem( 'foo' );
  90. collection.add( item1 );
  91. expect( () => {
  92. collection.add( item2 );
  93. } ).to.throw( CKEditorError, /^collection-add-item-already-exists/ );
  94. } );
  95. it( 'should throw when item\'s id is not a string', () => {
  96. const item = { id: 1 };
  97. expect( () => {
  98. collection.add( item );
  99. } ).to.throw( CKEditorError, /^collection-add-invalid-id/ );
  100. } );
  101. it(
  102. 'should generate an id when not defined, which is globally unique ' +
  103. 'so it is possible to move items between collections and avoid id collisions',
  104. () => {
  105. const collectionA = new Collection();
  106. const collectionB = new Collection();
  107. const itemA = {};
  108. const itemB = {};
  109. collectionA.add( itemA );
  110. collectionB.add( itemB );
  111. collectionB.add( collectionA.remove( itemA ) );
  112. expect( collectionA.length ).to.equal( 0 );
  113. expect( collectionB.length ).to.equal( 2 );
  114. expect( collectionB.get( 0 ) ).to.equal( itemB );
  115. expect( collectionB.get( 1 ) ).to.equal( itemA );
  116. expect( itemA.id ).to.not.equal( itemB.id );
  117. }
  118. );
  119. it(
  120. 'should generate an id when not defined, which is globally unique ' +
  121. 'so it is possible to move items between collections and avoid id collisions ' +
  122. '– custom id property',
  123. () => {
  124. const collectionA = new Collection( { idProperty: 'foo' } );
  125. const collectionB = new Collection( { idProperty: 'foo' } );
  126. const itemA = {};
  127. const itemB = {};
  128. collectionA.add( itemA );
  129. collectionB.add( itemB );
  130. collectionB.add( collectionA.remove( itemA ) );
  131. expect( collectionA.length ).to.equal( 0 );
  132. expect( collectionB.length ).to.equal( 2 );
  133. expect( collectionB.get( 0 ) ).to.equal( itemB );
  134. expect( collectionB.get( 1 ) ).to.equal( itemA );
  135. expect( itemA.foo ).to.not.equal( itemB.foo );
  136. }
  137. );
  138. it( 'should allow an item which is already in some other collection', () => {
  139. const collectionA = new Collection();
  140. const collectionB = new Collection();
  141. const item = {};
  142. collectionA.add( item );
  143. collectionB.add( item );
  144. expect( collectionA.length ).to.equal( 1 );
  145. expect( collectionB.length ).to.equal( 1 );
  146. expect( collectionA.get( item.id ) ).to.equal( collectionB.get( 0 ) );
  147. } );
  148. it( 'should fire the "add" event', () => {
  149. const spy = sinon.spy();
  150. const item = {};
  151. collection.on( 'add', spy );
  152. collection.add( item );
  153. sinon.assert.calledWithExactly( spy, sinon.match.has( 'source', collection ), item, 0 );
  154. } );
  155. it( 'should support an optional index argument', () => {
  156. const item1 = getItem( 'foo' );
  157. const item2 = getItem( 'bar' );
  158. const item3 = getItem( 'baz' );
  159. const item4 = getItem( 'abc' );
  160. collection.add( item1 );
  161. collection.add( item2, 0 );
  162. collection.add( item3, 1 );
  163. collection.add( item4, 3 );
  164. expect( collection.get( 0 ) ).to.equal( item2 );
  165. expect( collection.get( 1 ) ).to.equal( item3 );
  166. expect( collection.get( 2 ) ).to.equal( item1 );
  167. expect( collection.get( 3 ) ).to.equal( item4 );
  168. } );
  169. it( 'should throw when index argument is invalid', () => {
  170. const item1 = getItem( 'foo' );
  171. const item2 = getItem( 'bar' );
  172. const item3 = getItem( 'baz' );
  173. collection.add( item1 );
  174. expect( () => {
  175. collection.add( item2, -1 );
  176. } ).to.throw( /^collection-add-item-invalid-index/ );
  177. expect( () => {
  178. collection.add( item2, 2 );
  179. } ).to.throw( /^collection-add-item-invalid-index/ );
  180. collection.add( item2, 1 );
  181. collection.add( item3, 0 );
  182. expect( collection.length ).to.be.equal( 3 );
  183. } );
  184. it( 'should fire the "add" event with the index argument', () => {
  185. const spy = sinon.spy();
  186. collection.add( {} );
  187. collection.add( {} );
  188. collection.on( 'add', spy );
  189. const item = {};
  190. collection.add( item, 1 );
  191. sinon.assert.calledWithExactly( spy, sinon.match.has( 'source', collection ), item, 1 );
  192. } );
  193. } );
  194. describe( 'get()', () => {
  195. it( 'should return an item', () => {
  196. const item = getItem( 'foo' );
  197. collection.add( item );
  198. expect( collection.get( 'foo' ) ).to.equal( item );
  199. } );
  200. it( 'should return null if id does not exist', () => {
  201. collection.add( getItem( 'foo' ) );
  202. expect( collection.get( 'bar' ) ).to.be.null;
  203. } );
  204. it( 'should throw if neither string or number given', () => {
  205. expect( () => {
  206. collection.get( true );
  207. } ).to.throw( CKEditorError, /^collection-get-invalid-arg/ );
  208. } );
  209. } );
  210. describe( 'getIndex()', () => {
  211. it( 'should return index of given item', () => {
  212. const item1 = { foo: 'bar' };
  213. const item2 = { bar: 'biz' };
  214. const item3 = { foo: 'biz' };
  215. collection.add( item1 );
  216. collection.add( item2 );
  217. collection.add( item3 );
  218. expect( collection.getIndex( item1 ) ).to.equal( 0 );
  219. expect( collection.getIndex( item2 ) ).to.equal( 1 );
  220. expect( collection.getIndex( item3 ) ).to.equal( 2 );
  221. } );
  222. it( 'should return index of item with given id', () => {
  223. collection.add( { id: 'id1' } );
  224. collection.add( { id: 'id2' } );
  225. collection.add( { id: 'id3' } );
  226. expect( collection.getIndex( 'id1' ) ).to.equal( 0 );
  227. expect( collection.getIndex( 'id2' ) ).to.equal( 1 );
  228. expect( collection.getIndex( 'id3' ) ).to.equal( 2 );
  229. } );
  230. it( 'should return index equal to -1 when given item is not defined in the collection', () => {
  231. const item1 = { foo: 'bar' };
  232. expect( collection.getIndex( item1 ) ).to.equal( -1 );
  233. } );
  234. it( 'should return index equal to -1 when item of given id is not defined in the collection', () => {
  235. expect( collection.getIndex( 'id1' ) ).to.equal( -1 );
  236. } );
  237. } );
  238. describe( 'remove()', () => {
  239. it( 'should remove the model by index', () => {
  240. collection.add( getItem( 'bom' ) );
  241. collection.add( getItem( 'foo' ) );
  242. collection.add( getItem( 'bar' ) );
  243. expect( collection ).to.have.length( 3 );
  244. const removedItem = collection.remove( 1 );
  245. expect( collection ).to.have.length( 2 );
  246. expect( collection.get( 'foo' ) ).to.be.null;
  247. expect( collection.get( 1 ) ).to.have.property( 'id', 'bar' );
  248. expect( removedItem ).to.have.property( 'id', 'foo' );
  249. } );
  250. it( 'should remove the model by index - custom id property', () => {
  251. const collection = new Collection( { idProperty: 'name' } );
  252. collection.add( getItem( 'foo', 'name' ) );
  253. const removedItem = collection.remove( 0 );
  254. expect( collection ).to.have.length( 0 );
  255. expect( collection.get( 'foo' ) ).to.be.null;
  256. expect( removedItem ).to.have.property( 'name', 'foo' );
  257. } );
  258. it( 'should remove the model by id', () => {
  259. collection.add( getItem( 'bom' ) );
  260. collection.add( getItem( 'foo' ) );
  261. collection.add( getItem( 'bar' ) );
  262. expect( collection ).to.have.length( 3 );
  263. const removedItem = collection.remove( 'foo' );
  264. expect( collection ).to.have.length( 2 );
  265. expect( collection.get( 'foo' ) ).to.be.null;
  266. expect( collection.get( 1 ) ).to.have.property( 'id', 'bar' );
  267. expect( removedItem ).to.have.property( 'id', 'foo' );
  268. } );
  269. it( 'should remove the model by model', () => {
  270. const item = getItem( 'foo' );
  271. collection.add( getItem( 'bom' ) );
  272. collection.add( item );
  273. collection.add( getItem( 'bar' ) );
  274. expect( collection ).to.have.length( 3 );
  275. const removedItem = collection.remove( item );
  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.equal( item );
  280. } );
  281. it( 'should remove the model by model - custom id property', () => {
  282. const collection = new Collection( null, 'name' );
  283. const item = getItem( 'foo', 'name' );
  284. collection.add( item );
  285. const removedItem = collection.remove( item );
  286. expect( collection ).to.have.length( 0 );
  287. expect( collection.get( 'foo' ) ).to.be.null;
  288. expect( removedItem ).to.have.property( 'name', 'foo' );
  289. } );
  290. it( 'should fire the "remove" event', () => {
  291. const item1 = getItem( 'foo' );
  292. const item2 = getItem( 'bar' );
  293. const item3 = getItem( 'bom' );
  294. collection.add( item1 );
  295. collection.add( item2 );
  296. collection.add( item3 );
  297. const spy = sinon.spy();
  298. collection.on( 'remove', spy );
  299. collection.remove( 1 ); // by index
  300. collection.remove( item1 ); // by model
  301. collection.remove( 'bom' ); // by id
  302. sinon.assert.calledThrice( spy );
  303. sinon.assert.calledWithExactly( spy, sinon.match.has( 'source', collection ), item1 );
  304. sinon.assert.calledWithExactly( spy, sinon.match.has( 'source', collection ), item2 );
  305. sinon.assert.calledWithExactly( spy, sinon.match.has( 'source', collection ), item3 );
  306. } );
  307. it( 'should throw an error on invalid index', () => {
  308. collection.add( getItem( 'foo' ) );
  309. expect( () => {
  310. collection.remove( 1 );
  311. } ).to.throw( CKEditorError, /^collection-remove-404/ );
  312. expect( collection ).to.have.length( 1 );
  313. } );
  314. it( 'should throw an error on invalid id', () => {
  315. collection.add( getItem( 'foo' ) );
  316. expect( () => {
  317. collection.remove( 'bar' );
  318. } ).to.throw( CKEditorError, /^collection-remove-404/ );
  319. expect( collection ).to.have.length( 1 );
  320. } );
  321. it( 'should throw an error on invalid model', () => {
  322. collection.add( getItem( 'foo' ) );
  323. expect( () => {
  324. collection.remove( getItem( 'bar' ) );
  325. } ).to.throw( CKEditorError, /^collection-remove-404/ );
  326. expect( collection ).to.have.length( 1 );
  327. } );
  328. } );
  329. describe( 'map()', () => {
  330. it( 'uses native map', () => {
  331. const spy = testUtils.sinon.stub( Array.prototype, 'map', () => {
  332. return [ 'foo' ];
  333. } );
  334. const ctx = {};
  335. const ret = collection.map( callback, ctx );
  336. sinon.assert.calledWithExactly( spy, callback, ctx );
  337. expect( ret ).to.deep.equal( [ 'foo' ], 'ret value was forwarded' );
  338. function callback() {}
  339. } );
  340. } );
  341. describe( 'find()', () => {
  342. it( 'uses native find', () => {
  343. const needl = getItem( 'foo' );
  344. const spy = testUtils.sinon.stub( Array.prototype, 'find', () => {
  345. return needl;
  346. } );
  347. const ctx = {};
  348. const ret = collection.find( callback, ctx );
  349. sinon.assert.calledWithExactly( spy, callback, ctx );
  350. expect( ret ).to.equal( needl, 'ret value was forwarded' );
  351. function callback() {}
  352. } );
  353. } );
  354. describe( 'filter()', () => {
  355. it( 'uses native filter', () => {
  356. const needl = getItem( 'foo' );
  357. const spy = testUtils.sinon.stub( Array.prototype, 'filter', () => {
  358. return [ needl ];
  359. } );
  360. const ctx = {};
  361. const ret = collection.filter( callback, ctx );
  362. sinon.assert.calledWithExactly( spy, callback, ctx );
  363. expect( ret ).to.deep.equal( [ needl ], 'ret value was forwarded' );
  364. function callback() {}
  365. } );
  366. } );
  367. describe( 'clear()', () => {
  368. it( 'removes all items', () => {
  369. const items = [ {}, {}, {} ];
  370. const spy = sinon.spy();
  371. collection.on( 'remove', spy );
  372. items.forEach( i => collection.add( i ) );
  373. collection.clear();
  374. expect( spy.callCount ).to.equal( 3 );
  375. expect( collection.length ).to.equal( 0 );
  376. } );
  377. it( 'breaks the binding', () => {
  378. const external = new Collection();
  379. collection.bindTo( external ).using( i => i );
  380. external.add( { foo: 'bar' } );
  381. expect( collection ).to.have.length( 1 );
  382. collection.clear();
  383. external.add( { foo: 'baz' } );
  384. expect( collection ).to.have.length( 0 );
  385. external.remove( 0 );
  386. expect( collection ).to.have.length( 0 );
  387. expect( collection._bindToCollection ).to.be.null;
  388. } );
  389. } );
  390. describe( 'bindTo()', () => {
  391. class FactoryClass {
  392. constructor( data ) {
  393. this.data = data;
  394. }
  395. }
  396. function assertItems( collection, expectedItems ) {
  397. expect( collection.map( i => i.v ) ).to.deep.equal( expectedItems );
  398. }
  399. it( 'throws when binding more than once', () => {
  400. collection.bindTo( {} );
  401. expect( () => {
  402. collection.bindTo( {} );
  403. } ).to.throw( CKEditorError, /^collection-bind-to-rebind/ );
  404. } );
  405. it( 'provides "using()" and "as()" interfaces', () => {
  406. const returned = collection.bindTo( {} );
  407. expect( returned ).to.have.keys( 'using', 'as' );
  408. expect( returned.using ).to.be.a( 'function' );
  409. expect( returned.as ).to.be.a( 'function' );
  410. } );
  411. it( 'stores reference to bound collection', () => {
  412. const collectionB = new Collection();
  413. expect( collection._bindToCollection ).to.be.undefined;
  414. expect( collectionB._bindToCollection ).to.be.undefined;
  415. collection.bindTo( collectionB ).as( FactoryClass );
  416. expect( collection._bindToCollection ).to.equal( collectionB );
  417. expect( collectionB._bindToCollection ).to.be.undefined;
  418. } );
  419. describe( 'as()', () => {
  420. let items;
  421. beforeEach( () => {
  422. items = new Collection();
  423. } );
  424. it( 'does not chain', () => {
  425. const returned = collection.bindTo( new Collection() ).as( FactoryClass );
  426. expect( returned ).to.be.undefined;
  427. } );
  428. it( 'creates a binding (initial content)', () => {
  429. items.add( { id: '1' } );
  430. items.add( { id: '2' } );
  431. collection.bindTo( items ).as( FactoryClass );
  432. expect( collection ).to.have.length( 2 );
  433. expect( collection.get( 0 ) ).to.be.instanceOf( FactoryClass );
  434. expect( collection.get( 1 ) ).to.be.instanceOf( FactoryClass );
  435. expect( collection.get( 1 ).data ).to.equal( items.get( 1 ) );
  436. } );
  437. it( 'creates a binding (new content)', () => {
  438. collection.bindTo( items ).as( FactoryClass );
  439. expect( collection ).to.have.length( 0 );
  440. items.add( { id: '1' } );
  441. items.add( { id: '2' } );
  442. expect( collection ).to.have.length( 2 );
  443. expect( collection.get( 0 ) ).to.be.instanceOf( FactoryClass );
  444. expect( collection.get( 1 ) ).to.be.instanceOf( FactoryClass );
  445. expect( collection.get( 1 ).data ).to.equal( items.get( 1 ) );
  446. } );
  447. it( 'creates a binding (item removal)', () => {
  448. collection.bindTo( items ).as( FactoryClass );
  449. expect( collection ).to.have.length( 0 );
  450. items.add( { id: '1' } );
  451. items.add( { id: '2' } );
  452. expect( collection ).to.have.length( 2 );
  453. expect( collection.get( 0 ) ).to.be.instanceOf( FactoryClass );
  454. expect( collection.get( 1 ) ).to.be.instanceOf( FactoryClass );
  455. expect( collection.get( 1 ).data ).to.equal( items.get( 1 ) );
  456. items.remove( 1 );
  457. expect( collection.get( 0 ).data ).to.equal( items.get( 0 ) );
  458. items.remove( 0 );
  459. expect( collection ).to.have.length( 0 );
  460. } );
  461. } );
  462. describe( 'using()', () => {
  463. let items;
  464. beforeEach( () => {
  465. items = new Collection();
  466. } );
  467. it( 'does not chain', () => {
  468. const returned = collection.bindTo( new Collection() ).using( () => {} );
  469. expect( returned ).to.be.undefined;
  470. } );
  471. describe( 'callback', () => {
  472. it( 'creates a binding (arrow function)', () => {
  473. collection.bindTo( items ).using( item => {
  474. return new FactoryClass( item );
  475. } );
  476. expect( collection ).to.have.length( 0 );
  477. items.add( { id: '1' } );
  478. items.add( { id: '2' } );
  479. expect( collection ).to.have.length( 2 );
  480. expect( collection.get( 0 ) ).to.be.instanceOf( FactoryClass );
  481. expect( collection.get( 1 ) ).to.be.instanceOf( FactoryClass );
  482. expect( collection.get( 1 ).data ).to.equal( items.get( 1 ) );
  483. } );
  484. // https://github.com/ckeditor/ckeditor5-ui/issues/113
  485. it( 'creates a binding (normal function)', () => {
  486. collection.bindTo( items ).using( function( item ) {
  487. return new FactoryClass( item );
  488. } );
  489. items.add( { id: '1' } );
  490. expect( collection ).to.have.length( 1 );
  491. const view = collection.get( 0 );
  492. // Wrong args will be passed to the callback if it's treated as the view constructor.
  493. expect( view ).to.be.instanceOf( FactoryClass );
  494. expect( view.data ).to.equal( items.get( 0 ) );
  495. } );
  496. it( 'creates a 1:1 binding', () => {
  497. collection.bindTo( items ).using( item => item );
  498. expect( collection ).to.have.length( 0 );
  499. const item1 = { id: '100' };
  500. const item2 = { id: '200' };
  501. items.add( item1 );
  502. items.add( item2 );
  503. expect( collection ).to.have.length( 2 );
  504. expect( collection.get( 0 ) ).to.equal( item1 );
  505. expect( collection.get( 1 ) ).to.equal( item2 );
  506. } );
  507. it( 'creates a conditional binding', () => {
  508. class CustomClass {
  509. constructor( data ) {
  510. this.data = data;
  511. }
  512. }
  513. collection.bindTo( items ).using( item => {
  514. if ( item.id == 'FactoryClass' ) {
  515. return new FactoryClass( item );
  516. } else {
  517. return new CustomClass( item );
  518. }
  519. } );
  520. expect( collection ).to.have.length( 0 );
  521. const item1 = { id: 'FactoryClass' };
  522. const item2 = { id: 'CustomClass' };
  523. items.add( item1 );
  524. items.add( item2 );
  525. expect( collection ).to.have.length( 2 );
  526. expect( collection.get( 0 ) ).to.be.instanceOf( FactoryClass );
  527. expect( collection.get( 1 ) ).to.be.instanceOf( CustomClass );
  528. } );
  529. it( 'creates a binding to a property name', () => {
  530. collection.bindTo( items ).using( item => item.prop );
  531. expect( collection ).to.have.length( 0 );
  532. items.add( { prop: { value: 'foo' } } );
  533. items.add( { prop: { value: 'bar' } } );
  534. expect( collection ).to.have.length( 2 );
  535. expect( collection.get( 0 ).value ).to.equal( 'foo' );
  536. expect( collection.get( 1 ).value ).to.equal( 'bar' );
  537. } );
  538. } );
  539. describe( 'property name', () => {
  540. it( 'creates a binding', () => {
  541. collection.bindTo( items ).using( 'prop' );
  542. expect( collection ).to.have.length( 0 );
  543. items.add( { prop: { value: 'foo' } } );
  544. items.add( { prop: { value: 'bar' } } );
  545. expect( collection ).to.have.length( 2 );
  546. expect( collection.get( 0 ).value ).to.equal( 'foo' );
  547. expect( collection.get( 1 ).value ).to.equal( 'bar' );
  548. } );
  549. it( 'creates a binding (item removal)', () => {
  550. collection.bindTo( items ).using( 'prop' );
  551. expect( collection ).to.have.length( 0 );
  552. items.add( { prop: { value: 'foo' } } );
  553. items.add( { prop: { value: 'bar' } } );
  554. expect( collection ).to.have.length( 2 );
  555. expect( collection.get( 0 ).value ).to.equal( 'foo' );
  556. expect( collection.get( 1 ).value ).to.equal( 'bar' );
  557. items.remove( 1 );
  558. expect( collection ).to.have.length( 1 );
  559. expect( collection.get( 0 ).value ).to.equal( 'foo' );
  560. items.remove( 0 );
  561. expect( collection ).to.have.length( 0 );
  562. } );
  563. } );
  564. } );
  565. describe( 'two–way data binding', () => {
  566. it( 'works with custom factories (1)', () => {
  567. const collectionA = new Collection();
  568. const collectionB = new Collection();
  569. const spyA = sinon.spy();
  570. const spyB = sinon.spy();
  571. collectionA.on( 'add', spyA );
  572. collectionB.on( 'add', spyB );
  573. // A<--->B
  574. collectionA.bindTo( collectionB ).using( i => ( { v: i.v * 2 } ) );
  575. collectionB.bindTo( collectionA ).using( i => ( { v: i.v / 2 } ) );
  576. assertItems( collectionA, [] );
  577. assertItems( collectionB, [] );
  578. collectionA.add( { v: 4 } );
  579. collectionA.add( { v: 6 } );
  580. assertItems( collectionA, [ 4, 6 ] );
  581. assertItems( collectionB, [ 2, 3 ] );
  582. collectionB.add( { v: 4 } );
  583. assertItems( collectionA, [ 4, 6, 8 ] );
  584. assertItems( collectionB, [ 2, 3, 4 ] );
  585. sinon.assert.callCount( spyA, 3 );
  586. sinon.assert.callCount( spyB, 3 );
  587. } );
  588. it( 'works with custom factories (2)', () => {
  589. const collectionA = new Collection();
  590. const collectionB = new Collection();
  591. const spyA = sinon.spy();
  592. const spyB = sinon.spy();
  593. collectionA.on( 'add', spyA );
  594. collectionB.on( 'add', spyB );
  595. // A<--->B
  596. collectionA.bindTo( collectionB ).using( 'data' );
  597. collectionB.bindTo( collectionA ).using( i => new FactoryClass( i ) );
  598. collectionA.add( { v: 4 } );
  599. collectionA.add( { v: 6 } );
  600. expect( [ ...collectionB ].every( i => i instanceof FactoryClass ) ).to.be.true;
  601. expect( [ ...collectionB ].map( i => i.data ) ).to.deep.equal( [ ...collectionA ] );
  602. expect( collectionB.map( i => i.data.v ) ).to.deep.equal( [ 4, 6 ] );
  603. expect( collectionA.map( i => i.v ) ).to.deep.equal( [ 4, 6 ] );
  604. collectionB.add( new FactoryClass( { v: 8 } ) );
  605. expect( [ ...collectionB ].every( i => i instanceof FactoryClass ) ).to.be.true;
  606. expect( [ ...collectionB ].map( i => i.data ) ).to.deep.equal( [ ...collectionA ] );
  607. expect( collectionB.map( i => i.data.v ) ).to.deep.equal( [ 4, 6, 8 ] );
  608. expect( collectionA.map( i => i.v ) ).to.deep.equal( [ 4, 6, 8 ] );
  609. } );
  610. it( 'works with custom factories (custom index)', () => {
  611. const collectionA = new Collection();
  612. const collectionB = new Collection();
  613. const spyA = sinon.spy();
  614. const spyB = sinon.spy();
  615. collectionA.on( 'add', spyA );
  616. collectionB.on( 'add', spyB );
  617. // A<--->B
  618. collectionA.bindTo( collectionB ).using( i => ( { v: i.v * 2 } ) );
  619. collectionB.bindTo( collectionA ).using( i => ( { v: i.v / 2 } ) );
  620. assertItems( collectionA, [] );
  621. assertItems( collectionB, [] );
  622. collectionA.add( { v: 4 } );
  623. collectionA.add( { v: 6 }, 0 );
  624. assertItems( collectionA, [ 6, 4 ] );
  625. assertItems( collectionB, [ 3, 2 ] );
  626. collectionB.add( { v: 4 }, 1 );
  627. assertItems( collectionA, [ 6, 8, 4 ] );
  628. assertItems( collectionB, [ 3, 4, 2 ] );
  629. sinon.assert.callCount( spyA, 3 );
  630. sinon.assert.callCount( spyB, 3 );
  631. } );
  632. it( 'works with 1:1 binding', () => {
  633. const collectionA = new Collection();
  634. const collectionB = new Collection();
  635. const spyA = sinon.spy();
  636. const spyB = sinon.spy();
  637. collectionA.on( 'add', spyA );
  638. collectionB.on( 'add', spyB );
  639. // A<--->B
  640. collectionA.bindTo( collectionB ).using( i => i );
  641. collectionB.bindTo( collectionA ).using( i => i );
  642. assertItems( collectionA, [], [] );
  643. assertItems( collectionB, [], [] );
  644. collectionA.add( { v: 4 } );
  645. collectionA.add( { v: 6 } );
  646. assertItems( collectionA, [ 4, 6 ] );
  647. assertItems( collectionB, [ 4, 6 ] );
  648. collectionB.add( { v: 8 } );
  649. assertItems( collectionA, [ 4, 6, 8 ] );
  650. assertItems( collectionB, [ 4, 6, 8 ] );
  651. expect( collectionA ).to.deep.equal( collectionB );
  652. sinon.assert.callCount( spyA, 3 );
  653. sinon.assert.callCount( spyB, 3 );
  654. } );
  655. it( 'works with double chaining', () => {
  656. const collectionA = new Collection();
  657. const collectionB = new Collection();
  658. const collectionC = new Collection();
  659. const spyA = sinon.spy();
  660. const spyB = sinon.spy();
  661. const spyC = sinon.spy();
  662. collectionA.on( 'add', spyA );
  663. collectionB.on( 'add', spyB );
  664. collectionC.on( 'add', spyC );
  665. // A<--->B--->C
  666. collectionA.bindTo( collectionB ).using( i => ( { v: i.v * 2 } ) );
  667. collectionB.bindTo( collectionA ).using( i => ( { v: i.v / 2 } ) );
  668. collectionC.bindTo( collectionB ).using( i => ( { v: -i.v } ) );
  669. assertItems( collectionA, [] );
  670. assertItems( collectionB, [] );
  671. assertItems( collectionC, [] );
  672. collectionA.add( { v: 4 } );
  673. collectionA.add( { v: 6 } );
  674. assertItems( collectionA, [ 4, 6 ] );
  675. assertItems( collectionB, [ 2, 3 ] );
  676. assertItems( collectionC, [ -2, -3 ] );
  677. collectionB.add( { v: 4 } );
  678. assertItems( collectionA, [ 4, 6, 8 ] );
  679. assertItems( collectionB, [ 2, 3, 4 ] );
  680. assertItems( collectionC, [ -2, -3, -4 ] );
  681. collectionC.add( { v: -1000 } );
  682. assertItems( collectionA, [ 4, 6, 8 ] );
  683. assertItems( collectionB, [ 2, 3, 4 ] );
  684. assertItems( collectionC, [ -2, -3, -4, -1000 ] );
  685. sinon.assert.callCount( spyA, 3 );
  686. sinon.assert.callCount( spyB, 3 );
  687. sinon.assert.callCount( spyC, 4 );
  688. } );
  689. it( 'removes items correctly', () => {
  690. const collectionA = new Collection();
  691. const collectionB = new Collection();
  692. const spyAddA = sinon.spy();
  693. const spyAddB = sinon.spy();
  694. const spyRemoveA = sinon.spy();
  695. const spyRemoveB = sinon.spy();
  696. collectionA.on( 'add', spyAddA );
  697. collectionB.on( 'add', spyAddB );
  698. collectionA.on( 'remove', spyRemoveA );
  699. collectionB.on( 'remove', spyRemoveB );
  700. // A<--->B
  701. collectionA.bindTo( collectionB ).using( i => ( { v: i.v * 2 } ) );
  702. collectionB.bindTo( collectionA ).using( i => ( { v: i.v / 2 } ) );
  703. assertItems( collectionA, [], [] );
  704. assertItems( collectionB, [], [] );
  705. collectionA.add( { v: 4 } );
  706. collectionA.add( { v: 6 } );
  707. assertItems( collectionA, [ 4, 6 ] );
  708. assertItems( collectionB, [ 2, 3 ] );
  709. collectionB.add( { v: 4 } );
  710. assertItems( collectionA, [ 4, 6, 8 ] );
  711. assertItems( collectionB, [ 2, 3, 4 ] );
  712. collectionB.remove( 0 );
  713. assertItems( collectionA, [ 6, 8 ] );
  714. assertItems( collectionB, [ 3, 4 ] );
  715. sinon.assert.callCount( spyAddA, 3 );
  716. sinon.assert.callCount( spyAddB, 3 );
  717. sinon.assert.callCount( spyRemoveA, 1 );
  718. sinon.assert.callCount( spyRemoveB, 1 );
  719. collectionA.remove( 1 );
  720. assertItems( collectionA, [ 6 ] );
  721. assertItems( collectionB, [ 3 ] );
  722. sinon.assert.callCount( spyAddA, 3 );
  723. sinon.assert.callCount( spyAddB, 3 );
  724. sinon.assert.callCount( spyRemoveA, 2 );
  725. sinon.assert.callCount( spyRemoveB, 2 );
  726. } );
  727. } );
  728. } );
  729. describe( 'iterator', () => {
  730. it( 'covers the whole collection', () => {
  731. const item1 = getItem( 'foo' );
  732. const item2 = getItem( 'bar' );
  733. const item3 = getItem( 'bom' );
  734. const items = [];
  735. collection.add( item1 );
  736. collection.add( item2 );
  737. collection.add( item3 );
  738. for ( const item of collection ) {
  739. items.push( item.id );
  740. }
  741. expect( items ).to.deep.equal( [ 'foo', 'bar', 'bom' ] );
  742. } );
  743. } );
  744. } );