viewcollection.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  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. // Render view#template along with collection of children.
  79. view.element;
  80. const viewC = getView( 'C' );
  81. // Modify the collection, while the view#element has already
  82. // been rendered but the view has **not** been inited yet.
  83. collection.add( viewC );
  84. collection.remove( 1 );
  85. view.render();
  86. expect( view.element.childNodes ).to.have.length( 2 );
  87. expect( view.element.childNodes[ 0 ] ).to.equal( viewA.element );
  88. expect( view.element.childNodes[ 1 ] ).to.equal( viewC.element );
  89. } );
  90. } );
  91. } );
  92. describe( 'destroy()', () => {
  93. it( 'calls #destroy on all views in the collection', () => {
  94. const viewA = new View();
  95. const viewB = new View();
  96. const spyA = testUtils.sinon.spy( viewA, 'destroy' );
  97. const spyB = testUtils.sinon.spy( viewB, 'destroy' );
  98. collection.add( viewA );
  99. collection.add( viewB );
  100. collection.destroy();
  101. sinon.assert.calledOnce( spyA );
  102. sinon.assert.calledOnce( spyB );
  103. sinon.assert.callOrder( spyA, spyB );
  104. } );
  105. } );
  106. describe( 'add()', () => {
  107. it( 'renders the new view in the collection', () => {
  108. const view = new View();
  109. const spy = testUtils.sinon.spy( view, 'render' );
  110. expect( view.isRendered ).to.be.false;
  111. collection.add( view );
  112. expect( view.isRendered ).to.be.true;
  113. sinon.assert.calledOnce( spy );
  114. } );
  115. it( 'works for a view with a Number view#id attribute', () => {
  116. const view = new View();
  117. view.set( 'id', 1 );
  118. collection.add( view );
  119. expect( view.id ).to.equal( 1 );
  120. expect( view.viewUid ).to.be.a( 'string' );
  121. } );
  122. } );
  123. describe( 'setParent()', () => {
  124. it( 'sets #_parentElement', () => {
  125. const el = {};
  126. expect( collection._parentElement ).to.be.null;
  127. collection.setParent( el );
  128. expect( collection._parentElement ).to.equal( el );
  129. } );
  130. it( 'udpates initial collection items in DOM', () => {
  131. const view1 = new View();
  132. view1.element = document.createElement( 'i' );
  133. sinon.spy( view1, 'render' );
  134. const view2 = new View();
  135. view2.element = document.createElement( 'b' );
  136. sinon.spy( view2, 'render' );
  137. const collection = new ViewCollection( [ view1, view2 ] );
  138. const parentElement = document.createElement( 'div' );
  139. expect( collection ).to.have.length( 2 );
  140. expect( collection.get( 0 ) ).to.equal( view1 );
  141. expect( collection.get( 1 ) ).to.equal( view2 );
  142. collection.setParent( parentElement );
  143. expect( normalizeHtml( parentElement.outerHTML ) ).to.equal( '<div><i></i><b></b></div>' );
  144. sinon.assert.calledOnce( view1.render );
  145. sinon.assert.calledOnce( view2.render );
  146. } );
  147. } );
  148. describe( 'delegate()', () => {
  149. it( 'should throw when event names are not strings', () => {
  150. expectToThrowCKEditorError( () => {
  151. collection.delegate();
  152. }, /ui-viewcollection-delegate-wrong-events/ );
  153. expectToThrowCKEditorError( () => {
  154. collection.delegate( new Date() );
  155. }, /ui-viewcollection-delegate-wrong-events/ );
  156. expectToThrowCKEditorError( () => {
  157. collection.delegate( 'color', new Date() );
  158. }, /ui-viewcollection-delegate-wrong-events/ );
  159. } );
  160. it( 'returns object', () => {
  161. expect( collection.delegate( 'foo' ) ).to.be.an( 'object' );
  162. } );
  163. it( 'provides "to()" interface', () => {
  164. const delegate = collection.delegate( 'foo' );
  165. expect( delegate ).to.have.keys( 'to' );
  166. expect( delegate.to ).to.be.a( 'function' );
  167. } );
  168. describe( 'to()', () => {
  169. it( 'does not chain', () => {
  170. const returned = collection.delegate( 'foo' ).to( {} );
  171. expect( returned ).to.be.undefined;
  172. } );
  173. it( 'forwards an event to another observable – existing view', done => {
  174. const target = new View();
  175. const view = new View();
  176. collection.add( view );
  177. collection.delegate( 'foo' ).to( target );
  178. target.on( 'foo', ( ...args ) => {
  179. assertDelegated( args, {
  180. expectedName: 'foo',
  181. expectedSource: view,
  182. expectedPath: [ view, target ],
  183. expectedData: []
  184. } );
  185. done();
  186. } );
  187. view.fire( 'foo' );
  188. } );
  189. it( 'forwards an event to another observable – new view', done => {
  190. const target = new View();
  191. const view = new View();
  192. collection.delegate( 'foo' ).to( target );
  193. collection.add( view );
  194. target.on( 'foo', ( ...args ) => {
  195. assertDelegated( args, {
  196. expectedName: 'foo',
  197. expectedSource: view,
  198. expectedPath: [ view, target ],
  199. expectedData: []
  200. } );
  201. done();
  202. } );
  203. view.fire( 'foo' );
  204. } );
  205. it( 'forwards multiple events to another observable', () => {
  206. const target = new View();
  207. const viewA = new View();
  208. const viewB = new View();
  209. const viewC = new View();
  210. const spyFoo = sinon.spy();
  211. const spyBar = sinon.spy();
  212. const spyBaz = sinon.spy();
  213. collection.delegate( 'foo', 'bar', 'baz' ).to( target );
  214. collection.add( viewA );
  215. collection.add( viewB );
  216. collection.add( viewC );
  217. target.on( 'foo', spyFoo );
  218. target.on( 'bar', spyBar );
  219. target.on( 'baz', spyBaz );
  220. viewA.fire( 'foo' );
  221. sinon.assert.calledOnce( spyFoo );
  222. sinon.assert.notCalled( spyBar );
  223. sinon.assert.notCalled( spyBaz );
  224. assertDelegated( spyFoo.args[ 0 ], {
  225. expectedName: 'foo',
  226. expectedSource: viewA,
  227. expectedPath: [ viewA, target ],
  228. expectedData: []
  229. } );
  230. viewB.fire( 'bar' );
  231. sinon.assert.calledOnce( spyFoo );
  232. sinon.assert.calledOnce( spyBar );
  233. sinon.assert.notCalled( spyBaz );
  234. assertDelegated( spyBar.args[ 0 ], {
  235. expectedName: 'bar',
  236. expectedSource: viewB,
  237. expectedPath: [ viewB, target ],
  238. expectedData: []
  239. } );
  240. viewC.fire( 'baz' );
  241. sinon.assert.calledOnce( spyFoo );
  242. sinon.assert.calledOnce( spyBar );
  243. sinon.assert.calledOnce( spyBaz );
  244. assertDelegated( spyBaz.args[ 0 ], {
  245. expectedName: 'baz',
  246. expectedSource: viewC,
  247. expectedPath: [ viewC, target ],
  248. expectedData: []
  249. } );
  250. viewC.fire( 'not-delegated' );
  251. sinon.assert.calledOnce( spyFoo );
  252. sinon.assert.calledOnce( spyBar );
  253. sinon.assert.calledOnce( spyBaz );
  254. } );
  255. it( 'does not forward events which are not supposed to be delegated', () => {
  256. const target = new View();
  257. const view = new View();
  258. const spyFoo = sinon.spy();
  259. const spyBar = sinon.spy();
  260. const spyBaz = sinon.spy();
  261. collection.delegate( 'foo', 'bar', 'baz' ).to( target );
  262. collection.add( view );
  263. target.on( 'foo', spyFoo );
  264. target.on( 'bar', spyBar );
  265. target.on( 'baz', spyBaz );
  266. view.fire( 'foo' );
  267. view.fire( 'bar' );
  268. view.fire( 'baz' );
  269. view.fire( 'not-delegated' );
  270. sinon.assert.callOrder( spyFoo, spyBar, spyBaz );
  271. sinon.assert.callCount( spyFoo, 1 );
  272. sinon.assert.callCount( spyBar, 1 );
  273. sinon.assert.callCount( spyBaz, 1 );
  274. } );
  275. it( 'stops forwarding when view removed from the collection', () => {
  276. const target = new View();
  277. const view = new View();
  278. const spy = sinon.spy();
  279. collection.delegate( 'foo' ).to( target );
  280. target.on( 'foo', spy );
  281. collection.add( view );
  282. view.fire( 'foo' );
  283. sinon.assert.callCount( spy, 1 );
  284. collection.remove( 0 );
  285. view.fire( 'foo' );
  286. sinon.assert.callCount( spy, 1 );
  287. } );
  288. it( 'supports deep event delegation', done => {
  289. const target = new View();
  290. const viewA = new View();
  291. const viewAA = new View();
  292. const data = {};
  293. const deepCollection = viewA.createCollection();
  294. collection.add( viewA );
  295. collection.delegate( 'foo' ).to( target );
  296. deepCollection.add( viewAA );
  297. deepCollection.delegate( 'foo' ).to( viewA );
  298. target.on( 'foo', ( ...args ) => {
  299. assertDelegated( args, {
  300. expectedName: 'foo',
  301. expectedSource: viewAA,
  302. expectedPath: [ viewAA, viewA, target ],
  303. expectedData: [ data ]
  304. } );
  305. done();
  306. } );
  307. viewAA.fire( 'foo', data );
  308. } );
  309. } );
  310. } );
  311. } );
  312. function createTestCollection() {
  313. collection = new ViewCollection();
  314. }
  315. function assertDelegated( evtArgs, { expectedName, expectedSource, expectedPath, expectedData } ) {
  316. const evtInfo = evtArgs[ 0 ];
  317. expect( evtInfo.name ).to.equal( expectedName );
  318. expect( evtInfo.source ).to.equal( expectedSource );
  319. expect( evtInfo.path ).to.deep.equal( expectedPath );
  320. expect( evtArgs.slice( 1 ) ).to.deep.equal( expectedData );
  321. }