collection.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515
  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. import CKEditorError from '/ckeditor5/utils/ckeditorerror.js';
  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 not override item under an existing id in case of a collision ' +
  103. 'between existing items and one with an automatically generated id',
  104. () => {
  105. // Add many items to the collection
  106. // to be sure that next item id will be higher than id already stored in uid cache.
  107. for ( let i = 0; i < 100; i++ ) {
  108. collection.add( getItem( String( i ) ) );
  109. }
  110. let item = {};
  111. collection.add( item );
  112. expect( item.id ).to.be.at.least( 100 );
  113. }
  114. );
  115. it(
  116. 'should generate an id when not defined, which is globally unique ' +
  117. 'so it is possible to move items between collections and avoid id collisions',
  118. () => {
  119. const collectionA = new Collection();
  120. const collectionB = new Collection();
  121. const itemA = {};
  122. const itemB = {};
  123. collectionA.add( itemA );
  124. collectionB.add( itemB );
  125. collectionB.add( collectionA.remove( itemA ) );
  126. expect( collectionA.length ).to.equal( 0 );
  127. expect( collectionB.length ).to.equal( 2 );
  128. expect( collectionB.get( 0 ) ).to.equal( itemB );
  129. expect( collectionB.get( 1 ) ).to.equal( itemA );
  130. expect( itemA.id ).to.not.equal( itemB.id );
  131. }
  132. );
  133. it(
  134. 'should generate an id when not defined, which is globally unique ' +
  135. 'so it is possible to move items between collections and avoid id collisions ' +
  136. '– custom id property',
  137. () => {
  138. const collectionA = new Collection( { idProperty: 'foo' } );
  139. const collectionB = new Collection( { idProperty: 'foo' } );
  140. const itemA = {};
  141. const itemB = {};
  142. collectionA.add( itemA );
  143. collectionB.add( itemB );
  144. collectionB.add( collectionA.remove( itemA ) );
  145. expect( collectionA.length ).to.equal( 0 );
  146. expect( collectionB.length ).to.equal( 2 );
  147. expect( collectionB.get( 0 ) ).to.equal( itemB );
  148. expect( collectionB.get( 1 ) ).to.equal( itemA );
  149. expect( itemA.foo ).to.not.equal( itemB.foo );
  150. }
  151. );
  152. it( 'should allow an item which is already in some other collection', () => {
  153. const collectionA = new Collection();
  154. const collectionB = new Collection();
  155. const item = {};
  156. collectionA.add( item );
  157. collectionB.add( item );
  158. expect( collectionA.length ).to.equal( 1 );
  159. expect( collectionB.length ).to.equal( 1 );
  160. expect( collectionA.get( item.id ) ).to.equal( collectionB.get( 0 ) );
  161. } );
  162. it( 'should fire the "add" event', () => {
  163. let spy = sinon.spy();
  164. let item = {};
  165. collection.on( 'add', spy );
  166. collection.add( item );
  167. sinon.assert.calledWithExactly( spy, sinon.match.has( 'source', collection ), item, 0 );
  168. } );
  169. it( 'should support an optional index argument', () => {
  170. let collection = new Collection();
  171. let item1 = getItem( 'foo' );
  172. let item2 = getItem( 'bar' );
  173. let item3 = getItem( 'baz' );
  174. let item4 = getItem( 'abc' );
  175. collection.add( item1 );
  176. collection.add( item2, 0 );
  177. collection.add( item3, 1 );
  178. collection.add( item4, 3 );
  179. expect( collection.get( 0 ) ).to.equal( item2 );
  180. expect( collection.get( 1 ) ).to.equal( item3 );
  181. expect( collection.get( 2 ) ).to.equal( item1 );
  182. expect( collection.get( 3 ) ).to.equal( item4 );
  183. } );
  184. it( 'should throw when index argument is invalid', () => {
  185. let collection = new Collection();
  186. let item1 = getItem( 'foo' );
  187. let item2 = getItem( 'bar' );
  188. let item3 = getItem( 'baz' );
  189. collection.add( item1 );
  190. expect( () => {
  191. collection.add( item2, -1 );
  192. } ).to.throw( /^collection-add-item-invalid-index/ );
  193. expect( () => {
  194. collection.add( item2, 2 );
  195. } ).to.throw( /^collection-add-item-invalid-index/ );
  196. collection.add( item2, 1 );
  197. collection.add( item3, 0 );
  198. expect( collection.length ).to.be.equal( 3 );
  199. } );
  200. it( 'should fire the "add" event with the index argument', () => {
  201. let spy = sinon.spy();
  202. collection.add( {} );
  203. collection.add( {} );
  204. collection.on( 'add', spy );
  205. const item = {};
  206. collection.add( item, 1 );
  207. sinon.assert.calledWithExactly( spy, sinon.match.has( 'source', collection ), item, 1 );
  208. } );
  209. } );
  210. describe( 'get', () => {
  211. it( 'should return an item', () => {
  212. let item = getItem( 'foo' );
  213. collection.add( item );
  214. expect( collection.get( 'foo' ) ).to.equal( item );
  215. } );
  216. it( 'should return null if id does not exist', () => {
  217. collection.add( getItem( 'foo' ) );
  218. expect( collection.get( 'bar' ) ).to.be.null;
  219. } );
  220. it( 'should throw if neither string or number given', () => {
  221. expect( () => {
  222. collection.get( true );
  223. } ).to.throw( CKEditorError, /^collection-get-invalid-arg/ );
  224. } );
  225. } );
  226. describe( 'remove', () => {
  227. it( 'should remove the model by index', () => {
  228. collection.add( getItem( 'bom' ) );
  229. collection.add( getItem( 'foo' ) );
  230. collection.add( getItem( 'bar' ) );
  231. expect( collection ).to.have.length( 3 );
  232. let removedItem = collection.remove( 1 );
  233. expect( collection ).to.have.length( 2 );
  234. expect( collection.get( 'foo' ) ).to.be.null;
  235. expect( collection.get( 1 ) ).to.have.property( 'id', 'bar' );
  236. expect( removedItem ).to.have.property( 'id', 'foo' );
  237. } );
  238. it( 'should remove the model by index - custom id property', () => {
  239. let collection = new Collection( { idProperty: 'name' } );
  240. collection.add( getItem( 'foo', 'name' ) );
  241. let removedItem = collection.remove( 0 );
  242. expect( collection ).to.have.length( 0 );
  243. expect( collection.get( 'foo' ) ).to.be.null;
  244. expect( removedItem ).to.have.property( 'name', 'foo' );
  245. } );
  246. it( 'should remove the model by id', () => {
  247. collection.add( getItem( 'bom' ) );
  248. collection.add( getItem( 'foo' ) );
  249. collection.add( getItem( 'bar' ) );
  250. expect( collection ).to.have.length( 3 );
  251. let removedItem = collection.remove( 'foo' );
  252. expect( collection ).to.have.length( 2 );
  253. expect( collection.get( 'foo' ) ).to.be.null;
  254. expect( collection.get( 1 ) ).to.have.property( 'id', 'bar' );
  255. expect( removedItem ).to.have.property( 'id', 'foo' );
  256. } );
  257. it( 'should remove the model by model', () => {
  258. let item = getItem( 'foo' );
  259. collection.add( getItem( 'bom' ) );
  260. collection.add( item );
  261. collection.add( getItem( 'bar' ) );
  262. expect( collection ).to.have.length( 3 );
  263. let removedItem = collection.remove( item );
  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.equal( item );
  268. } );
  269. it( 'should remove the model by model - custom id property', () => {
  270. let collection = new Collection( null, 'name' );
  271. let item = getItem( 'foo', 'name' );
  272. collection.add( item );
  273. let removedItem = collection.remove( item );
  274. expect( collection ).to.have.length( 0 );
  275. expect( collection.get( 'foo' ) ).to.be.null;
  276. expect( removedItem ).to.have.property( 'name', 'foo' );
  277. } );
  278. it( 'should fire the "remove" event', () => {
  279. let item1 = getItem( 'foo' );
  280. let item2 = getItem( 'bar' );
  281. let item3 = getItem( 'bom' );
  282. collection.add( item1 );
  283. collection.add( item2 );
  284. collection.add( item3 );
  285. let spy = sinon.spy();
  286. collection.on( 'remove', spy );
  287. collection.remove( 1 ); // by index
  288. collection.remove( item1 ); // by model
  289. collection.remove( 'bom' ); // by id
  290. sinon.assert.calledThrice( spy );
  291. sinon.assert.calledWithExactly( spy, sinon.match.has( 'source', collection ), item1 );
  292. sinon.assert.calledWithExactly( spy, sinon.match.has( 'source', collection ), item2 );
  293. sinon.assert.calledWithExactly( spy, sinon.match.has( 'source', collection ), item3 );
  294. } );
  295. it( 'should throw an error on invalid index', () => {
  296. collection.add( getItem( 'foo' ) );
  297. expect( () => {
  298. collection.remove( 1 );
  299. } ).to.throw( CKEditorError, /^collection-remove-404/ );
  300. expect( collection ).to.have.length( 1 );
  301. } );
  302. it( 'should throw an error on invalid id', () => {
  303. collection.add( getItem( 'foo' ) );
  304. expect( () => {
  305. collection.remove( 'bar' );
  306. } ).to.throw( CKEditorError, /^collection-remove-404/ );
  307. expect( collection ).to.have.length( 1 );
  308. } );
  309. it( 'should throw an error on invalid model', () => {
  310. collection.add( getItem( 'foo' ) );
  311. expect( () => {
  312. collection.remove( getItem( 'bar' ) );
  313. } ).to.throw( CKEditorError, /^collection-remove-404/ );
  314. expect( collection ).to.have.length( 1 );
  315. } );
  316. } );
  317. describe( 'map', () => {
  318. it( 'uses native map', () => {
  319. let spy = testUtils.sinon.stub( Array.prototype, 'map', () => {
  320. return [ 'foo' ];
  321. } );
  322. let ctx = {};
  323. let ret = collection.map( callback, ctx );
  324. sinon.assert.calledWithExactly( spy, callback, ctx );
  325. expect( ret ).to.deep.equal( [ 'foo' ], 'ret value was forwarded' );
  326. function callback() {}
  327. } );
  328. } );
  329. describe( 'find', () => {
  330. it( 'uses native find', () => {
  331. let needl = getItem( 'foo' );
  332. let spy = testUtils.sinon.stub( Array.prototype, 'find', () => {
  333. return needl;
  334. } );
  335. let ctx = {};
  336. let ret = collection.find( callback, ctx );
  337. sinon.assert.calledWithExactly( spy, callback, ctx );
  338. expect( ret ).to.equal( needl, 'ret value was forwarded' );
  339. function callback() {}
  340. } );
  341. } );
  342. describe( 'filter', () => {
  343. it( 'uses native filter', () => {
  344. let needl = getItem( 'foo' );
  345. let spy = testUtils.sinon.stub( Array.prototype, 'filter', () => {
  346. return [ needl ];
  347. } );
  348. let ctx = {};
  349. let ret = collection.filter( callback, ctx );
  350. sinon.assert.calledWithExactly( spy, callback, ctx );
  351. expect( ret ).to.deep.equal( [ needl ], 'ret value was forwarded' );
  352. function callback() {}
  353. } );
  354. } );
  355. describe( 'clear', () => {
  356. it( 'removes all items', () => {
  357. const items = [ {}, {}, {} ];
  358. const spy = sinon.spy();
  359. collection.on( 'remove', spy );
  360. items.forEach( i => collection.add( i ) );
  361. collection.clear();
  362. expect( spy.callCount ).to.equal( 3 );
  363. expect( collection.length ).to.equal( 0 );
  364. } );
  365. } );
  366. describe( 'iterator', () => {
  367. it( 'covers the whole collection', () => {
  368. let item1 = getItem( 'foo' );
  369. let item2 = getItem( 'bar' );
  370. let item3 = getItem( 'bom' );
  371. let items = [];
  372. collection.add( item1 );
  373. collection.add( item2 );
  374. collection.add( item3 );
  375. for ( let item of collection ) {
  376. items.push( item.id );
  377. }
  378. expect( items ).to.deep.equal( [ 'foo', 'bar', 'bom' ] );
  379. } );
  380. } );
  381. } );