collection.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /**
  2. * @license Copyright (c) 2003-2015, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* globals describe, it, expect, bender, sinon */
  6. 'use strict';
  7. var modules = bender.amd.require( 'mvc/collection', 'mvc/model' );
  8. describe( 'add', function() {
  9. it( 'should change the length and enable get', function() {
  10. var Model = modules[ 'mvc/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 throw an error on invalid index', function() {
  28. var box = getCollection();
  29. box.add( getItem() );
  30. expect( function() {
  31. box.get( 1 );
  32. } ).to.throw( Error, 'Index not found' );
  33. } );
  34. } );
  35. describe( 'remove', function() {
  36. it( 'should remove the model by index', function() {
  37. var box = getCollection();
  38. var item = getItem();
  39. box.add( item );
  40. expect( box ).to.have.length( 1 );
  41. box.remove( 0 );
  42. expect( box ).to.have.length( 0 );
  43. } );
  44. it( 'should remove the model by model', function() {
  45. var box = getCollection();
  46. var item = getItem();
  47. box.add( item );
  48. expect( box ).to.have.length( 1 );
  49. box.remove( item );
  50. expect( box ).to.have.length( 0 );
  51. } );
  52. it( 'should fire the "remove" event', function() {
  53. var box = getCollection();
  54. var item1 = getItem();
  55. var item2 = getItem();
  56. box.add( item1 );
  57. box.add( item2 );
  58. var spy = sinon.spy();
  59. box.on( 'remove', spy );
  60. box.remove( 1 ); // by index
  61. box.remove( item1 ); // by model
  62. sinon.assert.calledTwice( spy );
  63. sinon.assert.calledWithExactly( spy, sinon.match.has( 'source', box ), item1 );
  64. sinon.assert.calledWithExactly( spy, sinon.match.has( 'source', box ), item2 );
  65. } );
  66. it( 'should throw an error on invalid index', function() {
  67. var box = getCollection();
  68. box.add( getItem() );
  69. expect( function() {
  70. box.remove( 1 );
  71. } ).to.throw( Error, 'Index not found' );
  72. } );
  73. it( 'should throw an error on invalid model', function() {
  74. var box = getCollection();
  75. box.add( getItem() );
  76. expect( function() {
  77. box.remove( getItem() );
  78. } ).to.throw( Error, 'Model not found' );
  79. } );
  80. } );
  81. function getCollection() {
  82. var Collection = modules[ 'mvc/collection' ];
  83. return new Collection();
  84. }
  85. function getItem() {
  86. var Model = modules[ 'mvc/model' ];
  87. return new Model();
  88. }