viewcollection.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  1. /**
  2. * @license Copyright (c) 2003-2017, 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 Template from '../src/template';
  11. import normalizeHtml from '@ckeditor/ckeditor5-utils/tests/_utils/normalizehtml';
  12. let collection;
  13. testUtils.createSinonSandbox();
  14. describe( 'ViewCollection', () => {
  15. beforeEach( createTestCollection );
  16. describe( 'constructor()', () => {
  17. it( 'sets basic properties and attributes', () => {
  18. expect( collection.locale ).to.be.undefined;
  19. expect( collection._parentElement ).to.be.null;
  20. expect( collection._idProperty ).to.equal( 'viewUid' );
  21. } );
  22. it( 'accepts locale and defines the locale property', () => {
  23. const locale = { t() {} };
  24. expect( new ViewCollection( locale ).locale ).to.equal( locale );
  25. } );
  26. describe( 'child view management in DOM', () => {
  27. it( 'manages view#element of the children in DOM', () => {
  28. const parentElement = document.createElement( 'p' );
  29. collection.setParent( parentElement );
  30. const viewA = new View();
  31. expect( () => {
  32. collection.add( viewA );
  33. collection.remove( viewA );
  34. } ).to.not.throw();
  35. expect( () => {
  36. collection.add( viewA );
  37. collection.remove( viewA );
  38. } ).to.not.throw();
  39. viewA.element = document.createElement( 'b' );
  40. collection.add( viewA );
  41. expect( normalizeHtml( parentElement.outerHTML ) ).to.equal( '<p><b></b></p>' );
  42. const viewB = new View();
  43. viewB.element = document.createElement( 'i' );
  44. collection.add( viewB, 0 );
  45. expect( normalizeHtml( parentElement.outerHTML ) ).to.equal( '<p><i></i><b></b></p>' );
  46. collection.remove( viewA );
  47. expect( normalizeHtml( parentElement.outerHTML ) ).to.equal( '<p><i></i></p>' );
  48. collection.remove( viewB );
  49. expect( normalizeHtml( parentElement.outerHTML ) ).to.equal( '<p></p>' );
  50. } );
  51. // #145
  52. it( 'always keeps the collection synchronized with DOM', () => {
  53. const view = new View();
  54. const viewA = getView( 'A' );
  55. const viewB = getView( 'B' );
  56. function getView( text ) {
  57. const view = new View();
  58. view.template = new Template( {
  59. tag: 'li',
  60. children: [
  61. {
  62. text
  63. }
  64. ]
  65. } );
  66. return view;
  67. }
  68. // Fill the collection with children.
  69. collection.add( viewA );
  70. collection.add( viewB );
  71. // Put the collection in the template.
  72. view.template = new Template( {
  73. tag: 'ul',
  74. children: collection
  75. } );
  76. // Render view#template along with collection of children.
  77. view.element;
  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. } );
  129. describe( 'delegate()', () => {
  130. it( 'should throw when event names are not strings', () => {
  131. expect( () => {
  132. collection.delegate();
  133. } ).to.throw( CKEditorError, /ui-viewcollection-delegate-wrong-events/ );
  134. expect( () => {
  135. collection.delegate( new Date() );
  136. } ).to.throw( CKEditorError, /ui-viewcollection-delegate-wrong-events/ );
  137. expect( () => {
  138. collection.delegate( 'color', new Date() );
  139. } ).to.throw( CKEditorError, /ui-viewcollection-delegate-wrong-events/ );
  140. } );
  141. it( 'returns object', () => {
  142. expect( collection.delegate( 'foo' ) ).to.be.an( 'object' );
  143. } );
  144. it( 'provides "to()" interface', () => {
  145. const delegate = collection.delegate( 'foo' );
  146. expect( delegate ).to.have.keys( 'to' );
  147. expect( delegate.to ).to.be.a( 'function' );
  148. } );
  149. describe( 'to()', () => {
  150. it( 'does not chain', () => {
  151. const returned = collection.delegate( 'foo' ).to( {} );
  152. expect( returned ).to.be.undefined;
  153. } );
  154. it( 'forwards an event to another observable – existing view', done => {
  155. const target = new View();
  156. const view = new View();
  157. collection.add( view );
  158. collection.delegate( 'foo' ).to( target );
  159. target.on( 'foo', ( ...args ) => {
  160. assertDelegated( args, {
  161. expectedName: 'foo',
  162. expectedSource: view,
  163. expectedPath: [ view, target ],
  164. expectedData: []
  165. } );
  166. done();
  167. } );
  168. view.fire( 'foo' );
  169. } );
  170. it( 'forwards an event to another observable – new view', done => {
  171. const target = new View();
  172. const view = new View();
  173. collection.delegate( 'foo' ).to( target );
  174. collection.add( view );
  175. target.on( 'foo', ( ...args ) => {
  176. assertDelegated( args, {
  177. expectedName: 'foo',
  178. expectedSource: view,
  179. expectedPath: [ view, target ],
  180. expectedData: []
  181. } );
  182. done();
  183. } );
  184. view.fire( 'foo' );
  185. } );
  186. it( 'forwards multiple events to another observable', () => {
  187. const target = new View();
  188. const viewA = new View();
  189. const viewB = new View();
  190. const viewC = new View();
  191. const spyFoo = sinon.spy();
  192. const spyBar = sinon.spy();
  193. const spyBaz = sinon.spy();
  194. collection.delegate( 'foo', 'bar', 'baz' ).to( target );
  195. collection.add( viewA );
  196. collection.add( viewB );
  197. collection.add( viewC );
  198. target.on( 'foo', spyFoo );
  199. target.on( 'bar', spyBar );
  200. target.on( 'baz', spyBaz );
  201. viewA.fire( 'foo' );
  202. sinon.assert.calledOnce( spyFoo );
  203. sinon.assert.notCalled( spyBar );
  204. sinon.assert.notCalled( spyBaz );
  205. assertDelegated( spyFoo.args[ 0 ], {
  206. expectedName: 'foo',
  207. expectedSource: viewA,
  208. expectedPath: [ viewA, target ],
  209. expectedData: []
  210. } );
  211. viewB.fire( 'bar' );
  212. sinon.assert.calledOnce( spyFoo );
  213. sinon.assert.calledOnce( spyBar );
  214. sinon.assert.notCalled( spyBaz );
  215. assertDelegated( spyBar.args[ 0 ], {
  216. expectedName: 'bar',
  217. expectedSource: viewB,
  218. expectedPath: [ viewB, target ],
  219. expectedData: []
  220. } );
  221. viewC.fire( 'baz' );
  222. sinon.assert.calledOnce( spyFoo );
  223. sinon.assert.calledOnce( spyBar );
  224. sinon.assert.calledOnce( spyBaz );
  225. assertDelegated( spyBaz.args[ 0 ], {
  226. expectedName: 'baz',
  227. expectedSource: viewC,
  228. expectedPath: [ viewC, target ],
  229. expectedData: []
  230. } );
  231. viewC.fire( 'not-delegated' );
  232. sinon.assert.calledOnce( spyFoo );
  233. sinon.assert.calledOnce( spyBar );
  234. sinon.assert.calledOnce( spyBaz );
  235. } );
  236. it( 'does not forward events which are not supposed to be delegated', () => {
  237. const target = new View();
  238. const view = new View();
  239. const spyFoo = sinon.spy();
  240. const spyBar = sinon.spy();
  241. const spyBaz = sinon.spy();
  242. collection.delegate( 'foo', 'bar', 'baz' ).to( target );
  243. collection.add( view );
  244. target.on( 'foo', spyFoo );
  245. target.on( 'bar', spyBar );
  246. target.on( 'baz', spyBaz );
  247. view.fire( 'foo' );
  248. view.fire( 'bar' );
  249. view.fire( 'baz' );
  250. view.fire( 'not-delegated' );
  251. sinon.assert.callOrder( spyFoo, spyBar, spyBaz );
  252. sinon.assert.callCount( spyFoo, 1 );
  253. sinon.assert.callCount( spyBar, 1 );
  254. sinon.assert.callCount( spyBaz, 1 );
  255. } );
  256. it( 'stops forwarding when view removed from the collection', () => {
  257. const target = new View();
  258. const view = new View();
  259. const spy = sinon.spy();
  260. collection.delegate( 'foo' ).to( target );
  261. target.on( 'foo', spy );
  262. collection.add( view );
  263. view.fire( 'foo' );
  264. sinon.assert.callCount( spy, 1 );
  265. collection.remove( 0 );
  266. view.fire( 'foo' );
  267. sinon.assert.callCount( spy, 1 );
  268. } );
  269. it( 'supports deep event delegation', done => {
  270. const target = new View();
  271. const viewA = new View();
  272. const viewAA = new View();
  273. const data = {};
  274. const deepCollection = viewA.createCollection();
  275. collection.add( viewA );
  276. collection.delegate( 'foo' ).to( target );
  277. deepCollection.add( viewAA );
  278. deepCollection.delegate( 'foo' ).to( viewA );
  279. target.on( 'foo', ( ...args ) => {
  280. assertDelegated( args, {
  281. expectedName: 'foo',
  282. expectedSource: viewAA,
  283. expectedPath: [ viewAA, viewA, target ],
  284. expectedData: [ data ]
  285. } );
  286. done();
  287. } );
  288. viewAA.fire( 'foo', data );
  289. } );
  290. } );
  291. } );
  292. } );
  293. function createTestCollection() {
  294. collection = new ViewCollection();
  295. }
  296. function assertDelegated( evtArgs, { expectedName, expectedSource, expectedPath, expectedData } ) {
  297. const evtInfo = evtArgs[ 0 ];
  298. expect( evtInfo.name ).to.equal( expectedName );
  299. expect( evtInfo.source ).to.equal( expectedSource );
  300. expect( evtInfo.path ).to.deep.equal( expectedPath );
  301. expect( evtArgs.slice( 1 ) ).to.deep.equal( expectedData );
  302. }