view.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808
  1. /**
  2. * @license Copyright (c) 2003-2015, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* global document, HTMLElement */
  6. /* bender-tags: core, ui */
  7. 'use strict';
  8. const modules = bender.amd.require(
  9. 'ckeditor',
  10. 'ui/view',
  11. 'ui/region',
  12. 'ckeditorerror',
  13. 'model',
  14. 'eventinfo'
  15. );
  16. let View, TestView, Model, Region, CKEditorError;
  17. let view;
  18. bender.tools.createSinonSandbox();
  19. describe( 'View', () => {
  20. beforeEach( updateModuleReference );
  21. describe( 'constructor', () => {
  22. beforeEach( () => {
  23. setTestViewClass();
  24. setTestViewInstance();
  25. } );
  26. it( 'accepts the model', () => {
  27. setTestViewInstance( { a: 'foo', b: 42 } );
  28. expect( view.model ).to.be.an.instanceof( modules.model );
  29. expect( view ).to.have.deep.property( 'model.a', 'foo' );
  30. expect( view ).to.have.deep.property( 'model.b', 42 );
  31. } );
  32. it( 'defines basic view properties', () => {
  33. view = new View();
  34. expect( view.model ).to.be.null;
  35. expect( view.regions.length ).to.be.equal( 0 );
  36. expect( view.template ).to.be.null;
  37. expect( view._regionsSelectors ).to.be.empty;
  38. expect( view._el ).to.be.null;
  39. expect( view._template ).to.be.null;
  40. expect( () => {
  41. view.el;
  42. } ).to.throw( CKEditorError, /ui-view-notemplate/ );
  43. } );
  44. } );
  45. describe( 'init', () => {
  46. beforeEach( () => {
  47. setTestViewClass( () => ( {
  48. tag: 'p',
  49. children: [
  50. { tag: 'span' },
  51. { tag: 'strong' }
  52. ]
  53. } ) );
  54. } );
  55. it( 'calls child regions #init', () => {
  56. setTestViewInstance();
  57. const region1 = new Region( 'x' );
  58. const region2 = new Region( 'y' );
  59. view.register( region1, el => el );
  60. view.register( region2, el => el );
  61. const spy1 = bender.sinon.spy( region1, 'init' );
  62. const spy2 = bender.sinon.spy( region2, 'init' );
  63. view.init();
  64. sinon.assert.calledOnce( spy1 );
  65. sinon.assert.calledOnce( spy2 );
  66. } );
  67. it( 'initializes view regions with string selector', () => {
  68. setTestViewInstance();
  69. const region1 = new Region( 'x' );
  70. const region2 = new Region( 'y' );
  71. view.register( region1, 'span' );
  72. view.register( region2, 'strong' );
  73. view.init();
  74. expect( region1.el ).to.be.equal( view.el.firstChild );
  75. expect( region2.el ).to.be.equal( view.el.lastChild );
  76. } );
  77. it( 'initializes view regions with function selector', () => {
  78. setTestViewInstance();
  79. const region1 = new Region( 'x' );
  80. const region2 = new Region( 'y' );
  81. view.register( region1, el => el.firstChild );
  82. view.register( region2, el => el.lastChild );
  83. view.init();
  84. expect( region1.el ).to.be.equal( view.el.firstChild );
  85. expect( region2.el ).to.be.equal( view.el.lastChild );
  86. } );
  87. it( 'initializes view regions with boolean selector', () => {
  88. setTestViewInstance();
  89. const region1 = new Region( 'x' );
  90. const region2 = new Region( 'y' );
  91. view.register( region1, true );
  92. view.register( region2, true );
  93. view.init();
  94. expect( region1.el ).to.be.null;
  95. expect( region2.el ).to.be.null;
  96. } );
  97. } );
  98. describe( 'addChild', () => {
  99. beforeEach( () => {
  100. setTestViewClass(
  101. () => ( { tag: 'p', text: 'x' } ),
  102. function() {
  103. this.register( 'x', el => el );
  104. }
  105. );
  106. setTestViewInstance();
  107. } );
  108. it( 'should throw when no region name', () => {
  109. expect( () => {
  110. view.addChild();
  111. } ).to.throw( CKEditorError, /ui-view-addchild-badrname/ );
  112. } );
  113. it( 'should throw when region does not exist', () => {
  114. expect( () => {
  115. view.addChild( 'nonexistent' );
  116. } ).to.throw( CKEditorError, /ui-view-addchild-noreg/ );
  117. } );
  118. it( 'should throw when child view is of a wrong type', () => {
  119. expect( () => {
  120. view.addChild( 'x' );
  121. } ).to.throw( CKEditorError, /ui-view-addchild-badtype/ );
  122. expect( () => {
  123. view.addChild( 'x', new Date() );
  124. } ).to.throw( CKEditorError, /ui-view-addchild-badtype/ );
  125. } );
  126. it( 'should add a child to the region views', () => {
  127. const childView = new View();
  128. view.addChild( 'x', childView );
  129. expect( view.getChild( 'x', 0 ) ).to.be.equal( childView );
  130. } );
  131. it( 'should add a child to the region views at the right index', () => {
  132. const childView1 = new View();
  133. const childView2 = new View();
  134. view.addChild( 'x', childView1 );
  135. view.addChild( 'x', childView2, 0 );
  136. expect( view.getChild( 'x', 0 ) ).to.be.equal( childView2 );
  137. expect( view.getChild( 'x', 1 ) ).to.be.equal( childView1 );
  138. } );
  139. } );
  140. describe( 'removeChild', () => {
  141. beforeEach( () => {
  142. setTestViewClass(
  143. () => ( { tag: 'p', text: 'x' } ),
  144. function() {
  145. this.register( 'x', el => el );
  146. }
  147. );
  148. setTestViewInstance();
  149. } );
  150. it( 'should throw when child view is of a wrong type', () => {
  151. expect( () => {
  152. view.removeChild( 'x' );
  153. } ).to.throw( CKEditorError, /ui-view-removechild-badtype/ );
  154. expect( () => {
  155. view.removeChild( 'x', new Date() );
  156. } ).to.throw( CKEditorError, /ui-view-removechild-badtype/ );
  157. } );
  158. it( 'should throw when region does not exist', () => {
  159. expect( () => {
  160. view.removeChild( 'nonexistent', new View() );
  161. } ).to.throw( CKEditorError, /ui-view-removechild-noreg/ );
  162. } );
  163. it( 'should remove a child from the region views', () => {
  164. const childView1 = new View();
  165. const childView2 = new View();
  166. view.addChild( 'x', childView1 );
  167. view.addChild( 'x', childView2, 0 );
  168. expect( view.getChild( 'x', 0 ) ).to.be.equal( childView2 );
  169. expect( view.getChild( 'x', 1 ) ).to.be.equal( childView1 );
  170. view.removeChild( 'x', childView2 );
  171. expect( view.getChild( 'x', 0 ) ).to.be.equal( childView1 );
  172. view.removeChild( 'x', childView1 );
  173. expect( view.regions.get( 'x' ).views.length ).to.be.equal( 0 );
  174. } );
  175. } );
  176. describe( 'getChild', () => {
  177. beforeEach( () => {
  178. setTestViewClass(
  179. () => ( { tag: 'p', text: 'x' } ),
  180. function() {
  181. this.register( 'x', el => el );
  182. }
  183. );
  184. setTestViewInstance();
  185. } );
  186. it( 'should throw when region does not exist', () => {
  187. expect( () => {
  188. view.getChild( 'nonexistent', 0 );
  189. } ).to.throw( CKEditorError, /ui-view-getchild-noreg/ );
  190. } );
  191. it( 'should get a child from the region views', () => {
  192. const childView1 = new View();
  193. const childView2 = new View();
  194. view.addChild( 'x', childView1 );
  195. view.addChild( 'x', childView2, 0 );
  196. expect( view.getChild( 'x', 0 ) ).to.be.equal( childView2 );
  197. expect( view.getChild( 'x', 1 ) ).to.be.equal( childView1 );
  198. } );
  199. } );
  200. describe( 'register', () => {
  201. beforeEach( () => {
  202. setTestViewClass();
  203. setTestViewInstance();
  204. } );
  205. it( 'should throw when first argument is neither Region instance nor string', () => {
  206. expect( () => {
  207. view.register( new Date() );
  208. } ).to.throw( CKEditorError, /ui-view-register-wrongtype/ );
  209. } );
  210. it( 'should throw when missing the selector argument', () => {
  211. expect( () => {
  212. view.register( 'x' );
  213. } ).to.throw( CKEditorError, /ui-view-register-badselector/ );
  214. } );
  215. it( 'should throw when selector argument is of a wrong type', () => {
  216. expect( () => {
  217. view.register( 'x', new Date() );
  218. } ).to.throw( CKEditorError, /ui-view-register-badselector/ );
  219. expect( () => {
  220. view.register( 'x', false );
  221. } ).to.throw( CKEditorError, /ui-view-register-badselector/ );
  222. } );
  223. it( 'should throw when overriding an existing region but without override flag set', () => {
  224. expect( () => {
  225. view.register( 'x', true );
  226. view.register( new Region( 'x' ), true );
  227. } ).to.throw( CKEditorError, /ui-view-register-override/ );
  228. } );
  229. it( 'should register a new region with region name as a first argument', () => {
  230. view.register( 'x', true );
  231. expect( view.regions.get( 'x' ) ).to.be.an.instanceof( Region );
  232. } );
  233. it( 'should register a new region with Region instance as a first argument', () => {
  234. view.register( new Region( 'y' ), true );
  235. expect( view.regions.get( 'y' ) ).to.be.an.instanceof( Region );
  236. } );
  237. it( 'should override an existing region with override flag', () => {
  238. const region1 = new Region( 'x' );
  239. const region2 = new Region( 'x' );
  240. view.register( region1, true );
  241. view.register( region2, true, true );
  242. view.register( 'x', 'span', true );
  243. expect( view.regions.get( 'x' ) ).to.be.equal( region2 );
  244. expect( view._regionsSelectors.x ).to.be.equal( 'span' );
  245. } );
  246. it( 'should not override an existing region with the same region with override flag', () => {
  247. const region = new Region( 'x' );
  248. const spy = bender.sinon.spy( view.regions, 'remove' );
  249. view.register( region, true );
  250. view.register( region, true, true );
  251. sinon.assert.notCalled( spy );
  252. } );
  253. } );
  254. describe( 'el', () => {
  255. beforeEach( createViewInstanceWithTemplate );
  256. it( 'invokes out of #template', () => {
  257. setTestViewInstance( { a: 1 } );
  258. expect( view.el ).to.be.an.instanceof( HTMLElement );
  259. expect( view.el.nodeName ).to.be.equal( 'A' );
  260. } );
  261. it( 'can be explicitly declared', () => {
  262. class CustomView extends View {
  263. constructor() {
  264. super();
  265. this.el = document.createElement( 'span' );
  266. }
  267. }
  268. view = new CustomView();
  269. expect( view.el ).to.be.an.instanceof( HTMLElement );
  270. } );
  271. } );
  272. describe( 'bindToAttribute', () => {
  273. beforeEach( createViewInstanceWithTemplate );
  274. it( 'returns a function that passes arguments', () => {
  275. setTestViewInstance( { a: 'foo' } );
  276. let spy = bender.sinon.spy();
  277. let callback = view.bindToAttribute( 'a', spy );
  278. expect( spy.called ).to.be.false;
  279. callback( 'el', 'updater' );
  280. sinon.assert.calledOnce( spy );
  281. sinon.assert.calledWithExactly( spy, 'el', 'foo' );
  282. view.model.a = 'bar';
  283. sinon.assert.calledTwice( spy );
  284. expect( spy.secondCall.calledWithExactly( 'el', 'bar' ) ).to.be.true;
  285. } );
  286. it( 'allows binding attribute to the model', () => {
  287. setTestViewClass( function() {
  288. return {
  289. tag: 'p',
  290. attrs: {
  291. 'class': this.bindToAttribute( 'foo' )
  292. },
  293. text: 'abc'
  294. };
  295. } );
  296. setTestViewInstance( { foo: 'bar' } );
  297. expect( view.el.outerHTML ).to.be.equal( '<p class="bar">abc</p>' );
  298. view.model.foo = 'baz';
  299. expect( view.el.outerHTML ).to.be.equal( '<p class="baz">abc</p>' );
  300. } );
  301. it( 'allows binding "text" to the model', () => {
  302. setTestViewClass( function() {
  303. return {
  304. tag: 'p',
  305. children: [
  306. {
  307. tag: 'b',
  308. text: 'baz'
  309. }
  310. ],
  311. text: this.bindToAttribute( 'foo' )
  312. };
  313. } );
  314. setTestViewInstance( { foo: 'bar' } );
  315. expect( view.el.outerHTML ).to.be.equal( '<p>bar<b>baz</b></p>' );
  316. // TODO: A solution to avoid nuking the children?
  317. view.model.foo = 'qux';
  318. expect( view.el.outerHTML ).to.be.equal( '<p>qux</p>' );
  319. } );
  320. it( 'allows binding to the model with value processing', () => {
  321. let callback = ( el, value ) =>
  322. ( value > 0 ? 'positive' : 'negative' );
  323. setTestViewClass( function() {
  324. return {
  325. tag: 'p',
  326. attrs: {
  327. 'class': this.bindToAttribute( 'foo', callback )
  328. },
  329. text: this.bindToAttribute( 'foo', callback )
  330. };
  331. } );
  332. setTestViewInstance( { foo: 3 } );
  333. expect( view.el.outerHTML ).to.be.equal( '<p class="positive">positive</p>' );
  334. view.model.foo = -7;
  335. expect( view.el.outerHTML ).to.be.equal( '<p class="negative">negative</p>' );
  336. } );
  337. it( 'allows binding to the model with custom callback', () => {
  338. setTestViewClass( function() {
  339. return {
  340. tag: 'p',
  341. attrs: {
  342. 'class': this.bindToAttribute( 'foo', ( el, value ) => {
  343. el.innerHTML = value;
  344. if ( value == 'changed' ) {
  345. return value;
  346. }
  347. } )
  348. },
  349. text: 'bar'
  350. };
  351. } );
  352. setTestViewInstance( { foo: 'moo' } );
  353. expect( view.el.outerHTML ).to.be.equal( '<p>moo</p>' );
  354. view.model.foo = 'changed';
  355. expect( view.el.outerHTML ).to.be.equal( '<p class="changed">changed</p>' );
  356. } );
  357. } );
  358. describe( 'on', () => {
  359. it( 'accepts plain binding', () => {
  360. let spy = bender.sinon.spy();
  361. setTestViewClass( function() {
  362. return {
  363. tag: 'p',
  364. on: {
  365. x: 'a',
  366. }
  367. };
  368. } );
  369. setTestViewInstance();
  370. view.on( 'a', spy );
  371. dispatchEvent( view.el, 'x' );
  372. sinon.assert.calledWithExactly( spy,
  373. sinon.match.has( 'name', 'a' ),
  374. sinon.match.has( 'target', view.el )
  375. );
  376. } );
  377. it( 'accepts an array of event bindings', () => {
  378. let spy1 = bender.sinon.spy();
  379. let spy2 = bender.sinon.spy();
  380. setTestViewClass( function() {
  381. return {
  382. tag: 'p',
  383. on: {
  384. x: [ 'a', 'b' ]
  385. }
  386. };
  387. } );
  388. setTestViewInstance();
  389. view.on( 'a', spy1 );
  390. view.on( 'b', spy2 );
  391. dispatchEvent( view.el, 'x' );
  392. sinon.assert.calledWithExactly( spy1,
  393. sinon.match.has( 'name', 'a' ),
  394. sinon.match.has( 'target', view.el )
  395. );
  396. sinon.assert.calledWithExactly( spy2,
  397. sinon.match.has( 'name', 'b' ),
  398. sinon.match.has( 'target', view.el )
  399. );
  400. } );
  401. it( 'accepts DOM selectors', () => {
  402. let spy1 = bender.sinon.spy();
  403. let spy2 = bender.sinon.spy();
  404. let spy3 = bender.sinon.spy();
  405. setTestViewClass( function() {
  406. return {
  407. tag: 'p',
  408. children: [
  409. {
  410. tag: 'span',
  411. attrs: {
  412. 'class': 'y',
  413. },
  414. on: {
  415. 'test@p': 'c'
  416. }
  417. },
  418. {
  419. tag: 'div',
  420. children: [
  421. {
  422. tag: 'span',
  423. attrs: {
  424. 'class': 'y',
  425. }
  426. }
  427. ],
  428. }
  429. ],
  430. on: {
  431. 'test@.y': 'a',
  432. 'test@div': 'b'
  433. }
  434. };
  435. } );
  436. setTestViewInstance();
  437. view.on( 'a', spy1 );
  438. view.on( 'b', spy2 );
  439. view.on( 'c', spy3 );
  440. // Test "test@p".
  441. dispatchEvent( view.el, 'test' );
  442. sinon.assert.callCount( spy1, 0 );
  443. sinon.assert.callCount( spy2, 0 );
  444. sinon.assert.callCount( spy3, 0 );
  445. // Test "test@.y".
  446. dispatchEvent( view.el.firstChild, 'test' );
  447. expect( spy1.firstCall.calledWithExactly(
  448. sinon.match.has( 'name', 'a' ),
  449. sinon.match.has( 'target', view.el.firstChild )
  450. ) ).to.be.true;
  451. sinon.assert.callCount( spy2, 0 );
  452. sinon.assert.callCount( spy3, 0 );
  453. // Test "test@div".
  454. dispatchEvent( view.el.lastChild, 'test' );
  455. sinon.assert.callCount( spy1, 1 );
  456. expect( spy2.firstCall.calledWithExactly(
  457. sinon.match.has( 'name', 'b' ),
  458. sinon.match.has( 'target', view.el.lastChild )
  459. ) ).to.be.true;
  460. sinon.assert.callCount( spy3, 0 );
  461. // Test "test@.y".
  462. dispatchEvent( view.el.lastChild.firstChild, 'test' );
  463. expect( spy1.secondCall.calledWithExactly(
  464. sinon.match.has( 'name', 'a' ),
  465. sinon.match.has( 'target', view.el.lastChild.firstChild )
  466. ) ).to.be.true;
  467. sinon.assert.callCount( spy2, 1 );
  468. sinon.assert.callCount( spy3, 0 );
  469. } );
  470. it( 'accepts function callbacks', () => {
  471. let spy1 = bender.sinon.spy();
  472. let spy2 = bender.sinon.spy();
  473. setTestViewClass( function() {
  474. return {
  475. tag: 'p',
  476. children: [
  477. {
  478. tag: 'span'
  479. }
  480. ],
  481. on: {
  482. x: spy1,
  483. 'y@span': [ spy2, 'c' ],
  484. }
  485. };
  486. } );
  487. setTestViewInstance();
  488. dispatchEvent( view.el, 'x' );
  489. dispatchEvent( view.el.firstChild, 'y' );
  490. sinon.assert.calledWithExactly( spy1,
  491. sinon.match.has( 'target', view.el )
  492. );
  493. sinon.assert.calledWithExactly( spy2,
  494. sinon.match.has( 'target', view.el.firstChild )
  495. );
  496. } );
  497. it( 'supports event delegation', () => {
  498. let spy = bender.sinon.spy();
  499. setTestViewClass( function() {
  500. return {
  501. tag: 'p',
  502. children: [
  503. {
  504. tag: 'span'
  505. }
  506. ],
  507. on: {
  508. x: 'a',
  509. }
  510. };
  511. } );
  512. setTestViewInstance();
  513. view.on( 'a', spy );
  514. dispatchEvent( view.el.firstChild, 'x' );
  515. sinon.assert.calledWithExactly( spy,
  516. sinon.match.has( 'name', 'a' ),
  517. sinon.match.has( 'target', view.el.firstChild )
  518. );
  519. } );
  520. it( 'works for future elements', () => {
  521. let spy = bender.sinon.spy();
  522. setTestViewClass( function() {
  523. return {
  524. tag: 'p',
  525. on: {
  526. 'test@div': 'a'
  527. }
  528. };
  529. } );
  530. setTestViewInstance();
  531. view.on( 'a', spy );
  532. let div = document.createElement( 'div' );
  533. view.el.appendChild( div );
  534. dispatchEvent( div, 'test' );
  535. sinon.assert.calledWithExactly( spy, sinon.match.has( 'name', 'a' ), sinon.match.has( 'target', div ) );
  536. } );
  537. } );
  538. describe( 'destroy', () => {
  539. beforeEach( createViewInstanceWithTemplate );
  540. it( 'should destroy the view', () => {
  541. view.destroy();
  542. expect( view.model ).to.be.null;
  543. expect( view.regions ).to.be.null;
  544. expect( view.template ).to.be.null;
  545. expect( view._regionsSelectors ).to.be.null;
  546. expect( view._el ).to.be.null;
  547. expect( view._template ).to.be.null;
  548. } );
  549. it( 'detaches the element from DOM', () => {
  550. const elRef = view.el;
  551. document.createElement( 'div' ).appendChild( view.el );
  552. view.destroy();
  553. expect( elRef.parentNode ).to.be.null;
  554. } );
  555. it( 'destroys child regions', () => {
  556. const region = new Region( 'x' );
  557. const spy = bender.sinon.spy( region, 'destroy' );
  558. const regionsRef = view.regions;
  559. const regionViewsRef = region.views;
  560. view.register( region, true );
  561. view.addChild( 'x', new View() );
  562. view.destroy();
  563. expect( regionsRef.length ).to.be.equal( 0 );
  564. expect( regionViewsRef.length ).to.be.equal( 0 );
  565. expect( spy.calledOnce ).to.be.true;
  566. } );
  567. it( 'detaches bound model listeners', () => {
  568. setTestViewClass( function() {
  569. return {
  570. tag: 'p',
  571. text: this.bindToAttribute( 'foo' )
  572. };
  573. } );
  574. setTestViewInstance( { foo: 'bar' } );
  575. const modelRef = view.model;
  576. const elRef = view.el;
  577. expect( view.el.outerHTML ).to.be.equal( '<p>bar</p>' );
  578. modelRef.foo = 'baz';
  579. expect( view.el.outerHTML ).to.be.equal( '<p>baz</p>' );
  580. view.destroy();
  581. modelRef.foo = 'abc';
  582. expect( elRef.outerHTML ).to.be.equal( '<p>baz</p>' );
  583. } );
  584. it( 'destroy a template–less view', () => {
  585. view = new View();
  586. expect( () => {
  587. view.destroy();
  588. } ).to.not.throw();
  589. } );
  590. } );
  591. } );
  592. function updateModuleReference() {
  593. View = modules[ 'ui/view' ];
  594. Region = modules[ 'ui/region' ];
  595. Model = modules.model;
  596. CKEditorError = modules.ckeditorerror;
  597. }
  598. function createViewInstanceWithTemplate() {
  599. setTestViewClass( () => ( { tag: 'a' } ) );
  600. setTestViewInstance();
  601. }
  602. function setTestViewClass( templateFn, regionsFn ) {
  603. TestView = class V extends View {
  604. constructor( model ) {
  605. super( model );
  606. if ( templateFn ) {
  607. this.template = templateFn.call( this );
  608. }
  609. if ( templateFn && regionsFn ) {
  610. regionsFn.call( this );
  611. }
  612. }
  613. };
  614. }
  615. function setTestViewInstance( model ) {
  616. view = new TestView( new Model( model ) );
  617. if ( view.template ) {
  618. document.body.appendChild( view.el );
  619. }
  620. }
  621. function dispatchEvent( el, domEvtName ) {
  622. if ( !el.parentNode ) {
  623. throw new Error( 'To dispatch an event, element must be in DOM. Otherwise #target is null.' );
  624. }
  625. el.dispatchEvent( new Event( domEvtName, {
  626. bubbles: true
  627. } ) );
  628. }