controller.js 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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 View from '/ckeditor5/core/ui/view.js';
  9. import Controller from '/ckeditor5/core/ui/controller.js';
  10. import ControllerCollection from '/ckeditor5/core/ui/controllercollection.js';
  11. import CKEditorError from '/ckeditor5/core/ckeditorerror.js';
  12. import Model from '/ckeditor5/core/model.js';
  13. let ParentController, ParentView;
  14. testUtils.createSinonSandbox();
  15. describe( 'Controller', () => {
  16. describe( 'constructor', () => {
  17. it( 'defines basic properties', () => {
  18. const controller = new Controller();
  19. expect( controller.model ).to.be.null;
  20. expect( controller.ready ).to.be.false;
  21. expect( controller.view ).to.be.null;
  22. expect( controller.collections.length ).to.be.equal( 0 );
  23. } );
  24. it( 'should accept model and view', () => {
  25. const model = new Model();
  26. const view = new View();
  27. const controller = new Controller( model, view );
  28. expect( controller.model ).to.be.equal( model );
  29. expect( controller.view ).to.be.equal( view );
  30. } );
  31. } );
  32. describe( 'init', () => {
  33. it( 'should throw when already initialized', () => {
  34. const controller = new Controller();
  35. return controller.init()
  36. .then( () => {
  37. controller.init();
  38. throw new Error( 'This should not be executed.' );
  39. } )
  40. .catch( ( err ) => {
  41. expect( err ).to.be.instanceof( CKEditorError );
  42. expect( err.message ).to.match( /ui-controller-init-re/ );
  43. } );
  44. } );
  45. it( 'should set #ready flag', () => {
  46. const controller = new Controller();
  47. return controller.init().then( () => {
  48. expect( controller.ready ).to.be.true;
  49. } );
  50. } );
  51. it( 'should initialize own view', () => {
  52. const view = new View();
  53. const controller = new Controller( null, view );
  54. const spy = testUtils.sinon.spy( view, 'init' );
  55. return controller.init().then( () => {
  56. sinon.assert.calledOnce( spy );
  57. } );
  58. } );
  59. it( 'should initialize child controllers in own collections', () => {
  60. const parentController = new Controller();
  61. const buttonCollection = new ControllerCollection( 'buttons' );
  62. parentController.collections.add( buttonCollection );
  63. const childController1 = new Controller();
  64. const childController2 = new Controller();
  65. const spy1 = testUtils.sinon.spy( childController1, 'init' );
  66. const spy2 = testUtils.sinon.spy( childController2, 'init' );
  67. buttonCollection.add( childController1 );
  68. buttonCollection.add( childController2 );
  69. return parentController.init().then( () => {
  70. expect( buttonCollection.get( 0 ) ).to.be.equal( childController1 );
  71. expect( buttonCollection.get( 1 ) ).to.be.equal( childController2 );
  72. sinon.assert.calledOnce( spy1 );
  73. sinon.assert.calledOnce( spy2 );
  74. } );
  75. } );
  76. } );
  77. describe( 'collections', () => {
  78. describe( 'add', () => {
  79. beforeEach( defineParentViewClass );
  80. beforeEach( defineParentControllerClass );
  81. it( 'should add a child controller which has no view', () => {
  82. const parentController = new ParentController( null, new ParentView() );
  83. const collection = parentController.collections.get( 'x' );
  84. const childController = new Controller();
  85. return parentController.init()
  86. .then( () => {
  87. return collection.add( childController );
  88. } )
  89. .then( () => {
  90. expect( collection.get( 0 ) ).to.be.equal( childController );
  91. } );
  92. } );
  93. it( 'should append child controller\'s view to parent controller\'s view', () => {
  94. const parentView = new ParentView();
  95. const parentController = new ParentController( null, parentView );
  96. const collection = parentController.collections.get( 'x' );
  97. const childController = new Controller( null, new View() );
  98. const spy1 = testUtils.sinon.spy( parentView, 'addChild' );
  99. collection.add( childController );
  100. sinon.assert.notCalled( spy1 );
  101. collection.remove( childController );
  102. return parentController.init()
  103. .then( () => {
  104. return collection.add( childController );
  105. } )
  106. .then( () => {
  107. sinon.assert.calledOnce( spy1 );
  108. sinon.assert.calledWithExactly( spy1, 'x', childController.view, 0 );
  109. } );
  110. } );
  111. it( 'should append child controller\'s view to parent controller\'s view at given index', () => {
  112. const parentController = new ParentController( null, new ParentView() );
  113. const collection = parentController.collections.get( 'x' );
  114. const childView1 = new View();
  115. const childController1 = new Controller( null, childView1 );
  116. const childView2 = new View();
  117. const childController2 = new Controller( null, childView2 );
  118. return parentController.init()
  119. .then( () => {
  120. return collection.add( childController1 ).then( () => {
  121. return collection.add( childController2, 0 );
  122. } );
  123. } )
  124. .then( () => {
  125. expect( parentController.view.getChild( 'x', 0 ) ).to.be.equal( childView2 );
  126. expect( parentController.view.getChild( 'x', 1 ) ).to.be.equal( childView1 );
  127. } );
  128. } );
  129. } );
  130. describe( 'remove', () => {
  131. beforeEach( defineParentViewClass );
  132. it( 'should remove child controller\'s view from parent controller\'s view', () => {
  133. const parentView = new ParentView();
  134. const parentController = new ParentController( null, parentView );
  135. const collection = parentController.collections.get( 'x' );
  136. const childController = new Controller( null, new View() );
  137. const spy = testUtils.sinon.spy( parentView, 'removeChild' );
  138. collection.add( childController );
  139. sinon.assert.notCalled( spy );
  140. return parentController.init()
  141. .then( () => {
  142. collection.remove( childController );
  143. sinon.assert.calledOnce( spy );
  144. sinon.assert.calledWithExactly( spy, 'x', childController.view );
  145. } );
  146. } );
  147. } );
  148. } );
  149. describe( 'destroy', () => {
  150. beforeEach( defineParentViewClass );
  151. beforeEach( defineParentControllerClass );
  152. it( 'should destroy the controller', () => {
  153. const view = new View();
  154. const controller = new Controller( null, view );
  155. const spy = testUtils.sinon.spy( view, 'destroy' );
  156. return controller.init()
  157. .then( () => {
  158. return controller.destroy();
  159. } )
  160. .then( () => {
  161. sinon.assert.calledOnce( spy );
  162. expect( controller.model ).to.be.null;
  163. expect( controller.ready ).to.be.null;
  164. expect( controller.view ).to.be.null;
  165. expect( controller.collections ).to.be.null;
  166. } );
  167. } );
  168. it( 'should destroy the controller which has no view', () => {
  169. const controller = new Controller( null, null );
  170. return controller.init()
  171. .then( () => {
  172. return controller.destroy();
  173. } )
  174. .then( () => {
  175. expect( controller.model ).to.be.null;
  176. expect( controller.view ).to.be.null;
  177. expect( controller.collections ).to.be.null;
  178. } );
  179. } );
  180. it( 'should destroy child controllers in collections with their views', () => {
  181. const parentController = new ParentController( null, new ParentView() );
  182. const collection = parentController.collections.get( 'x' );
  183. const childView = new View();
  184. const childController = new Controller( null, childView );
  185. const spy = testUtils.sinon.spy( childView, 'destroy' );
  186. collection.add( childController );
  187. return parentController.init()
  188. .then( () => {
  189. return parentController.destroy();
  190. } )
  191. .then( () => {
  192. sinon.assert.calledOnce( spy );
  193. expect( childController.model ).to.be.null;
  194. expect( childController.view ).to.be.null;
  195. expect( childController.collections ).to.be.null;
  196. } );
  197. } );
  198. it( 'should destroy child controllers in collections when they have no views', () => {
  199. const parentController = new ParentController( null, new ParentView() );
  200. const collection = parentController.collections.get( 'x' );
  201. const childController = new Controller( null, null );
  202. collection.add( childController );
  203. return parentController.init()
  204. .then( () => {
  205. return parentController.destroy();
  206. } )
  207. .then( () => {
  208. expect( childController.model ).to.be.null;
  209. expect( childController.view ).to.be.null;
  210. expect( childController.collections ).to.be.null;
  211. } );
  212. } );
  213. } );
  214. } );
  215. function defineParentViewClass() {
  216. ParentView = class extends View {
  217. constructor() {
  218. super();
  219. this.el = document.createElement( 'span' );
  220. this.register( 'x', true );
  221. }
  222. };
  223. }
  224. function defineParentControllerClass() {
  225. ParentController = class extends Controller {
  226. constructor( ...args ) {
  227. super( ...args );
  228. this.collections.add( new ControllerCollection( 'x' ) );
  229. }
  230. };
  231. }