controller.js 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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;
  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 spy1 = bender.sinon.spy( parentView, 'addChild' );
  105. collection.add( childController );
  106. sinon.assert.notCalled( spy1 );
  107. collection.remove( childController );
  108. return parentController.init()
  109. .then( () => {
  110. return collection.add( childController );
  111. } )
  112. .then( () => {
  113. sinon.assert.calledOnce( spy1 );
  114. sinon.assert.calledWithExactly( spy1, 'x', childController.view, 0 );
  115. } );
  116. } );
  117. it( 'should append child controller\'s view to parent controller\'s view at given index', () => {
  118. const parentController = new ParentController( null, new ParentView() );
  119. const collection = parentController.collections.get( 'x' );
  120. const childView1 = new View();
  121. const childController1 = new Controller( null, childView1 );
  122. const childView2 = new View();
  123. const childController2 = new Controller( null, childView2 );
  124. return parentController.init()
  125. .then( () => {
  126. return collection.add( childController1 ).then( () => {
  127. return collection.add( childController2, 0 );
  128. } );
  129. } )
  130. .then( () => {
  131. expect( parentController.view.getChild( 'x', 0 ) ).to.be.equal( childView2 );
  132. expect( parentController.view.getChild( 'x', 1 ) ).to.be.equal( childView1 );
  133. } );
  134. } );
  135. } );
  136. describe( 'remove', () => {
  137. beforeEach( defineParentViewClass );
  138. it( 'should remove child controller\'s view from parent controller\'s view', () => {
  139. const parentView = new ParentView();
  140. const parentController = new ParentController( null, parentView );
  141. const collection = parentController.collections.get( 'x' );
  142. const childController = new Controller( null, new View() );
  143. const spy = bender.sinon.spy( parentView, 'removeChild' );
  144. collection.add( childController );
  145. sinon.assert.notCalled( spy );
  146. return parentController.init()
  147. .then( () => {
  148. collection.remove( childController );
  149. sinon.assert.calledOnce( spy );
  150. sinon.assert.calledWithExactly( spy, 'x', 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 = bender.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 = bender.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 updateModuleReference() {
  222. View = modules[ 'core/ui/view' ];
  223. Controller = modules[ 'core/ui/controller' ];
  224. Model = modules[ 'core/model' ];
  225. Collection = modules[ 'core/collection ' ];
  226. ControllerCollection = modules[ 'core/ui/controllercollection' ];
  227. CKEditorError = modules[ 'core/ckeditorerror' ];
  228. }
  229. function defineParentViewClass() {
  230. ParentView = class extends View {
  231. constructor() {
  232. super();
  233. this.el = document.createElement( 'span' );
  234. this.register( 'x', true );
  235. }
  236. };
  237. }
  238. function defineParentControllerClass() {
  239. ParentController = class extends Controller {
  240. constructor( ...args ) {
  241. super( ...args );
  242. this.collections.add( new ControllerCollection( 'x' ) );
  243. }
  244. };
  245. }