collection.js 14 KB

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