controller.js 8.7 KB

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