| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205 |
- /**
- * @license Copyright (c) 2003-2015, CKSource - Frederico Knabben. All rights reserved.
- * For licensing, see LICENSE.md.
- */
- 'use strict';
- var modules = bender.amd.require( 'namedcollection', 'model', 'ckeditorerror' );
- describe( 'add', function() {
- it( 'changes the length and enables get', function() {
- var box = getCollection();
- expect( box ).to.have.length( 0 );
- var item = getItem( 'foo' );
- box.add( item );
- expect( box ).to.have.length( 1 );
- expect( box.get( 'foo' ) ).to.equal( item );
- } );
- it( 'fires the "add" event', function() {
- var spy = sinon.spy();
- var box = getCollection();
- box.on( 'add', spy );
- var item = getItem( 'foo' );
- box.add( item );
- sinon.assert.calledWithExactly( spy, sinon.match.has( 'source', box ), item );
- } );
- it( 'throws an error if model is not named', function() {
- var Model = modules.model;
- var CKEditorError = modules.ckeditorerror;
- var box = getCollection();
- expect( box ).to.have.length( 0 );
- var item = new Model();
- expect( function() {
- box.add( item );
- } ).to.throw( CKEditorError, /^namedcollection-add/ );
- } );
- it( 'throws an error if some model already exists under the same name', function() {
- var CKEditorError = modules.ckeditorerror;
- var box = getCollection();
- expect( box ).to.have.length( 0 );
- box.add( getItem( 'foo' ) );
- expect( function() {
- box.add( getItem( 'foo' ) );
- } ).to.throw( CKEditorError, /^namedcollection-add/ );
- } );
- } );
- describe( 'get', function() {
- it( 'should throw an error on invalid name', function() {
- var CKEditorError = modules.ckeditorerror;
- var box = getCollection();
- box.add( getItem( 'foo' ) );
- expect( function() {
- box.get( 'bar' );
- } ).to.throw( CKEditorError, /^namedcollection-get/ );
- } );
- } );
- describe( 'remove', function() {
- it( 'should remove the model by name', function() {
- var box = getCollection();
- var item = getItem( 'foo' );
- box.add( item );
- expect( box ).to.have.length( 1 );
- box.remove( 'foo' );
- expect( box ).to.have.length( 0 );
- } );
- it( 'should remove the model by model', function() {
- var box = getCollection();
- var item = getItem( 'foo' );
- box.add( item );
- expect( box ).to.have.length( 1 );
- box.remove( item );
- expect( box ).to.have.length( 0 );
- } );
- it( 'should fire the "remove" event', function() {
- var box = getCollection();
- var item1 = getItem( 'foo' );
- var item2 = getItem( 'bar' );
- box.add( item1 );
- box.add( item2 );
- var spy = sinon.spy();
- box.on( 'remove', spy );
- box.remove( 'foo' ); // by name
- box.remove( item2 ); // by model
- sinon.assert.calledTwice( spy );
- sinon.assert.calledWithExactly( spy, sinon.match.has( 'source', box ), item1 );
- sinon.assert.calledWithExactly( spy, sinon.match.has( 'source', box ), item2 );
- } );
- it( 'should throw an error if model is not named', function() {
- var CKEditorError = modules.ckeditorerror;
- var Model = modules.model;
- var box = getCollection();
- expect( function() {
- box.remove( new Model() );
- } ).to.throw( CKEditorError, /^namedcollection-remove/ );
- } );
- it( 'should throw an error if model does not exist (by name)', function() {
- var CKEditorError = modules.ckeditorerror;
- var box = getCollection();
- expect( function() {
- box.remove( 'foo' );
- } ).to.throw( CKEditorError, /^namedcollection-remove/ );
- } );
- it( 'should throw an error if model does not exist (by model)', function() {
- var CKEditorError = modules.ckeditorerror;
- var box = getCollection();
- expect( function() {
- box.remove( getItem( 'foo' ) );
- } ).to.throw( CKEditorError, /^namedcollection-remove/ );
- } );
- it( 'should throw an error if model does not exist (by model)', function() {
- var CKEditorError = modules.ckeditorerror;
- var box = getCollection();
- expect( function() {
- box.remove( getItem( 'foo' ) );
- } ).to.throw( CKEditorError, /^namedcollection-remove/ );
- } );
- it( 'should throw an error if a different model exists under the same name', function() {
- var CKEditorError = modules.ckeditorerror;
- var box = getCollection();
- var item = getItem( 'foo' );
- box.add( item );
- expect( function() {
- box.remove( getItem( 'foo' ) );
- } ).to.throw( CKEditorError, /^namedcollection-remove/ );
- } );
- } );
- describe( 'forEach', function() {
- it( 'should iterate over the models', function() {
- var box = getCollection();
- var item1 = getItem( 'foo' );
- var item2 = getItem( 'bar' );
- box.add( item1 );
- box.add( item2 );
- expect( box ).to.have.length( 2 );
- var spy = sinon.spy();
- box.forEach( spy );
- sinon.assert.callOrder( spy.withArgs( item1, 'foo' ), spy.withArgs( item2, 'bar' ) );
- } );
- } );
- function getCollection() {
- var NamedCollection = modules.namedcollection;
- return new NamedCollection();
- }
- function getItem( name ) {
- var Model = modules.model;
- var model = new Model();
- model.name = name;
- return model;
- }
|