viewcollection.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553
  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 Collection from '@ckeditor/ckeditor5-utils/src/collection';
  8. import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
  9. import View from '../src/view';
  10. import ViewCollection from '../src/viewcollection';
  11. import Template from '../src/template';
  12. import normalizeHtml from '@ckeditor/ckeditor5-utils/tests/_utils/normalizehtml';
  13. let collection;
  14. testUtils.createSinonSandbox();
  15. describe( 'ViewCollection', () => {
  16. beforeEach( createTestCollection );
  17. describe( 'constructor()', () => {
  18. it( 'sets basic properties and attributes', () => {
  19. expect( collection.locale ).to.be.undefined;
  20. expect( collection.ready ).to.be.false;
  21. expect( collection._parentElement ).to.be.null;
  22. expect( collection._boundItemsToViewsMap ).to.be.instanceOf( Map );
  23. expect( collection._idProperty ).to.equal( 'viewUid' );
  24. } );
  25. it( 'accepts locale and defines the locale property', () => {
  26. const locale = { t() {} };
  27. expect( new ViewCollection( locale ).locale ).to.equal( locale );
  28. } );
  29. it( 'manages view#element of the children in DOM', () => {
  30. const parentElement = document.createElement( 'p' );
  31. collection.setParent( parentElement );
  32. const viewA = new View();
  33. expect( () => {
  34. collection.add( viewA );
  35. collection.remove( viewA );
  36. } ).to.not.throw();
  37. expect( () => {
  38. collection.ready = true;
  39. collection.add( viewA );
  40. collection.remove( viewA );
  41. } ).to.not.throw();
  42. viewA.element = document.createElement( 'b' );
  43. collection.add( viewA );
  44. expect( normalizeHtml( parentElement.outerHTML ) ).to.equal( '<p><b></b></p>' );
  45. const viewB = new View();
  46. viewB.element = document.createElement( 'i' );
  47. collection.add( viewB, 0 );
  48. expect( normalizeHtml( parentElement.outerHTML ) ).to.equal( '<p><i></i><b></b></p>' );
  49. collection.remove( viewA );
  50. expect( normalizeHtml( parentElement.outerHTML ) ).to.equal( '<p><i></i></p>' );
  51. collection.remove( viewB );
  52. expect( normalizeHtml( parentElement.outerHTML ) ).to.equal( '<p></p>' );
  53. } );
  54. } );
  55. describe( 'init()', () => {
  56. it( 'should return a promise', () => {
  57. expect( collection.init() ).to.be.instanceof( Promise );
  58. } );
  59. it( 'should throw if already initialized', () => {
  60. return collection.init()
  61. .then( () => {
  62. collection.init();
  63. throw new Error( 'This should not be executed.' );
  64. } )
  65. .catch( err => {
  66. expect( err ).to.be.instanceof( CKEditorError );
  67. expect( err.message ).to.match( /ui-viewcollection-init-reinit/ );
  68. } );
  69. } );
  70. it( 'calls #init on all views in the collection', () => {
  71. const viewA = new View();
  72. const viewB = new View();
  73. viewA.element = document.createElement( 'a' );
  74. viewB.element = document.createElement( 'b' );
  75. const spyA = testUtils.sinon.spy( viewA, 'init' );
  76. const spyB = testUtils.sinon.spy( viewB, 'init' );
  77. collection.setParent( document.body );
  78. collection.add( viewA );
  79. collection.add( viewB );
  80. return collection.init().then( () => {
  81. sinon.assert.calledOnce( spyA );
  82. sinon.assert.calledOnce( spyB );
  83. sinon.assert.callOrder( spyA, spyB );
  84. expect( viewA.element.parentNode ).to.equal( collection._parentElement );
  85. expect( viewA.element.nextSibling ).to.equal( viewB.element );
  86. expect( collection.ready ).to.be.true;
  87. } );
  88. } );
  89. } );
  90. describe( 'destroy()', () => {
  91. it( 'should return a promise', () => {
  92. expect( collection.destroy() ).to.be.instanceof( Promise );
  93. } );
  94. it( 'calls #destroy on all views in the collection', () => {
  95. const viewA = new View();
  96. const viewB = new View();
  97. const spyA = testUtils.sinon.spy( viewA, 'destroy' );
  98. const spyB = testUtils.sinon.spy( viewB, 'destroy' );
  99. collection.add( viewA );
  100. collection.add( viewB );
  101. return collection.destroy().then( () => {
  102. sinon.assert.calledOnce( spyA );
  103. sinon.assert.calledOnce( spyB );
  104. sinon.assert.callOrder( spyA, spyB );
  105. } );
  106. } );
  107. } );
  108. describe( 'add()', () => {
  109. it( 'returns a promise', () => {
  110. expect( collection.add( {} ) ).to.be.instanceof( Promise );
  111. } );
  112. it( 'initializes the new view in the collection', () => {
  113. let view = new View();
  114. let spy = testUtils.sinon.spy( view, 'init' );
  115. expect( collection.ready ).to.be.false;
  116. expect( view.ready ).to.be.false;
  117. return collection.add( view ).then( () => {
  118. expect( collection.ready ).to.be.false;
  119. expect( view.ready ).to.be.false;
  120. sinon.assert.notCalled( spy );
  121. view = new View();
  122. spy = testUtils.sinon.spy( view, 'init' );
  123. collection.ready = true;
  124. return collection.add( view ).then( () => {
  125. expect( view.ready ).to.be.true;
  126. sinon.assert.calledOnce( spy );
  127. } );
  128. } );
  129. } );
  130. it( 'works for a view with a Number view#id attribute', () => {
  131. const view = new View();
  132. view.set( 'id', 1 );
  133. return collection.add( view ).then( () => {
  134. expect( view.id ).to.equal( 1 );
  135. expect( view.viewUid ).to.be.a( 'string' );
  136. } );
  137. } );
  138. } );
  139. describe( 'setParent', () => {
  140. it( 'sets #_parentElement', () => {
  141. const el = {};
  142. expect( collection._parentElement ).to.be.null;
  143. collection.setParent( el );
  144. expect( collection._parentElement ).to.equal( el );
  145. } );
  146. } );
  147. describe( 'bindTo()', () => {
  148. class ViewClass extends View {
  149. constructor( locale, data ) {
  150. super( locale );
  151. this.template = new Template( {
  152. tag: 'b'
  153. } );
  154. this.data = data;
  155. }
  156. }
  157. it( 'provides "as()" interface', () => {
  158. const returned = collection.bindTo( {} );
  159. expect( returned ).to.have.keys( 'as' );
  160. expect( returned.as ).to.be.a( 'function' );
  161. } );
  162. describe( 'as()', () => {
  163. it( 'does not chain', () => {
  164. const returned = collection.bindTo( new Collection() ).as( ViewClass );
  165. expect( returned ).to.be.undefined;
  166. } );
  167. it( 'binds collection as a view factory – initial content', () => {
  168. const locale = {};
  169. const items = new Collection();
  170. items.add( { id: '1' } );
  171. items.add( { id: '2' } );
  172. collection = new ViewCollection( locale );
  173. collection.bindTo( items ).as( ViewClass );
  174. expect( collection ).to.have.length( 2 );
  175. expect( collection.get( 0 ) ).to.be.instanceOf( ViewClass );
  176. expect( collection.get( 1 ) ).to.be.instanceOf( ViewClass );
  177. expect( collection.get( 0 ).locale ).to.equal( locale );
  178. expect( collection.get( 1 ).data ).to.equal( items.get( 1 ) );
  179. } );
  180. it( 'binds collection as a view factory – new content', () => {
  181. const locale = {};
  182. const items = new Collection();
  183. collection = new ViewCollection( locale );
  184. collection.bindTo( items ).as( ViewClass );
  185. expect( collection ).to.have.length( 0 );
  186. items.add( { id: '1' } );
  187. items.add( { id: '2' } );
  188. expect( collection ).to.have.length( 2 );
  189. expect( collection.get( 0 ) ).to.be.instanceOf( ViewClass );
  190. expect( collection.get( 1 ) ).to.be.instanceOf( ViewClass );
  191. expect( collection.get( 0 ).locale ).to.equal( locale );
  192. expect( collection.get( 1 ).data ).to.equal( items.get( 1 ) );
  193. } );
  194. it( 'binds collection as a view factory – item removal', () => {
  195. const locale = {};
  196. const items = new Collection();
  197. collection = new ViewCollection( locale );
  198. collection.bindTo( items ).as( ViewClass );
  199. expect( collection ).to.have.length( 0 );
  200. items.add( { id: '1' } );
  201. items.add( { id: '2' } );
  202. expect( collection ).to.have.length( 2 );
  203. expect( collection.get( 0 ) ).to.be.instanceOf( ViewClass );
  204. expect( collection.get( 1 ) ).to.be.instanceOf( ViewClass );
  205. expect( collection.get( 0 ).locale ).to.equal( locale );
  206. expect( collection.get( 1 ).data ).to.equal( items.get( 1 ) );
  207. items.remove( 1 );
  208. expect( collection.get( 0 ).data ).to.equal( items.get( 0 ) );
  209. items.remove( 0 );
  210. expect( collection ).to.have.length( 0 );
  211. } );
  212. it( 'binds collection as a view factory – custom factory (arrow function)', () => {
  213. const locale = {};
  214. const items = new Collection();
  215. collection = new ViewCollection( locale );
  216. collection.bindTo( items ).as( ( item ) => {
  217. return new ViewClass( locale, item );
  218. } );
  219. expect( collection ).to.have.length( 0 );
  220. items.add( { id: '1' } );
  221. items.add( { id: '2' } );
  222. expect( collection ).to.have.length( 2 );
  223. expect( collection.get( 0 ) ).to.be.instanceOf( ViewClass );
  224. expect( collection.get( 1 ) ).to.be.instanceOf( ViewClass );
  225. expect( collection.get( 0 ).locale ).to.equal( locale );
  226. expect( collection.get( 1 ).data ).to.equal( items.get( 1 ) );
  227. } );
  228. // https://github.com/ckeditor/ckeditor5-ui/issues/113
  229. it( 'binds collection as a view factory – custom factory (normal function)', () => {
  230. const locale = { locale: true };
  231. const items = new Collection();
  232. collection = new ViewCollection( locale );
  233. collection.bindTo( items ).as( function( item ) {
  234. return new ViewClass( locale, item );
  235. } );
  236. items.add( { id: '1' } );
  237. expect( collection ).to.have.length( 1 );
  238. const view = collection.get( 0 );
  239. // Wrong args will be passed to the callback if it's treated as the view constructor.
  240. expect( view ).to.be.instanceOf( ViewClass );
  241. expect( view.locale ).to.equal( locale );
  242. expect( view.data ).to.equal( items.get( 0 ) );
  243. } );
  244. } );
  245. } );
  246. describe( 'delegate()', () => {
  247. it( 'should throw when event names are not strings', () => {
  248. expect( () => {
  249. collection.delegate();
  250. } ).to.throw( CKEditorError, /ui-viewcollection-delegate-wrong-events/ );
  251. expect( () => {
  252. collection.delegate( new Date() );
  253. } ).to.throw( CKEditorError, /ui-viewcollection-delegate-wrong-events/ );
  254. expect( () => {
  255. collection.delegate( 'color', new Date() );
  256. } ).to.throw( CKEditorError, /ui-viewcollection-delegate-wrong-events/ );
  257. } );
  258. it( 'returns object', () => {
  259. expect( collection.delegate( 'foo' ) ).to.be.an( 'object' );
  260. } );
  261. it( 'provides "to()" interface', () => {
  262. const delegate = collection.delegate( 'foo' );
  263. expect( delegate ).to.have.keys( 'to' );
  264. expect( delegate.to ).to.be.a( 'function' );
  265. } );
  266. describe( 'to()', () => {
  267. it( 'does not chain', () => {
  268. const returned = collection.delegate( 'foo' ).to( {} );
  269. expect( returned ).to.be.undefined;
  270. } );
  271. it( 'forwards an event to another observable – existing view', ( done ) => {
  272. const target = new View();
  273. const view = new View();
  274. collection.add( view );
  275. collection.delegate( 'foo' ).to( target );
  276. target.on( 'foo', ( ...args ) => {
  277. assertDelegated( args, {
  278. expectedName: 'foo',
  279. expectedSource: view,
  280. expectedPath: [ view, target ],
  281. expectedData: []
  282. } );
  283. done();
  284. } );
  285. view.fire( 'foo' );
  286. } );
  287. it( 'forwards an event to another observable – new view', ( done ) => {
  288. const target = new View();
  289. const view = new View();
  290. collection.delegate( 'foo' ).to( target );
  291. collection.add( view );
  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 multiple events to another observable', () => {
  304. const target = new View();
  305. const viewA = new View();
  306. const viewB = new View();
  307. const viewC = new View();
  308. const spyFoo = sinon.spy();
  309. const spyBar = sinon.spy();
  310. const spyBaz = sinon.spy();
  311. collection.delegate( 'foo', 'bar', 'baz' ).to( target );
  312. collection.add( viewA );
  313. collection.add( viewB );
  314. collection.add( viewC );
  315. target.on( 'foo', spyFoo );
  316. target.on( 'bar', spyBar );
  317. target.on( 'baz', spyBaz );
  318. viewA.fire( 'foo' );
  319. sinon.assert.calledOnce( spyFoo );
  320. sinon.assert.notCalled( spyBar );
  321. sinon.assert.notCalled( spyBaz );
  322. assertDelegated( spyFoo.args[ 0 ], {
  323. expectedName: 'foo',
  324. expectedSource: viewA,
  325. expectedPath: [ viewA, target ],
  326. expectedData: []
  327. } );
  328. viewB.fire( 'bar' );
  329. sinon.assert.calledOnce( spyFoo );
  330. sinon.assert.calledOnce( spyBar );
  331. sinon.assert.notCalled( spyBaz );
  332. assertDelegated( spyBar.args[ 0 ], {
  333. expectedName: 'bar',
  334. expectedSource: viewB,
  335. expectedPath: [ viewB, target ],
  336. expectedData: []
  337. } );
  338. viewC.fire( 'baz' );
  339. sinon.assert.calledOnce( spyFoo );
  340. sinon.assert.calledOnce( spyBar );
  341. sinon.assert.calledOnce( spyBaz );
  342. assertDelegated( spyBaz.args[ 0 ], {
  343. expectedName: 'baz',
  344. expectedSource: viewC,
  345. expectedPath: [ viewC, target ],
  346. expectedData: []
  347. } );
  348. viewC.fire( 'not-delegated' );
  349. sinon.assert.calledOnce( spyFoo );
  350. sinon.assert.calledOnce( spyBar );
  351. sinon.assert.calledOnce( spyBaz );
  352. } );
  353. it( 'does not forward events which are not supposed to be delegated', () => {
  354. const target = new View();
  355. const view = new View();
  356. const spyFoo = sinon.spy();
  357. const spyBar = sinon.spy();
  358. const spyBaz = sinon.spy();
  359. collection.delegate( 'foo', 'bar', 'baz' ).to( target );
  360. collection.add( view );
  361. target.on( 'foo', spyFoo );
  362. target.on( 'bar', spyBar );
  363. target.on( 'baz', spyBaz );
  364. view.fire( 'foo' );
  365. view.fire( 'bar' );
  366. view.fire( 'baz' );
  367. view.fire( 'not-delegated' );
  368. sinon.assert.callOrder( spyFoo, spyBar, spyBaz );
  369. sinon.assert.callCount( spyFoo, 1 );
  370. sinon.assert.callCount( spyBar, 1 );
  371. sinon.assert.callCount( spyBaz, 1 );
  372. } );
  373. it( 'stops forwarding when view removed from the collection', () => {
  374. const target = new View();
  375. const view = new View();
  376. const spy = sinon.spy();
  377. collection.delegate( 'foo' ).to( target );
  378. target.on( 'foo', spy );
  379. collection.add( view );
  380. view.fire( 'foo' );
  381. sinon.assert.callCount( spy, 1 );
  382. collection.remove( 0 );
  383. view.fire( 'foo' );
  384. sinon.assert.callCount( spy, 1 );
  385. } );
  386. it( 'supports deep event delegation', ( done ) => {
  387. const target = new View();
  388. const viewA = new View();
  389. const viewAA = new View();
  390. const data = {};
  391. const deepCollection = viewA.createCollection();
  392. collection.add( viewA );
  393. collection.delegate( 'foo' ).to( target );
  394. deepCollection.add( viewAA );
  395. deepCollection.delegate( 'foo' ).to( viewA );
  396. target.on( 'foo', ( ...args ) => {
  397. assertDelegated( args, {
  398. expectedName: 'foo',
  399. expectedSource: viewAA,
  400. expectedPath: [ viewAA, viewA, target ],
  401. expectedData: [ data ]
  402. } );
  403. done();
  404. } );
  405. viewAA.fire( 'foo', data );
  406. } );
  407. } );
  408. } );
  409. } );
  410. function createTestCollection() {
  411. collection = new ViewCollection();
  412. }
  413. function assertDelegated( evtArgs, { expectedName, expectedSource, expectedPath, expectedData } ) {
  414. const evtInfo = evtArgs[ 0 ];
  415. expect( evtInfo.name ).to.equal( expectedName );
  416. expect( evtInfo.source ).to.equal( expectedSource );
  417. expect( evtInfo.path ).to.deep.equal( expectedPath );
  418. expect( evtArgs.slice( 1 ) ).to.deep.equal( expectedData );
  419. }