collection.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533
  1. /**
  2. * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. import testUtils from 'ckeditor5-core/tests/_utils/utils';
  6. import Collection from 'ckeditor5-utils/src/collection';
  7. import CKEditorError from 'ckeditor5-utils/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. let item1 = { id: 'foo', name: 'xx' };
  23. let item2 = { id: 'foo', name: 'yy' };
  24. let 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. let item1 = {};
  45. let 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. let item1 = getItem( 'foo' );
  54. let 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. let collection = new Collection( { idProperty: 'name' } );
  62. let item1 = getItem( 'foo', 'name' );
  63. let 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. let 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. let collection = new Collection( { idProperty: 'name' } );
  77. let 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. let 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. let item1 = getItem( 'foo' );
  89. let 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. let 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. let spy = sinon.spy();
  150. let 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. let collection = new Collection();
  157. let item1 = getItem( 'foo' );
  158. let item2 = getItem( 'bar' );
  159. let item3 = getItem( 'baz' );
  160. let item4 = getItem( 'abc' );
  161. collection.add( item1 );
  162. collection.add( item2, 0 );
  163. collection.add( item3, 1 );
  164. collection.add( item4, 3 );
  165. expect( collection.get( 0 ) ).to.equal( item2 );
  166. expect( collection.get( 1 ) ).to.equal( item3 );
  167. expect( collection.get( 2 ) ).to.equal( item1 );
  168. expect( collection.get( 3 ) ).to.equal( item4 );
  169. } );
  170. it( 'should throw when index argument is invalid', () => {
  171. let collection = new Collection();
  172. let item1 = getItem( 'foo' );
  173. let item2 = getItem( 'bar' );
  174. let item3 = getItem( 'baz' );
  175. collection.add( item1 );
  176. expect( () => {
  177. collection.add( item2, -1 );
  178. } ).to.throw( /^collection-add-item-invalid-index/ );
  179. expect( () => {
  180. collection.add( item2, 2 );
  181. } ).to.throw( /^collection-add-item-invalid-index/ );
  182. collection.add( item2, 1 );
  183. collection.add( item3, 0 );
  184. expect( collection.length ).to.be.equal( 3 );
  185. } );
  186. it( 'should fire the "add" event with the index argument', () => {
  187. let spy = sinon.spy();
  188. collection.add( {} );
  189. collection.add( {} );
  190. collection.on( 'add', spy );
  191. const item = {};
  192. collection.add( item, 1 );
  193. sinon.assert.calledWithExactly( spy, sinon.match.has( 'source', collection ), item, 1 );
  194. } );
  195. } );
  196. describe( 'get', () => {
  197. it( 'should return an item', () => {
  198. let item = getItem( 'foo' );
  199. collection.add( item );
  200. expect( collection.get( 'foo' ) ).to.equal( item );
  201. } );
  202. it( 'should return null if id does not exist', () => {
  203. collection.add( getItem( 'foo' ) );
  204. expect( collection.get( 'bar' ) ).to.be.null;
  205. } );
  206. it( 'should throw if neither string or number given', () => {
  207. expect( () => {
  208. collection.get( true );
  209. } ).to.throw( CKEditorError, /^collection-get-invalid-arg/ );
  210. } );
  211. } );
  212. describe( 'getIndex', () => {
  213. it( 'should return index of given item', () => {
  214. const item1 = { foo: 'bar' };
  215. const item2 = { bar: 'biz' };
  216. const item3 = { foo: 'biz' };
  217. collection.add( item1 );
  218. collection.add( item2 );
  219. collection.add( item3 );
  220. expect( collection.getIndex( item1 ) ).to.equal( 0 );
  221. expect( collection.getIndex( item2 ) ).to.equal( 1 );
  222. expect( collection.getIndex( item3 ) ).to.equal( 2 );
  223. } );
  224. it( 'should return index of item with given id', () => {
  225. collection.add( { id: 'id1' } );
  226. collection.add( { id: 'id2' } );
  227. collection.add( { id: 'id3' } );
  228. expect( collection.getIndex( 'id1' ) ).to.equal( 0 );
  229. expect( collection.getIndex( 'id2' ) ).to.equal( 1 );
  230. expect( collection.getIndex( 'id3' ) ).to.equal( 2 );
  231. } );
  232. it( 'should return index equal to -1 when given item is not defined in the collection', () => {
  233. const item1 = { foo: 'bar' };
  234. expect( collection.getIndex( item1 ) ).to.equal( -1 );
  235. } );
  236. it( 'should return index equal to -1 when item of given id is not defined in the collection', () => {
  237. expect( collection.getIndex( 'id1' ) ).to.equal( -1 );
  238. } );
  239. } );
  240. describe( 'remove', () => {
  241. it( 'should remove the model by index', () => {
  242. collection.add( getItem( 'bom' ) );
  243. collection.add( getItem( 'foo' ) );
  244. collection.add( getItem( 'bar' ) );
  245. expect( collection ).to.have.length( 3 );
  246. let removedItem = collection.remove( 1 );
  247. expect( collection ).to.have.length( 2 );
  248. expect( collection.get( 'foo' ) ).to.be.null;
  249. expect( collection.get( 1 ) ).to.have.property( 'id', 'bar' );
  250. expect( removedItem ).to.have.property( 'id', 'foo' );
  251. } );
  252. it( 'should remove the model by index - custom id property', () => {
  253. let collection = new Collection( { idProperty: 'name' } );
  254. collection.add( getItem( 'foo', 'name' ) );
  255. let removedItem = collection.remove( 0 );
  256. expect( collection ).to.have.length( 0 );
  257. expect( collection.get( 'foo' ) ).to.be.null;
  258. expect( removedItem ).to.have.property( 'name', 'foo' );
  259. } );
  260. it( 'should remove the model by id', () => {
  261. collection.add( getItem( 'bom' ) );
  262. collection.add( getItem( 'foo' ) );
  263. collection.add( getItem( 'bar' ) );
  264. expect( collection ).to.have.length( 3 );
  265. let removedItem = collection.remove( 'foo' );
  266. expect( collection ).to.have.length( 2 );
  267. expect( collection.get( 'foo' ) ).to.be.null;
  268. expect( collection.get( 1 ) ).to.have.property( 'id', 'bar' );
  269. expect( removedItem ).to.have.property( 'id', 'foo' );
  270. } );
  271. it( 'should remove the model by model', () => {
  272. let item = getItem( 'foo' );
  273. collection.add( getItem( 'bom' ) );
  274. collection.add( item );
  275. collection.add( getItem( 'bar' ) );
  276. expect( collection ).to.have.length( 3 );
  277. let removedItem = collection.remove( item );
  278. expect( collection ).to.have.length( 2 );
  279. expect( collection.get( 'foo' ) ).to.be.null;
  280. expect( collection.get( 1 ) ).to.have.property( 'id', 'bar' );
  281. expect( removedItem ).to.equal( item );
  282. } );
  283. it( 'should remove the model by model - custom id property', () => {
  284. let collection = new Collection( null, 'name' );
  285. let item = getItem( 'foo', 'name' );
  286. collection.add( item );
  287. let removedItem = collection.remove( item );
  288. expect( collection ).to.have.length( 0 );
  289. expect( collection.get( 'foo' ) ).to.be.null;
  290. expect( removedItem ).to.have.property( 'name', 'foo' );
  291. } );
  292. it( 'should fire the "remove" event', () => {
  293. let item1 = getItem( 'foo' );
  294. let item2 = getItem( 'bar' );
  295. let item3 = getItem( 'bom' );
  296. collection.add( item1 );
  297. collection.add( item2 );
  298. collection.add( item3 );
  299. let spy = sinon.spy();
  300. collection.on( 'remove', spy );
  301. collection.remove( 1 ); // by index
  302. collection.remove( item1 ); // by model
  303. collection.remove( 'bom' ); // by id
  304. sinon.assert.calledThrice( spy );
  305. sinon.assert.calledWithExactly( spy, sinon.match.has( 'source', collection ), item1 );
  306. sinon.assert.calledWithExactly( spy, sinon.match.has( 'source', collection ), item2 );
  307. sinon.assert.calledWithExactly( spy, sinon.match.has( 'source', collection ), item3 );
  308. } );
  309. it( 'should throw an error on invalid index', () => {
  310. collection.add( getItem( 'foo' ) );
  311. expect( () => {
  312. collection.remove( 1 );
  313. } ).to.throw( CKEditorError, /^collection-remove-404/ );
  314. expect( collection ).to.have.length( 1 );
  315. } );
  316. it( 'should throw an error on invalid id', () => {
  317. collection.add( getItem( 'foo' ) );
  318. expect( () => {
  319. collection.remove( 'bar' );
  320. } ).to.throw( CKEditorError, /^collection-remove-404/ );
  321. expect( collection ).to.have.length( 1 );
  322. } );
  323. it( 'should throw an error on invalid model', () => {
  324. collection.add( getItem( 'foo' ) );
  325. expect( () => {
  326. collection.remove( getItem( 'bar' ) );
  327. } ).to.throw( CKEditorError, /^collection-remove-404/ );
  328. expect( collection ).to.have.length( 1 );
  329. } );
  330. } );
  331. describe( 'map', () => {
  332. it( 'uses native map', () => {
  333. let spy = testUtils.sinon.stub( Array.prototype, 'map', () => {
  334. return [ 'foo' ];
  335. } );
  336. let ctx = {};
  337. let ret = collection.map( callback, ctx );
  338. sinon.assert.calledWithExactly( spy, callback, ctx );
  339. expect( ret ).to.deep.equal( [ 'foo' ], 'ret value was forwarded' );
  340. function callback() {}
  341. } );
  342. } );
  343. describe( 'find', () => {
  344. it( 'uses native find', () => {
  345. let needl = getItem( 'foo' );
  346. let spy = testUtils.sinon.stub( Array.prototype, 'find', () => {
  347. return needl;
  348. } );
  349. let ctx = {};
  350. let ret = collection.find( callback, ctx );
  351. sinon.assert.calledWithExactly( spy, callback, ctx );
  352. expect( ret ).to.equal( needl, 'ret value was forwarded' );
  353. function callback() {}
  354. } );
  355. } );
  356. describe( 'filter', () => {
  357. it( 'uses native filter', () => {
  358. let needl = getItem( 'foo' );
  359. let spy = testUtils.sinon.stub( Array.prototype, 'filter', () => {
  360. return [ needl ];
  361. } );
  362. let ctx = {};
  363. let ret = collection.filter( callback, ctx );
  364. sinon.assert.calledWithExactly( spy, callback, ctx );
  365. expect( ret ).to.deep.equal( [ needl ], 'ret value was forwarded' );
  366. function callback() {}
  367. } );
  368. } );
  369. describe( 'clear', () => {
  370. it( 'removes all items', () => {
  371. const items = [ {}, {}, {} ];
  372. const spy = sinon.spy();
  373. collection.on( 'remove', spy );
  374. items.forEach( i => collection.add( i ) );
  375. collection.clear();
  376. expect( spy.callCount ).to.equal( 3 );
  377. expect( collection.length ).to.equal( 0 );
  378. } );
  379. } );
  380. describe( 'iterator', () => {
  381. it( 'covers the whole collection', () => {
  382. let item1 = getItem( 'foo' );
  383. let item2 = getItem( 'bar' );
  384. let item3 = getItem( 'bom' );
  385. let items = [];
  386. collection.add( item1 );
  387. collection.add( item2 );
  388. collection.add( item3 );
  389. for ( let item of collection ) {
  390. items.push( item.id );
  391. }
  392. expect( items ).to.deep.equal( [ 'foo', 'bar', 'bom' ] );
  393. } );
  394. } );
  395. } );