view.js 18 KB

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