8
0

namedcollection.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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 CKEditorError = modules.ckeditorerror;
  47. var box = getCollection();
  48. box.add( getItem( 'foo' ) );
  49. expect( function() {
  50. box.get( 'bar' );
  51. } ).to.throw( CKEditorError, /^namedcollection-get/ );
  52. } );
  53. } );
  54. describe( 'remove', function() {
  55. it( 'should remove the model by name', function() {
  56. var box = getCollection();
  57. var item = getItem( 'foo' );
  58. box.add( item );
  59. expect( box ).to.have.length( 1 );
  60. box.remove( 'foo' );
  61. expect( box ).to.have.length( 0 );
  62. } );
  63. it( 'should remove the model by model', function() {
  64. var box = getCollection();
  65. var item = getItem( 'foo' );
  66. box.add( item );
  67. expect( box ).to.have.length( 1 );
  68. box.remove( item );
  69. expect( box ).to.have.length( 0 );
  70. } );
  71. it( 'should fire the "remove" event', function() {
  72. var box = getCollection();
  73. var item1 = getItem( 'foo' );
  74. var item2 = getItem( 'bar' );
  75. box.add( item1 );
  76. box.add( item2 );
  77. var spy = sinon.spy();
  78. box.on( 'remove', spy );
  79. box.remove( 'foo' ); // by name
  80. box.remove( item2 ); // by model
  81. sinon.assert.calledTwice( spy );
  82. sinon.assert.calledWithExactly( spy, sinon.match.has( 'source', box ), item1 );
  83. sinon.assert.calledWithExactly( spy, sinon.match.has( 'source', box ), item2 );
  84. } );
  85. it( 'should throw an error if model is not named', function() {
  86. var CKEditorError = modules.ckeditorerror;
  87. var Model = modules.model;
  88. var box = getCollection();
  89. expect( function() {
  90. box.remove( new Model() );
  91. } ).to.throw( CKEditorError, /^namedcollection-remove/ );
  92. } );
  93. it( 'should throw an error if model does not exist (by name)', function() {
  94. var CKEditorError = modules.ckeditorerror;
  95. var box = getCollection();
  96. expect( function() {
  97. box.remove( 'foo' );
  98. } ).to.throw( CKEditorError, /^namedcollection-remove/ );
  99. } );
  100. it( 'should throw an error if model does not exist (by model)', function() {
  101. var CKEditorError = modules.ckeditorerror;
  102. var box = getCollection();
  103. expect( function() {
  104. box.remove( getItem( 'foo' ) );
  105. } ).to.throw( CKEditorError, /^namedcollection-remove/ );
  106. } );
  107. it( 'should throw an error if model does not exist (by model)', function() {
  108. var CKEditorError = modules.ckeditorerror;
  109. var box = getCollection();
  110. expect( function() {
  111. box.remove( getItem( 'foo' ) );
  112. } ).to.throw( CKEditorError, /^namedcollection-remove/ );
  113. } );
  114. it( 'should throw an error if a different model exists under the same name', function() {
  115. var CKEditorError = modules.ckeditorerror;
  116. var box = getCollection();
  117. var item = getItem( 'foo' );
  118. box.add( item );
  119. expect( function() {
  120. box.remove( getItem( 'foo' ) );
  121. } ).to.throw( CKEditorError, /^namedcollection-remove/ );
  122. } );
  123. } );
  124. describe( 'forEach', function() {
  125. it( 'should iterate over the models', function() {
  126. var box = getCollection();
  127. var item1 = getItem( 'foo' );
  128. var item2 = getItem( 'bar' );
  129. box.add( item1 );
  130. box.add( item2 );
  131. expect( box ).to.have.length( 2 );
  132. var spy = sinon.spy();
  133. box.forEach( spy );
  134. sinon.assert.callOrder( spy.withArgs( item1, 'foo' ), spy.withArgs( item2, 'bar' ) );
  135. } );
  136. } );
  137. function getCollection() {
  138. var NamedCollection = modules.namedcollection;
  139. return new NamedCollection();
  140. }
  141. function getItem( name ) {
  142. var Model = modules.model;
  143. var model = new Model();
  144. model.name = name;
  145. return model;
  146. }