8
0

viewcollection.js 10 KB

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