controllercollection.js 3.1 KB

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