collection.js 13 KB

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