8
0

collection.js 10.0 KB

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