collection.js 9.9 KB

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