viewcollection.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517
  1. /**
  2. * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* global document, setTimeout */
  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: 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. return view.init().then( () => {
  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. } );
  94. describe( 'init()', () => {
  95. it( 'should return a promise', () => {
  96. expect( collection.init() ).to.be.instanceof( Promise );
  97. } );
  98. it( 'should return a composition of child init() promises', () => {
  99. const spyA = sinon.spy().named( 'A' );
  100. const spyB = sinon.spy().named( 'B' );
  101. const spyC = sinon.spy().named( 'C' );
  102. const childA = {
  103. init: () => {
  104. return new Promise( ( resolve ) => {
  105. setTimeout( () => {
  106. spyA();
  107. resolve();
  108. }, 20 );
  109. } );
  110. }
  111. };
  112. const childB = {
  113. init: () => {
  114. return new Promise( ( resolve ) => {
  115. setTimeout( () => {
  116. spyB();
  117. resolve();
  118. }, 10 );
  119. } );
  120. }
  121. };
  122. const childC = {
  123. init: () => {
  124. return new Promise( ( resolve ) => {
  125. setTimeout( () => {
  126. spyC();
  127. resolve();
  128. }, 0 );
  129. } );
  130. }
  131. };
  132. collection.add( childA );
  133. collection.add( childB );
  134. collection.add( childC );
  135. return collection.init()
  136. .then( () => {
  137. sinon.assert.callOrder( spyC, spyB, spyA );
  138. } );
  139. } );
  140. it( 'should throw if already initialized', () => {
  141. return collection.init()
  142. .then( () => {
  143. collection.init();
  144. throw new Error( 'This should not be executed.' );
  145. } )
  146. .catch( err => {
  147. expect( err ).to.be.instanceof( CKEditorError );
  148. expect( err.message ).to.match( /ui-viewcollection-init-reinit/ );
  149. } );
  150. } );
  151. it( 'calls #init on all views in the collection', () => {
  152. const viewA = new View();
  153. const viewB = new View();
  154. viewA.element = document.createElement( 'a' );
  155. viewB.element = document.createElement( 'b' );
  156. const spyA = testUtils.sinon.spy( viewA, 'init' );
  157. const spyB = testUtils.sinon.spy( viewB, 'init' );
  158. collection.setParent( document.body );
  159. collection.add( viewA );
  160. collection.add( viewB );
  161. return collection.init().then( () => {
  162. sinon.assert.calledOnce( spyA );
  163. sinon.assert.calledOnce( spyB );
  164. sinon.assert.callOrder( spyA, spyB );
  165. expect( viewA.element.parentNode ).to.equal( collection._parentElement );
  166. expect( viewA.element.nextSibling ).to.equal( viewB.element );
  167. expect( collection.ready ).to.be.true;
  168. } );
  169. } );
  170. } );
  171. describe( 'destroy()', () => {
  172. it( 'should return a promise', () => {
  173. expect( collection.destroy() ).to.be.instanceof( Promise );
  174. } );
  175. it( 'calls #destroy on all views in the collection', () => {
  176. const viewA = new View();
  177. const viewB = new View();
  178. const spyA = testUtils.sinon.spy( viewA, 'destroy' );
  179. const spyB = testUtils.sinon.spy( viewB, 'destroy' );
  180. collection.add( viewA );
  181. collection.add( viewB );
  182. return collection.destroy().then( () => {
  183. sinon.assert.calledOnce( spyA );
  184. sinon.assert.calledOnce( spyB );
  185. sinon.assert.callOrder( spyA, spyB );
  186. } );
  187. } );
  188. } );
  189. describe( 'add()', () => {
  190. it( 'returns a promise', () => {
  191. expect( collection.add( {} ) ).to.be.instanceof( Promise );
  192. } );
  193. it( 'initializes the new view in the collection', () => {
  194. let view = new View();
  195. let spy = testUtils.sinon.spy( view, 'init' );
  196. expect( collection.ready ).to.be.false;
  197. expect( view.ready ).to.be.false;
  198. return collection.add( view ).then( () => {
  199. expect( collection.ready ).to.be.false;
  200. expect( view.ready ).to.be.false;
  201. sinon.assert.notCalled( spy );
  202. view = new View();
  203. spy = testUtils.sinon.spy( view, 'init' );
  204. collection.ready = true;
  205. return collection.add( view ).then( () => {
  206. expect( view.ready ).to.be.true;
  207. sinon.assert.calledOnce( spy );
  208. } );
  209. } );
  210. } );
  211. it( 'works for a view with a Number view#id attribute', () => {
  212. const view = new View();
  213. view.set( 'id', 1 );
  214. return collection.add( view ).then( () => {
  215. expect( view.id ).to.equal( 1 );
  216. expect( view.viewUid ).to.be.a( 'string' );
  217. } );
  218. } );
  219. } );
  220. describe( 'setParent()', () => {
  221. it( 'sets #_parentElement', () => {
  222. const el = {};
  223. expect( collection._parentElement ).to.be.null;
  224. collection.setParent( el );
  225. expect( collection._parentElement ).to.equal( el );
  226. } );
  227. } );
  228. describe( 'delegate()', () => {
  229. it( 'should throw when event names are not strings', () => {
  230. expect( () => {
  231. collection.delegate();
  232. } ).to.throw( CKEditorError, /ui-viewcollection-delegate-wrong-events/ );
  233. expect( () => {
  234. collection.delegate( new Date() );
  235. } ).to.throw( CKEditorError, /ui-viewcollection-delegate-wrong-events/ );
  236. expect( () => {
  237. collection.delegate( 'color', new Date() );
  238. } ).to.throw( CKEditorError, /ui-viewcollection-delegate-wrong-events/ );
  239. } );
  240. it( 'returns object', () => {
  241. expect( collection.delegate( 'foo' ) ).to.be.an( 'object' );
  242. } );
  243. it( 'provides "to()" interface', () => {
  244. const delegate = collection.delegate( 'foo' );
  245. expect( delegate ).to.have.keys( 'to' );
  246. expect( delegate.to ).to.be.a( 'function' );
  247. } );
  248. describe( 'to()', () => {
  249. it( 'does not chain', () => {
  250. const returned = collection.delegate( 'foo' ).to( {} );
  251. expect( returned ).to.be.undefined;
  252. } );
  253. it( 'forwards an event to another observable – existing view', ( done ) => {
  254. const target = new View();
  255. const view = new View();
  256. collection.add( view );
  257. collection.delegate( 'foo' ).to( target );
  258. target.on( 'foo', ( ...args ) => {
  259. assertDelegated( args, {
  260. expectedName: 'foo',
  261. expectedSource: view,
  262. expectedPath: [ view, target ],
  263. expectedData: []
  264. } );
  265. done();
  266. } );
  267. view.fire( 'foo' );
  268. } );
  269. it( 'forwards an event to another observable – new view', ( done ) => {
  270. const target = new View();
  271. const view = new View();
  272. collection.delegate( 'foo' ).to( target );
  273. collection.add( view );
  274. target.on( 'foo', ( ...args ) => {
  275. assertDelegated( args, {
  276. expectedName: 'foo',
  277. expectedSource: view,
  278. expectedPath: [ view, target ],
  279. expectedData: []
  280. } );
  281. done();
  282. } );
  283. view.fire( 'foo' );
  284. } );
  285. it( 'forwards multiple events to another observable', () => {
  286. const target = new View();
  287. const viewA = new View();
  288. const viewB = new View();
  289. const viewC = new View();
  290. const spyFoo = sinon.spy();
  291. const spyBar = sinon.spy();
  292. const spyBaz = sinon.spy();
  293. collection.delegate( 'foo', 'bar', 'baz' ).to( target );
  294. collection.add( viewA );
  295. collection.add( viewB );
  296. collection.add( viewC );
  297. target.on( 'foo', spyFoo );
  298. target.on( 'bar', spyBar );
  299. target.on( 'baz', spyBaz );
  300. viewA.fire( 'foo' );
  301. sinon.assert.calledOnce( spyFoo );
  302. sinon.assert.notCalled( spyBar );
  303. sinon.assert.notCalled( spyBaz );
  304. assertDelegated( spyFoo.args[ 0 ], {
  305. expectedName: 'foo',
  306. expectedSource: viewA,
  307. expectedPath: [ viewA, target ],
  308. expectedData: []
  309. } );
  310. viewB.fire( 'bar' );
  311. sinon.assert.calledOnce( spyFoo );
  312. sinon.assert.calledOnce( spyBar );
  313. sinon.assert.notCalled( spyBaz );
  314. assertDelegated( spyBar.args[ 0 ], {
  315. expectedName: 'bar',
  316. expectedSource: viewB,
  317. expectedPath: [ viewB, target ],
  318. expectedData: []
  319. } );
  320. viewC.fire( 'baz' );
  321. sinon.assert.calledOnce( spyFoo );
  322. sinon.assert.calledOnce( spyBar );
  323. sinon.assert.calledOnce( spyBaz );
  324. assertDelegated( spyBaz.args[ 0 ], {
  325. expectedName: 'baz',
  326. expectedSource: viewC,
  327. expectedPath: [ viewC, target ],
  328. expectedData: []
  329. } );
  330. viewC.fire( 'not-delegated' );
  331. sinon.assert.calledOnce( spyFoo );
  332. sinon.assert.calledOnce( spyBar );
  333. sinon.assert.calledOnce( spyBaz );
  334. } );
  335. it( 'does not forward events which are not supposed to be delegated', () => {
  336. const target = new View();
  337. const view = new View();
  338. const spyFoo = sinon.spy();
  339. const spyBar = sinon.spy();
  340. const spyBaz = sinon.spy();
  341. collection.delegate( 'foo', 'bar', 'baz' ).to( target );
  342. collection.add( view );
  343. target.on( 'foo', spyFoo );
  344. target.on( 'bar', spyBar );
  345. target.on( 'baz', spyBaz );
  346. view.fire( 'foo' );
  347. view.fire( 'bar' );
  348. view.fire( 'baz' );
  349. view.fire( 'not-delegated' );
  350. sinon.assert.callOrder( spyFoo, spyBar, spyBaz );
  351. sinon.assert.callCount( spyFoo, 1 );
  352. sinon.assert.callCount( spyBar, 1 );
  353. sinon.assert.callCount( spyBaz, 1 );
  354. } );
  355. it( 'stops forwarding when view removed from the collection', () => {
  356. const target = new View();
  357. const view = new View();
  358. const spy = sinon.spy();
  359. collection.delegate( 'foo' ).to( target );
  360. target.on( 'foo', spy );
  361. collection.add( view );
  362. view.fire( 'foo' );
  363. sinon.assert.callCount( spy, 1 );
  364. collection.remove( 0 );
  365. view.fire( 'foo' );
  366. sinon.assert.callCount( spy, 1 );
  367. } );
  368. it( 'supports deep event delegation', ( done ) => {
  369. const target = new View();
  370. const viewA = new View();
  371. const viewAA = new View();
  372. const data = {};
  373. const deepCollection = viewA.createCollection();
  374. collection.add( viewA );
  375. collection.delegate( 'foo' ).to( target );
  376. deepCollection.add( viewAA );
  377. deepCollection.delegate( 'foo' ).to( viewA );
  378. target.on( 'foo', ( ...args ) => {
  379. assertDelegated( args, {
  380. expectedName: 'foo',
  381. expectedSource: viewAA,
  382. expectedPath: [ viewAA, viewA, target ],
  383. expectedData: [ data ]
  384. } );
  385. done();
  386. } );
  387. viewAA.fire( 'foo', data );
  388. } );
  389. } );
  390. } );
  391. } );
  392. function createTestCollection() {
  393. collection = new ViewCollection();
  394. }
  395. function assertDelegated( evtArgs, { expectedName, expectedSource, expectedPath, expectedData } ) {
  396. const evtInfo = evtArgs[ 0 ];
  397. expect( evtInfo.name ).to.equal( expectedName );
  398. expect( evtInfo.source ).to.equal( expectedSource );
  399. expect( evtInfo.path ).to.deep.equal( expectedPath );
  400. expect( evtArgs.slice( 1 ) ).to.deep.equal( expectedData );
  401. }