8
0

namedcollection.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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( 'namedcollection', 'model', 'ckeditorerror' );
  7. describe( 'add', function() {
  8. it( 'changes the length and enables get', function() {
  9. var box = getCollection();
  10. expect( box ).to.have.length( 0 );
  11. var item = getItem( 'foo' );
  12. box.add( item );
  13. expect( box ).to.have.length( 1 );
  14. expect( box.get( 'foo' ) ).to.equal( item );
  15. } );
  16. it( 'fires the "add" event', function() {
  17. var spy = sinon.spy();
  18. var box = getCollection();
  19. box.on( 'add', spy );
  20. var item = getItem( 'foo' );
  21. box.add( item );
  22. sinon.assert.calledWithExactly( spy, sinon.match.has( 'source', box ), item );
  23. } );
  24. it( 'throws an error if model is not named', function() {
  25. var Model = modules.model;
  26. var CKEditorError = modules.ckeditorerror;
  27. var box = getCollection();
  28. expect( box ).to.have.length( 0 );
  29. var item = new Model();
  30. expect( function() {
  31. box.add( item );
  32. } ).to.throw( CKEditorError, /^namedcollection-add/ );
  33. } );
  34. it( 'throws an error if some model already exists under the same name', function() {
  35. var CKEditorError = modules.ckeditorerror;
  36. var box = getCollection();
  37. expect( box ).to.have.length( 0 );
  38. box.add( getItem( 'foo' ) );
  39. expect( function() {
  40. box.add( getItem( 'foo' ) );
  41. } ).to.throw( CKEditorError, /^namedcollection-add/ );
  42. } );
  43. } );
  44. describe( 'get', function() {
  45. it( 'should throw an error on invalid name', function() {
  46. var box = getCollection();
  47. box.add( getItem( 'foo' ) );
  48. expect( box.get( 'bar' ) ).to.be.null;
  49. } );
  50. } );
  51. describe( 'remove', function() {
  52. it( 'should remove the model by name', function() {
  53. var box = getCollection();
  54. var item = getItem( 'foo' );
  55. box.add( item );
  56. expect( box ).to.have.length( 1 );
  57. box.remove( 'foo' );
  58. expect( box ).to.have.length( 0 );
  59. } );
  60. it( 'should remove the model by model', function() {
  61. var box = getCollection();
  62. var item = getItem( 'foo' );
  63. box.add( item );
  64. expect( box ).to.have.length( 1 );
  65. box.remove( item );
  66. expect( box ).to.have.length( 0 );
  67. } );
  68. it( 'should fire the "remove" event', function() {
  69. var box = getCollection();
  70. var item1 = getItem( 'foo' );
  71. var item2 = getItem( 'bar' );
  72. box.add( item1 );
  73. box.add( item2 );
  74. var spy = sinon.spy();
  75. box.on( 'remove', spy );
  76. box.remove( 'foo' ); // by name
  77. box.remove( item2 ); // by model
  78. sinon.assert.calledTwice( spy );
  79. sinon.assert.calledWithExactly( spy, sinon.match.has( 'source', box ), item1 );
  80. sinon.assert.calledWithExactly( spy, sinon.match.has( 'source', box ), item2 );
  81. } );
  82. it( 'should throw an error if model is not named', function() {
  83. var CKEditorError = modules.ckeditorerror;
  84. var Model = modules.model;
  85. var box = getCollection();
  86. expect( function() {
  87. box.remove( new Model() );
  88. } ).to.throw( CKEditorError, /^namedcollection-remove/ );
  89. } );
  90. it( 'should throw an error if model does not exist (by name)', function() {
  91. var CKEditorError = modules.ckeditorerror;
  92. var box = getCollection();
  93. expect( function() {
  94. box.remove( 'foo' );
  95. } ).to.throw( CKEditorError, /^namedcollection-remove/ );
  96. } );
  97. it( 'should throw an error if model does not exist (by model)', function() {
  98. var CKEditorError = modules.ckeditorerror;
  99. var box = getCollection();
  100. expect( function() {
  101. box.remove( getItem( 'foo' ) );
  102. } ).to.throw( CKEditorError, /^namedcollection-remove/ );
  103. } );
  104. it( 'should throw an error if model does not exist (by model)', function() {
  105. var CKEditorError = modules.ckeditorerror;
  106. var box = getCollection();
  107. expect( function() {
  108. box.remove( getItem( 'foo' ) );
  109. } ).to.throw( CKEditorError, /^namedcollection-remove/ );
  110. } );
  111. it( 'should throw an error if a different model exists under the same name', function() {
  112. var CKEditorError = modules.ckeditorerror;
  113. var box = getCollection();
  114. var item = getItem( 'foo' );
  115. box.add( item );
  116. expect( function() {
  117. box.remove( getItem( 'foo' ) );
  118. } ).to.throw( CKEditorError, /^namedcollection-remove/ );
  119. } );
  120. } );
  121. describe( 'forEach', function() {
  122. it( 'executes callback for each item', function() {
  123. var collection = getCollection();
  124. collection.add( getItem( 'foo' ) );
  125. collection.add( getItem( 'bar' ) );
  126. var ctx = {};
  127. var models = [];
  128. var names = [];
  129. collection.forEach( callback, ctx );
  130. expect( models.sort() ).deep.equals( [ 'bar', 'foo' ] );
  131. expect( names.sort() ).deep.equals( [ 'bar', 'foo' ] );
  132. function callback( model, name ) {
  133. expect( this ).to.equal( ctx ); /* jshint ignore:line */
  134. models.push( model.name );
  135. names.push( name );
  136. }
  137. } );
  138. } );
  139. describe( 'find', function() {
  140. it( 'finds the right item', function() {
  141. var Model = modules.model;
  142. var collection = getCollection();
  143. var needl = getItem( 'foo' );
  144. collection.add( getItem( 'bar' ) );
  145. collection.add( needl );
  146. collection.add( getItem( 'bom' ) );
  147. var ctx = {};
  148. var ret = collection.find( callback, ctx );
  149. expect( ret ).to.equal( needl );
  150. function callback( model, name ) {
  151. expect( this ).to.equal( ctx ); /* jshint ignore:line */
  152. expect( model ).is.an.instanceof( Model );
  153. expect( name ).to.be.a( 'string' );
  154. return model.name == 'foo';
  155. }
  156. } );
  157. } );
  158. describe( 'filter', function() {
  159. it( 'finds the right items', function() {
  160. var Model = modules.model;
  161. var collection = getCollection();
  162. collection.add( getItem( 'bar' ) );
  163. collection.add( getItem( 'foo' ) );
  164. collection.add( getItem( 'bom' ) );
  165. var ctx = {};
  166. var ret = collection.filter( callback, ctx );
  167. expect( ret ).to.have.keys( [ 'bar', 'bom' ] );
  168. function callback( model, name ) {
  169. expect( this ).to.equal( ctx ); /* jshint ignore:line */
  170. expect( model ).is.an.instanceof( Model );
  171. expect( name ).to.be.a( 'string' );
  172. return model.name[ 0 ] == 'b';
  173. }
  174. } );
  175. } );
  176. function getCollection() {
  177. var NamedCollection = modules.namedcollection;
  178. return new NamedCollection();
  179. }
  180. function getItem( name ) {
  181. var Model = modules.model;
  182. var model = new Model();
  183. model.name = name;
  184. return model;
  185. }