collection.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497
  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 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( 'remove', () => {
  213. it( 'should remove the model by index', () => {
  214. collection.add( getItem( 'bom' ) );
  215. collection.add( getItem( 'foo' ) );
  216. collection.add( getItem( 'bar' ) );
  217. expect( collection ).to.have.length( 3 );
  218. let removedItem = collection.remove( 1 );
  219. expect( collection ).to.have.length( 2 );
  220. expect( collection.get( 'foo' ) ).to.be.null;
  221. expect( collection.get( 1 ) ).to.have.property( 'id', 'bar' );
  222. expect( removedItem ).to.have.property( 'id', 'foo' );
  223. } );
  224. it( 'should remove the model by index - custom id property', () => {
  225. let collection = new Collection( { idProperty: 'name' } );
  226. collection.add( getItem( 'foo', 'name' ) );
  227. let removedItem = collection.remove( 0 );
  228. expect( collection ).to.have.length( 0 );
  229. expect( collection.get( 'foo' ) ).to.be.null;
  230. expect( removedItem ).to.have.property( 'name', 'foo' );
  231. } );
  232. it( 'should remove the model by id', () => {
  233. collection.add( getItem( 'bom' ) );
  234. collection.add( getItem( 'foo' ) );
  235. collection.add( getItem( 'bar' ) );
  236. expect( collection ).to.have.length( 3 );
  237. let removedItem = collection.remove( 'foo' );
  238. expect( collection ).to.have.length( 2 );
  239. expect( collection.get( 'foo' ) ).to.be.null;
  240. expect( collection.get( 1 ) ).to.have.property( 'id', 'bar' );
  241. expect( removedItem ).to.have.property( 'id', 'foo' );
  242. } );
  243. it( 'should remove the model by model', () => {
  244. let item = getItem( 'foo' );
  245. collection.add( getItem( 'bom' ) );
  246. collection.add( item );
  247. collection.add( getItem( 'bar' ) );
  248. expect( collection ).to.have.length( 3 );
  249. let removedItem = collection.remove( item );
  250. expect( collection ).to.have.length( 2 );
  251. expect( collection.get( 'foo' ) ).to.be.null;
  252. expect( collection.get( 1 ) ).to.have.property( 'id', 'bar' );
  253. expect( removedItem ).to.equal( item );
  254. } );
  255. it( 'should remove the model by model - custom id property', () => {
  256. let collection = new Collection( null, 'name' );
  257. let item = getItem( 'foo', 'name' );
  258. collection.add( item );
  259. let removedItem = collection.remove( item );
  260. expect( collection ).to.have.length( 0 );
  261. expect( collection.get( 'foo' ) ).to.be.null;
  262. expect( removedItem ).to.have.property( 'name', 'foo' );
  263. } );
  264. it( 'should fire the "remove" event', () => {
  265. let item1 = getItem( 'foo' );
  266. let item2 = getItem( 'bar' );
  267. let item3 = getItem( 'bom' );
  268. collection.add( item1 );
  269. collection.add( item2 );
  270. collection.add( item3 );
  271. let spy = sinon.spy();
  272. collection.on( 'remove', spy );
  273. collection.remove( 1 ); // by index
  274. collection.remove( item1 ); // by model
  275. collection.remove( 'bom' ); // by id
  276. sinon.assert.calledThrice( spy );
  277. sinon.assert.calledWithExactly( spy, sinon.match.has( 'source', collection ), item1 );
  278. sinon.assert.calledWithExactly( spy, sinon.match.has( 'source', collection ), item2 );
  279. sinon.assert.calledWithExactly( spy, sinon.match.has( 'source', collection ), item3 );
  280. } );
  281. it( 'should throw an error on invalid index', () => {
  282. collection.add( getItem( 'foo' ) );
  283. expect( () => {
  284. collection.remove( 1 );
  285. } ).to.throw( CKEditorError, /^collection-remove-404/ );
  286. expect( collection ).to.have.length( 1 );
  287. } );
  288. it( 'should throw an error on invalid id', () => {
  289. collection.add( getItem( 'foo' ) );
  290. expect( () => {
  291. collection.remove( 'bar' );
  292. } ).to.throw( CKEditorError, /^collection-remove-404/ );
  293. expect( collection ).to.have.length( 1 );
  294. } );
  295. it( 'should throw an error on invalid model', () => {
  296. collection.add( getItem( 'foo' ) );
  297. expect( () => {
  298. collection.remove( getItem( 'bar' ) );
  299. } ).to.throw( CKEditorError, /^collection-remove-404/ );
  300. expect( collection ).to.have.length( 1 );
  301. } );
  302. } );
  303. describe( 'map', () => {
  304. it( 'uses native map', () => {
  305. let spy = testUtils.sinon.stub( Array.prototype, 'map', () => {
  306. return [ 'foo' ];
  307. } );
  308. let ctx = {};
  309. let ret = collection.map( callback, ctx );
  310. sinon.assert.calledWithExactly( spy, callback, ctx );
  311. expect( ret ).to.deep.equal( [ 'foo' ], 'ret value was forwarded' );
  312. function callback() {}
  313. } );
  314. } );
  315. describe( 'find', () => {
  316. it( 'uses native find', () => {
  317. let needl = getItem( 'foo' );
  318. let spy = testUtils.sinon.stub( Array.prototype, 'find', () => {
  319. return needl;
  320. } );
  321. let ctx = {};
  322. let ret = collection.find( callback, ctx );
  323. sinon.assert.calledWithExactly( spy, callback, ctx );
  324. expect( ret ).to.equal( needl, 'ret value was forwarded' );
  325. function callback() {}
  326. } );
  327. } );
  328. describe( 'filter', () => {
  329. it( 'uses native filter', () => {
  330. let needl = getItem( 'foo' );
  331. let spy = testUtils.sinon.stub( Array.prototype, 'filter', () => {
  332. return [ needl ];
  333. } );
  334. let ctx = {};
  335. let ret = collection.filter( callback, ctx );
  336. sinon.assert.calledWithExactly( spy, callback, ctx );
  337. expect( ret ).to.deep.equal( [ needl ], 'ret value was forwarded' );
  338. function callback() {}
  339. } );
  340. } );
  341. describe( 'clear', () => {
  342. it( 'removes all items', () => {
  343. const items = [ {}, {}, {} ];
  344. const spy = sinon.spy();
  345. collection.on( 'remove', spy );
  346. items.forEach( i => collection.add( i ) );
  347. collection.clear();
  348. expect( spy.callCount ).to.equal( 3 );
  349. expect( collection.length ).to.equal( 0 );
  350. } );
  351. } );
  352. describe( 'iterator', () => {
  353. it( 'covers the whole collection', () => {
  354. let item1 = getItem( 'foo' );
  355. let item2 = getItem( 'bar' );
  356. let item3 = getItem( 'bom' );
  357. let items = [];
  358. collection.add( item1 );
  359. collection.add( item2 );
  360. collection.add( item3 );
  361. for ( let item of collection ) {
  362. items.push( item.id );
  363. }
  364. expect( items ).to.deep.equal( [ 'foo', 'bar', 'bom' ] );
  365. } );
  366. } );
  367. } );