controllercollection.js 3.3 KB

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