controller.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515
  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/region',
  11. 'ckeditorerror',
  12. 'model',
  13. 'collection',
  14. 'eventinfo'
  15. );
  16. let View, Controller, Model, CKEditorError, Collection;
  17. let ParentView;
  18. bender.tools.createSinonSandbox();
  19. describe( 'Controller', () => {
  20. beforeEach( updateModuleReference );
  21. describe( 'constructor', () => {
  22. it( 'should accept model and view', () => {
  23. const model = new Model();
  24. const view = new View();
  25. const controller = new Controller( model, view );
  26. expect( controller.model ).to.be.equal( model );
  27. expect( controller.view ).to.be.equal( view );
  28. } );
  29. it( 'should not initialize the controller', () => {
  30. const controller = new Controller();
  31. expect( controller.ready ).to.be.false;
  32. } );
  33. } );
  34. describe( 'init', () => {
  35. it( 'should throw when already initialized', () => {
  36. const controller = new Controller();
  37. return controller.init()
  38. .then( () => {
  39. controller.init();
  40. throw new Error( 'This should not be executed.' );
  41. } )
  42. .catch( ( err ) => {
  43. expect( err ).to.be.instanceof( CKEditorError );
  44. expect( err.message ).to.match( /ui-controller-init-re/ );
  45. } );
  46. } );
  47. it( 'should set #ready flag', () => {
  48. const controller = new Controller();
  49. return controller.init().then( () => {
  50. expect( controller.ready ).to.be.true;
  51. } );
  52. } );
  53. it( 'should initialize own view', () => {
  54. const view = new View();
  55. const controller = new Controller( null, view );
  56. const spy = bender.sinon.spy( view, 'init' );
  57. return controller.init().then( () => {
  58. sinon.assert.calledOnce( spy );
  59. } );
  60. } );
  61. it( 'should initialize child controllers in own collections', () => {
  62. const parentController = new Controller();
  63. parentController.register( 'buttons', new Collection() );
  64. const childController1 = new Controller();
  65. const childController2 = new Controller();
  66. const spy1 = bender.sinon.spy( childController1, 'init' );
  67. const spy2 = bender.sinon.spy( childController2, 'init' );
  68. parentController.addChild( 'buttons', childController1 );
  69. parentController.addChild( 'buttons', childController2 );
  70. return parentController.init().then( () => {
  71. expect( parentController.getChild( 'buttons', 0 ) ).to.be.equal( childController1 );
  72. expect( parentController.getChild( 'buttons', 1 ) ).to.be.equal( childController2 );
  73. sinon.assert.calledOnce( spy1 );
  74. sinon.assert.calledOnce( spy2 );
  75. } );
  76. } );
  77. } );
  78. describe( 'register', () => {
  79. it( 'should throw when bad type of argument', () => {
  80. const controller = new Controller();
  81. controller.register( 'x', new Collection() );
  82. expect( () => {
  83. controller.register();
  84. } ).to.throw;
  85. expect( () => {
  86. controller.register( 'x' );
  87. } ).to.throw( CKEditorError, /ui-controller-register-badtype/ );
  88. expect( () => {
  89. controller.register( 'x', new Date() );
  90. } ).to.throw( CKEditorError, /ui-controller-register-badtype/ );
  91. } );
  92. it( 'should throw when already registered but no override flag', () => {
  93. const controller = new Controller();
  94. controller.register( 'x', new Collection() );
  95. expect( () => {
  96. controller.register( 'x', new Collection() );
  97. } ).to.throw( CKEditorError, /ui-controller-register-noverride/ );
  98. } );
  99. it( 'should register a collection', () => {
  100. const controller = new Controller();
  101. const collection = new Collection();
  102. controller.register( 'x', collection );
  103. expect( controller._collections.get( 'x' ) ).to.be.equal( collection );
  104. } );
  105. it( 'should override existing collection with override flag', () => {
  106. const controller = new Controller();
  107. const newCollection = new Collection();
  108. controller.register( 'x', new Collection() );
  109. controller.register( 'x', newCollection, true );
  110. expect( controller._collections.get( 'x' ) ).to.be.equal( newCollection );
  111. } );
  112. it( 'should override existing collection with the same collection', () => {
  113. const controller = new Controller();
  114. const newCollection = new Collection();
  115. controller.register( 'x', newCollection );
  116. controller.register( 'x', newCollection );
  117. expect( controller._collections.get( 'x' ) ).to.be.equal( newCollection );
  118. } );
  119. } );
  120. describe( 'addChild', () => {
  121. beforeEach( defineParentViewClass );
  122. it( 'should throw when no collection name', () => {
  123. const controller = new Controller();
  124. controller.register( 'x', new Collection() );
  125. expect( () => {
  126. controller.addChild();
  127. } ).to.throw( CKEditorError, /ui-controller-addchild-badcname/ );
  128. } );
  129. it( 'should throw when collection of given name does not exist', () => {
  130. const controller = new Controller();
  131. controller.register( 'x', new Collection() );
  132. expect( () => {
  133. controller.addChild( 'y', new Controller() );
  134. } ).to.throw( CKEditorError, /ui-controller-addchild-nocol/ );
  135. } );
  136. it( 'should throw when controller or wrong controller is passed', () => {
  137. const controller = new Controller();
  138. controller.register( 'x', new Collection() );
  139. expect( () => {
  140. controller.addChild( 'x' );
  141. } ).to.throw( CKEditorError, /ui-controller-addchild-badtype/ );
  142. expect( () => {
  143. controller.addChild( 'x', new Date() );
  144. } ).to.throw( CKEditorError, /ui-controller-addchild-badtype/ );
  145. } );
  146. it( 'should add a child controller to given collection and return promise', () => {
  147. const parentController = new Controller();
  148. const childController = new Controller();
  149. const collection = new Collection();
  150. parentController.register( 'x', collection );
  151. const returned = parentController.addChild( 'x', childController );
  152. expect( returned ).to.be.an.instanceof( Promise );
  153. expect( collection.get( 0 ) ).to.be.equal( childController );
  154. } );
  155. it( 'should add a child controller at given position', () => {
  156. const parentController = new Controller();
  157. const childController1 = new Controller();
  158. const childController2 = new Controller();
  159. const collection = new Collection();
  160. parentController.register( 'x', collection );
  161. parentController.addChild( 'x', childController1 );
  162. parentController.addChild( 'x', childController2, 0 );
  163. expect( collection.get( 0 ) ).to.be.equal( childController2 );
  164. expect( collection.get( 1 ) ).to.be.equal( childController1 );
  165. } );
  166. it( 'should add a child controller which has no view', () => {
  167. const parentController = new Controller( null, new ParentView() );
  168. const childController = new Controller();
  169. parentController.register( 'x', new Collection() );
  170. return parentController.init()
  171. .then( () => {
  172. return parentController.addChild( 'x', childController );
  173. } )
  174. .then( () => {
  175. expect( parentController.getChild( 'x', 0 ) ).to.be.equal( childController );
  176. } );
  177. } );
  178. it( 'should append child controller\'s view to parent controller\'s view', () => {
  179. const parentView = new ParentView();
  180. const parentController = new Controller( null, parentView );
  181. const childController = new Controller( null, new View() );
  182. const spy1 = bender.sinon.spy( parentView, 'addChild' );
  183. parentController.register( 'x', new Collection() );
  184. parentController.addChild( 'x', childController );
  185. sinon.assert.notCalled( spy1 );
  186. parentController.removeChild( 'x', childController );
  187. return parentController.init()
  188. .then( () => {
  189. return parentController.addChild( 'x', childController );
  190. } )
  191. .then( () => {
  192. sinon.assert.calledOnce( spy1 );
  193. sinon.assert.calledWithExactly( spy1, 'x', childController.view, undefined );
  194. } );
  195. } );
  196. it( 'should append child controller\'s view to parent controller\'s view at given index', () => {
  197. const parentController = new Controller( null, new ParentView() );
  198. const childView1 = new View();
  199. const childController1 = new Controller( null, childView1 );
  200. const childView2 = new View();
  201. const childController2 = new Controller( null, childView2 );
  202. parentController.register( 'x', new Collection() );
  203. return parentController.init()
  204. .then( () => {
  205. return parentController.addChild( 'x', childController1 ).then( () => {
  206. return parentController.addChild( 'x', childController2, 0 );
  207. } );
  208. } )
  209. .then( () => {
  210. expect( parentController.view.getChild( 'x', 0 ) ).to.be.equal( childView2 );
  211. expect( parentController.view.getChild( 'x', 1 ) ).to.be.equal( childView1 );
  212. } );
  213. } );
  214. it( 'should initialize child controller if parent is ready', () => {
  215. const parentController = new Controller( null, new ParentView() );
  216. const childController = new Controller( null, new View() );
  217. const spy = bender.sinon.spy( childController, 'init' );
  218. parentController.register( 'x', new Collection() );
  219. parentController.addChild( 'x', childController );
  220. parentController.removeChild( 'x', childController );
  221. sinon.assert.notCalled( spy );
  222. return parentController.init()
  223. .then( () => {
  224. return parentController.addChild( 'x', childController );
  225. } )
  226. .then( () => {
  227. sinon.assert.calledOnce( spy );
  228. } );
  229. } );
  230. it( 'should not initialize child controller twice', () => {
  231. const parentController = new Controller( null, new ParentView() );
  232. const childController = new Controller( null, new View() );
  233. const spy = bender.sinon.spy( childController, 'init' );
  234. parentController.register( 'x', new Collection() );
  235. return parentController.init()
  236. .then( () => {
  237. return childController.init();
  238. } )
  239. .then( () => {
  240. return parentController.addChild( 'x', childController );
  241. } )
  242. .then( () => {
  243. sinon.assert.calledOnce( spy );
  244. } );
  245. } );
  246. } );
  247. describe( 'removeChild', () => {
  248. beforeEach( defineParentViewClass );
  249. it( 'should throw when no collection name', () => {
  250. const controller = new Controller();
  251. controller.register( 'x', new Collection() );
  252. expect( () => {
  253. controller.removeChild();
  254. } ).to.throw( CKEditorError, /ui-controller-removechild-badcname/ );
  255. } );
  256. it( 'should throw when collection of given name does not exist', () => {
  257. const controller = new Controller();
  258. controller.register( 'x', new Collection() );
  259. expect( () => {
  260. controller.removeChild( 'y' );
  261. } ).to.throw( CKEditorError, /ui-controller-removechild-nocol/ );
  262. } );
  263. it( 'should throw when controller or wrong controller is passed', () => {
  264. const controller = new Controller();
  265. controller.register( 'x', new Collection() );
  266. expect( () => {
  267. controller.removeChild( 'x' );
  268. } ).to.throw( CKEditorError, /ui-controller-removechild-badtype/ );
  269. expect( () => {
  270. controller.removeChild( 'x', new Date() );
  271. } ).to.throw( CKEditorError, /ui-controller-removechild-badtype/ );
  272. } );
  273. it( 'should remove child controller and return it', () => {
  274. const parentController = new Controller();
  275. const childController = new Controller();
  276. const collection = new Collection();
  277. parentController.register( 'x', collection );
  278. parentController.addChild( 'x', childController );
  279. const returned = parentController.removeChild( 'x', childController );
  280. expect( returned ).to.be.equal( childController );
  281. expect( collection.length ).to.be.equal( 0 );
  282. } );
  283. it( 'should remove child controller\'s view from parent controller\'s view', () => {
  284. const parentView = new ParentView();
  285. const parentController = new Controller( null, parentView );
  286. const childController = new Controller( null, new View() );
  287. const spy = bender.sinon.spy( parentView, 'removeChild' );
  288. parentController.register( 'x', new Collection() );
  289. parentController.addChild( 'x', childController );
  290. sinon.assert.notCalled( spy );
  291. return parentController.init()
  292. .then( () => {
  293. parentController.removeChild( 'x', childController );
  294. sinon.assert.calledOnce( spy );
  295. sinon.assert.calledWithExactly( spy, 'x', childController.view );
  296. } );
  297. } );
  298. } );
  299. describe( 'getChild', () => {
  300. beforeEach( defineParentViewClass );
  301. it( 'should throw when collection of given name does not exist', () => {
  302. const controller = new Controller();
  303. controller.register( 'x', new Collection() );
  304. expect( () => {
  305. controller.getChild( 'y', 0 );
  306. } ).to.throw( CKEditorError, /ui-controller-getchild-nocol/ );
  307. } );
  308. it( 'should get child controller by index', () => {
  309. const parentController = new Controller();
  310. const childController = new Controller();
  311. const collection = new Collection();
  312. parentController.register( 'x', collection );
  313. parentController.addChild( 'x', childController );
  314. expect( parentController.getChild( 'x', 0 ) ).to.be.equal( childController );
  315. } );
  316. } );
  317. describe( 'destroy', () => {
  318. beforeEach( defineParentViewClass );
  319. it( 'should destroy the controller', () => {
  320. const view = new View();
  321. const controller = new Controller( null, view );
  322. const spy = bender.sinon.spy( view, 'destroy' );
  323. return controller.init()
  324. .then( () => {
  325. return controller.destroy();
  326. } )
  327. .then( () => {
  328. sinon.assert.calledOnce( spy );
  329. expect( controller.model ).to.be.null;
  330. expect( controller.view ).to.be.null;
  331. expect( controller._collections ).to.be.null;
  332. } );
  333. } );
  334. it( 'should destroy the controller which has no view', () => {
  335. const controller = new Controller( null, null );
  336. return controller.init()
  337. .then( () => {
  338. return controller.destroy();
  339. } )
  340. .then( () => {
  341. expect( controller.model ).to.be.null;
  342. expect( controller.view ).to.be.null;
  343. expect( controller._collections ).to.be.null;
  344. } );
  345. } );
  346. it( 'should destroy child controllers in collections with their views', () => {
  347. const parentController = new Controller( null, new ParentView() );
  348. const childView = new View();
  349. const childController = new Controller( null, childView );
  350. const spy = bender.sinon.spy( childView, 'destroy' );
  351. parentController.register( 'x', new Collection() );
  352. parentController.addChild( 'x', childController );
  353. return parentController.init()
  354. .then( () => {
  355. return parentController.destroy();
  356. } )
  357. .then( () => {
  358. sinon.assert.calledOnce( spy );
  359. expect( childController.model ).to.be.null;
  360. expect( childController.view ).to.be.null;
  361. expect( childController._collections ).to.be.null;
  362. } );
  363. } );
  364. it( 'should destroy child controllers in collections when they have no views', () => {
  365. const parentController = new Controller( null, new ParentView() );
  366. const childController = new Controller( null, null );
  367. parentController.register( 'x', new Collection() );
  368. parentController.addChild( 'x', childController );
  369. return parentController.init()
  370. .then( () => {
  371. return parentController.destroy();
  372. } )
  373. .then( () => {
  374. expect( childController.model ).to.be.null;
  375. expect( childController.view ).to.be.null;
  376. expect( childController._collections ).to.be.null;
  377. } );
  378. } );
  379. } );
  380. } );
  381. function updateModuleReference() {
  382. View = modules[ 'ui/view' ];
  383. Controller = modules[ 'ui/controller' ];
  384. Model = modules.model;
  385. Collection = modules.collection;
  386. CKEditorError = modules.ckeditorerror;
  387. }
  388. function defineParentViewClass() {
  389. ParentView = class extends View {
  390. constructor() {
  391. super();
  392. this.el = document.createElement( 'span' );
  393. this.register( 'x', true );
  394. }
  395. };
  396. }