8
0

collection.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  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( item ).to.have.property( 'id', '3' );
  115. }
  116. );
  117. it( 'should fire the "add" event', () => {
  118. let spy = sinon.spy();
  119. let item = {};
  120. collection.on( 'add', spy );
  121. collection.add( item );
  122. sinon.assert.calledWithExactly( spy, sinon.match.has( 'source', collection ), item, 0 );
  123. } );
  124. it( 'should support an optional index argument', () => {
  125. let collection = new Collection();
  126. let item1 = getItem( 'foo' );
  127. let item2 = getItem( 'bar' );
  128. let item3 = getItem( 'baz' );
  129. let item4 = getItem( 'abc' );
  130. collection.add( item1 );
  131. collection.add( item2, 0 );
  132. collection.add( item3, 1 );
  133. collection.add( item4, 3 );
  134. expect( collection.get( 0 ) ).to.equal( item2 );
  135. expect( collection.get( 1 ) ).to.equal( item3 );
  136. expect( collection.get( 2 ) ).to.equal( item1 );
  137. expect( collection.get( 3 ) ).to.equal( item4 );
  138. } );
  139. it( 'should throw when index argument is invalid', () => {
  140. let collection = new Collection();
  141. let item1 = getItem( 'foo' );
  142. let item2 = getItem( 'bar' );
  143. let item3 = getItem( 'baz' );
  144. collection.add( item1 );
  145. expect( () => {
  146. collection.add( item2, -1 );
  147. } ).to.throw( /^collection-add-item-invalid-index/ );
  148. expect( () => {
  149. collection.add( item2, 2 );
  150. } ).to.throw( /^collection-add-item-invalid-index/ );
  151. collection.add( item2, 1 );
  152. collection.add( item3, 0 );
  153. expect( collection.length ).to.be.equal( 3 );
  154. } );
  155. it( 'should fire the "add" event with the index argument', () => {
  156. let spy = sinon.spy();
  157. collection.add( {} );
  158. collection.add( {} );
  159. collection.on( 'add', spy );
  160. const item = {};
  161. collection.add( item, 1 );
  162. sinon.assert.calledWithExactly( spy, sinon.match.has( 'source', collection ), item, 1 );
  163. } );
  164. } );
  165. describe( 'get', () => {
  166. it( 'should return an item', () => {
  167. let item = getItem( 'foo' );
  168. collection.add( item );
  169. expect( collection.get( 'foo' ) ).to.equal( item );
  170. } );
  171. it( 'should return null if id does not exist', () => {
  172. collection.add( getItem( 'foo' ) );
  173. expect( collection.get( 'bar' ) ).to.be.null;
  174. } );
  175. it( 'should throw if neither string or number given', () => {
  176. expect( () => {
  177. collection.get( true );
  178. } ).to.throw( CKEditorError, /^collection-get-invalid-arg/ );
  179. } );
  180. } );
  181. describe( 'remove', () => {
  182. it( 'should remove the model by index', () => {
  183. collection.add( getItem( 'bom' ) );
  184. collection.add( getItem( 'foo' ) );
  185. collection.add( getItem( 'bar' ) );
  186. expect( collection ).to.have.length( 3 );
  187. let removedItem = collection.remove( 1 );
  188. expect( collection ).to.have.length( 2 );
  189. expect( collection.get( 'foo' ) ).to.be.null;
  190. expect( collection.get( 1 ) ).to.have.property( 'id', 'bar' );
  191. expect( removedItem ).to.have.property( 'id', 'foo' );
  192. } );
  193. it( 'should remove the model by index - custom id property', () => {
  194. let collection = new Collection( { idProperty: 'name' } );
  195. collection.add( getItem( 'foo', 'name' ) );
  196. let removedItem = collection.remove( 0 );
  197. expect( collection ).to.have.length( 0 );
  198. expect( collection.get( 'foo' ) ).to.be.null;
  199. expect( removedItem ).to.have.property( 'name', 'foo' );
  200. } );
  201. it( 'should remove the model by id', () => {
  202. collection.add( getItem( 'bom' ) );
  203. collection.add( getItem( 'foo' ) );
  204. collection.add( getItem( 'bar' ) );
  205. expect( collection ).to.have.length( 3 );
  206. let removedItem = collection.remove( 'foo' );
  207. expect( collection ).to.have.length( 2 );
  208. expect( collection.get( 'foo' ) ).to.be.null;
  209. expect( collection.get( 1 ) ).to.have.property( 'id', 'bar' );
  210. expect( removedItem ).to.have.property( 'id', 'foo' );
  211. } );
  212. it( 'should remove the model by model', () => {
  213. let item = getItem( 'foo' );
  214. collection.add( getItem( 'bom' ) );
  215. collection.add( item );
  216. collection.add( getItem( 'bar' ) );
  217. expect( collection ).to.have.length( 3 );
  218. let removedItem = collection.remove( item );
  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.equal( item );
  223. } );
  224. it( 'should remove the model by model - custom id property', () => {
  225. let collection = new Collection( null, 'name' );
  226. let item = getItem( 'foo', 'name' );
  227. collection.add( item );
  228. let removedItem = collection.remove( item );
  229. expect( collection ).to.have.length( 0 );
  230. expect( collection.get( 'foo' ) ).to.be.null;
  231. expect( removedItem ).to.have.property( 'name', 'foo' );
  232. } );
  233. it( 'should fire the "remove" event', () => {
  234. let item1 = getItem( 'foo' );
  235. let item2 = getItem( 'bar' );
  236. let item3 = getItem( 'bom' );
  237. collection.add( item1 );
  238. collection.add( item2 );
  239. collection.add( item3 );
  240. let spy = sinon.spy();
  241. collection.on( 'remove', spy );
  242. collection.remove( 1 ); // by index
  243. collection.remove( item1 ); // by model
  244. collection.remove( 'bom' ); // by id
  245. sinon.assert.calledThrice( spy );
  246. sinon.assert.calledWithExactly( spy, sinon.match.has( 'source', collection ), item1 );
  247. sinon.assert.calledWithExactly( spy, sinon.match.has( 'source', collection ), item2 );
  248. sinon.assert.calledWithExactly( spy, sinon.match.has( 'source', collection ), item3 );
  249. } );
  250. it( 'should throw an error on invalid index', () => {
  251. collection.add( getItem( 'foo' ) );
  252. expect( () => {
  253. collection.remove( 1 );
  254. } ).to.throw( CKEditorError, /^collection-remove-404/ );
  255. expect( collection ).to.have.length( 1 );
  256. } );
  257. it( 'should throw an error on invalid id', () => {
  258. collection.add( getItem( 'foo' ) );
  259. expect( () => {
  260. collection.remove( 'bar' );
  261. } ).to.throw( CKEditorError, /^collection-remove-404/ );
  262. expect( collection ).to.have.length( 1 );
  263. } );
  264. it( 'should throw an error on invalid model', () => {
  265. collection.add( getItem( 'foo' ) );
  266. expect( () => {
  267. collection.remove( getItem( 'bar' ) );
  268. } ).to.throw( CKEditorError, /^collection-remove-404/ );
  269. expect( collection ).to.have.length( 1 );
  270. } );
  271. } );
  272. describe( 'map', () => {
  273. it( 'uses native map', () => {
  274. let spy = bender.sinon.stub( Array.prototype, 'map', () => {
  275. return [ 'foo' ];
  276. } );
  277. let ctx = {};
  278. let ret = collection.map( callback, ctx );
  279. sinon.assert.calledWithExactly( spy, callback, ctx );
  280. expect( ret ).to.deep.equal( [ 'foo' ], 'ret value was forwarded' );
  281. function callback() {}
  282. } );
  283. } );
  284. describe( 'find', () => {
  285. it( 'uses native find', () => {
  286. let needl = getItem( 'foo' );
  287. let spy = bender.sinon.stub( Array.prototype, 'find', () => {
  288. return needl;
  289. } );
  290. let ctx = {};
  291. let ret = collection.find( callback, ctx );
  292. sinon.assert.calledWithExactly( spy, callback, ctx );
  293. expect( ret ).to.equal( needl, 'ret value was forwarded' );
  294. function callback() {}
  295. } );
  296. } );
  297. describe( 'filter', () => {
  298. it( 'uses native filter', () => {
  299. let needl = getItem( 'foo' );
  300. let spy = bender.sinon.stub( Array.prototype, 'filter', () => {
  301. return [ needl ];
  302. } );
  303. let ctx = {};
  304. let ret = collection.filter( callback, ctx );
  305. sinon.assert.calledWithExactly( spy, callback, ctx );
  306. expect( ret ).to.deep.equal( [ needl ], 'ret value was forwarded' );
  307. function callback() {}
  308. } );
  309. } );
  310. describe( 'iterator', () => {
  311. it( 'covers the whole collection', () => {
  312. let item1 = getItem( 'foo' );
  313. let item2 = getItem( 'bar' );
  314. let item3 = getItem( 'bom' );
  315. let items = [];
  316. collection.add( item1 );
  317. collection.add( item2 );
  318. collection.add( item3 );
  319. for ( let item of collection ) {
  320. items.push( item.id );
  321. }
  322. expect( items ).to.deep.equal( [ 'foo', 'bar', 'bom' ] );
  323. } );
  324. } );
  325. } );