| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120 |
- /**
- * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
- * For licensing, see LICENSE.md.
- */
- /* bender-tags: ui */
- 'use strict';
- const modules = bender.amd.require( 'ckeditor',
- 'core/ui/controllercollection',
- 'core/ui/controller',
- 'core/ui/view'
- );
- bender.tools.createSinonSandbox();
- let ControllerCollection, Controller, View;
- let ParentView;
- describe( 'ControllerCollection', () => {
- beforeEach( updateModuleReference );
- beforeEach( defineParentViewClass );
- describe( 'constructor', () => {
- it( 'should throw when no name is passed', () => {
- expect( () => {
- new ControllerCollection();
- } ).to.throw( /^ui-controllercollection-no-name/ );
- } );
- } );
- describe( 'add', () => {
- it( 'should add a child controller and return promise', () => {
- const parentController = new Controller();
- const childController = new Controller();
- const collection = new ControllerCollection( 'x' );
- parentController.collections.add( collection );
- const returned = collection.add( childController );
- expect( returned ).to.be.an.instanceof( Promise );
- expect( collection.get( 0 ) ).to.be.equal( childController );
- } );
- it( 'should add a child controller at given position', () => {
- const parentController = new Controller();
- const childController1 = new Controller();
- const childController2 = new Controller();
- const collection = new ControllerCollection( 'x' );
- parentController.collections.add( collection );
- collection.add( childController1 );
- collection.add( childController2, 0 );
- expect( collection.get( 0 ) ).to.be.equal( childController2 );
- expect( collection.get( 1 ) ).to.be.equal( childController1 );
- } );
- it( 'should initialize child controller if parent is ready', () => {
- const parentController = new Controller( null, new ParentView() );
- const childController = new Controller( null, new View() );
- const spy = bender.sinon.spy( childController, 'init' );
- const collection = new ControllerCollection( 'x' );
- parentController.collections.add( collection );
- collection.add( childController );
- collection.remove( childController );
- sinon.assert.notCalled( spy );
- return parentController.init()
- .then( () => {
- return collection.add( childController );
- } )
- .then( () => {
- sinon.assert.calledOnce( spy );
- } );
- } );
- it( 'should not initialize child controller twice', () => {
- const parentController = new Controller( null, new ParentView() );
- const childController = new Controller( null, new View() );
- const spy = bender.sinon.spy( childController, 'init' );
- const collection = new ControllerCollection( 'x' );
- parentController.collections.add( collection );
- return parentController.init()
- .then( () => {
- return childController.init();
- } )
- .then( () => {
- return collection.add( childController );
- } )
- .then( () => {
- sinon.assert.calledOnce( spy );
- } );
- } );
- } );
- } );
- function updateModuleReference() {
- View = modules[ 'core/ui/view' ];
- Controller = modules[ 'core/ui/controller' ];
- ControllerCollection = modules[ 'core/ui/controllercollection' ];
- }
- function defineParentViewClass() {
- ParentView = class extends View {
- constructor() {
- super();
- this.el = document.createElement( 'span' );
- this.register( 'x', true );
- }
- };
- }
|