viewcollection.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  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.ready ).to.be.false;
  20. expect( collection._parentElement ).to.be.null;
  21. expect( collection._idProperty ).to.equal( 'viewUid' );
  22. } );
  23. it( 'accepts locale and defines the locale property', () => {
  24. const locale = { t() {} };
  25. expect( new ViewCollection( locale ).locale ).to.equal( locale );
  26. } );
  27. describe( 'child view management in DOM', () => {
  28. it( 'manages view#element of the children in DOM', () => {
  29. const parentElement = document.createElement( 'p' );
  30. collection.setParent( parentElement );
  31. const viewA = new View();
  32. expect( () => {
  33. collection.add( viewA );
  34. collection.remove( viewA );
  35. } ).to.not.throw();
  36. expect( () => {
  37. collection.ready = true;
  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.template = new Template( {
  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.template = new Template( {
  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. // Finally init the view. Check what's in there.
  86. view.init();
  87. expect( view.element.childNodes ).to.have.length( 2 );
  88. expect( view.element.childNodes[ 0 ] ).to.equal( viewA.element );
  89. expect( view.element.childNodes[ 1 ] ).to.equal( viewC.element );
  90. } );
  91. } );
  92. } );
  93. describe( 'init()', () => {
  94. it( 'should throw if already initialized', () => {
  95. collection.init();
  96. try {
  97. collection.init();
  98. throw new Error( 'This should not be executed.' );
  99. } catch ( err ) {
  100. expect( err ).to.be.instanceof( CKEditorError );
  101. expect( err.message ).to.match( /ui-viewcollection-init-reinit/ );
  102. }
  103. } );
  104. it( 'calls #init on all views in the collection', () => {
  105. const viewA = new View();
  106. const viewB = new View();
  107. viewA.element = document.createElement( 'a' );
  108. viewB.element = document.createElement( 'b' );
  109. const spyA = testUtils.sinon.spy( viewA, 'init' );
  110. const spyB = testUtils.sinon.spy( viewB, 'init' );
  111. collection.setParent( document.body );
  112. collection.add( viewA );
  113. collection.add( viewB );
  114. collection.init();
  115. sinon.assert.calledOnce( spyA );
  116. sinon.assert.calledOnce( spyB );
  117. sinon.assert.callOrder( spyA, spyB );
  118. expect( viewA.element.parentNode ).to.equal( collection._parentElement );
  119. expect( viewA.element.nextSibling ).to.equal( viewB.element );
  120. expect( collection.ready ).to.be.true;
  121. } );
  122. } );
  123. describe( 'destroy()', () => {
  124. it( 'calls #destroy on all views in the collection', () => {
  125. const viewA = new View();
  126. const viewB = new View();
  127. const spyA = testUtils.sinon.spy( viewA, 'destroy' );
  128. const spyB = testUtils.sinon.spy( viewB, 'destroy' );
  129. collection.add( viewA );
  130. collection.add( viewB );
  131. collection.destroy();
  132. sinon.assert.calledOnce( spyA );
  133. sinon.assert.calledOnce( spyB );
  134. sinon.assert.callOrder( spyA, spyB );
  135. } );
  136. } );
  137. describe( 'add()', () => {
  138. it( 'initializes the new view in the collection', () => {
  139. let view = new View();
  140. let spy = testUtils.sinon.spy( view, 'init' );
  141. expect( collection.ready ).to.be.false;
  142. expect( view.ready ).to.be.false;
  143. collection.add( view );
  144. expect( collection.ready ).to.be.false;
  145. expect( view.ready ).to.be.false;
  146. sinon.assert.notCalled( spy );
  147. view = new View();
  148. spy = testUtils.sinon.spy( view, 'init' );
  149. collection.ready = true;
  150. collection.add( view );
  151. expect( view.ready ).to.be.true;
  152. sinon.assert.calledOnce( spy );
  153. } );
  154. it( 'works for a view with a Number view#id attribute', () => {
  155. const view = new View();
  156. view.set( 'id', 1 );
  157. collection.add( view );
  158. expect( view.id ).to.equal( 1 );
  159. expect( view.viewUid ).to.be.a( 'string' );
  160. } );
  161. } );
  162. describe( 'setParent()', () => {
  163. it( 'sets #_parentElement', () => {
  164. const el = {};
  165. expect( collection._parentElement ).to.be.null;
  166. collection.setParent( el );
  167. expect( collection._parentElement ).to.equal( el );
  168. } );
  169. } );
  170. describe( 'delegate()', () => {
  171. it( 'should throw when event names are not strings', () => {
  172. expect( () => {
  173. collection.delegate();
  174. } ).to.throw( CKEditorError, /ui-viewcollection-delegate-wrong-events/ );
  175. expect( () => {
  176. collection.delegate( new Date() );
  177. } ).to.throw( CKEditorError, /ui-viewcollection-delegate-wrong-events/ );
  178. expect( () => {
  179. collection.delegate( 'color', new Date() );
  180. } ).to.throw( CKEditorError, /ui-viewcollection-delegate-wrong-events/ );
  181. } );
  182. it( 'returns object', () => {
  183. expect( collection.delegate( 'foo' ) ).to.be.an( 'object' );
  184. } );
  185. it( 'provides "to()" interface', () => {
  186. const delegate = collection.delegate( 'foo' );
  187. expect( delegate ).to.have.keys( 'to' );
  188. expect( delegate.to ).to.be.a( 'function' );
  189. } );
  190. describe( 'to()', () => {
  191. it( 'does not chain', () => {
  192. const returned = collection.delegate( 'foo' ).to( {} );
  193. expect( returned ).to.be.undefined;
  194. } );
  195. it( 'forwards an event to another observable – existing view', done => {
  196. const target = new View();
  197. const view = new View();
  198. collection.add( view );
  199. collection.delegate( 'foo' ).to( target );
  200. target.on( 'foo', ( ...args ) => {
  201. assertDelegated( args, {
  202. expectedName: 'foo',
  203. expectedSource: view,
  204. expectedPath: [ view, target ],
  205. expectedData: []
  206. } );
  207. done();
  208. } );
  209. view.fire( 'foo' );
  210. } );
  211. it( 'forwards an event to another observable – new view', done => {
  212. const target = new View();
  213. const view = new View();
  214. collection.delegate( 'foo' ).to( target );
  215. collection.add( view );
  216. target.on( 'foo', ( ...args ) => {
  217. assertDelegated( args, {
  218. expectedName: 'foo',
  219. expectedSource: view,
  220. expectedPath: [ view, target ],
  221. expectedData: []
  222. } );
  223. done();
  224. } );
  225. view.fire( 'foo' );
  226. } );
  227. it( 'forwards multiple events to another observable', () => {
  228. const target = new View();
  229. const viewA = new View();
  230. const viewB = new View();
  231. const viewC = new View();
  232. const spyFoo = sinon.spy();
  233. const spyBar = sinon.spy();
  234. const spyBaz = sinon.spy();
  235. collection.delegate( 'foo', 'bar', 'baz' ).to( target );
  236. collection.add( viewA );
  237. collection.add( viewB );
  238. collection.add( viewC );
  239. target.on( 'foo', spyFoo );
  240. target.on( 'bar', spyBar );
  241. target.on( 'baz', spyBaz );
  242. viewA.fire( 'foo' );
  243. sinon.assert.calledOnce( spyFoo );
  244. sinon.assert.notCalled( spyBar );
  245. sinon.assert.notCalled( spyBaz );
  246. assertDelegated( spyFoo.args[ 0 ], {
  247. expectedName: 'foo',
  248. expectedSource: viewA,
  249. expectedPath: [ viewA, target ],
  250. expectedData: []
  251. } );
  252. viewB.fire( 'bar' );
  253. sinon.assert.calledOnce( spyFoo );
  254. sinon.assert.calledOnce( spyBar );
  255. sinon.assert.notCalled( spyBaz );
  256. assertDelegated( spyBar.args[ 0 ], {
  257. expectedName: 'bar',
  258. expectedSource: viewB,
  259. expectedPath: [ viewB, target ],
  260. expectedData: []
  261. } );
  262. viewC.fire( 'baz' );
  263. sinon.assert.calledOnce( spyFoo );
  264. sinon.assert.calledOnce( spyBar );
  265. sinon.assert.calledOnce( spyBaz );
  266. assertDelegated( spyBaz.args[ 0 ], {
  267. expectedName: 'baz',
  268. expectedSource: viewC,
  269. expectedPath: [ viewC, target ],
  270. expectedData: []
  271. } );
  272. viewC.fire( 'not-delegated' );
  273. sinon.assert.calledOnce( spyFoo );
  274. sinon.assert.calledOnce( spyBar );
  275. sinon.assert.calledOnce( spyBaz );
  276. } );
  277. it( 'does not forward events which are not supposed to be delegated', () => {
  278. const target = new View();
  279. const view = new View();
  280. const spyFoo = sinon.spy();
  281. const spyBar = sinon.spy();
  282. const spyBaz = sinon.spy();
  283. collection.delegate( 'foo', 'bar', 'baz' ).to( target );
  284. collection.add( view );
  285. target.on( 'foo', spyFoo );
  286. target.on( 'bar', spyBar );
  287. target.on( 'baz', spyBaz );
  288. view.fire( 'foo' );
  289. view.fire( 'bar' );
  290. view.fire( 'baz' );
  291. view.fire( 'not-delegated' );
  292. sinon.assert.callOrder( spyFoo, spyBar, spyBaz );
  293. sinon.assert.callCount( spyFoo, 1 );
  294. sinon.assert.callCount( spyBar, 1 );
  295. sinon.assert.callCount( spyBaz, 1 );
  296. } );
  297. it( 'stops forwarding when view removed from the collection', () => {
  298. const target = new View();
  299. const view = new View();
  300. const spy = sinon.spy();
  301. collection.delegate( 'foo' ).to( target );
  302. target.on( 'foo', spy );
  303. collection.add( view );
  304. view.fire( 'foo' );
  305. sinon.assert.callCount( spy, 1 );
  306. collection.remove( 0 );
  307. view.fire( 'foo' );
  308. sinon.assert.callCount( spy, 1 );
  309. } );
  310. it( 'supports deep event delegation', done => {
  311. const target = new View();
  312. const viewA = new View();
  313. const viewAA = new View();
  314. const data = {};
  315. const deepCollection = viewA.createCollection();
  316. collection.add( viewA );
  317. collection.delegate( 'foo' ).to( target );
  318. deepCollection.add( viewAA );
  319. deepCollection.delegate( 'foo' ).to( viewA );
  320. target.on( 'foo', ( ...args ) => {
  321. assertDelegated( args, {
  322. expectedName: 'foo',
  323. expectedSource: viewAA,
  324. expectedPath: [ viewAA, viewA, target ],
  325. expectedData: [ data ]
  326. } );
  327. done();
  328. } );
  329. viewAA.fire( 'foo', data );
  330. } );
  331. } );
  332. } );
  333. } );
  334. function createTestCollection() {
  335. collection = new ViewCollection();
  336. }
  337. function assertDelegated( evtArgs, { expectedName, expectedSource, expectedPath, expectedData } ) {
  338. const evtInfo = evtArgs[ 0 ];
  339. expect( evtInfo.name ).to.equal( expectedName );
  340. expect( evtInfo.source ).to.equal( expectedSource );
  341. expect( evtInfo.path ).to.deep.equal( expectedPath );
  342. expect( evtArgs.slice( 1 ) ).to.deep.equal( expectedData );
  343. }