viewcollection.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  1. /**
  2. * @license Copyright (c) 2003-2020, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
  4. */
  5. /* global document */
  6. import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
  7. import View from '../src/view';
  8. import ViewCollection from '../src/viewcollection';
  9. import normalizeHtml from '@ckeditor/ckeditor5-utils/tests/_utils/normalizehtml';
  10. import { expectToThrowCKEditorError } from '@ckeditor/ckeditor5-utils/tests/_utils/utils';
  11. let collection;
  12. describe( 'ViewCollection', () => {
  13. testUtils.createSinonSandbox();
  14. beforeEach( createTestCollection );
  15. describe( 'constructor()', () => {
  16. it( 'sets basic properties and attributes', () => {
  17. expect( collection._parentElement ).to.be.null;
  18. expect( collection._idProperty ).to.equal( 'viewUid' );
  19. } );
  20. it( 'allows setting initial collection items', () => {
  21. const view1 = new View();
  22. const view2 = new View();
  23. const collection = new ViewCollection( [ view1, view2 ] );
  24. expect( collection ).to.have.length( 2 );
  25. expect( collection.get( 0 ) ).to.equal( view1 );
  26. expect( collection.get( 1 ) ).to.equal( view2 );
  27. } );
  28. describe( 'child view management in DOM', () => {
  29. it( 'manages view#element of the children in DOM', () => {
  30. const parentElement = document.createElement( 'p' );
  31. collection.setParent( parentElement );
  32. const viewA = new View();
  33. expect( () => {
  34. collection.add( viewA );
  35. collection.remove( viewA );
  36. } ).to.not.throw();
  37. expect( () => {
  38. collection.add( viewA );
  39. collection.remove( viewA );
  40. } ).to.not.throw();
  41. viewA.element = document.createElement( 'b' );
  42. collection.add( viewA );
  43. expect( normalizeHtml( parentElement.outerHTML ) ).to.equal( '<p><b></b></p>' );
  44. const viewB = new View();
  45. viewB.element = document.createElement( 'i' );
  46. collection.add( viewB, 0 );
  47. expect( normalizeHtml( parentElement.outerHTML ) ).to.equal( '<p><i></i><b></b></p>' );
  48. collection.remove( viewA );
  49. expect( normalizeHtml( parentElement.outerHTML ) ).to.equal( '<p><i></i></p>' );
  50. collection.remove( viewB );
  51. expect( normalizeHtml( parentElement.outerHTML ) ).to.equal( '<p></p>' );
  52. } );
  53. // #145
  54. it( 'always keeps the collection synchronized with DOM', () => {
  55. const view = new View();
  56. const viewA = getView( 'A' );
  57. const viewB = getView( 'B' );
  58. function getView( text ) {
  59. const view = new View();
  60. view.setTemplate( {
  61. tag: 'li',
  62. children: [
  63. {
  64. text
  65. }
  66. ]
  67. } );
  68. return view;
  69. }
  70. // Fill the collection with children.
  71. collection.add( viewA );
  72. collection.add( viewB );
  73. // Put the collection in the template.
  74. view.setTemplate( {
  75. tag: 'ul',
  76. children: collection
  77. } );
  78. const viewC = getView( 'C' );
  79. // Modify the collection, while the view#element has already
  80. // been rendered but the view has **not** been inited yet.
  81. collection.add( viewC );
  82. collection.remove( 1 );
  83. view.render();
  84. expect( view.element.childNodes ).to.have.length( 2 );
  85. expect( view.element.childNodes[ 0 ] ).to.equal( viewA.element );
  86. expect( view.element.childNodes[ 1 ] ).to.equal( viewC.element );
  87. } );
  88. } );
  89. } );
  90. describe( 'destroy()', () => {
  91. it( 'calls #destroy on all views in the collection', () => {
  92. const viewA = new View();
  93. const viewB = new View();
  94. const spyA = testUtils.sinon.spy( viewA, 'destroy' );
  95. const spyB = testUtils.sinon.spy( viewB, 'destroy' );
  96. collection.add( viewA );
  97. collection.add( viewB );
  98. collection.destroy();
  99. sinon.assert.calledOnce( spyA );
  100. sinon.assert.calledOnce( spyB );
  101. sinon.assert.callOrder( spyA, spyB );
  102. } );
  103. } );
  104. describe( 'add()', () => {
  105. it( 'renders the new view in the collection', () => {
  106. const view = new View();
  107. const spy = testUtils.sinon.spy( view, 'render' );
  108. expect( view.isRendered ).to.be.false;
  109. collection.add( view );
  110. expect( view.isRendered ).to.be.true;
  111. sinon.assert.calledOnce( spy );
  112. } );
  113. it( 'works for a view with a Number view#id attribute', () => {
  114. const view = new View();
  115. view.set( 'id', 1 );
  116. collection.add( view );
  117. expect( view.id ).to.equal( 1 );
  118. expect( view.viewUid ).to.be.a( 'string' );
  119. } );
  120. } );
  121. describe( 'setParent()', () => {
  122. it( 'sets #_parentElement', () => {
  123. const el = {};
  124. expect( collection._parentElement ).to.be.null;
  125. collection.setParent( el );
  126. expect( collection._parentElement ).to.equal( el );
  127. } );
  128. it( 'udpates initial collection items in DOM', () => {
  129. const view1 = new View();
  130. view1.element = document.createElement( 'i' );
  131. sinon.spy( view1, 'render' );
  132. const view2 = new View();
  133. view2.element = document.createElement( 'b' );
  134. sinon.spy( view2, 'render' );
  135. const collection = new ViewCollection( [ view1, view2 ] );
  136. const parentElement = document.createElement( 'div' );
  137. expect( collection ).to.have.length( 2 );
  138. expect( collection.get( 0 ) ).to.equal( view1 );
  139. expect( collection.get( 1 ) ).to.equal( view2 );
  140. collection.setParent( parentElement );
  141. expect( normalizeHtml( parentElement.outerHTML ) ).to.equal( '<div><i></i><b></b></div>' );
  142. sinon.assert.calledOnce( view1.render );
  143. sinon.assert.calledOnce( view2.render );
  144. } );
  145. } );
  146. describe( 'delegate()', () => {
  147. it( 'should throw when event names are not strings', () => {
  148. expectToThrowCKEditorError( () => {
  149. collection.delegate();
  150. }, /ui-viewcollection-delegate-wrong-events/ );
  151. expectToThrowCKEditorError( () => {
  152. collection.delegate( new Date() );
  153. }, /ui-viewcollection-delegate-wrong-events/ );
  154. expectToThrowCKEditorError( () => {
  155. collection.delegate( 'color', new Date() );
  156. }, /ui-viewcollection-delegate-wrong-events/ );
  157. } );
  158. it( 'returns object', () => {
  159. expect( collection.delegate( 'foo' ) ).to.be.an( 'object' );
  160. } );
  161. it( 'provides "to()" interface', () => {
  162. const delegate = collection.delegate( 'foo' );
  163. expect( delegate ).to.have.keys( 'to' );
  164. expect( delegate.to ).to.be.a( 'function' );
  165. } );
  166. describe( 'to()', () => {
  167. it( 'does not chain', () => {
  168. const returned = collection.delegate( 'foo' ).to( {} );
  169. expect( returned ).to.be.undefined;
  170. } );
  171. it( 'forwards an event to another observable – existing view', done => {
  172. const target = new View();
  173. const view = new View();
  174. collection.add( view );
  175. collection.delegate( 'foo' ).to( target );
  176. target.on( 'foo', ( ...args ) => {
  177. assertDelegated( args, {
  178. expectedName: 'foo',
  179. expectedSource: view,
  180. expectedPath: [ view, target ],
  181. expectedData: []
  182. } );
  183. done();
  184. } );
  185. view.fire( 'foo' );
  186. } );
  187. it( 'forwards an event to another observable – new view', done => {
  188. const target = new View();
  189. const view = new View();
  190. collection.delegate( 'foo' ).to( target );
  191. collection.add( view );
  192. target.on( 'foo', ( ...args ) => {
  193. assertDelegated( args, {
  194. expectedName: 'foo',
  195. expectedSource: view,
  196. expectedPath: [ view, target ],
  197. expectedData: []
  198. } );
  199. done();
  200. } );
  201. view.fire( 'foo' );
  202. } );
  203. it( 'forwards multiple events to another observable', () => {
  204. const target = new View();
  205. const viewA = new View();
  206. const viewB = new View();
  207. const viewC = new View();
  208. const spyFoo = sinon.spy();
  209. const spyBar = sinon.spy();
  210. const spyBaz = sinon.spy();
  211. collection.delegate( 'foo', 'bar', 'baz' ).to( target );
  212. collection.add( viewA );
  213. collection.add( viewB );
  214. collection.add( viewC );
  215. target.on( 'foo', spyFoo );
  216. target.on( 'bar', spyBar );
  217. target.on( 'baz', spyBaz );
  218. viewA.fire( 'foo' );
  219. sinon.assert.calledOnce( spyFoo );
  220. sinon.assert.notCalled( spyBar );
  221. sinon.assert.notCalled( spyBaz );
  222. assertDelegated( spyFoo.args[ 0 ], {
  223. expectedName: 'foo',
  224. expectedSource: viewA,
  225. expectedPath: [ viewA, target ],
  226. expectedData: []
  227. } );
  228. viewB.fire( 'bar' );
  229. sinon.assert.calledOnce( spyFoo );
  230. sinon.assert.calledOnce( spyBar );
  231. sinon.assert.notCalled( spyBaz );
  232. assertDelegated( spyBar.args[ 0 ], {
  233. expectedName: 'bar',
  234. expectedSource: viewB,
  235. expectedPath: [ viewB, target ],
  236. expectedData: []
  237. } );
  238. viewC.fire( 'baz' );
  239. sinon.assert.calledOnce( spyFoo );
  240. sinon.assert.calledOnce( spyBar );
  241. sinon.assert.calledOnce( spyBaz );
  242. assertDelegated( spyBaz.args[ 0 ], {
  243. expectedName: 'baz',
  244. expectedSource: viewC,
  245. expectedPath: [ viewC, target ],
  246. expectedData: []
  247. } );
  248. viewC.fire( 'not-delegated' );
  249. sinon.assert.calledOnce( spyFoo );
  250. sinon.assert.calledOnce( spyBar );
  251. sinon.assert.calledOnce( spyBaz );
  252. } );
  253. it( 'does not forward events which are not supposed to be delegated', () => {
  254. const target = new View();
  255. const view = new View();
  256. const spyFoo = sinon.spy();
  257. const spyBar = sinon.spy();
  258. const spyBaz = sinon.spy();
  259. collection.delegate( 'foo', 'bar', 'baz' ).to( target );
  260. collection.add( view );
  261. target.on( 'foo', spyFoo );
  262. target.on( 'bar', spyBar );
  263. target.on( 'baz', spyBaz );
  264. view.fire( 'foo' );
  265. view.fire( 'bar' );
  266. view.fire( 'baz' );
  267. view.fire( 'not-delegated' );
  268. sinon.assert.callOrder( spyFoo, spyBar, spyBaz );
  269. sinon.assert.callCount( spyFoo, 1 );
  270. sinon.assert.callCount( spyBar, 1 );
  271. sinon.assert.callCount( spyBaz, 1 );
  272. } );
  273. it( 'stops forwarding when view removed from the collection', () => {
  274. const target = new View();
  275. const view = new View();
  276. const spy = sinon.spy();
  277. collection.delegate( 'foo' ).to( target );
  278. target.on( 'foo', spy );
  279. collection.add( view );
  280. view.fire( 'foo' );
  281. sinon.assert.callCount( spy, 1 );
  282. collection.remove( 0 );
  283. view.fire( 'foo' );
  284. sinon.assert.callCount( spy, 1 );
  285. } );
  286. it( 'supports deep event delegation', done => {
  287. const target = new View();
  288. const viewA = new View();
  289. const viewAA = new View();
  290. const data = {};
  291. const deepCollection = viewA.createCollection();
  292. collection.add( viewA );
  293. collection.delegate( 'foo' ).to( target );
  294. deepCollection.add( viewAA );
  295. deepCollection.delegate( 'foo' ).to( viewA );
  296. target.on( 'foo', ( ...args ) => {
  297. assertDelegated( args, {
  298. expectedName: 'foo',
  299. expectedSource: viewAA,
  300. expectedPath: [ viewAA, viewA, target ],
  301. expectedData: [ data ]
  302. } );
  303. done();
  304. } );
  305. viewAA.fire( 'foo', data );
  306. } );
  307. } );
  308. } );
  309. } );
  310. function createTestCollection() {
  311. collection = new ViewCollection();
  312. }
  313. function assertDelegated( evtArgs, { expectedName, expectedSource, expectedPath, expectedData } ) {
  314. const evtInfo = evtArgs[ 0 ];
  315. expect( evtInfo.name ).to.equal( expectedName );
  316. expect( evtInfo.source ).to.equal( expectedSource );
  317. expect( evtInfo.path ).to.deep.equal( expectedPath );
  318. expect( evtArgs.slice( 1 ) ).to.deep.equal( expectedData );
  319. }