controllercollection.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /**
  2. * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* bender-tags: ui */
  6. 'use strict';
  7. import testUtils from '/tests/_utils/utils.js';
  8. import ControllerCollection from '/ckeditor5/core/ui/controllercollection.js';
  9. import Controller from '/ckeditor5/core/ui/controller.js';
  10. import View from '/ckeditor5/core/ui/view.js';
  11. testUtils.createSinonSandbox();
  12. let ParentView;
  13. describe( 'ControllerCollection', () => {
  14. beforeEach( defineParentViewClass );
  15. describe( 'constructor', () => {
  16. it( 'should throw when no name is passed', () => {
  17. expect( () => {
  18. new ControllerCollection();
  19. } ).to.throw( /^ui-controllercollection-no-name/ );
  20. } );
  21. } );
  22. describe( 'add', () => {
  23. it( 'should add a child controller and return promise', () => {
  24. const parentController = new Controller();
  25. const childController = new Controller();
  26. const collection = new ControllerCollection( 'x' );
  27. parentController.collections.add( collection );
  28. const returned = collection.add( childController );
  29. expect( returned ).to.be.an.instanceof( Promise );
  30. expect( collection.get( 0 ) ).to.be.equal( childController );
  31. } );
  32. it( 'should add a child controller at given position', () => {
  33. const parentController = new Controller();
  34. const childController1 = new Controller();
  35. const childController2 = new Controller();
  36. const collection = new ControllerCollection( 'x' );
  37. parentController.collections.add( collection );
  38. collection.add( childController1 );
  39. collection.add( childController2, 0 );
  40. expect( collection.get( 0 ) ).to.be.equal( childController2 );
  41. expect( collection.get( 1 ) ).to.be.equal( childController1 );
  42. } );
  43. it( 'should initialize child controller if parent is ready', () => {
  44. const parentController = new Controller( null, new ParentView() );
  45. const childController = new Controller( null, new View() );
  46. const spy = testUtils.sinon.spy( childController, 'init' );
  47. const collection = new ControllerCollection( 'x' );
  48. parentController.collections.add( collection );
  49. collection.add( childController );
  50. collection.remove( childController );
  51. sinon.assert.notCalled( spy );
  52. return parentController.init()
  53. .then( () => {
  54. return collection.add( childController );
  55. } )
  56. .then( () => {
  57. sinon.assert.calledOnce( spy );
  58. } );
  59. } );
  60. it( 'should not initialize child controller twice', () => {
  61. const parentController = new Controller( null, new ParentView() );
  62. const childController = new Controller( null, new View() );
  63. const spy = testUtils.sinon.spy( childController, 'init' );
  64. const collection = new ControllerCollection( 'x' );
  65. parentController.collections.add( collection );
  66. return parentController.init()
  67. .then( () => {
  68. return childController.init();
  69. } )
  70. .then( () => {
  71. return collection.add( childController );
  72. } )
  73. .then( () => {
  74. sinon.assert.calledOnce( spy );
  75. } );
  76. } );
  77. } );
  78. } );
  79. function defineParentViewClass() {
  80. ParentView = class extends View {
  81. constructor() {
  82. super();
  83. this.el = document.createElement( 'span' );
  84. this.register( 'x', true );
  85. }
  86. };
  87. }