namedcollection.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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( 'should iterate over the models', function() {
  123. var box = getCollection();
  124. var item1 = getItem( 'foo' );
  125. var item2 = getItem( 'bar' );
  126. box.add( item1 );
  127. box.add( item2 );
  128. expect( box ).to.have.length( 2 );
  129. var spy = sinon.spy();
  130. box.forEach( spy );
  131. sinon.assert.callOrder( spy.withArgs( item1, 'foo' ), spy.withArgs( item2, 'bar' ) );
  132. } );
  133. } );
  134. function getCollection() {
  135. var NamedCollection = modules.namedcollection;
  136. return new NamedCollection();
  137. }
  138. function getItem( name ) {
  139. var Model = modules.model;
  140. var model = new Model();
  141. model.name = name;
  142. return model;
  143. }