controllercollection.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554
  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 ControllerCollection from '/ckeditor5/ui/controllercollection.js';
  9. import Controller from '/ckeditor5/ui/controller.js';
  10. import Collection from '/ckeditor5/utils/collection.js';
  11. import Model from '/ckeditor5/ui/model.js';
  12. import View from '/ckeditor5/ui/view.js';
  13. import Template from '/ckeditor5/ui/template.js';
  14. testUtils.createSinonSandbox();
  15. let ParentView, ItemController, ItemView;
  16. let models;
  17. describe( 'ControllerCollection', () => {
  18. beforeEach( () => {
  19. defineParentViewClass();
  20. defineItemControllerClass();
  21. defineItemViewClass();
  22. createModelCollection();
  23. } );
  24. describe( 'constructor', () => {
  25. it( 'should throw when no name is passed', () => {
  26. expect( () => {
  27. new ControllerCollection();
  28. } ).to.throw( /^ui-controllercollection-no-name/ );
  29. } );
  30. it( 'accepts locale', () => {
  31. const locale = {};
  32. const collection = new ControllerCollection( 'foo', locale );
  33. expect( collection.locale ).to.equal( locale );
  34. } );
  35. } );
  36. describe( 'add', () => {
  37. it( 'should add a child controller and return promise', () => {
  38. const parentController = new Controller();
  39. const childController = new Controller();
  40. const collection = parentController.addCollection( 'x' );
  41. const returned = collection.add( childController );
  42. expect( returned ).to.be.an.instanceof( Promise );
  43. expect( collection.get( 0 ) ).to.be.equal( childController );
  44. } );
  45. it( 'should add a child controller at given position', () => {
  46. const parentController = new Controller();
  47. const childController1 = new Controller();
  48. const childController2 = new Controller();
  49. const collection = parentController.addCollection( 'x' );
  50. collection.add( childController1 );
  51. collection.add( childController2, 0 );
  52. expect( collection.get( 0 ) ).to.be.equal( childController2 );
  53. expect( collection.get( 1 ) ).to.be.equal( childController1 );
  54. } );
  55. it( 'should initialize child controller if parent is ready', () => {
  56. const parentController = new Controller( null, new ParentView() );
  57. const childController = new Controller( null, new View() );
  58. const spy = testUtils.sinon.spy( childController, 'init' );
  59. const collection = parentController.addCollection( 'x' );
  60. collection.add( childController );
  61. collection.remove( childController );
  62. sinon.assert.notCalled( spy );
  63. return parentController.init()
  64. .then( () => {
  65. return collection.add( childController );
  66. } )
  67. .then( () => {
  68. sinon.assert.calledOnce( spy );
  69. } );
  70. } );
  71. it( 'should not initialize child controller twice', () => {
  72. const parentController = new Controller( null, new ParentView() );
  73. const childController = new Controller( null, new View() );
  74. const spy = testUtils.sinon.spy( childController, 'init' );
  75. const collection = parentController.addCollection( 'x' );
  76. return parentController.init()
  77. .then( () => {
  78. return childController.init();
  79. } )
  80. .then( () => {
  81. return collection.add( childController );
  82. } )
  83. .then( () => {
  84. sinon.assert.calledOnce( spy );
  85. } );
  86. } );
  87. } );
  88. describe( 'bind', () => {
  89. it( 'returns object', () => {
  90. expect( new ControllerCollection( 'foo' ).bind( {} ) ).to.be.an( 'object' );
  91. } );
  92. it( 'provides "as" interface', () => {
  93. const bind = new ControllerCollection( 'foo' ).bind( {} );
  94. expect( bind ).to.have.keys( 'as' );
  95. expect( bind.as ).to.be.a( 'function' );
  96. } );
  97. describe( 'as', () => {
  98. it( 'does not chain', () => {
  99. const controllers = new ControllerCollection( 'synced' );
  100. const returned = controllers.bind( models ).as( ItemController, ItemView );
  101. expect( returned ).to.be.undefined;
  102. } );
  103. describe( 'standard factory', () => {
  104. it( 'expands the initial collection of the models', () => {
  105. const controllers = new ControllerCollection( 'synced' );
  106. controllers.bind( models ).as( ItemController, ItemView );
  107. expect( controllers ).to.have.length( 5 );
  108. expect( controllers.get( 0 ).model.uid ).to.equal( '0' );
  109. expect( controllers.get( 4 ).model.uid ).to.equal( '4' );
  110. } );
  111. it( 'uses the controller and view classes to expand the collection', () => {
  112. const controllers = new ControllerCollection( 'synced' );
  113. controllers.bind( models ).as( ItemController, ItemView );
  114. expect( controllers.get( 0 ) ).to.be.instanceOf( ItemController );
  115. expect( controllers.get( 0 ).view ).to.be.instanceOf( ItemView );
  116. } );
  117. it( 'supports adding new models to the collection', () => {
  118. const controllers = new ControllerCollection( 'synced' );
  119. controllers.bind( models ).as( ItemController, ItemView );
  120. models.add( new Model( { uid: '6' } ) );
  121. models.add( new Model( { uid: '5' } ), 5 );
  122. expect( controllers.get( 5 ).model.uid ).to.equal( '5' );
  123. expect( controllers.get( 6 ).model.uid ).to.equal( '6' );
  124. expect( controllers ).to.have.length( 7 );
  125. } );
  126. it( 'supports removing models from the collection', () => {
  127. const controllers = new ControllerCollection( 'synced' );
  128. controllers.bind( models ).as( ItemController, ItemView );
  129. models.remove( 2 );
  130. models.remove( 3 );
  131. expect( controllers.map( c => c.model.uid ) ).to.have.members( [ '0', '1', '3' ] );
  132. } );
  133. it( 'passes controller collection\'s locale to the views', () => {
  134. const locale = {};
  135. const controllers = new ControllerCollection( 'synced', locale );
  136. controllers.bind( models ).as( ItemController, ItemView );
  137. expect( controllers.get( 0 ).view.locale ).to.equal( locale );
  138. } );
  139. } );
  140. describe( 'custom factory', () => {
  141. it( 'expands the initial collection of the models', () => {
  142. const controllers = new ControllerCollection( 'synced' );
  143. controllers.bind( models ).as( ( model, locale ) => {
  144. return new ItemController( model, new ItemView( locale ) );
  145. } );
  146. expect( controllers ).to.have.length( 5 );
  147. expect( controllers.get( 0 ).model.uid ).to.equal( '0' );
  148. expect( controllers.get( 4 ).model.uid ).to.equal( '4' );
  149. } );
  150. it( 'uses the controller and view classes to expand the collection', () => {
  151. const controllers = new ControllerCollection( 'synced' );
  152. controllers.bind( models ).as( ( model, locale ) => {
  153. return new ItemController( model, new ItemView( locale ) );
  154. } );
  155. expect( controllers.get( 0 ) ).to.be.instanceOf( ItemController );
  156. expect( controllers.get( 0 ).view ).to.be.instanceOf( ItemView );
  157. } );
  158. it( 'supports adding new models to the collection', () => {
  159. const controllers = new ControllerCollection( 'synced' );
  160. controllers.bind( models ).as( ( model, locale ) => {
  161. return new ItemController( model, new ItemView( locale ) );
  162. } );
  163. models.add( new Model( { uid: '6' } ) );
  164. models.add( new Model( { uid: '5' } ), 5 );
  165. expect( controllers.get( 5 ).model.uid ).to.equal( '5' );
  166. expect( controllers.get( 6 ).model.uid ).to.equal( '6' );
  167. expect( controllers ).to.have.length( 7 );
  168. } );
  169. it( 'supports removing models from the collection', () => {
  170. const controllers = new ControllerCollection( 'synced' );
  171. controllers.bind( models ).as( ( model, locale ) => {
  172. return new ItemController( model, new ItemView( locale ) );
  173. } );
  174. models.remove( 2 );
  175. models.remove( 3 );
  176. expect( controllers.map( c => c.model.uid ) ).to.have.members( [ '0', '1', '3' ] );
  177. } );
  178. it( 'passes controller collection\'s locale to the views', () => {
  179. const locale = {};
  180. const controllers = new ControllerCollection( 'synced', locale );
  181. controllers.bind( models ).as( ( model, locale ) => {
  182. return new ItemController( model, new ItemView( locale ) );
  183. } );
  184. expect( controllers.get( 0 ).view.locale ).to.equal( locale );
  185. } );
  186. } );
  187. describe( 'custom data format with custom factory', () => {
  188. it( 'expands the initial collection of the models', () => {
  189. const controllers = new ControllerCollection( 'synced' );
  190. const data = new Collection();
  191. data.add( { foo: 'a' } );
  192. data.add( { foo: 'b' } );
  193. controllers.bind( data ).as( ( item, locale ) => {
  194. const model = new Model( {
  195. custom: item.foo
  196. } );
  197. return new ItemController( model, new ItemView( locale ) );
  198. } );
  199. expect( controllers ).to.have.length( 2 );
  200. expect( controllers.get( 0 ).model.custom ).to.equal( 'a' );
  201. expect( controllers.get( 1 ).model.custom ).to.equal( 'b' );
  202. expect( controllers.get( 0 ) ).to.be.instanceOf( ItemController );
  203. expect( controllers.get( 0 ).view ).to.be.instanceOf( ItemView );
  204. } );
  205. } );
  206. } );
  207. } );
  208. describe( 'delegate', () => {
  209. it( 'should throw when event names are not strings', () => {
  210. const collection = new ControllerCollection( 'foo' );
  211. expect( () => {
  212. collection.delegate();
  213. } ).to.throwCKEditorError( /ui-controllercollection-delegate-wrong-events/ );
  214. expect( () => {
  215. collection.delegate( new Date() );
  216. } ).to.throwCKEditorError( /ui-controllercollection-delegate-wrong-events/ );
  217. expect( () => {
  218. collection.delegate( 'color', new Date() );
  219. } ).to.throwCKEditorError( /ui-controllercollection-delegate-wrong-events/ );
  220. } );
  221. it( 'returns object', () => {
  222. expect( new ControllerCollection( 'foo' ).delegate( 'foo' ) ).to.be.an( 'object' );
  223. } );
  224. it( 'provides "to" interface', () => {
  225. const delegate = new ControllerCollection( 'foo' ).delegate( 'foo' );
  226. expect( delegate ).to.have.keys( 'to' );
  227. expect( delegate.to ).to.be.a( 'function' );
  228. } );
  229. describe( 'to', () => {
  230. it( 'does not chain', () => {
  231. const collection = new ControllerCollection( 'foo' );
  232. const returned = collection.delegate( 'foo' ).to( {} );
  233. expect( returned ).to.be.undefined;
  234. } );
  235. it( 'forwards an event to another observable – existing controller', ( done ) => {
  236. const target = new Model();
  237. const collection = new ControllerCollection( 'foo' );
  238. const model = new Model();
  239. collection.add( new Controller( model ) );
  240. collection.delegate( 'foo' ).to( target );
  241. target.on( 'foo', ( ...args ) => {
  242. assertDelegated( args, {
  243. expectedName: 'foo',
  244. expectedSource: model,
  245. expectedPath: [ model, target ],
  246. expectedData: []
  247. } );
  248. done();
  249. } );
  250. model.fire( 'foo' );
  251. } );
  252. it( 'forwards an event to another observable – new controller', ( done ) => {
  253. const target = new Model();
  254. const collection = new ControllerCollection( 'foo' );
  255. const model = new Model();
  256. collection.delegate( 'foo' ).to( target );
  257. collection.add( new Controller( model ) );
  258. target.on( 'foo', ( ...args ) => {
  259. assertDelegated( args, {
  260. expectedName: 'foo',
  261. expectedSource: model,
  262. expectedPath: [ model, target ],
  263. expectedData: []
  264. } );
  265. done();
  266. } );
  267. model.fire( 'foo' );
  268. } );
  269. it( 'forwards multiple events to another observable', () => {
  270. const target = new Model();
  271. const collection = new ControllerCollection( 'foo' );
  272. const modelA = new Model();
  273. const modelB = new Model();
  274. const modelC = new Model();
  275. const spyFoo = sinon.spy();
  276. const spyBar = sinon.spy();
  277. const spyBaz = sinon.spy();
  278. collection.delegate( 'foo', 'bar', 'baz' ).to( target );
  279. collection.add( new Controller( modelA ) );
  280. collection.add( new Controller( modelB ) );
  281. collection.add( new Controller( modelC ) );
  282. target.on( 'foo', spyFoo );
  283. target.on( 'bar', spyBar );
  284. target.on( 'baz', spyBaz );
  285. modelA.fire( 'foo' );
  286. sinon.assert.calledOnce( spyFoo );
  287. sinon.assert.notCalled( spyBar );
  288. sinon.assert.notCalled( spyBaz );
  289. assertDelegated( spyFoo.args[ 0 ], {
  290. expectedName: 'foo',
  291. expectedSource: modelA,
  292. expectedPath: [ modelA, target ],
  293. expectedData: []
  294. } );
  295. modelB.fire( 'bar' );
  296. sinon.assert.calledOnce( spyFoo );
  297. sinon.assert.calledOnce( spyBar );
  298. sinon.assert.notCalled( spyBaz );
  299. assertDelegated( spyBar.args[ 0 ], {
  300. expectedName: 'bar',
  301. expectedSource: modelB,
  302. expectedPath: [ modelB, target ],
  303. expectedData: []
  304. } );
  305. modelC.fire( 'baz' );
  306. sinon.assert.calledOnce( spyFoo );
  307. sinon.assert.calledOnce( spyBar );
  308. sinon.assert.calledOnce( spyBaz );
  309. assertDelegated( spyBaz.args[ 0 ], {
  310. expectedName: 'baz',
  311. expectedSource: modelC,
  312. expectedPath: [ modelC, target ],
  313. expectedData: []
  314. } );
  315. modelC.fire( 'not-delegated' );
  316. sinon.assert.calledOnce( spyFoo );
  317. sinon.assert.calledOnce( spyBar );
  318. sinon.assert.calledOnce( spyBaz );
  319. } );
  320. it( 'does not forward events which are not supposed to be delegated', () => {
  321. const target = new Model();
  322. const collection = new ControllerCollection( 'foo' );
  323. const model = new Model();
  324. const spyFoo = sinon.spy();
  325. const spyBar = sinon.spy();
  326. const spyBaz = sinon.spy();
  327. collection.delegate( 'foo', 'bar', 'baz' ).to( target );
  328. collection.add( new Controller( model ) );
  329. target.on( 'foo', spyFoo );
  330. target.on( 'bar', spyBar );
  331. target.on( 'baz', spyBaz );
  332. model.fire( 'foo' );
  333. model.fire( 'bar' );
  334. model.fire( 'baz' );
  335. model.fire( 'not-delegated' );
  336. sinon.assert.callOrder( spyFoo, spyBar, spyBaz );
  337. sinon.assert.callCount( spyFoo, 1 );
  338. sinon.assert.callCount( spyBar, 1 );
  339. sinon.assert.callCount( spyBaz, 1 );
  340. } );
  341. it( 'stops forwarding when controller removed from the collection', () => {
  342. const target = new Model();
  343. const collection = new ControllerCollection( 'foo' );
  344. const model = new Model();
  345. const spy = sinon.spy();
  346. collection.delegate( 'foo' ).to( target );
  347. target.on( 'foo', spy );
  348. collection.add( new Controller( model ) );
  349. model.fire( 'foo' );
  350. sinon.assert.callCount( spy, 1 );
  351. collection.remove( 0 );
  352. model.fire( 'foo' );
  353. sinon.assert.callCount( spy, 1 );
  354. } );
  355. it( 'supports deep event delegation', ( done ) => {
  356. const collection = new ControllerCollection( 'foo' );
  357. const target = new Model();
  358. const modelA = new Model();
  359. const modelAA = new Model();
  360. const data = {};
  361. const controllerA = new Controller( modelA );
  362. const controllerAA = new Controller( modelAA );
  363. const barCollection = controllerA.addCollection( 'bar' );
  364. collection.add( controllerA );
  365. collection.delegate( 'foo' ).to( target );
  366. barCollection.add( controllerAA );
  367. barCollection.delegate( 'foo' ).to( modelA );
  368. target.on( 'foo', ( ...args ) => {
  369. assertDelegated( args, {
  370. expectedName: 'foo',
  371. expectedSource: modelAA,
  372. expectedPath: [ modelAA, modelA, target ],
  373. expectedData: [ data ]
  374. } );
  375. done();
  376. } );
  377. modelAA.fire( 'foo', data );
  378. } );
  379. } );
  380. } );
  381. } );
  382. function defineParentViewClass() {
  383. ParentView = class extends View {
  384. constructor() {
  385. super();
  386. this.element = document.createElement( 'span' );
  387. this.register( 'x', true );
  388. }
  389. };
  390. }
  391. function defineItemControllerClass() {
  392. ItemController = class extends Controller {
  393. constructor( model, view ) {
  394. super( model, view );
  395. view.model.bind( 'uid' ).to( model );
  396. }
  397. };
  398. }
  399. function defineItemViewClass() {
  400. ItemView = class extends View {
  401. constructor( locale ) {
  402. super( locale );
  403. const bind = this.bind;
  404. this.template = new Template( {
  405. tag: 'li',
  406. attributes: {
  407. id: bind.to( 'uid' )
  408. }
  409. } );
  410. }
  411. };
  412. }
  413. function createModelCollection() {
  414. models = new Collection( { idProperty: 'uid' } );
  415. for ( let i = 0; i < 5; i++ ) {
  416. models.add( new Model( {
  417. uid: Number( i ).toString()
  418. } ) );
  419. }
  420. }
  421. function assertDelegated( evtArgs, { expectedName, expectedSource, expectedPath, expectedData } ) {
  422. const evtInfo = evtArgs[ 0 ];
  423. expect( evtInfo.name ).to.equal( expectedName );
  424. expect( evtInfo.source ).to.equal( expectedSource );
  425. expect( evtInfo.path ).to.deep.equal( expectedPath );
  426. expect( evtArgs.slice( 1 ) ).to.deep.equal( expectedData );
  427. }