8
0

collection.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. describe( 'add', function() {
  8. it( 'should change the length and enable get', function() {
  9. var Model = modules.model;
  10. var box = getCollection();
  11. expect( box ).to.have.length( 0 );
  12. box.add( getItem() );
  13. expect( box ).to.have.length( 1 );
  14. expect( box.get( 0 ) ).to.be.an.instanceof( Model );
  15. } );
  16. it( 'should fire the "add" event', function() {
  17. var spy = sinon.spy();
  18. var box = getCollection();
  19. box.on( 'add', spy );
  20. var item = getItem();
  21. box.add( item );
  22. sinon.assert.calledWithExactly( spy, sinon.match.has( 'source', box ), item );
  23. } );
  24. } );
  25. describe( 'get', function() {
  26. it( 'should throw an error on invalid index', function() {
  27. var box = getCollection();
  28. box.add( getItem() );
  29. expect( function() {
  30. box.get( 1 );
  31. } ).to.throw( Error, 'Index not found' );
  32. } );
  33. } );
  34. describe( 'remove', function() {
  35. it( 'should remove the model by index', function() {
  36. var box = getCollection();
  37. var item = getItem();
  38. box.add( item );
  39. expect( box ).to.have.length( 1 );
  40. box.remove( 0 );
  41. expect( box ).to.have.length( 0 );
  42. } );
  43. it( 'should remove the model by model', function() {
  44. var box = getCollection();
  45. var item = getItem();
  46. box.add( item );
  47. expect( box ).to.have.length( 1 );
  48. box.remove( item );
  49. expect( box ).to.have.length( 0 );
  50. } );
  51. it( 'should fire the "remove" event', function() {
  52. var box = getCollection();
  53. var item1 = getItem();
  54. var item2 = getItem();
  55. box.add( item1 );
  56. box.add( item2 );
  57. var spy = sinon.spy();
  58. box.on( 'remove', spy );
  59. box.remove( 1 ); // by index
  60. box.remove( item1 ); // by model
  61. sinon.assert.calledTwice( spy );
  62. sinon.assert.calledWithExactly( spy, sinon.match.has( 'source', box ), item1 );
  63. sinon.assert.calledWithExactly( spy, sinon.match.has( 'source', box ), item2 );
  64. } );
  65. it( 'should throw an error on invalid index', function() {
  66. var CKEditorError = modules.ckeditorerror;
  67. var box = getCollection();
  68. box.add( getItem() );
  69. expect( function() {
  70. box.remove( 1 );
  71. } ).to.throw( CKEditorError, /^collection-index-404/ );
  72. } );
  73. it( 'should throw an error on invalid model', function() {
  74. var CKEditorError = modules.ckeditorerror;
  75. var box = getCollection();
  76. box.add( getItem() );
  77. expect( function() {
  78. box.remove( getItem() );
  79. } ).to.throw( CKEditorError, /^collection-model-404/ );
  80. } );
  81. } );
  82. function getCollection() {
  83. var Collection = modules.collection;
  84. return new Collection();
  85. }
  86. function getItem() {
  87. var Model = modules.model;
  88. return new Model();
  89. }