controller.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565
  1. /**
  2. * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* globals document */
  6. /* bender-tags: ui */
  7. import testUtils from 'tests/core/_utils/utils.js';
  8. import View from 'ckeditor5/ui/view.js';
  9. import Controller from 'ckeditor5/ui/controller.js';
  10. import ControllerCollection from 'ckeditor5/ui/controllercollection.js';
  11. import CKEditorError from 'ckeditor5/utils/ckeditorerror.js';
  12. import Model from 'ckeditor5/ui/model.js';
  13. import EventInfo from 'ckeditor5/utils/eventinfo.js';
  14. let ParentController, ParentView;
  15. testUtils.createSinonSandbox();
  16. describe( 'Controller', () => {
  17. describe( 'constructor', () => {
  18. it( 'defines basic properties', () => {
  19. const controller = new Controller();
  20. expect( controller.model ).to.be.null;
  21. expect( controller.ready ).to.be.false;
  22. expect( controller.view ).to.be.null;
  23. expect( controller.collections.length ).to.equal( 1 );
  24. expect( controller.collections.get( '$anonymous' ) ).to.be.instanceOf( ControllerCollection );
  25. } );
  26. it( 'should accept model and view', () => {
  27. const model = new Model();
  28. const view = new View();
  29. const controller = new Controller( model, view );
  30. expect( controller.model ).to.equal( model );
  31. expect( controller.view ).to.equal( view );
  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 and fire #ready event', () => {
  48. const controller = new Controller();
  49. const spy = sinon.spy( () => {
  50. expect( controller ).to.have.property( 'ready', true );
  51. } );
  52. controller.on( 'ready', spy );
  53. return controller.init().then( () => {
  54. expect( spy.calledOnce ).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 = testUtils.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 = testUtils.sinon.spy( childController1, 'init' );
  72. const spy2 = testUtils.sinon.spy( childController2, 'init' );
  73. buttonCollection.add( childController1 );
  74. buttonCollection.add( childController2 );
  75. return parentController.init().then( () => {
  76. expect( buttonCollection.get( 0 ) ).to.equal( childController1 );
  77. expect( buttonCollection.get( 1 ) ).to.equal( childController2 );
  78. sinon.assert.calledOnce( spy1 );
  79. sinon.assert.calledOnce( spy2 );
  80. } );
  81. } );
  82. it( 'should initialize child controllers in anonymous collection', () => {
  83. const parentController = new Controller( null, new View() );
  84. const child1 = new Controller( null, new View() );
  85. const child2 = new Controller( null, new View() );
  86. const spy1 = testUtils.sinon.spy( child1, 'init' );
  87. const spy2 = testUtils.sinon.spy( child2, 'init' );
  88. const collection = parentController.collections.get( '$anonymous' );
  89. parentController.add( child1 );
  90. parentController.add( child2 );
  91. expect( collection ).to.have.length( 2 );
  92. expect( collection.get( 0 ) ).to.equal( child1 );
  93. expect( collection.get( 1 ) ).to.equal( child2 );
  94. return parentController.init().then( () => {
  95. expect( collection.get( 0 ) ).to.equal( child1 );
  96. expect( collection.get( 1 ) ).to.equal( child2 );
  97. sinon.assert.calledOnce( spy1 );
  98. sinon.assert.calledOnce( spy2 );
  99. } );
  100. } );
  101. } );
  102. describe( 'add', () => {
  103. beforeEach( defineParentControllerClass );
  104. it( 'should return a promise', () => {
  105. const parentController = new ParentController();
  106. expect( parentController.add( 'x', new Controller() ) ).to.be.an.instanceof( Promise );
  107. } );
  108. it( 'should add a controller to specific collection', () => {
  109. const parentController = new ParentController();
  110. const child1 = new Controller();
  111. const child2 = new Controller();
  112. const collection = parentController.collections.get( 'x' );
  113. parentController.add( 'x', child1 );
  114. parentController.add( 'x', child2 );
  115. expect( collection ).to.have.length( 2 );
  116. expect( collection.get( 0 ) ).to.equal( child1 );
  117. expect( collection.get( 1 ) ).to.equal( child2 );
  118. } );
  119. it( 'should add a controller at specific index', () => {
  120. const parentController = new ParentController();
  121. const child1 = new Controller();
  122. const child2 = new Controller();
  123. const collection = parentController.collections.get( 'x' );
  124. parentController.add( 'x', child1 );
  125. parentController.add( 'x', child2, 0 );
  126. expect( collection ).to.have.length( 2 );
  127. expect( collection.get( 0 ) ).to.equal( child2 );
  128. expect( collection.get( 1 ) ).to.equal( child1 );
  129. } );
  130. it( 'should add a controller to the anonymous collection', () => {
  131. const parentController = new ParentController( null, new View() );
  132. const child1 = new Controller( null, new View() );
  133. const child2 = new Controller( null, new View() );
  134. const collection = parentController.collections.get( '$anonymous' );
  135. parentController.add( child1 );
  136. parentController.add( child2 );
  137. expect( collection ).to.have.length( 2 );
  138. expect( collection.get( 0 ) ).to.equal( child1 );
  139. expect( collection.get( 1 ) ).to.equal( child2 );
  140. } );
  141. } );
  142. describe( 'remove', () => {
  143. beforeEach( defineParentControllerClass );
  144. it( 'should remove a controller from specific collection – by instance', () => {
  145. const parentController = new ParentController();
  146. const child1 = new Controller();
  147. const child2 = new Controller();
  148. const child3 = new Controller();
  149. const collection = parentController.collections.get( 'x' );
  150. parentController.add( 'x', child1 );
  151. parentController.add( 'x', child2 );
  152. parentController.add( 'x', child3 );
  153. const removed = parentController.remove( 'x', child2 );
  154. expect( collection ).to.have.length( 2 );
  155. expect( collection.get( 0 ) ).to.equal( child1 );
  156. expect( collection.get( 1 ) ).to.equal( child3 );
  157. expect( removed ).to.equal( child2 );
  158. } );
  159. it( 'should remove a controller from specific collection – by index', () => {
  160. const parentController = new ParentController();
  161. const child1 = new Controller();
  162. const child2 = new Controller();
  163. const child3 = new Controller();
  164. const collection = parentController.collections.get( 'x' );
  165. parentController.add( 'x', child1 );
  166. parentController.add( 'x', child2 );
  167. parentController.add( 'x', child3 );
  168. const removed = parentController.remove( 'x', 1 );
  169. expect( collection ).to.have.length( 2 );
  170. expect( collection.get( 0 ) ).to.equal( child1 );
  171. expect( collection.get( 1 ) ).to.equal( child3 );
  172. expect( removed ).to.equal( child2 );
  173. } );
  174. it( 'should remove a controller from the anonymous collection', () => {
  175. const parentController = new ParentController();
  176. const child1 = new Controller();
  177. const child2 = new Controller();
  178. const child3 = new Controller();
  179. parentController.add( child1 );
  180. parentController.add( child2 );
  181. parentController.add( child3 );
  182. const collection = parentController.collections.get( '$anonymous' );
  183. const removed = parentController.remove( child2 );
  184. expect( collection ).to.have.length( 2 );
  185. expect( collection.get( 0 ) ).to.equal( child1 );
  186. expect( collection.get( 1 ) ).to.equal( child3 );
  187. expect( removed ).to.equal( child2 );
  188. } );
  189. } );
  190. describe( 'collections', () => {
  191. describe( 'add', () => {
  192. beforeEach( defineParentViewClass );
  193. beforeEach( defineParentControllerClass );
  194. it( 'should add a child controller which has no view', () => {
  195. const parentController = new ParentController( null, new ParentView() );
  196. const collection = parentController.collections.get( 'x' );
  197. const childController = new Controller();
  198. return parentController.init()
  199. .then( () => {
  200. return collection.add( childController );
  201. } )
  202. .then( () => {
  203. expect( collection.get( 0 ) ).to.equal( childController );
  204. } );
  205. } );
  206. it( 'should append child controller\'s view to parent controller\'s view', () => {
  207. const parentView = new ParentView();
  208. const parentController = new ParentController( null, parentView );
  209. const collection = parentController.collections.get( 'x' );
  210. const childController = new Controller( null, new View() );
  211. const spy = testUtils.sinon.spy();
  212. parentView.regions.get( 'x' ).views.on( 'add', spy );
  213. collection.add( childController );
  214. sinon.assert.notCalled( spy );
  215. collection.remove( childController );
  216. return parentController.init()
  217. .then( () => {
  218. return collection.add( childController );
  219. } )
  220. .then( () => {
  221. sinon.assert.calledOnce( spy );
  222. sinon.assert.calledWithExactly( spy,
  223. sinon.match.instanceOf( EventInfo ), childController.view, 0 );
  224. } );
  225. } );
  226. it( 'should append child controller\'s view to parent controller\'s view at given index', () => {
  227. const parentController = new ParentController( null, new ParentView() );
  228. const collection = parentController.collections.get( 'x' );
  229. const childView1 = new View();
  230. const childController1 = new Controller( null, childView1 );
  231. const childView2 = new View();
  232. const childController2 = new Controller( null, childView2 );
  233. return parentController.init()
  234. .then( () => {
  235. return collection.add( childController1 ).then( () => {
  236. return collection.add( childController2, 0 );
  237. } );
  238. } )
  239. .then( () => {
  240. const region = parentController.view.regions.get( 'x' );
  241. expect( region.views.get( 0 ) ).to.equal( childView2 );
  242. expect( region.views.get( 1 ) ).to.equal( childView1 );
  243. } );
  244. } );
  245. it( 'should not handle views of anonymous collection children', () => {
  246. const parentController = new ParentController( null, new ParentView() );
  247. const childView1 = new View();
  248. const childController1 = new Controller( null, childView1 );
  249. const childView2 = new View();
  250. const childController2 = new Controller( null, childView2 );
  251. return parentController.init()
  252. .then( () => {
  253. return parentController.add( childController1 ).then( () => {
  254. return parentController.add( childController2 );
  255. } );
  256. } )
  257. .then( () => {
  258. const region = parentController.view.regions.get( 'x' );
  259. expect( region.views ).to.have.length( 0 );
  260. } );
  261. } );
  262. } );
  263. describe( 'remove', () => {
  264. beforeEach( defineParentViewClass );
  265. it( 'should remove child controller\'s view from parent controller\'s view', () => {
  266. const parentView = new ParentView();
  267. const parentController = new ParentController( null, parentView );
  268. const collection = parentController.collections.get( 'x' );
  269. const childController = new Controller( null, new View() );
  270. const spy = testUtils.sinon.spy();
  271. parentView.regions.get( 'x' ).views.on( 'remove', spy );
  272. collection.add( childController );
  273. sinon.assert.notCalled( spy );
  274. return parentController.init()
  275. .then( () => {
  276. collection.remove( childController );
  277. sinon.assert.calledOnce( spy );
  278. sinon.assert.calledWithExactly( spy,
  279. sinon.match.instanceOf( EventInfo ), childController.view );
  280. } );
  281. } );
  282. } );
  283. } );
  284. describe( 'destroy', () => {
  285. beforeEach( defineParentViewClass );
  286. beforeEach( defineParentControllerClass );
  287. it( 'should destroy the controller', () => {
  288. const view = new View();
  289. const controller = new Controller( null, view );
  290. const spy = testUtils.sinon.spy( view, 'destroy' );
  291. return controller.init()
  292. .then( () => {
  293. return controller.destroy();
  294. } )
  295. .then( () => {
  296. sinon.assert.calledOnce( spy );
  297. expect( controller.model ).to.be.null;
  298. expect( controller.ready ).to.be.null;
  299. expect( controller.view ).to.be.null;
  300. expect( controller.collections ).to.be.null;
  301. } );
  302. } );
  303. it( 'should destroy the controller which has no view', () => {
  304. const controller = new Controller( null, null );
  305. return controller.init()
  306. .then( () => {
  307. return controller.destroy();
  308. } )
  309. .then( () => {
  310. expect( controller.model ).to.be.null;
  311. expect( controller.view ).to.be.null;
  312. expect( controller.collections ).to.be.null;
  313. } );
  314. } );
  315. it( 'should destroy child controllers in collections with their views', () => {
  316. const parentController = new ParentController( null, new ParentView() );
  317. const collection = parentController.collections.get( 'x' );
  318. const childView = new View();
  319. const childController = new Controller( null, childView );
  320. const spy = testUtils.sinon.spy( childView, 'destroy' );
  321. collection.add( childController );
  322. return parentController.init()
  323. .then( () => {
  324. return parentController.destroy();
  325. } )
  326. .then( () => {
  327. sinon.assert.calledOnce( spy );
  328. expect( childController.model ).to.be.null;
  329. expect( childController.view ).to.be.null;
  330. expect( childController.collections ).to.be.null;
  331. } );
  332. } );
  333. it( 'should destroy child controllers in collections when they have no views', () => {
  334. const parentController = new ParentController( null, new ParentView() );
  335. const collection = parentController.collections.get( 'x' );
  336. const childController = new Controller( null, null );
  337. collection.add( childController );
  338. return parentController.init()
  339. .then( () => {
  340. return parentController.destroy();
  341. } )
  342. .then( () => {
  343. expect( childController.model ).to.be.null;
  344. expect( childController.view ).to.be.null;
  345. expect( childController.collections ).to.be.null;
  346. } );
  347. } );
  348. it( 'should destroy child controllers in anonymous collection along with their views', () => {
  349. const parentController = new ParentController( null, new ParentView() );
  350. const childView = new View();
  351. const childController = new Controller( null, childView );
  352. const spy = testUtils.sinon.spy( childView, 'destroy' );
  353. parentController.add( childController );
  354. return parentController.init()
  355. .then( () => {
  356. return parentController.destroy();
  357. } )
  358. .then( () => {
  359. sinon.assert.calledOnce( spy );
  360. expect( childController.model ).to.be.null;
  361. expect( childController.view ).to.be.null;
  362. expect( childController.collections ).to.be.null;
  363. } );
  364. } );
  365. // See #11
  366. it( 'should correctly destroy multiple controller collections', () => {
  367. const parentController = new Controller();
  368. const controllerCollectionCollection = parentController.collections; // Yep... it's correct :D.
  369. const childControllers = [];
  370. const collections = [ 'a', 'b', 'c' ].map( name => {
  371. const collection = new ControllerCollection( name );
  372. const childController = new Controller();
  373. childController.destroy = sinon.spy();
  374. parentController.collections.add( collection );
  375. collection.add( childController );
  376. childControllers.push( childController );
  377. return collection;
  378. } );
  379. return parentController.init()
  380. .then( () => {
  381. return parentController.destroy();
  382. } )
  383. .then( () => {
  384. expect( controllerCollectionCollection ).to.have.lengthOf( 0, 'parentController.collections is empty' );
  385. expect( collections.map( collection => collection.length ) )
  386. .to.deep.equal( [ 0, 0, 0 ], 'all collections are empty' );
  387. expect( childControllers.map( controller => controller.destroy.calledOnce ) )
  388. .to.deep.equal( [ true, true, true ], 'all child controllers were destroyed' );
  389. } );
  390. } );
  391. // See #11
  392. it( 'should correctly destroy collections with multiple child controllers', () => {
  393. const parentController = new Controller();
  394. const controllerCollectionCollection = parentController.collections; // Yep... it's correct :D.
  395. const controllerCollection = new ControllerCollection( 'foo' );
  396. const childControllers = [];
  397. parentController.collections.add( controllerCollection );
  398. for ( let i = 0; i < 3; i++ ) {
  399. const childController = new Controller();
  400. childController.destroy = sinon.spy();
  401. childControllers.push( childController );
  402. parentController.add( 'foo', childController );
  403. }
  404. return parentController.init()
  405. .then( () => {
  406. return parentController.destroy();
  407. } )
  408. .then( () => {
  409. expect( controllerCollectionCollection ).to.have.lengthOf( 0, 'parentController.collections is empty' );
  410. expect( controllerCollection ).to.have.lengthOf( 0, 'child controller collection is empty' );
  411. expect( childControllers.map( controller => controller.destroy.calledOnce ) )
  412. .to.deep.equal( [ true, true, true ], 'all child controllers were destroyed' );
  413. } );
  414. } );
  415. } );
  416. describe( 'addCollection', () => {
  417. it( 'should add a new collection', () => {
  418. const controller = new Controller();
  419. controller.addCollection( 'foo' );
  420. expect( controller.collections ).to.have.length( 2 );
  421. expect( controller.collections.get( 'foo' ).name ).to.equal( 'foo' );
  422. } );
  423. it( 'should return the collection which has been created (chaining)', () => {
  424. const controller = new Controller();
  425. const returned = controller.addCollection( 'foo' );
  426. expect( returned ).to.be.instanceOf( ControllerCollection );
  427. } );
  428. it( 'should pass locale to controller collection', () => {
  429. const locale = {};
  430. const view = new View( locale );
  431. expect( new Controller( {}, view ).addCollection( 'foo' ).locale ).to.equal( locale );
  432. } );
  433. } );
  434. } );
  435. function defineParentViewClass() {
  436. ParentView = class extends View {
  437. constructor() {
  438. super();
  439. this.element = document.createElement( 'span' );
  440. this.register( 'x', true );
  441. }
  442. };
  443. }
  444. function defineParentControllerClass() {
  445. ParentController = class extends Controller {
  446. constructor( ...args ) {
  447. super( ...args );
  448. this.addCollection( 'x' );
  449. }
  450. };
  451. }