8
0

controller.js 15 KB

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