collection.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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', 'model' );
  7. bender.tools.createSinonSandbox();
  8. describe( 'add', function() {
  9. it( 'should change the length and enable get', function() {
  10. var Model = modules.model;
  11. var box = getCollection();
  12. expect( box ).to.have.length( 0 );
  13. box.add( getItem() );
  14. expect( box ).to.have.length( 1 );
  15. expect( box.get( 0 ) ).to.be.an.instanceof( Model );
  16. } );
  17. it( 'should fire the "add" event', function() {
  18. var spy = sinon.spy();
  19. var box = getCollection();
  20. box.on( 'add', spy );
  21. var item = getItem();
  22. box.add( item );
  23. sinon.assert.calledWithExactly( spy, sinon.match.has( 'source', box ), item );
  24. } );
  25. } );
  26. describe( 'get', function() {
  27. it( 'should return null if index does not exist', function() {
  28. var box = getCollection();
  29. box.add( getItem() );
  30. expect( box.get( 1 ) ).to.be.null;
  31. } );
  32. } );
  33. describe( 'remove', function() {
  34. it( 'should remove the model by index', function() {
  35. var box = getCollection();
  36. var item = getItem();
  37. box.add( item );
  38. expect( box ).to.have.length( 1 );
  39. box.remove( 0 );
  40. expect( box ).to.have.length( 0 );
  41. } );
  42. it( 'should remove the model by model', function() {
  43. var box = getCollection();
  44. var item = getItem();
  45. box.add( item );
  46. expect( box ).to.have.length( 1 );
  47. box.remove( item );
  48. expect( box ).to.have.length( 0 );
  49. } );
  50. it( 'should fire the "remove" event', function() {
  51. var box = getCollection();
  52. var item1 = getItem();
  53. var item2 = getItem();
  54. box.add( item1 );
  55. box.add( item2 );
  56. var spy = sinon.spy();
  57. box.on( 'remove', spy );
  58. box.remove( 1 ); // by index
  59. box.remove( item1 ); // by model
  60. sinon.assert.calledTwice( spy );
  61. sinon.assert.calledWithExactly( spy, sinon.match.has( 'source', box ), item1 );
  62. sinon.assert.calledWithExactly( spy, sinon.match.has( 'source', box ), item2 );
  63. } );
  64. it( 'should throw an error on invalid index', function() {
  65. var CKEditorError = modules.ckeditorerror;
  66. var box = getCollection();
  67. box.add( getItem() );
  68. expect( function() {
  69. box.remove( 1 );
  70. } ).to.throw( CKEditorError, /^collection-index-404/ );
  71. } );
  72. it( 'should throw an error on invalid model', function() {
  73. var CKEditorError = modules.ckeditorerror;
  74. var box = getCollection();
  75. box.add( getItem() );
  76. expect( function() {
  77. box.remove( getItem() );
  78. } ).to.throw( CKEditorError, /^collection-model-404/ );
  79. } );
  80. } );
  81. describe( 'forEach', function() {
  82. it( 'uses native forEach', function() {
  83. var collection = getCollection();
  84. collection.add( getItem() );
  85. var spy = bender.sinon.spy( Array.prototype, 'forEach' );
  86. var ctx = {};
  87. collection.forEach( callback, ctx );
  88. sinon.assert.calledWithExactly( spy, callback, ctx );
  89. function callback() {}
  90. } );
  91. } );
  92. describe( 'find', function() {
  93. it( 'uses native find', function() {
  94. var collection = getCollection();
  95. var needl = getItem();
  96. var spy = bender.sinon.stub( Array.prototype, 'find', function() {
  97. return needl;
  98. } );
  99. var ctx = {};
  100. var ret = collection.find( callback, ctx );
  101. sinon.assert.calledWithExactly( spy, callback, ctx );
  102. expect( ret ).to.equal( needl, 'ret value was forwarded' );
  103. function callback() {}
  104. } );
  105. } );
  106. describe( 'filter', function() {
  107. it( 'uses native filter', function() {
  108. var collection = getCollection();
  109. var needl = getItem();
  110. var spy = bender.sinon.stub( Array.prototype, 'filter', function() {
  111. return needl;
  112. } );
  113. var ctx = {};
  114. var ret = collection.filter( callback, ctx );
  115. sinon.assert.calledWithExactly( spy, callback, ctx );
  116. expect( ret ).to.equal( needl, 'ret value was forwarded' );
  117. function callback() {}
  118. } );
  119. } );
  120. function getCollection() {
  121. var Collection = modules.collection;
  122. return new Collection();
  123. }
  124. function getItem() {
  125. var Model = modules.model;
  126. return new Model();
  127. }