collection.js 13 KB

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