controller.js 9.0 KB

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