controller.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  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 throw when no collection name', () => {
  85. const controller = new Controller();
  86. controller.collections.add( new ControllerCollection( 'x' ) );
  87. expect( () => {
  88. controller.addChild();
  89. } ).to.throw( CKEditorError, /ui-controller-addchild-badcname/ );
  90. } );
  91. it( 'should throw when collection of given name does not exist', () => {
  92. const controller = new Controller();
  93. controller.collections.add( new ControllerCollection( 'x' ) );
  94. expect( () => {
  95. controller.addChild( 'y', new Controller() );
  96. } ).to.throw( CKEditorError, /ui-controller-addchild-nocol/ );
  97. } );
  98. it( 'should throw when no controller is passed', () => {
  99. const controller = new Controller();
  100. controller.collections.add( new ControllerCollection( 'x' ) );
  101. expect( () => {
  102. controller.addChild( 'x' );
  103. } ).to.throw( CKEditorError, /ui-controller-addchild-no-controller/ );
  104. } );
  105. it( 'should add a child controller to given collection and return promise', () => {
  106. const parentController = new Controller();
  107. const childController = new Controller();
  108. const collection = new ControllerCollection( 'x' );
  109. parentController.collections.add( collection );
  110. const returned = parentController.addChild( 'x', childController );
  111. expect( returned ).to.be.an.instanceof( Promise );
  112. expect( collection.get( 0 ) ).to.be.equal( childController );
  113. } );
  114. it( 'should add a child controller at given position', () => {
  115. const parentController = new Controller();
  116. const childController1 = new Controller();
  117. const childController2 = new Controller();
  118. const collection = new ControllerCollection( 'x' );
  119. parentController.collections.add( collection );
  120. parentController.addChild( 'x', childController1 );
  121. parentController.addChild( 'x', childController2, 0 );
  122. expect( collection.get( 0 ) ).to.be.equal( childController2 );
  123. expect( collection.get( 1 ) ).to.be.equal( childController1 );
  124. } );
  125. it( 'should add a child controller which has no view', () => {
  126. const parentController = new Controller( null, new ParentView() );
  127. const childController = new Controller();
  128. parentController.collections.add( new ControllerCollection( 'x' ) );
  129. return parentController.init()
  130. .then( () => {
  131. return parentController.addChild( 'x', childController );
  132. } )
  133. .then( () => {
  134. expect( parentController.getChild( 'x', 0 ) ).to.be.equal( childController );
  135. } );
  136. } );
  137. it( 'should append child controller\'s view to parent controller\'s view', () => {
  138. const parentView = new ParentView();
  139. const parentController = new Controller( null, parentView );
  140. const childController = new Controller( null, new View() );
  141. const spy1 = bender.sinon.spy( parentView, 'addChild' );
  142. parentController.collections.add( new ControllerCollection( 'x' ) );
  143. parentController.addChild( 'x', childController );
  144. sinon.assert.notCalled( spy1 );
  145. parentController.removeChild( 'x', childController );
  146. return parentController.init()
  147. .then( () => {
  148. return parentController.addChild( 'x', childController );
  149. } )
  150. .then( () => {
  151. sinon.assert.calledOnce( spy1 );
  152. sinon.assert.calledWithExactly( spy1, 'x', childController.view, undefined );
  153. } );
  154. } );
  155. it( 'should append child controller\'s view to parent controller\'s view at given index', () => {
  156. const parentController = new Controller( null, new ParentView() );
  157. const childView1 = new View();
  158. const childController1 = new Controller( null, childView1 );
  159. const childView2 = new View();
  160. const childController2 = new Controller( null, childView2 );
  161. parentController.collections.add( new ControllerCollection( 'x' ) );
  162. return parentController.init()
  163. .then( () => {
  164. return parentController.addChild( 'x', childController1 ).then( () => {
  165. return parentController.addChild( 'x', childController2, 0 );
  166. } );
  167. } )
  168. .then( () => {
  169. expect( parentController.view.getChild( 'x', 0 ) ).to.be.equal( childView2 );
  170. expect( parentController.view.getChild( 'x', 1 ) ).to.be.equal( childView1 );
  171. } );
  172. } );
  173. it( 'should initialize child controller if parent is ready', () => {
  174. const parentController = new Controller( null, new ParentView() );
  175. const childController = new Controller( null, new View() );
  176. const spy = bender.sinon.spy( childController, 'init' );
  177. parentController.collections.add( new ControllerCollection( 'x' ) );
  178. parentController.addChild( 'x', childController );
  179. parentController.removeChild( 'x', childController );
  180. sinon.assert.notCalled( spy );
  181. return parentController.init()
  182. .then( () => {
  183. return parentController.addChild( 'x', childController );
  184. } )
  185. .then( () => {
  186. sinon.assert.calledOnce( spy );
  187. } );
  188. } );
  189. it( 'should not initialize child controller twice', () => {
  190. const parentController = new Controller( null, new ParentView() );
  191. const childController = new Controller( null, new View() );
  192. const spy = bender.sinon.spy( childController, 'init' );
  193. parentController.collections.add( new ControllerCollection( 'x' ) );
  194. return parentController.init()
  195. .then( () => {
  196. return childController.init();
  197. } )
  198. .then( () => {
  199. return parentController.addChild( 'x', childController );
  200. } )
  201. .then( () => {
  202. sinon.assert.calledOnce( spy );
  203. } );
  204. } );
  205. } );
  206. describe( 'removeChild', () => {
  207. beforeEach( defineParentViewClass );
  208. it( 'should throw when no collection name', () => {
  209. const controller = new Controller();
  210. controller.collections.add( new ControllerCollection( 'x' ) );
  211. expect( () => {
  212. controller.removeChild();
  213. } ).to.throw( CKEditorError, /ui-controller-removechild-badcname/ );
  214. } );
  215. it( 'should throw when collection of given name does not exist', () => {
  216. const controller = new Controller();
  217. controller.collections.add( new ControllerCollection( 'x' ) );
  218. expect( () => {
  219. controller.removeChild( 'y' );
  220. } ).to.throw( CKEditorError, /ui-controller-removechild-nocol/ );
  221. } );
  222. it( 'should throw when controller or wrong controller is passed', () => {
  223. const controller = new Controller();
  224. controller.collections.add( new ControllerCollection( 'x' ) );
  225. expect( () => {
  226. controller.removeChild( 'x' );
  227. } ).to.throw( CKEditorError, /ui-controller-removechild-no-controller/ );
  228. } );
  229. it( 'should remove child controller and return it', () => {
  230. const parentController = new Controller();
  231. const childController = new Controller();
  232. const collection = new ControllerCollection( 'x' );
  233. parentController.collections.add( collection );
  234. parentController.addChild( 'x', childController );
  235. const returned = parentController.removeChild( 'x', childController );
  236. expect( returned ).to.be.equal( childController );
  237. expect( collection.length ).to.be.equal( 0 );
  238. } );
  239. it( 'should remove child controller\'s view from parent controller\'s view', () => {
  240. const parentView = new ParentView();
  241. const parentController = new Controller( null, parentView );
  242. const childController = new Controller( null, new View() );
  243. const spy = bender.sinon.spy( parentView, 'removeChild' );
  244. parentController.collections.add( new ControllerCollection( 'x' ) );
  245. parentController.addChild( 'x', childController );
  246. sinon.assert.notCalled( spy );
  247. return parentController.init()
  248. .then( () => {
  249. parentController.removeChild( 'x', childController );
  250. sinon.assert.calledOnce( spy );
  251. sinon.assert.calledWithExactly( spy, 'x', childController.view );
  252. } );
  253. } );
  254. } );
  255. describe( 'getChild', () => {
  256. beforeEach( defineParentViewClass );
  257. it( 'should throw when collection of given name does not exist', () => {
  258. const controller = new Controller();
  259. controller.collections.add( new ControllerCollection( 'x' ) );
  260. expect( () => {
  261. controller.getChild( 'y', 0 );
  262. } ).to.throw( CKEditorError, /ui-controller-getchild-nocol/ );
  263. } );
  264. it( 'should get child controller by index', () => {
  265. const parentController = new Controller();
  266. const childController = new Controller();
  267. parentController.collections.add( new ControllerCollection( 'x' ) );
  268. parentController.addChild( 'x', childController );
  269. expect( parentController.getChild( 'x', 0 ) ).to.be.equal( childController );
  270. } );
  271. } );
  272. describe( 'destroy', () => {
  273. beforeEach( defineParentViewClass );
  274. it( 'should destroy the controller', () => {
  275. const view = new View();
  276. const controller = new Controller( null, view );
  277. const spy = bender.sinon.spy( view, 'destroy' );
  278. return controller.init()
  279. .then( () => {
  280. return controller.destroy();
  281. } )
  282. .then( () => {
  283. sinon.assert.calledOnce( spy );
  284. expect( controller.model ).to.be.null;
  285. expect( controller.ready ).to.be.null;
  286. expect( controller.view ).to.be.null;
  287. expect( controller.collections ).to.be.null;
  288. } );
  289. } );
  290. it( 'should destroy the controller which has no view', () => {
  291. const controller = new Controller( null, null );
  292. return controller.init()
  293. .then( () => {
  294. return controller.destroy();
  295. } )
  296. .then( () => {
  297. expect( controller.model ).to.be.null;
  298. expect( controller.view ).to.be.null;
  299. expect( controller.collections ).to.be.null;
  300. } );
  301. } );
  302. it( 'should destroy child controllers in collections with their views', () => {
  303. const parentController = new Controller( null, new ParentView() );
  304. const childView = new View();
  305. const childController = new Controller( null, childView );
  306. const spy = bender.sinon.spy( childView, 'destroy' );
  307. parentController.collections.add( new ControllerCollection( 'x' ) );
  308. parentController.addChild( 'x', childController );
  309. return parentController.init()
  310. .then( () => {
  311. return parentController.destroy();
  312. } )
  313. .then( () => {
  314. sinon.assert.calledOnce( spy );
  315. expect( childController.model ).to.be.null;
  316. expect( childController.view ).to.be.null;
  317. expect( childController.collections ).to.be.null;
  318. } );
  319. } );
  320. it( 'should destroy child controllers in collections when they have no views', () => {
  321. const parentController = new Controller( null, new ParentView() );
  322. const childController = new Controller( null, null );
  323. parentController.collections.add( new ControllerCollection( 'x' ) );
  324. parentController.addChild( 'x', childController );
  325. return parentController.init()
  326. .then( () => {
  327. return parentController.destroy();
  328. } )
  329. .then( () => {
  330. expect( childController.model ).to.be.null;
  331. expect( childController.view ).to.be.null;
  332. expect( childController.collections ).to.be.null;
  333. } );
  334. } );
  335. } );
  336. } );
  337. function updateModuleReference() {
  338. View = modules[ 'ui/view' ];
  339. Controller = modules[ 'ui/controller' ];
  340. Model = modules.model;
  341. Collection = modules.collection;
  342. ControllerCollection = modules[ 'ui/controllercollection' ];
  343. CKEditorError = modules.ckeditorerror;
  344. }
  345. function defineParentViewClass() {
  346. ParentView = class extends View {
  347. constructor() {
  348. super();
  349. this.el = document.createElement( 'span' );
  350. this.register( 'x', true );
  351. }
  352. };
  353. }