collection.js 10.0 KB

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