8
0

controller.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  1. /**
  2. * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* bender-tags: ui */
  6. import testUtils from '/tests/ckeditor5/_utils/utils.js';
  7. import View from '/ckeditor5/ui/view.js';
  8. import Controller from '/ckeditor5/ui/controller.js';
  9. import ControllerCollection from '/ckeditor5/ui/controllercollection.js';
  10. import CKEditorError from '/ckeditor5/utils/ckeditorerror.js';
  11. import Model from '/ckeditor5/ui/model.js';
  12. import EventInfo from '/ckeditor5/utils/eventinfo.js';
  13. let ParentController, ParentView;
  14. testUtils.createSinonSandbox();
  15. describe( 'Controller', () => {
  16. describe( 'constructor', () => {
  17. it( 'defines basic properties', () => {
  18. const controller = new Controller();
  19. expect( controller.model ).to.be.null;
  20. expect( controller.ready ).to.be.false;
  21. expect( controller.view ).to.be.null;
  22. expect( controller.collections.length ).to.be.equal( 0 );
  23. } );
  24. it( 'should accept model and view', () => {
  25. const model = new Model();
  26. const view = new View();
  27. const controller = new Controller( model, view );
  28. expect( controller.model ).to.be.equal( model );
  29. expect( controller.view ).to.be.equal( view );
  30. } );
  31. } );
  32. describe( 'init', () => {
  33. it( 'should throw when already initialized', () => {
  34. const controller = new Controller();
  35. return controller.init()
  36. .then( () => {
  37. controller.init();
  38. throw new Error( 'This should not be executed.' );
  39. } )
  40. .catch( ( err ) => {
  41. expect( err ).to.be.instanceof( CKEditorError );
  42. expect( err.message ).to.match( /ui-controller-init-re/ );
  43. } );
  44. } );
  45. it( 'should set #ready flag and fire #ready event', () => {
  46. const controller = new Controller();
  47. const spy = sinon.spy( () => {
  48. expect( controller ).to.have.property( 'ready', true );
  49. } );
  50. controller.on( 'ready', spy );
  51. return controller.init().then( () => {
  52. expect( spy.calledOnce ).to.be.true;
  53. } );
  54. } );
  55. it( 'should initialize own view', () => {
  56. const view = new View();
  57. const controller = new Controller( null, view );
  58. const spy = testUtils.sinon.spy( view, 'init' );
  59. return controller.init().then( () => {
  60. sinon.assert.calledOnce( spy );
  61. } );
  62. } );
  63. it( 'should initialize child controllers in own collections', () => {
  64. const parentController = new Controller();
  65. const buttonCollection = new ControllerCollection( 'buttons' );
  66. parentController.collections.add( buttonCollection );
  67. const childController1 = new Controller();
  68. const childController2 = new Controller();
  69. const spy1 = testUtils.sinon.spy( childController1, 'init' );
  70. const spy2 = testUtils.sinon.spy( childController2, 'init' );
  71. buttonCollection.add( childController1 );
  72. buttonCollection.add( childController2 );
  73. return parentController.init().then( () => {
  74. expect( buttonCollection.get( 0 ) ).to.be.equal( childController1 );
  75. expect( buttonCollection.get( 1 ) ).to.be.equal( childController2 );
  76. sinon.assert.calledOnce( spy1 );
  77. sinon.assert.calledOnce( spy2 );
  78. } );
  79. } );
  80. } );
  81. describe( 'add', () => {
  82. beforeEach( defineParentControllerClass );
  83. it( 'should add a controller to specific collection', () => {
  84. const parentController = new ParentController();
  85. const child1 = new Controller();
  86. const child2 = new Controller();
  87. const collection = parentController.collections.get( 'x' );
  88. parentController.add( 'x', child1 );
  89. parentController.add( 'x', child2 );
  90. expect( collection ).to.have.length( 2 );
  91. expect( collection.get( 0 ) ).to.be.equal( child1 );
  92. expect( collection.get( 1 ) ).to.be.equal( child2 );
  93. } );
  94. it( 'should add a controller at specific index', () => {
  95. const parentController = new ParentController();
  96. const child1 = new Controller();
  97. const child2 = new Controller();
  98. const collection = parentController.collections.get( 'x' );
  99. parentController.add( 'x', child1 );
  100. parentController.add( 'x', child2, 0 );
  101. expect( collection ).to.have.length( 2 );
  102. expect( collection.get( 0 ) ).to.be.equal( child2 );
  103. expect( collection.get( 1 ) ).to.be.equal( child1 );
  104. } );
  105. } );
  106. describe( 'remove', () => {
  107. beforeEach( defineParentControllerClass );
  108. it( 'should remove a controller from specific collection – by instance', () => {
  109. const parentController = new ParentController();
  110. const child1 = new Controller();
  111. const child2 = new Controller();
  112. const child3 = new Controller();
  113. const collection = parentController.collections.get( 'x' );
  114. parentController.add( 'x', child1 );
  115. parentController.add( 'x', child2 );
  116. parentController.add( 'x', child3 );
  117. const removed = parentController.remove( 'x', child2 );
  118. expect( collection ).to.have.length( 2 );
  119. expect( collection.get( 0 ) ).to.be.equal( child1 );
  120. expect( collection.get( 1 ) ).to.be.equal( child3 );
  121. expect( removed ).to.be.equal( child2 );
  122. } );
  123. it( 'should remove a controller from specific collection – by index', () => {
  124. const parentController = new ParentController();
  125. const child1 = new Controller();
  126. const child2 = new Controller();
  127. const child3 = new Controller();
  128. const collection = parentController.collections.get( 'x' );
  129. parentController.add( 'x', child1 );
  130. parentController.add( 'x', child2 );
  131. parentController.add( 'x', child3 );
  132. const removed = parentController.remove( 'x', 1 );
  133. expect( collection ).to.have.length( 2 );
  134. expect( collection.get( 0 ) ).to.be.equal( child1 );
  135. expect( collection.get( 1 ) ).to.be.equal( child3 );
  136. expect( removed ).to.be.equal( child2 );
  137. } );
  138. } );
  139. describe( 'collections', () => {
  140. describe( 'add', () => {
  141. beforeEach( defineParentViewClass );
  142. beforeEach( defineParentControllerClass );
  143. it( 'should add a child controller which has no view', () => {
  144. const parentController = new ParentController( null, new ParentView() );
  145. const collection = parentController.collections.get( 'x' );
  146. const childController = new Controller();
  147. return parentController.init()
  148. .then( () => {
  149. return collection.add( childController );
  150. } )
  151. .then( () => {
  152. expect( collection.get( 0 ) ).to.be.equal( childController );
  153. } );
  154. } );
  155. it( 'should append child controller\'s view to parent controller\'s view', () => {
  156. const parentView = new ParentView();
  157. const parentController = new ParentController( null, parentView );
  158. const collection = parentController.collections.get( 'x' );
  159. const childController = new Controller( null, new View() );
  160. const spy = testUtils.sinon.spy();
  161. parentView.regions.get( 'x' ).views.on( 'add', spy );
  162. collection.add( childController );
  163. sinon.assert.notCalled( spy );
  164. collection.remove( childController );
  165. return parentController.init()
  166. .then( () => {
  167. return collection.add( childController );
  168. } )
  169. .then( () => {
  170. sinon.assert.calledOnce( spy );
  171. sinon.assert.calledWithExactly( spy,
  172. sinon.match.instanceOf( EventInfo ), childController.view, 0 );
  173. } );
  174. } );
  175. it( 'should append child controller\'s view to parent controller\'s view at given index', () => {
  176. const parentController = new ParentController( null, new ParentView() );
  177. const collection = parentController.collections.get( 'x' );
  178. const childView1 = new View();
  179. const childController1 = new Controller( null, childView1 );
  180. const childView2 = new View();
  181. const childController2 = new Controller( null, childView2 );
  182. return parentController.init()
  183. .then( () => {
  184. return collection.add( childController1 ).then( () => {
  185. return collection.add( childController2, 0 );
  186. } );
  187. } )
  188. .then( () => {
  189. const region = parentController.view.regions.get( 'x' );
  190. expect( region.views.get( 0 ) ).to.be.equal( childView2 );
  191. expect( region.views.get( 1 ) ).to.be.equal( childView1 );
  192. } );
  193. } );
  194. } );
  195. describe( 'remove', () => {
  196. beforeEach( defineParentViewClass );
  197. it( 'should remove child controller\'s view from parent controller\'s view', () => {
  198. const parentView = new ParentView();
  199. const parentController = new ParentController( null, parentView );
  200. const collection = parentController.collections.get( 'x' );
  201. const childController = new Controller( null, new View() );
  202. const spy = testUtils.sinon.spy();
  203. parentView.regions.get( 'x' ).views.on( 'remove', spy );
  204. collection.add( childController );
  205. sinon.assert.notCalled( spy );
  206. return parentController.init()
  207. .then( () => {
  208. collection.remove( childController );
  209. sinon.assert.calledOnce( spy );
  210. sinon.assert.calledWithExactly( spy,
  211. sinon.match.instanceOf( EventInfo ), childController.view );
  212. } );
  213. } );
  214. } );
  215. } );
  216. describe( 'destroy', () => {
  217. beforeEach( defineParentViewClass );
  218. beforeEach( defineParentControllerClass );
  219. it( 'should destroy the controller', () => {
  220. const view = new View();
  221. const controller = new Controller( null, view );
  222. const spy = testUtils.sinon.spy( view, 'destroy' );
  223. return controller.init()
  224. .then( () => {
  225. return controller.destroy();
  226. } )
  227. .then( () => {
  228. sinon.assert.calledOnce( spy );
  229. expect( controller.model ).to.be.null;
  230. expect( controller.ready ).to.be.null;
  231. expect( controller.view ).to.be.null;
  232. expect( controller.collections ).to.be.null;
  233. } );
  234. } );
  235. it( 'should destroy the controller which has no view', () => {
  236. const controller = new Controller( null, null );
  237. return controller.init()
  238. .then( () => {
  239. return controller.destroy();
  240. } )
  241. .then( () => {
  242. expect( controller.model ).to.be.null;
  243. expect( controller.view ).to.be.null;
  244. expect( controller.collections ).to.be.null;
  245. } );
  246. } );
  247. it( 'should destroy child controllers in collections with their views', () => {
  248. const parentController = new ParentController( null, new ParentView() );
  249. const collection = parentController.collections.get( 'x' );
  250. const childView = new View();
  251. const childController = new Controller( null, childView );
  252. const spy = testUtils.sinon.spy( childView, 'destroy' );
  253. collection.add( childController );
  254. return parentController.init()
  255. .then( () => {
  256. return parentController.destroy();
  257. } )
  258. .then( () => {
  259. sinon.assert.calledOnce( spy );
  260. expect( childController.model ).to.be.null;
  261. expect( childController.view ).to.be.null;
  262. expect( childController.collections ).to.be.null;
  263. } );
  264. } );
  265. it( 'should destroy child controllers in collections when they have no views', () => {
  266. const parentController = new ParentController( null, new ParentView() );
  267. const collection = parentController.collections.get( 'x' );
  268. const childController = new Controller( null, null );
  269. collection.add( childController );
  270. return parentController.init()
  271. .then( () => {
  272. return parentController.destroy();
  273. } )
  274. .then( () => {
  275. expect( childController.model ).to.be.null;
  276. expect( childController.view ).to.be.null;
  277. expect( childController.collections ).to.be.null;
  278. } );
  279. } );
  280. // See #11
  281. it( 'should correctly destroy multiple controller collections', () => {
  282. const parentController = new Controller();
  283. const controllerCollectionCollection = parentController.collections; // Yep... it's correct :D.
  284. const childControllers = [];
  285. const collections = [ 'a', 'b', 'c' ].map( name => {
  286. const collection = new ControllerCollection( name );
  287. const childController = new Controller();
  288. childController.destroy = sinon.spy();
  289. parentController.collections.add( collection );
  290. collection.add( childController );
  291. childControllers.push( childController );
  292. return collection;
  293. } );
  294. return parentController.init()
  295. .then( () => {
  296. return parentController.destroy();
  297. } )
  298. .then( () => {
  299. expect( controllerCollectionCollection ).to.have.lengthOf( 0, 'parentController.collections is empty' );
  300. expect( collections.map( collection => collection.length ) )
  301. .to.deep.equal( [ 0, 0, 0 ], 'all collections are empty' );
  302. expect( childControllers.map( controller => controller.destroy.calledOnce ) )
  303. .to.deep.equal( [ true, true, true ], 'all child controllers were destroyed' );
  304. } );
  305. } );
  306. // See #11
  307. it( 'should correctly destroy collections with multiple child controllers', () => {
  308. const parentController = new Controller();
  309. const controllerCollectionCollection = parentController.collections; // Yep... it's correct :D.
  310. const controllerCollection = new ControllerCollection( 'foo' );
  311. const childControllers = [];
  312. parentController.collections.add( controllerCollection );
  313. for ( let i = 0; i < 3; i++ ) {
  314. const childController = new Controller();
  315. childController.destroy = sinon.spy();
  316. childControllers.push( childController );
  317. parentController.add( 'foo', childController );
  318. }
  319. return parentController.init()
  320. .then( () => {
  321. return parentController.destroy();
  322. } )
  323. .then( () => {
  324. expect( controllerCollectionCollection ).to.have.lengthOf( 0, 'parentController.collections is empty' );
  325. expect( controllerCollection ).to.have.lengthOf( 0, 'child controller collection is empty' );
  326. expect( childControllers.map( controller => controller.destroy.calledOnce ) )
  327. .to.deep.equal( [ true, true, true ], 'all child controllers were destroyed' );
  328. } );
  329. } );
  330. } );
  331. describe( 'addCollection', () => {
  332. it( 'should add a new collection', () => {
  333. const controller = new Controller();
  334. controller.addCollection( 'foo' );
  335. expect( controller.collections ).to.have.length( 1 );
  336. expect( controller.collections.get( 'foo' ).name ).to.equal( 'foo' );
  337. } );
  338. it( 'should return the collection which has been created (chaining)', () => {
  339. const controller = new Controller();
  340. const returned = controller.addCollection( 'foo' );
  341. expect( returned ).to.be.instanceOf( ControllerCollection );
  342. } );
  343. it( 'should pass locale to controller collection', () => {
  344. const controller = new Controller();
  345. const locale = {};
  346. controller.addCollection( 'foo', locale );
  347. expect( controller.collections.get( 'foo' ).locale ).to.equal( locale );
  348. } );
  349. } );
  350. } );
  351. function defineParentViewClass() {
  352. ParentView = class extends View {
  353. constructor() {
  354. super();
  355. this.element = document.createElement( 'span' );
  356. this.register( 'x', true );
  357. }
  358. };
  359. }
  360. function defineParentControllerClass() {
  361. ParentController = class extends Controller {
  362. constructor( ...args ) {
  363. super( ...args );
  364. this.addCollection( 'x' );
  365. }
  366. };
  367. }