8
0

collection.js 10 KB

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