viewcollection.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557
  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
  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. // https://github.com/ckeditor/ckeditor5-ui/issues/203
  189. it( 'waits for all #add promises to resolve', () => {
  190. const spyA = sinon.spy();
  191. const spyB = sinon.spy();
  192. class DelayedInitView extends View {
  193. constructor( delay, spy ) {
  194. super();
  195. this.delay = delay;
  196. this.spy = spy;
  197. }
  198. init() {
  199. return new Promise( resolve => {
  200. setTimeout( () => resolve(), this.delay );
  201. } )
  202. .then( () => super.init() )
  203. .then( () => {
  204. this.spy();
  205. } );
  206. }
  207. }
  208. const viewA = new DelayedInitView( 200, spyA );
  209. const viewB = new DelayedInitView( 100, spyB );
  210. return collection.init().then( () => {
  211. collection.add( viewA );
  212. collection.add( viewB );
  213. } )
  214. .then( () => {
  215. return collection.destroy().then( () => {
  216. expect( viewA.ready ).to.be.true;
  217. expect( viewB.ready ).to.be.true;
  218. sinon.assert.callOrder( spyB, spyA );
  219. } );
  220. } );
  221. } );
  222. } );
  223. describe( 'add()', () => {
  224. it( 'returns a promise', () => {
  225. expect( collection.add( {} ) ).to.be.instanceof( Promise );
  226. } );
  227. it( 'initializes the new view in the collection', () => {
  228. let view = new View();
  229. let spy = testUtils.sinon.spy( view, 'init' );
  230. expect( collection.ready ).to.be.false;
  231. expect( view.ready ).to.be.false;
  232. return collection.add( view ).then( () => {
  233. expect( collection.ready ).to.be.false;
  234. expect( view.ready ).to.be.false;
  235. sinon.assert.notCalled( spy );
  236. view = new View();
  237. spy = testUtils.sinon.spy( view, 'init' );
  238. collection.ready = true;
  239. return collection.add( view ).then( () => {
  240. expect( view.ready ).to.be.true;
  241. sinon.assert.calledOnce( spy );
  242. } );
  243. } );
  244. } );
  245. it( 'works for a view with a Number view#id attribute', () => {
  246. const view = new View();
  247. view.set( 'id', 1 );
  248. return collection.add( view ).then( () => {
  249. expect( view.id ).to.equal( 1 );
  250. expect( view.viewUid ).to.be.a( 'string' );
  251. } );
  252. } );
  253. } );
  254. describe( 'setParent()', () => {
  255. it( 'sets #_parentElement', () => {
  256. const el = {};
  257. expect( collection._parentElement ).to.be.null;
  258. collection.setParent( el );
  259. expect( collection._parentElement ).to.equal( el );
  260. } );
  261. } );
  262. describe( 'delegate()', () => {
  263. it( 'should throw when event names are not strings', () => {
  264. expect( () => {
  265. collection.delegate();
  266. } ).to.throw( CKEditorError, /ui-viewcollection-delegate-wrong-events/ );
  267. expect( () => {
  268. collection.delegate( new Date() );
  269. } ).to.throw( CKEditorError, /ui-viewcollection-delegate-wrong-events/ );
  270. expect( () => {
  271. collection.delegate( 'color', new Date() );
  272. } ).to.throw( CKEditorError, /ui-viewcollection-delegate-wrong-events/ );
  273. } );
  274. it( 'returns object', () => {
  275. expect( collection.delegate( 'foo' ) ).to.be.an( 'object' );
  276. } );
  277. it( 'provides "to()" interface', () => {
  278. const delegate = collection.delegate( 'foo' );
  279. expect( delegate ).to.have.keys( 'to' );
  280. expect( delegate.to ).to.be.a( 'function' );
  281. } );
  282. describe( 'to()', () => {
  283. it( 'does not chain', () => {
  284. const returned = collection.delegate( 'foo' ).to( {} );
  285. expect( returned ).to.be.undefined;
  286. } );
  287. it( 'forwards an event to another observable – existing view', done => {
  288. const target = new View();
  289. const view = new View();
  290. collection.add( view );
  291. collection.delegate( 'foo' ).to( target );
  292. target.on( 'foo', ( ...args ) => {
  293. assertDelegated( args, {
  294. expectedName: 'foo',
  295. expectedSource: view,
  296. expectedPath: [ view, target ],
  297. expectedData: []
  298. } );
  299. done();
  300. } );
  301. view.fire( 'foo' );
  302. } );
  303. it( 'forwards an event to another observable – new view', done => {
  304. const target = new View();
  305. const view = new View();
  306. collection.delegate( 'foo' ).to( target );
  307. collection.add( view );
  308. target.on( 'foo', ( ...args ) => {
  309. assertDelegated( args, {
  310. expectedName: 'foo',
  311. expectedSource: view,
  312. expectedPath: [ view, target ],
  313. expectedData: []
  314. } );
  315. done();
  316. } );
  317. view.fire( 'foo' );
  318. } );
  319. it( 'forwards multiple events to another observable', () => {
  320. const target = new View();
  321. const viewA = new View();
  322. const viewB = new View();
  323. const viewC = new View();
  324. const spyFoo = sinon.spy();
  325. const spyBar = sinon.spy();
  326. const spyBaz = sinon.spy();
  327. collection.delegate( 'foo', 'bar', 'baz' ).to( target );
  328. collection.add( viewA );
  329. collection.add( viewB );
  330. collection.add( viewC );
  331. target.on( 'foo', spyFoo );
  332. target.on( 'bar', spyBar );
  333. target.on( 'baz', spyBaz );
  334. viewA.fire( 'foo' );
  335. sinon.assert.calledOnce( spyFoo );
  336. sinon.assert.notCalled( spyBar );
  337. sinon.assert.notCalled( spyBaz );
  338. assertDelegated( spyFoo.args[ 0 ], {
  339. expectedName: 'foo',
  340. expectedSource: viewA,
  341. expectedPath: [ viewA, target ],
  342. expectedData: []
  343. } );
  344. viewB.fire( 'bar' );
  345. sinon.assert.calledOnce( spyFoo );
  346. sinon.assert.calledOnce( spyBar );
  347. sinon.assert.notCalled( spyBaz );
  348. assertDelegated( spyBar.args[ 0 ], {
  349. expectedName: 'bar',
  350. expectedSource: viewB,
  351. expectedPath: [ viewB, target ],
  352. expectedData: []
  353. } );
  354. viewC.fire( 'baz' );
  355. sinon.assert.calledOnce( spyFoo );
  356. sinon.assert.calledOnce( spyBar );
  357. sinon.assert.calledOnce( spyBaz );
  358. assertDelegated( spyBaz.args[ 0 ], {
  359. expectedName: 'baz',
  360. expectedSource: viewC,
  361. expectedPath: [ viewC, target ],
  362. expectedData: []
  363. } );
  364. viewC.fire( 'not-delegated' );
  365. sinon.assert.calledOnce( spyFoo );
  366. sinon.assert.calledOnce( spyBar );
  367. sinon.assert.calledOnce( spyBaz );
  368. } );
  369. it( 'does not forward events which are not supposed to be delegated', () => {
  370. const target = new View();
  371. const view = new View();
  372. const spyFoo = sinon.spy();
  373. const spyBar = sinon.spy();
  374. const spyBaz = sinon.spy();
  375. collection.delegate( 'foo', 'bar', 'baz' ).to( target );
  376. collection.add( view );
  377. target.on( 'foo', spyFoo );
  378. target.on( 'bar', spyBar );
  379. target.on( 'baz', spyBaz );
  380. view.fire( 'foo' );
  381. view.fire( 'bar' );
  382. view.fire( 'baz' );
  383. view.fire( 'not-delegated' );
  384. sinon.assert.callOrder( spyFoo, spyBar, spyBaz );
  385. sinon.assert.callCount( spyFoo, 1 );
  386. sinon.assert.callCount( spyBar, 1 );
  387. sinon.assert.callCount( spyBaz, 1 );
  388. } );
  389. it( 'stops forwarding when view removed from the collection', () => {
  390. const target = new View();
  391. const view = new View();
  392. const spy = sinon.spy();
  393. collection.delegate( 'foo' ).to( target );
  394. target.on( 'foo', spy );
  395. collection.add( view );
  396. view.fire( 'foo' );
  397. sinon.assert.callCount( spy, 1 );
  398. collection.remove( 0 );
  399. view.fire( 'foo' );
  400. sinon.assert.callCount( spy, 1 );
  401. } );
  402. it( 'supports deep event delegation', done => {
  403. const target = new View();
  404. const viewA = new View();
  405. const viewAA = new View();
  406. const data = {};
  407. const deepCollection = viewA.createCollection();
  408. collection.add( viewA );
  409. collection.delegate( 'foo' ).to( target );
  410. deepCollection.add( viewAA );
  411. deepCollection.delegate( 'foo' ).to( viewA );
  412. target.on( 'foo', ( ...args ) => {
  413. assertDelegated( args, {
  414. expectedName: 'foo',
  415. expectedSource: viewAA,
  416. expectedPath: [ viewAA, viewA, target ],
  417. expectedData: [ data ]
  418. } );
  419. done();
  420. } );
  421. viewAA.fire( 'foo', data );
  422. } );
  423. } );
  424. } );
  425. } );
  426. function createTestCollection() {
  427. collection = new ViewCollection();
  428. }
  429. function assertDelegated( evtArgs, { expectedName, expectedSource, expectedPath, expectedData } ) {
  430. const evtInfo = evtArgs[ 0 ];
  431. expect( evtInfo.name ).to.equal( expectedName );
  432. expect( evtInfo.source ).to.equal( expectedSource );
  433. expect( evtInfo.path ).to.deep.equal( expectedPath );
  434. expect( evtArgs.slice( 1 ) ).to.deep.equal( expectedData );
  435. }