controllercollection.js 16 KB

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