8
0

controller.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  1. /**
  2. * @license Copyright (c) 2003-2015, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* bender-tags: core, ui */
  6. 'use strict';
  7. const modules = bender.amd.require( 'ckeditor',
  8. 'ui/view',
  9. 'ui/controller',
  10. 'ui/controllercollection',
  11. 'ui/region',
  12. 'ckeditorerror',
  13. 'model',
  14. 'collection',
  15. 'eventinfo'
  16. );
  17. let View, Controller, Model, CKEditorError, Collection, ControllerCollection;
  18. let 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. parentController.collections.add( new ControllerCollection( 'buttons' ) );
  68. const childController1 = new Controller();
  69. const childController2 = new Controller();
  70. const spy1 = bender.sinon.spy( childController1, 'init' );
  71. const spy2 = bender.sinon.spy( childController2, 'init' );
  72. parentController.addChild( 'buttons', childController1 );
  73. parentController.addChild( 'buttons', childController2 );
  74. return parentController.init().then( () => {
  75. expect( parentController.getChild( 'buttons', 0 ) ).to.be.equal( childController1 );
  76. expect( parentController.getChild( 'buttons', 1 ) ).to.be.equal( childController2 );
  77. sinon.assert.calledOnce( spy1 );
  78. sinon.assert.calledOnce( spy2 );
  79. } );
  80. } );
  81. } );
  82. describe( 'addChild', () => {
  83. beforeEach( defineParentViewClass );
  84. it( 'should add a child controller to given collection and return promise', () => {
  85. const parentController = new Controller();
  86. const childController = new Controller();
  87. const collection = new ControllerCollection( 'x' );
  88. parentController.collections.add( collection );
  89. const returned = parentController.addChild( 'x', childController );
  90. expect( returned ).to.be.an.instanceof( Promise );
  91. expect( collection.get( 0 ) ).to.be.equal( childController );
  92. } );
  93. it( 'should add a child controller at given position', () => {
  94. const parentController = new Controller();
  95. const childController1 = new Controller();
  96. const childController2 = new Controller();
  97. const collection = new ControllerCollection( 'x' );
  98. parentController.collections.add( collection );
  99. parentController.addChild( 'x', childController1 );
  100. parentController.addChild( 'x', childController2, 0 );
  101. expect( collection.get( 0 ) ).to.be.equal( childController2 );
  102. expect( collection.get( 1 ) ).to.be.equal( childController1 );
  103. } );
  104. it( 'should add a child controller which has no view', () => {
  105. const parentController = new Controller( null, new ParentView() );
  106. const childController = new Controller();
  107. parentController.collections.add( new ControllerCollection( 'x' ) );
  108. return parentController.init()
  109. .then( () => {
  110. return parentController.addChild( 'x', childController );
  111. } )
  112. .then( () => {
  113. expect( parentController.getChild( 'x', 0 ) ).to.be.equal( childController );
  114. } );
  115. } );
  116. it( 'should append child controller\'s view to parent controller\'s view', () => {
  117. const parentView = new ParentView();
  118. const parentController = new Controller( null, parentView );
  119. const childController = new Controller( null, new View() );
  120. const spy1 = bender.sinon.spy( parentView, 'addChild' );
  121. parentController.collections.add( new ControllerCollection( 'x' ) );
  122. parentController.addChild( 'x', childController );
  123. sinon.assert.notCalled( spy1 );
  124. parentController.removeChild( 'x', childController );
  125. return parentController.init()
  126. .then( () => {
  127. return parentController.addChild( 'x', childController );
  128. } )
  129. .then( () => {
  130. sinon.assert.calledOnce( spy1 );
  131. sinon.assert.calledWithExactly( spy1, 'x', childController.view, undefined );
  132. } );
  133. } );
  134. it( 'should append child controller\'s view to parent controller\'s view at given index', () => {
  135. const parentController = new Controller( null, new ParentView() );
  136. const childView1 = new View();
  137. const childController1 = new Controller( null, childView1 );
  138. const childView2 = new View();
  139. const childController2 = new Controller( null, childView2 );
  140. parentController.collections.add( new ControllerCollection( 'x' ) );
  141. return parentController.init()
  142. .then( () => {
  143. return parentController.addChild( 'x', childController1 ).then( () => {
  144. return parentController.addChild( 'x', childController2, 0 );
  145. } );
  146. } )
  147. .then( () => {
  148. expect( parentController.view.getChild( 'x', 0 ) ).to.be.equal( childView2 );
  149. expect( parentController.view.getChild( 'x', 1 ) ).to.be.equal( childView1 );
  150. } );
  151. } );
  152. it( 'should initialize child controller if parent is ready', () => {
  153. const parentController = new Controller( null, new ParentView() );
  154. const childController = new Controller( null, new View() );
  155. const spy = bender.sinon.spy( childController, 'init' );
  156. parentController.collections.add( new ControllerCollection( 'x' ) );
  157. parentController.addChild( 'x', childController );
  158. parentController.removeChild( 'x', childController );
  159. sinon.assert.notCalled( spy );
  160. return parentController.init()
  161. .then( () => {
  162. return parentController.addChild( 'x', childController );
  163. } )
  164. .then( () => {
  165. sinon.assert.calledOnce( spy );
  166. } );
  167. } );
  168. it( 'should not initialize child controller twice', () => {
  169. const parentController = new Controller( null, new ParentView() );
  170. const childController = new Controller( null, new View() );
  171. const spy = bender.sinon.spy( childController, 'init' );
  172. parentController.collections.add( new ControllerCollection( 'x' ) );
  173. return parentController.init()
  174. .then( () => {
  175. return childController.init();
  176. } )
  177. .then( () => {
  178. return parentController.addChild( 'x', childController );
  179. } )
  180. .then( () => {
  181. sinon.assert.calledOnce( spy );
  182. } );
  183. } );
  184. } );
  185. describe( 'removeChild', () => {
  186. beforeEach( defineParentViewClass );
  187. it( 'should remove child controller and return it', () => {
  188. const parentController = new Controller();
  189. const childController = new Controller();
  190. const collection = new ControllerCollection( 'x' );
  191. parentController.collections.add( collection );
  192. parentController.addChild( 'x', childController );
  193. const returned = parentController.removeChild( 'x', childController );
  194. expect( returned ).to.be.equal( childController );
  195. expect( collection.length ).to.be.equal( 0 );
  196. } );
  197. it( 'should remove child controller\'s view from parent controller\'s view', () => {
  198. const parentView = new ParentView();
  199. const parentController = new Controller( null, parentView );
  200. const childController = new Controller( null, new View() );
  201. const spy = bender.sinon.spy( parentView, 'removeChild' );
  202. parentController.collections.add( new ControllerCollection( 'x' ) );
  203. parentController.addChild( 'x', childController );
  204. sinon.assert.notCalled( spy );
  205. return parentController.init()
  206. .then( () => {
  207. parentController.removeChild( 'x', childController );
  208. sinon.assert.calledOnce( spy );
  209. sinon.assert.calledWithExactly( spy, 'x', childController.view );
  210. } );
  211. } );
  212. } );
  213. describe( 'getChild', () => {
  214. beforeEach( defineParentViewClass );
  215. it( 'should get child controller by index', () => {
  216. const parentController = new Controller();
  217. const childController = new Controller();
  218. parentController.collections.add( new ControllerCollection( 'x' ) );
  219. parentController.addChild( 'x', childController );
  220. expect( parentController.getChild( 'x', 0 ) ).to.be.equal( childController );
  221. } );
  222. } );
  223. describe( 'destroy', () => {
  224. beforeEach( defineParentViewClass );
  225. it( 'should destroy the controller', () => {
  226. const view = new View();
  227. const controller = new Controller( null, view );
  228. const spy = bender.sinon.spy( view, 'destroy' );
  229. return controller.init()
  230. .then( () => {
  231. return controller.destroy();
  232. } )
  233. .then( () => {
  234. sinon.assert.calledOnce( spy );
  235. expect( controller.model ).to.be.null;
  236. expect( controller.ready ).to.be.null;
  237. expect( controller.view ).to.be.null;
  238. expect( controller.collections ).to.be.null;
  239. } );
  240. } );
  241. it( 'should destroy the controller which has no view', () => {
  242. const controller = new Controller( null, null );
  243. return controller.init()
  244. .then( () => {
  245. return controller.destroy();
  246. } )
  247. .then( () => {
  248. expect( controller.model ).to.be.null;
  249. expect( controller.view ).to.be.null;
  250. expect( controller.collections ).to.be.null;
  251. } );
  252. } );
  253. it( 'should destroy child controllers in collections with their views', () => {
  254. const parentController = new Controller( null, new ParentView() );
  255. const childView = new View();
  256. const childController = new Controller( null, childView );
  257. const spy = bender.sinon.spy( childView, 'destroy' );
  258. parentController.collections.add( new ControllerCollection( 'x' ) );
  259. parentController.addChild( 'x', childController );
  260. return parentController.init()
  261. .then( () => {
  262. return parentController.destroy();
  263. } )
  264. .then( () => {
  265. sinon.assert.calledOnce( spy );
  266. expect( childController.model ).to.be.null;
  267. expect( childController.view ).to.be.null;
  268. expect( childController.collections ).to.be.null;
  269. } );
  270. } );
  271. it( 'should destroy child controllers in collections when they have no views', () => {
  272. const parentController = new Controller( null, new ParentView() );
  273. const childController = new Controller( null, null );
  274. parentController.collections.add( new ControllerCollection( 'x' ) );
  275. parentController.addChild( 'x', childController );
  276. return parentController.init()
  277. .then( () => {
  278. return parentController.destroy();
  279. } )
  280. .then( () => {
  281. expect( childController.model ).to.be.null;
  282. expect( childController.view ).to.be.null;
  283. expect( childController.collections ).to.be.null;
  284. } );
  285. } );
  286. } );
  287. } );
  288. function updateModuleReference() {
  289. View = modules[ 'ui/view' ];
  290. Controller = modules[ 'ui/controller' ];
  291. Model = modules.model;
  292. Collection = modules.collection;
  293. ControllerCollection = modules[ 'ui/controllercollection' ];
  294. CKEditorError = modules.ckeditorerror;
  295. }
  296. function defineParentViewClass() {
  297. ParentView = class extends View {
  298. constructor() {
  299. super();
  300. this.el = document.createElement( 'span' );
  301. this.register( 'x', true );
  302. }
  303. };
  304. }