8
0

collection.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514
  1. /**
  2. * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. import testUtils from '/tests/core/_utils/utils.js';
  6. import Collection from '/ckeditor5/utils/collection.js';
  7. testUtils.createSinonSandbox();
  8. function getItem( id, idProperty ) {
  9. idProperty = idProperty || 'id';
  10. return {
  11. [ idProperty ]: id
  12. };
  13. }
  14. describe( 'Collection', () => {
  15. let collection;
  16. beforeEach( () => {
  17. collection = new Collection();
  18. } );
  19. describe( 'constructor', () => {
  20. it( 'allows to change the id property used by the collection', () => {
  21. let item1 = { id: 'foo', name: 'xx' };
  22. let item2 = { id: 'foo', name: 'yy' };
  23. let collection = new Collection( { idProperty: 'name' } );
  24. collection.add( item1 );
  25. collection.add( item2 );
  26. expect( collection ).to.have.length( 2 );
  27. expect( collection.get( 'xx' ) ).to.equal( item1 );
  28. expect( collection.remove( 'yy' ) ).to.equal( item2 );
  29. } );
  30. } );
  31. describe( 'add', () => {
  32. it( 'should be chainable', () => {
  33. expect( collection.add( {} ) ).to.equal( collection );
  34. } );
  35. it( 'should change the length', () => {
  36. expect( collection ).to.have.length( 0 );
  37. collection.add( {} );
  38. expect( collection ).to.have.length( 1 );
  39. collection.add( {} );
  40. expect( collection ).to.have.length( 2 );
  41. } );
  42. it( 'should enable get( index )', () => {
  43. let item1 = {};
  44. let item2 = {};
  45. collection.add( item1 );
  46. expect( collection.get( 0 ) ).to.equal( item1 );
  47. collection.add( item2 );
  48. expect( collection.get( 0 ) ).to.equal( item1 );
  49. expect( collection.get( 1 ) ).to.equal( item2 );
  50. } );
  51. it( 'should enable get( id )', () => {
  52. let item1 = getItem( 'foo' );
  53. let item2 = getItem( 'bar' );
  54. collection.add( item1 );
  55. collection.add( item2 );
  56. expect( collection.get( 'foo' ) ).to.equal( item1 );
  57. expect( collection.get( 'bar' ) ).to.equal( item2 );
  58. } );
  59. it( 'should enable get( id ) - custom id property', () => {
  60. let collection = new Collection( { idProperty: 'name' } );
  61. let item1 = getItem( 'foo', 'name' );
  62. let item2 = getItem( 'bar', 'name' );
  63. collection.add( item1 );
  64. collection.add( item2 );
  65. expect( collection.get( 'foo' ) ).to.equal( item1 );
  66. expect( collection.get( 'bar' ) ).to.equal( item2 );
  67. } );
  68. it( 'should generate an id when not defined', () => {
  69. let item = {};
  70. collection.add( item );
  71. expect( item.id ).to.be.a( 'string' );
  72. expect( collection.get( item.id ) ).to.equal( item );
  73. } );
  74. it( 'should generate an id when not defined - custom id property', () => {
  75. let collection = new Collection( { idProperty: 'name' } );
  76. let item = {};
  77. collection.add( item );
  78. expect( item.name ).to.be.a( 'string' );
  79. expect( collection.get( item.name ) ).to.equal( item );
  80. } );
  81. it( 'should not change an existing id of an item', () => {
  82. let item = getItem( 'foo' );
  83. collection.add( item );
  84. expect( item.id ).to.equal( 'foo' );
  85. } );
  86. it( 'should throw when item with this id already exists', () => {
  87. let item1 = getItem( 'foo' );
  88. let item2 = getItem( 'foo' );
  89. collection.add( item1 );
  90. expect( () => {
  91. collection.add( item2 );
  92. } ).to.throwCKEditorError( /^collection-add-item-already-exists/ );
  93. } );
  94. it( 'should throw when item\'s id is not a string', () => {
  95. let item = { id: 1 };
  96. expect( () => {
  97. collection.add( item );
  98. } ).to.throwCKEditorError( /^collection-add-invalid-id/ );
  99. } );
  100. it(
  101. 'should not override item under an existing id in case of a collision ' +
  102. 'between existing items and one with an automatically generated id',
  103. () => {
  104. // Add many items to the collection
  105. // to be sure that next item id will be higher than id already stored in uid cache.
  106. for ( let i = 0; i < 100; i++ ) {
  107. collection.add( getItem( String( i ) ) );
  108. }
  109. let item = {};
  110. collection.add( item );
  111. expect( item.id ).to.be.at.least( 100 );
  112. }
  113. );
  114. it(
  115. 'should generate an id when not defined, which is globally unique ' +
  116. 'so it is possible to move items between collections and avoid id collisions',
  117. () => {
  118. const collectionA = new Collection();
  119. const collectionB = new Collection();
  120. const itemA = {};
  121. const itemB = {};
  122. collectionA.add( itemA );
  123. collectionB.add( itemB );
  124. collectionB.add( collectionA.remove( itemA ) );
  125. expect( collectionA.length ).to.equal( 0 );
  126. expect( collectionB.length ).to.equal( 2 );
  127. expect( collectionB.get( 0 ) ).to.equal( itemB );
  128. expect( collectionB.get( 1 ) ).to.equal( itemA );
  129. expect( itemA.id ).to.not.equal( itemB.id );
  130. }
  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. '– custom id property',
  136. () => {
  137. const collectionA = new Collection( { idProperty: 'foo' } );
  138. const collectionB = new Collection( { idProperty: 'foo' } );
  139. const itemA = {};
  140. const itemB = {};
  141. collectionA.add( itemA );
  142. collectionB.add( itemB );
  143. collectionB.add( collectionA.remove( itemA ) );
  144. expect( collectionA.length ).to.equal( 0 );
  145. expect( collectionB.length ).to.equal( 2 );
  146. expect( collectionB.get( 0 ) ).to.equal( itemB );
  147. expect( collectionB.get( 1 ) ).to.equal( itemA );
  148. expect( itemA.foo ).to.not.equal( itemB.foo );
  149. }
  150. );
  151. it( 'should allow an item which is already in some other collection', () => {
  152. const collectionA = new Collection();
  153. const collectionB = new Collection();
  154. const item = {};
  155. collectionA.add( item );
  156. collectionB.add( item );
  157. expect( collectionA.length ).to.equal( 1 );
  158. expect( collectionB.length ).to.equal( 1 );
  159. expect( collectionA.get( item.id ) ).to.equal( collectionB.get( 0 ) );
  160. } );
  161. it( 'should fire the "add" event', () => {
  162. let spy = sinon.spy();
  163. let item = {};
  164. collection.on( 'add', spy );
  165. collection.add( item );
  166. sinon.assert.calledWithExactly( spy, sinon.match.has( 'source', collection ), item, 0 );
  167. } );
  168. it( 'should support an optional index argument', () => {
  169. let collection = new Collection();
  170. let item1 = getItem( 'foo' );
  171. let item2 = getItem( 'bar' );
  172. let item3 = getItem( 'baz' );
  173. let item4 = getItem( 'abc' );
  174. collection.add( item1 );
  175. collection.add( item2, 0 );
  176. collection.add( item3, 1 );
  177. collection.add( item4, 3 );
  178. expect( collection.get( 0 ) ).to.equal( item2 );
  179. expect( collection.get( 1 ) ).to.equal( item3 );
  180. expect( collection.get( 2 ) ).to.equal( item1 );
  181. expect( collection.get( 3 ) ).to.equal( item4 );
  182. } );
  183. it( 'should throw when index argument is invalid', () => {
  184. let collection = new Collection();
  185. let item1 = getItem( 'foo' );
  186. let item2 = getItem( 'bar' );
  187. let item3 = getItem( 'baz' );
  188. collection.add( item1 );
  189. expect( () => {
  190. collection.add( item2, -1 );
  191. } ).to.throw( /^collection-add-item-invalid-index/ );
  192. expect( () => {
  193. collection.add( item2, 2 );
  194. } ).to.throw( /^collection-add-item-invalid-index/ );
  195. collection.add( item2, 1 );
  196. collection.add( item3, 0 );
  197. expect( collection.length ).to.be.equal( 3 );
  198. } );
  199. it( 'should fire the "add" event with the index argument', () => {
  200. let spy = sinon.spy();
  201. collection.add( {} );
  202. collection.add( {} );
  203. collection.on( 'add', spy );
  204. const item = {};
  205. collection.add( item, 1 );
  206. sinon.assert.calledWithExactly( spy, sinon.match.has( 'source', collection ), item, 1 );
  207. } );
  208. } );
  209. describe( 'get', () => {
  210. it( 'should return an item', () => {
  211. let item = getItem( 'foo' );
  212. collection.add( item );
  213. expect( collection.get( 'foo' ) ).to.equal( item );
  214. } );
  215. it( 'should return null if id does not exist', () => {
  216. collection.add( getItem( 'foo' ) );
  217. expect( collection.get( 'bar' ) ).to.be.null;
  218. } );
  219. it( 'should throw if neither string or number given', () => {
  220. expect( () => {
  221. collection.get( true );
  222. } ).to.throwCKEditorError( /^collection-get-invalid-arg/ );
  223. } );
  224. } );
  225. describe( 'remove', () => {
  226. it( 'should remove the model by index', () => {
  227. collection.add( getItem( 'bom' ) );
  228. collection.add( getItem( 'foo' ) );
  229. collection.add( getItem( 'bar' ) );
  230. expect( collection ).to.have.length( 3 );
  231. let removedItem = collection.remove( 1 );
  232. expect( collection ).to.have.length( 2 );
  233. expect( collection.get( 'foo' ) ).to.be.null;
  234. expect( collection.get( 1 ) ).to.have.property( 'id', 'bar' );
  235. expect( removedItem ).to.have.property( 'id', 'foo' );
  236. } );
  237. it( 'should remove the model by index - custom id property', () => {
  238. let collection = new Collection( { idProperty: 'name' } );
  239. collection.add( getItem( 'foo', 'name' ) );
  240. let removedItem = collection.remove( 0 );
  241. expect( collection ).to.have.length( 0 );
  242. expect( collection.get( 'foo' ) ).to.be.null;
  243. expect( removedItem ).to.have.property( 'name', 'foo' );
  244. } );
  245. it( 'should remove the model by id', () => {
  246. collection.add( getItem( 'bom' ) );
  247. collection.add( getItem( 'foo' ) );
  248. collection.add( getItem( 'bar' ) );
  249. expect( collection ).to.have.length( 3 );
  250. let removedItem = collection.remove( 'foo' );
  251. expect( collection ).to.have.length( 2 );
  252. expect( collection.get( 'foo' ) ).to.be.null;
  253. expect( collection.get( 1 ) ).to.have.property( 'id', 'bar' );
  254. expect( removedItem ).to.have.property( 'id', 'foo' );
  255. } );
  256. it( 'should remove the model by model', () => {
  257. let item = getItem( 'foo' );
  258. collection.add( getItem( 'bom' ) );
  259. collection.add( item );
  260. collection.add( getItem( 'bar' ) );
  261. expect( collection ).to.have.length( 3 );
  262. let removedItem = collection.remove( item );
  263. expect( collection ).to.have.length( 2 );
  264. expect( collection.get( 'foo' ) ).to.be.null;
  265. expect( collection.get( 1 ) ).to.have.property( 'id', 'bar' );
  266. expect( removedItem ).to.equal( item );
  267. } );
  268. it( 'should remove the model by model - custom id property', () => {
  269. let collection = new Collection( null, 'name' );
  270. let item = getItem( 'foo', 'name' );
  271. collection.add( item );
  272. let removedItem = collection.remove( item );
  273. expect( collection ).to.have.length( 0 );
  274. expect( collection.get( 'foo' ) ).to.be.null;
  275. expect( removedItem ).to.have.property( 'name', 'foo' );
  276. } );
  277. it( 'should fire the "remove" event', () => {
  278. let item1 = getItem( 'foo' );
  279. let item2 = getItem( 'bar' );
  280. let item3 = getItem( 'bom' );
  281. collection.add( item1 );
  282. collection.add( item2 );
  283. collection.add( item3 );
  284. let spy = sinon.spy();
  285. collection.on( 'remove', spy );
  286. collection.remove( 1 ); // by index
  287. collection.remove( item1 ); // by model
  288. collection.remove( 'bom' ); // by id
  289. sinon.assert.calledThrice( spy );
  290. sinon.assert.calledWithExactly( spy, sinon.match.has( 'source', collection ), item1 );
  291. sinon.assert.calledWithExactly( spy, sinon.match.has( 'source', collection ), item2 );
  292. sinon.assert.calledWithExactly( spy, sinon.match.has( 'source', collection ), item3 );
  293. } );
  294. it( 'should throw an error on invalid index', () => {
  295. collection.add( getItem( 'foo' ) );
  296. expect( () => {
  297. collection.remove( 1 );
  298. } ).to.throwCKEditorError( /^collection-remove-404/ );
  299. expect( collection ).to.have.length( 1 );
  300. } );
  301. it( 'should throw an error on invalid id', () => {
  302. collection.add( getItem( 'foo' ) );
  303. expect( () => {
  304. collection.remove( 'bar' );
  305. } ).to.throwCKEditorError( /^collection-remove-404/ );
  306. expect( collection ).to.have.length( 1 );
  307. } );
  308. it( 'should throw an error on invalid model', () => {
  309. collection.add( getItem( 'foo' ) );
  310. expect( () => {
  311. collection.remove( getItem( 'bar' ) );
  312. } ).to.throwCKEditorError( /^collection-remove-404/ );
  313. expect( collection ).to.have.length( 1 );
  314. } );
  315. } );
  316. describe( 'map', () => {
  317. it( 'uses native map', () => {
  318. let spy = testUtils.sinon.stub( Array.prototype, 'map', () => {
  319. return [ 'foo' ];
  320. } );
  321. let ctx = {};
  322. let ret = collection.map( callback, ctx );
  323. sinon.assert.calledWithExactly( spy, callback, ctx );
  324. expect( ret ).to.deep.equal( [ 'foo' ], 'ret value was forwarded' );
  325. function callback() {}
  326. } );
  327. } );
  328. describe( 'find', () => {
  329. it( 'uses native find', () => {
  330. let needl = getItem( 'foo' );
  331. let spy = testUtils.sinon.stub( Array.prototype, 'find', () => {
  332. return needl;
  333. } );
  334. let ctx = {};
  335. let ret = collection.find( callback, ctx );
  336. sinon.assert.calledWithExactly( spy, callback, ctx );
  337. expect( ret ).to.equal( needl, 'ret value was forwarded' );
  338. function callback() {}
  339. } );
  340. } );
  341. describe( 'filter', () => {
  342. it( 'uses native filter', () => {
  343. let needl = getItem( 'foo' );
  344. let spy = testUtils.sinon.stub( Array.prototype, 'filter', () => {
  345. return [ needl ];
  346. } );
  347. let ctx = {};
  348. let ret = collection.filter( callback, ctx );
  349. sinon.assert.calledWithExactly( spy, callback, ctx );
  350. expect( ret ).to.deep.equal( [ needl ], 'ret value was forwarded' );
  351. function callback() {}
  352. } );
  353. } );
  354. describe( 'clear', () => {
  355. it( 'removes all items', () => {
  356. const items = [ {}, {}, {} ];
  357. const spy = sinon.spy();
  358. collection.on( 'remove', spy );
  359. items.forEach( i => collection.add( i ) );
  360. collection.clear();
  361. expect( spy.callCount ).to.equal( 3 );
  362. expect( collection.length ).to.equal( 0 );
  363. } );
  364. } );
  365. describe( 'iterator', () => {
  366. it( 'covers the whole collection', () => {
  367. let item1 = getItem( 'foo' );
  368. let item2 = getItem( 'bar' );
  369. let item3 = getItem( 'bom' );
  370. let items = [];
  371. collection.add( item1 );
  372. collection.add( item2 );
  373. collection.add( item3 );
  374. for ( let item of collection ) {
  375. items.push( item.id );
  376. }
  377. expect( items ).to.deep.equal( [ 'foo', 'bar', 'bom' ] );
  378. } );
  379. } );
  380. } );