viewcollection.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506
  1. /**
  2. * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* global document */
  6. /* bender-tags: ui */
  7. import CKEditorError from '/ckeditor5/utils/ckeditorerror.js';
  8. import Collection from '/ckeditor5/utils/collection.js';
  9. import testUtils from '/tests/core/_utils/utils.js';
  10. import View from '/ckeditor5/ui/view.js';
  11. import ViewCollection from '/ckeditor5/ui/viewcollection.js';
  12. import Template from '/ckeditor5/ui/template.js';
  13. import normalizeHtml from '/tests/utils/_utils/normalizehtml.js';
  14. let collection;
  15. testUtils.createSinonSandbox();
  16. describe( 'ViewCollection', () => {
  17. beforeEach( createTestCollection );
  18. describe( 'constructor', () => {
  19. it( 'sets basic properties and attributes', () => {
  20. expect( collection.locale ).to.be.undefined;
  21. expect( collection.ready ).to.be.false;
  22. expect( collection._parentElement ).to.be.null;
  23. expect( collection._boundItemsToViewsMap ).to.be.instanceOf( Map );
  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( 'calls #init on all views in the collection', () => {
  60. const viewA = new View();
  61. const viewB = new View();
  62. viewA.element = document.createElement( 'a' );
  63. viewB.element = document.createElement( 'b' );
  64. const spyA = testUtils.sinon.spy( viewA, 'init' );
  65. const spyB = testUtils.sinon.spy( viewB, 'init' );
  66. collection.setParent( document.body );
  67. collection.add( viewA );
  68. collection.add( viewB );
  69. return collection.init().then( () => {
  70. sinon.assert.calledOnce( spyA );
  71. sinon.assert.calledOnce( spyB );
  72. sinon.assert.callOrder( spyA, spyB );
  73. expect( viewA.element.parentNode ).to.equal( collection._parentElement );
  74. expect( viewA.element.nextSibling ).to.equal( viewB.element );
  75. } );
  76. } );
  77. } );
  78. describe( 'destroy', () => {
  79. it( 'should return a promise', () => {
  80. expect( collection.destroy() ).to.be.instanceof( Promise );
  81. } );
  82. it( 'calls #destroy on all views in the collection', () => {
  83. const viewA = new View();
  84. const viewB = new View();
  85. const spyA = testUtils.sinon.spy( viewA, 'destroy' );
  86. const spyB = testUtils.sinon.spy( viewB, 'destroy' );
  87. collection.add( viewA );
  88. collection.add( viewB );
  89. return collection.destroy().then( () => {
  90. sinon.assert.calledOnce( spyA );
  91. sinon.assert.calledOnce( spyB );
  92. sinon.assert.callOrder( spyA, spyB );
  93. } );
  94. } );
  95. } );
  96. describe( 'add', () => {
  97. it( 'returns a promise', () => {
  98. expect( collection.add( {} ) ).to.be.instanceof( Promise );
  99. } );
  100. it( 'initializes the new view in the collection', () => {
  101. let view = new View();
  102. let spy = testUtils.sinon.spy( view, 'init' );
  103. expect( collection.ready ).to.be.false;
  104. expect( view.ready ).to.be.false;
  105. return collection.add( view ).then( () => {
  106. expect( collection.ready ).to.be.false;
  107. expect( view.ready ).to.be.false;
  108. sinon.assert.notCalled( spy );
  109. view = new View();
  110. spy = testUtils.sinon.spy( view, 'init' );
  111. collection.ready = true;
  112. return collection.add( view ).then( () => {
  113. expect( view.ready ).to.be.true;
  114. sinon.assert.calledOnce( spy );
  115. } );
  116. } );
  117. } );
  118. } );
  119. describe( 'setParent', () => {
  120. it( 'sets #_parentElement', () => {
  121. const el = {};
  122. expect( collection._parentElement ).to.be.null;
  123. collection.setParent( el );
  124. expect( collection._parentElement ).to.equal( el );
  125. } );
  126. } );
  127. describe( 'bindTo', () => {
  128. class ViewClass extends View {
  129. constructor( locale, data ) {
  130. super( locale );
  131. this.template = new Template( {
  132. tag: 'b'
  133. } );
  134. this.data = data;
  135. }
  136. }
  137. it( 'provides "as" interface', () => {
  138. const returned = collection.bindTo( {} );
  139. expect( returned ).to.have.keys( 'as' );
  140. expect( returned.as ).to.be.a( 'function' );
  141. } );
  142. describe( 'as', () => {
  143. it( 'does not chain', () => {
  144. const returned = collection.bindTo( new Collection() ).as( ViewClass );
  145. expect( returned ).to.be.undefined;
  146. } );
  147. it( 'binds collection as a view factory – initial content', () => {
  148. const locale = {};
  149. const items = new Collection();
  150. items.add( { id: '1' } );
  151. items.add( { id: '2' } );
  152. collection = new ViewCollection( locale );
  153. collection.bindTo( items ).as( ViewClass );
  154. expect( collection ).to.have.length( 2 );
  155. expect( collection.get( 0 ) ).to.be.instanceOf( ViewClass );
  156. expect( collection.get( 1 ) ).to.be.instanceOf( ViewClass );
  157. expect( collection.get( 0 ).locale ).to.equal( locale );
  158. expect( collection.get( 1 ).data ).to.equal( items.get( 1 ) );
  159. } );
  160. it( 'binds collection as a view factory – new content', () => {
  161. const locale = {};
  162. const items = new Collection();
  163. collection = new ViewCollection( locale );
  164. collection.bindTo( items ).as( ViewClass );
  165. expect( collection ).to.have.length( 0 );
  166. items.add( { id: '1' } );
  167. items.add( { id: '2' } );
  168. expect( collection ).to.have.length( 2 );
  169. expect( collection.get( 0 ) ).to.be.instanceOf( ViewClass );
  170. expect( collection.get( 1 ) ).to.be.instanceOf( ViewClass );
  171. expect( collection.get( 0 ).locale ).to.equal( locale );
  172. expect( collection.get( 1 ).data ).to.equal( items.get( 1 ) );
  173. } );
  174. it( 'binds collection as a view factory – item removal', () => {
  175. const locale = {};
  176. const items = new Collection();
  177. collection = new ViewCollection( locale );
  178. collection.bindTo( items ).as( ViewClass );
  179. expect( collection ).to.have.length( 0 );
  180. items.add( { id: '1' } );
  181. items.add( { id: '2' } );
  182. expect( collection ).to.have.length( 2 );
  183. expect( collection.get( 0 ) ).to.be.instanceOf( ViewClass );
  184. expect( collection.get( 1 ) ).to.be.instanceOf( ViewClass );
  185. expect( collection.get( 0 ).locale ).to.equal( locale );
  186. expect( collection.get( 1 ).data ).to.equal( items.get( 1 ) );
  187. items.remove( 1 );
  188. expect( collection.get( 0 ).data ).to.equal( items.get( 0 ) );
  189. items.remove( 0 );
  190. expect( collection ).to.have.length( 0 );
  191. } );
  192. it( 'binds collection as a view factory – custom factory', () => {
  193. const locale = {};
  194. const items = new Collection();
  195. collection = new ViewCollection( locale );
  196. collection.bindTo( items ).as( ( item ) => {
  197. return new ViewClass( locale, item );
  198. } );
  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. } );
  208. } );
  209. } );
  210. describe( 'delegate', () => {
  211. it( 'should throw when event names are not strings', () => {
  212. expect( () => {
  213. collection.delegate();
  214. } ).to.throw( CKEditorError, /ui-viewcollection-delegate-wrong-events/ );
  215. expect( () => {
  216. collection.delegate( new Date() );
  217. } ).to.throw( CKEditorError, /ui-viewcollection-delegate-wrong-events/ );
  218. expect( () => {
  219. collection.delegate( 'color', new Date() );
  220. } ).to.throw( CKEditorError, /ui-viewcollection-delegate-wrong-events/ );
  221. } );
  222. it( 'returns object', () => {
  223. expect( collection.delegate( 'foo' ) ).to.be.an( 'object' );
  224. } );
  225. it( 'provides "to" interface', () => {
  226. const delegate = collection.delegate( 'foo' );
  227. expect( delegate ).to.have.keys( 'to' );
  228. expect( delegate.to ).to.be.a( 'function' );
  229. } );
  230. describe( 'to', () => {
  231. it( 'does not chain', () => {
  232. const returned = collection.delegate( 'foo' ).to( {} );
  233. expect( returned ).to.be.undefined;
  234. } );
  235. it( 'forwards an event to another observable – existing view', ( done ) => {
  236. const target = new View();
  237. const view = new View();
  238. collection.add( view );
  239. collection.delegate( 'foo' ).to( target );
  240. target.on( 'foo', ( ...args ) => {
  241. assertDelegated( args, {
  242. expectedName: 'foo',
  243. expectedSource: view,
  244. expectedPath: [ view, target ],
  245. expectedData: []
  246. } );
  247. done();
  248. } );
  249. view.fire( 'foo' );
  250. } );
  251. it( 'forwards an event to another observable – new view', ( done ) => {
  252. const target = new View();
  253. const view = new View();
  254. collection.delegate( 'foo' ).to( target );
  255. collection.add( view );
  256. target.on( 'foo', ( ...args ) => {
  257. assertDelegated( args, {
  258. expectedName: 'foo',
  259. expectedSource: view,
  260. expectedPath: [ view, target ],
  261. expectedData: []
  262. } );
  263. done();
  264. } );
  265. view.fire( 'foo' );
  266. } );
  267. it( 'forwards multiple events to another observable', () => {
  268. const target = new View();
  269. const viewA = new View();
  270. const viewB = new View();
  271. const viewC = new View();
  272. const spyFoo = sinon.spy();
  273. const spyBar = sinon.spy();
  274. const spyBaz = sinon.spy();
  275. collection.delegate( 'foo', 'bar', 'baz' ).to( target );
  276. collection.add( viewA );
  277. collection.add( viewB );
  278. collection.add( viewC );
  279. target.on( 'foo', spyFoo );
  280. target.on( 'bar', spyBar );
  281. target.on( 'baz', spyBaz );
  282. viewA.fire( 'foo' );
  283. sinon.assert.calledOnce( spyFoo );
  284. sinon.assert.notCalled( spyBar );
  285. sinon.assert.notCalled( spyBaz );
  286. assertDelegated( spyFoo.args[ 0 ], {
  287. expectedName: 'foo',
  288. expectedSource: viewA,
  289. expectedPath: [ viewA, target ],
  290. expectedData: []
  291. } );
  292. viewB.fire( 'bar' );
  293. sinon.assert.calledOnce( spyFoo );
  294. sinon.assert.calledOnce( spyBar );
  295. sinon.assert.notCalled( spyBaz );
  296. assertDelegated( spyBar.args[ 0 ], {
  297. expectedName: 'bar',
  298. expectedSource: viewB,
  299. expectedPath: [ viewB, target ],
  300. expectedData: []
  301. } );
  302. viewC.fire( 'baz' );
  303. sinon.assert.calledOnce( spyFoo );
  304. sinon.assert.calledOnce( spyBar );
  305. sinon.assert.calledOnce( spyBaz );
  306. assertDelegated( spyBaz.args[ 0 ], {
  307. expectedName: 'baz',
  308. expectedSource: viewC,
  309. expectedPath: [ viewC, target ],
  310. expectedData: []
  311. } );
  312. viewC.fire( 'not-delegated' );
  313. sinon.assert.calledOnce( spyFoo );
  314. sinon.assert.calledOnce( spyBar );
  315. sinon.assert.calledOnce( spyBaz );
  316. } );
  317. it( 'does not forward events which are not supposed to be delegated', () => {
  318. const target = new View();
  319. const view = new View();
  320. const spyFoo = sinon.spy();
  321. const spyBar = sinon.spy();
  322. const spyBaz = sinon.spy();
  323. collection.delegate( 'foo', 'bar', 'baz' ).to( target );
  324. collection.add( view );
  325. target.on( 'foo', spyFoo );
  326. target.on( 'bar', spyBar );
  327. target.on( 'baz', spyBaz );
  328. view.fire( 'foo' );
  329. view.fire( 'bar' );
  330. view.fire( 'baz' );
  331. view.fire( 'not-delegated' );
  332. sinon.assert.callOrder( spyFoo, spyBar, spyBaz );
  333. sinon.assert.callCount( spyFoo, 1 );
  334. sinon.assert.callCount( spyBar, 1 );
  335. sinon.assert.callCount( spyBaz, 1 );
  336. } );
  337. it( 'stops forwarding when view removed from the collection', () => {
  338. const target = new View();
  339. const view = new View();
  340. const spy = sinon.spy();
  341. collection.delegate( 'foo' ).to( target );
  342. target.on( 'foo', spy );
  343. collection.add( view );
  344. view.fire( 'foo' );
  345. sinon.assert.callCount( spy, 1 );
  346. collection.remove( 0 );
  347. view.fire( 'foo' );
  348. sinon.assert.callCount( spy, 1 );
  349. } );
  350. it( 'supports deep event delegation', ( done ) => {
  351. const target = new View();
  352. const viewA = new View();
  353. const viewAA = new View();
  354. const data = {};
  355. const deepCollection = viewA.createCollection();
  356. collection.add( viewA );
  357. collection.delegate( 'foo' ).to( target );
  358. deepCollection.add( viewAA );
  359. deepCollection.delegate( 'foo' ).to( viewA );
  360. target.on( 'foo', ( ...args ) => {
  361. assertDelegated( args, {
  362. expectedName: 'foo',
  363. expectedSource: viewAA,
  364. expectedPath: [ viewAA, viewA, target ],
  365. expectedData: [ data ]
  366. } );
  367. done();
  368. } );
  369. viewAA.fire( 'foo', data );
  370. } );
  371. } );
  372. } );
  373. } );
  374. function createTestCollection() {
  375. collection = new ViewCollection();
  376. }
  377. function assertDelegated( evtArgs, { expectedName, expectedSource, expectedPath, expectedData } ) {
  378. const evtInfo = evtArgs[ 0 ];
  379. expect( evtInfo.name ).to.equal( expectedName );
  380. expect( evtInfo.source ).to.equal( expectedSource );
  381. expect( evtInfo.path ).to.deep.equal( expectedPath );
  382. expect( evtArgs.slice( 1 ) ).to.deep.equal( expectedData );
  383. }