collection.js 13 KB

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