8
0

controller.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  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. 'use strict';
  7. import testUtils from '/tests/_utils/utils.js';
  8. import View from '/ckeditor5/core/ui/view.js';
  9. import Controller from '/ckeditor5/core/ui/controller.js';
  10. import ControllerCollection from '/ckeditor5/core/ui/controllercollection.js';
  11. import CKEditorError from '/ckeditor5/core/ckeditorerror.js';
  12. import Model from '/ckeditor5/core/ui/model.js';
  13. import EventInfo from '/ckeditor5/core/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', () => {
  47. const controller = new Controller();
  48. return controller.init().then( () => {
  49. expect( controller.ready ).to.be.true;
  50. } );
  51. } );
  52. it( 'should initialize own view', () => {
  53. const view = new View();
  54. const controller = new Controller( null, view );
  55. const spy = testUtils.sinon.spy( view, 'init' );
  56. return controller.init().then( () => {
  57. sinon.assert.calledOnce( spy );
  58. } );
  59. } );
  60. it( 'should initialize child controllers in own collections', () => {
  61. const parentController = new Controller();
  62. const buttonCollection = new ControllerCollection( 'buttons' );
  63. parentController.collections.add( buttonCollection );
  64. const childController1 = new Controller();
  65. const childController2 = new Controller();
  66. const spy1 = testUtils.sinon.spy( childController1, 'init' );
  67. const spy2 = testUtils.sinon.spy( childController2, 'init' );
  68. buttonCollection.add( childController1 );
  69. buttonCollection.add( childController2 );
  70. return parentController.init().then( () => {
  71. expect( buttonCollection.get( 0 ) ).to.be.equal( childController1 );
  72. expect( buttonCollection.get( 1 ) ).to.be.equal( childController2 );
  73. sinon.assert.calledOnce( spy1 );
  74. sinon.assert.calledOnce( spy2 );
  75. } );
  76. } );
  77. } );
  78. describe( 'add', () => {
  79. beforeEach( defineParentControllerClass );
  80. it( 'should add a controller to specific collection', () => {
  81. const parentController = new ParentController();
  82. const child1 = new Controller();
  83. const child2 = new Controller();
  84. const collection = parentController.collections.get( 'x' );
  85. parentController.add( 'x', child1 );
  86. parentController.add( 'x', child2 );
  87. expect( collection ).to.have.length( 2 );
  88. expect( collection.get( 0 ) ).to.be.equal( child1 );
  89. expect( collection.get( 1 ) ).to.be.equal( child2 );
  90. } );
  91. it( 'should add a controller at specific index', () => {
  92. const parentController = new ParentController();
  93. const child1 = new Controller();
  94. const child2 = new Controller();
  95. const collection = parentController.collections.get( 'x' );
  96. parentController.add( 'x', child1 );
  97. parentController.add( 'x', child2, 0 );
  98. expect( collection ).to.have.length( 2 );
  99. expect( collection.get( 0 ) ).to.be.equal( child2 );
  100. expect( collection.get( 1 ) ).to.be.equal( child1 );
  101. } );
  102. } );
  103. describe( 'remove', () => {
  104. beforeEach( defineParentControllerClass );
  105. it( 'should remove a controller from specific collection – by instance', () => {
  106. const parentController = new ParentController();
  107. const child1 = new Controller();
  108. const child2 = new Controller();
  109. const child3 = new Controller();
  110. const collection = parentController.collections.get( 'x' );
  111. parentController.add( 'x', child1 );
  112. parentController.add( 'x', child2 );
  113. parentController.add( 'x', child3 );
  114. const removed = parentController.remove( 'x', child2 );
  115. expect( collection ).to.have.length( 2 );
  116. expect( collection.get( 0 ) ).to.be.equal( child1 );
  117. expect( collection.get( 1 ) ).to.be.equal( child3 );
  118. expect( removed ).to.be.equal( child2 );
  119. } );
  120. it( 'should remove a controller from specific collection – by index', () => {
  121. const parentController = new ParentController();
  122. const child1 = new Controller();
  123. const child2 = new Controller();
  124. const child3 = new Controller();
  125. const collection = parentController.collections.get( 'x' );
  126. parentController.add( 'x', child1 );
  127. parentController.add( 'x', child2 );
  128. parentController.add( 'x', child3 );
  129. const removed = parentController.remove( 'x', 1 );
  130. expect( collection ).to.have.length( 2 );
  131. expect( collection.get( 0 ) ).to.be.equal( child1 );
  132. expect( collection.get( 1 ) ).to.be.equal( child3 );
  133. expect( removed ).to.be.equal( child2 );
  134. } );
  135. } );
  136. describe( 'collections', () => {
  137. describe( 'add', () => {
  138. beforeEach( defineParentViewClass );
  139. beforeEach( defineParentControllerClass );
  140. it( 'should add a child controller which has no view', () => {
  141. const parentController = new ParentController( null, new ParentView() );
  142. const collection = parentController.collections.get( 'x' );
  143. const childController = new Controller();
  144. return parentController.init()
  145. .then( () => {
  146. return collection.add( childController );
  147. } )
  148. .then( () => {
  149. expect( collection.get( 0 ) ).to.be.equal( childController );
  150. } );
  151. } );
  152. it( 'should append child controller\'s view to parent controller\'s view', () => {
  153. const parentView = new ParentView();
  154. const parentController = new ParentController( null, parentView );
  155. const collection = parentController.collections.get( 'x' );
  156. const childController = new Controller( null, new View() );
  157. const spy = testUtils.sinon.spy();
  158. parentView.regions.get( 'x' ).views.on( 'add', spy );
  159. collection.add( childController );
  160. sinon.assert.notCalled( spy );
  161. collection.remove( childController );
  162. return parentController.init()
  163. .then( () => {
  164. return collection.add( childController );
  165. } )
  166. .then( () => {
  167. sinon.assert.calledOnce( spy );
  168. sinon.assert.calledWithExactly( spy,
  169. sinon.match.instanceOf( EventInfo ), childController.view, 0 );
  170. } );
  171. } );
  172. it( 'should append child controller\'s view to parent controller\'s view at given index', () => {
  173. const parentController = new ParentController( null, new ParentView() );
  174. const collection = parentController.collections.get( 'x' );
  175. const childView1 = new View();
  176. const childController1 = new Controller( null, childView1 );
  177. const childView2 = new View();
  178. const childController2 = new Controller( null, childView2 );
  179. return parentController.init()
  180. .then( () => {
  181. return collection.add( childController1 ).then( () => {
  182. return collection.add( childController2, 0 );
  183. } );
  184. } )
  185. .then( () => {
  186. const region = parentController.view.regions.get( 'x' );
  187. expect( region.views.get( 0 ) ).to.be.equal( childView2 );
  188. expect( region.views.get( 1 ) ).to.be.equal( childView1 );
  189. } );
  190. } );
  191. } );
  192. describe( 'remove', () => {
  193. beforeEach( defineParentViewClass );
  194. it( 'should remove child controller\'s view from parent controller\'s view', () => {
  195. const parentView = new ParentView();
  196. const parentController = new ParentController( null, parentView );
  197. const collection = parentController.collections.get( 'x' );
  198. const childController = new Controller( null, new View() );
  199. const spy = testUtils.sinon.spy();
  200. parentView.regions.get( 'x' ).views.on( 'remove', spy );
  201. collection.add( childController );
  202. sinon.assert.notCalled( spy );
  203. return parentController.init()
  204. .then( () => {
  205. collection.remove( childController );
  206. sinon.assert.calledOnce( spy );
  207. sinon.assert.calledWithExactly( spy,
  208. sinon.match.instanceOf( EventInfo ), childController.view );
  209. } );
  210. } );
  211. } );
  212. } );
  213. describe( 'destroy', () => {
  214. beforeEach( defineParentViewClass );
  215. beforeEach( defineParentControllerClass );
  216. it( 'should destroy the controller', () => {
  217. const view = new View();
  218. const controller = new Controller( null, view );
  219. const spy = testUtils.sinon.spy( view, 'destroy' );
  220. return controller.init()
  221. .then( () => {
  222. return controller.destroy();
  223. } )
  224. .then( () => {
  225. sinon.assert.calledOnce( spy );
  226. expect( controller.model ).to.be.null;
  227. expect( controller.ready ).to.be.null;
  228. expect( controller.view ).to.be.null;
  229. expect( controller.collections ).to.be.null;
  230. } );
  231. } );
  232. it( 'should destroy the controller which has no view', () => {
  233. const controller = new Controller( null, null );
  234. return controller.init()
  235. .then( () => {
  236. return controller.destroy();
  237. } )
  238. .then( () => {
  239. expect( controller.model ).to.be.null;
  240. expect( controller.view ).to.be.null;
  241. expect( controller.collections ).to.be.null;
  242. } );
  243. } );
  244. it( 'should destroy child controllers in collections with their views', () => {
  245. const parentController = new ParentController( null, new ParentView() );
  246. const collection = parentController.collections.get( 'x' );
  247. const childView = new View();
  248. const childController = new Controller( null, childView );
  249. const spy = testUtils.sinon.spy( childView, 'destroy' );
  250. collection.add( childController );
  251. return parentController.init()
  252. .then( () => {
  253. return parentController.destroy();
  254. } )
  255. .then( () => {
  256. sinon.assert.calledOnce( spy );
  257. expect( childController.model ).to.be.null;
  258. expect( childController.view ).to.be.null;
  259. expect( childController.collections ).to.be.null;
  260. } );
  261. } );
  262. it( 'should destroy child controllers in collections when they have no views', () => {
  263. const parentController = new ParentController( null, new ParentView() );
  264. const collection = parentController.collections.get( 'x' );
  265. const childController = new Controller( null, null );
  266. collection.add( childController );
  267. return parentController.init()
  268. .then( () => {
  269. return parentController.destroy();
  270. } )
  271. .then( () => {
  272. expect( childController.model ).to.be.null;
  273. expect( childController.view ).to.be.null;
  274. expect( childController.collections ).to.be.null;
  275. } );
  276. } );
  277. } );
  278. } );
  279. function defineParentViewClass() {
  280. ParentView = class extends View {
  281. constructor() {
  282. super();
  283. this.element = document.createElement( 'span' );
  284. this.register( 'x', true );
  285. }
  286. };
  287. }
  288. function defineParentControllerClass() {
  289. ParentController = class extends Controller {
  290. constructor( ...args ) {
  291. super( ...args );
  292. this.collections.add( new ControllerCollection( 'x' ) );
  293. }
  294. };
  295. }