controller.js 18 KB

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