8
0

controller.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  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. } );
  82. describe( 'add', () => {
  83. beforeEach( defineParentControllerClass );
  84. it( 'should add a controller to specific collection', () => {
  85. const parentController = new ParentController();
  86. const child1 = new Controller();
  87. const child2 = new Controller();
  88. const collection = parentController.collections.get( 'x' );
  89. parentController.add( 'x', child1 );
  90. parentController.add( 'x', child2 );
  91. expect( collection ).to.have.length( 2 );
  92. expect( collection.get( 0 ) ).to.be.equal( child1 );
  93. expect( collection.get( 1 ) ).to.be.equal( child2 );
  94. } );
  95. it( 'should add a controller at specific index', () => {
  96. const parentController = new ParentController();
  97. const child1 = new Controller();
  98. const child2 = new Controller();
  99. const collection = parentController.collections.get( 'x' );
  100. parentController.add( 'x', child1 );
  101. parentController.add( 'x', child2, 0 );
  102. expect( collection ).to.have.length( 2 );
  103. expect( collection.get( 0 ) ).to.be.equal( child2 );
  104. expect( collection.get( 1 ) ).to.be.equal( child1 );
  105. } );
  106. } );
  107. describe( 'remove', () => {
  108. beforeEach( defineParentControllerClass );
  109. it( 'should remove a controller from specific collection – by instance', () => {
  110. const parentController = new ParentController();
  111. const child1 = new Controller();
  112. const child2 = new Controller();
  113. const child3 = new Controller();
  114. const collection = parentController.collections.get( 'x' );
  115. parentController.add( 'x', child1 );
  116. parentController.add( 'x', child2 );
  117. parentController.add( 'x', child3 );
  118. const removed = parentController.remove( 'x', child2 );
  119. expect( collection ).to.have.length( 2 );
  120. expect( collection.get( 0 ) ).to.be.equal( child1 );
  121. expect( collection.get( 1 ) ).to.be.equal( child3 );
  122. expect( removed ).to.be.equal( child2 );
  123. } );
  124. it( 'should remove a controller from specific collection – by index', () => {
  125. const parentController = new ParentController();
  126. const child1 = new Controller();
  127. const child2 = new Controller();
  128. const child3 = new Controller();
  129. const collection = parentController.collections.get( 'x' );
  130. parentController.add( 'x', child1 );
  131. parentController.add( 'x', child2 );
  132. parentController.add( 'x', child3 );
  133. const removed = parentController.remove( 'x', 1 );
  134. expect( collection ).to.have.length( 2 );
  135. expect( collection.get( 0 ) ).to.be.equal( child1 );
  136. expect( collection.get( 1 ) ).to.be.equal( child3 );
  137. expect( removed ).to.be.equal( child2 );
  138. } );
  139. } );
  140. describe( 'collections', () => {
  141. describe( 'add', () => {
  142. beforeEach( defineParentViewClass );
  143. beforeEach( defineParentControllerClass );
  144. it( 'should add a child controller which has no view', () => {
  145. const parentController = new ParentController( null, new ParentView() );
  146. const collection = parentController.collections.get( 'x' );
  147. const childController = new Controller();
  148. return parentController.init()
  149. .then( () => {
  150. return collection.add( childController );
  151. } )
  152. .then( () => {
  153. expect( collection.get( 0 ) ).to.be.equal( childController );
  154. } );
  155. } );
  156. it( 'should append child controller\'s view to parent controller\'s view', () => {
  157. const parentView = new ParentView();
  158. const parentController = new ParentController( null, parentView );
  159. const collection = parentController.collections.get( 'x' );
  160. const childController = new Controller( null, new View() );
  161. const spy = testUtils.sinon.spy();
  162. parentView.regions.get( 'x' ).views.on( 'add', spy );
  163. collection.add( childController );
  164. sinon.assert.notCalled( spy );
  165. collection.remove( childController );
  166. return parentController.init()
  167. .then( () => {
  168. return collection.add( childController );
  169. } )
  170. .then( () => {
  171. sinon.assert.calledOnce( spy );
  172. sinon.assert.calledWithExactly( spy,
  173. sinon.match.instanceOf( EventInfo ), childController.view, 0 );
  174. } );
  175. } );
  176. it( 'should append child controller\'s view to parent controller\'s view at given index', () => {
  177. const parentController = new ParentController( null, new ParentView() );
  178. const collection = parentController.collections.get( 'x' );
  179. const childView1 = new View();
  180. const childController1 = new Controller( null, childView1 );
  181. const childView2 = new View();
  182. const childController2 = new Controller( null, childView2 );
  183. return parentController.init()
  184. .then( () => {
  185. return collection.add( childController1 ).then( () => {
  186. return collection.add( childController2, 0 );
  187. } );
  188. } )
  189. .then( () => {
  190. const region = parentController.view.regions.get( 'x' );
  191. expect( region.views.get( 0 ) ).to.be.equal( childView2 );
  192. expect( region.views.get( 1 ) ).to.be.equal( childView1 );
  193. } );
  194. } );
  195. } );
  196. describe( 'remove', () => {
  197. beforeEach( defineParentViewClass );
  198. it( 'should remove child controller\'s view from parent controller\'s view', () => {
  199. const parentView = new ParentView();
  200. const parentController = new ParentController( null, parentView );
  201. const collection = parentController.collections.get( 'x' );
  202. const childController = new Controller( null, new View() );
  203. const spy = testUtils.sinon.spy();
  204. parentView.regions.get( 'x' ).views.on( 'remove', spy );
  205. collection.add( childController );
  206. sinon.assert.notCalled( spy );
  207. return parentController.init()
  208. .then( () => {
  209. collection.remove( childController );
  210. sinon.assert.calledOnce( spy );
  211. sinon.assert.calledWithExactly( spy,
  212. sinon.match.instanceOf( EventInfo ), childController.view );
  213. } );
  214. } );
  215. } );
  216. } );
  217. describe( 'destroy', () => {
  218. beforeEach( defineParentViewClass );
  219. beforeEach( defineParentControllerClass );
  220. it( 'should destroy the controller', () => {
  221. const view = new View();
  222. const controller = new Controller( null, view );
  223. const spy = testUtils.sinon.spy( view, 'destroy' );
  224. return controller.init()
  225. .then( () => {
  226. return controller.destroy();
  227. } )
  228. .then( () => {
  229. sinon.assert.calledOnce( spy );
  230. expect( controller.model ).to.be.null;
  231. expect( controller.ready ).to.be.null;
  232. expect( controller.view ).to.be.null;
  233. expect( controller.collections ).to.be.null;
  234. } );
  235. } );
  236. it( 'should destroy the controller which has no view', () => {
  237. const controller = new Controller( null, null );
  238. return controller.init()
  239. .then( () => {
  240. return controller.destroy();
  241. } )
  242. .then( () => {
  243. expect( controller.model ).to.be.null;
  244. expect( controller.view ).to.be.null;
  245. expect( controller.collections ).to.be.null;
  246. } );
  247. } );
  248. it( 'should destroy child controllers in collections with their views', () => {
  249. const parentController = new ParentController( null, new ParentView() );
  250. const collection = parentController.collections.get( 'x' );
  251. const childView = new View();
  252. const childController = new Controller( null, childView );
  253. const spy = testUtils.sinon.spy( childView, 'destroy' );
  254. collection.add( childController );
  255. return parentController.init()
  256. .then( () => {
  257. return parentController.destroy();
  258. } )
  259. .then( () => {
  260. sinon.assert.calledOnce( spy );
  261. expect( childController.model ).to.be.null;
  262. expect( childController.view ).to.be.null;
  263. expect( childController.collections ).to.be.null;
  264. } );
  265. } );
  266. it( 'should destroy child controllers in collections when they have no views', () => {
  267. const parentController = new ParentController( null, new ParentView() );
  268. const collection = parentController.collections.get( 'x' );
  269. const childController = new Controller( null, null );
  270. collection.add( childController );
  271. return parentController.init()
  272. .then( () => {
  273. return parentController.destroy();
  274. } )
  275. .then( () => {
  276. expect( childController.model ).to.be.null;
  277. expect( childController.view ).to.be.null;
  278. expect( childController.collections ).to.be.null;
  279. } );
  280. } );
  281. // See #11
  282. it( 'should correctly destroy multiple controller collections', () => {
  283. const parentController = new Controller();
  284. const controllerCollectionCollection = parentController.collections; // Yep... it's correct :D.
  285. const childControllers = [];
  286. const collections = [ 'a', 'b', 'c' ].map( name => {
  287. const collection = new ControllerCollection( name );
  288. const childController = new Controller();
  289. childController.destroy = sinon.spy();
  290. parentController.collections.add( collection );
  291. collection.add( childController );
  292. childControllers.push( childController );
  293. return collection;
  294. } );
  295. return parentController.init()
  296. .then( () => {
  297. return parentController.destroy();
  298. } )
  299. .then( () => {
  300. expect( controllerCollectionCollection ).to.have.lengthOf( 0, 'parentController.collections is empty' );
  301. expect( collections.map( collection => collection.length ) )
  302. .to.deep.equal( [ 0, 0, 0 ], 'all collections are empty' );
  303. expect( childControllers.map( controller => controller.destroy.calledOnce ) )
  304. .to.deep.equal( [ true, true, true ], 'all child controllers were destroyed' );
  305. } );
  306. } );
  307. // See #11
  308. it( 'should correctly destroy collections with multiple child controllers', () => {
  309. const parentController = new Controller();
  310. const controllerCollectionCollection = parentController.collections; // Yep... it's correct :D.
  311. const controllerCollection = new ControllerCollection( 'foo' );
  312. const childControllers = [];
  313. parentController.collections.add( controllerCollection );
  314. for ( let i = 0; i < 3; i++ ) {
  315. const childController = new Controller();
  316. childController.destroy = sinon.spy();
  317. childControllers.push( childController );
  318. parentController.add( 'foo', childController );
  319. }
  320. return parentController.init()
  321. .then( () => {
  322. return parentController.destroy();
  323. } )
  324. .then( () => {
  325. expect( controllerCollectionCollection ).to.have.lengthOf( 0, 'parentController.collections is empty' );
  326. expect( controllerCollection ).to.have.lengthOf( 0, 'child controller collection is empty' );
  327. expect( childControllers.map( controller => controller.destroy.calledOnce ) )
  328. .to.deep.equal( [ true, true, true ], 'all child controllers were destroyed' );
  329. } );
  330. } );
  331. } );
  332. describe( 'addCollection', () => {
  333. it( 'should add a new collection', () => {
  334. const controller = new Controller();
  335. controller.addCollection( 'foo' );
  336. expect( controller.collections ).to.have.length( 1 );
  337. expect( controller.collections.get( 'foo' ).name ).to.equal( 'foo' );
  338. } );
  339. it( 'should return the collection which has been created (chaining)', () => {
  340. const controller = new Controller();
  341. const returned = controller.addCollection( 'foo' );
  342. expect( returned ).to.be.instanceOf( ControllerCollection );
  343. } );
  344. it( 'should pass locale to controller collection', () => {
  345. const locale = {};
  346. const view = new View( locale );
  347. expect( new Controller( {}, view ).addCollection( '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. }