view.js 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859
  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/ui/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._element ).to.be.null;
  34. expect( view._template ).to.be.null;
  35. expect( () => {
  36. view.element;
  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.element ).to.be.equal( view.element.firstChild );
  70. expect( region2.element ).to.be.equal( view.element.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.element ).to.be.equal( view.element.firstChild );
  80. expect( region2.element ).to.be.equal( view.element.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.element ).to.be.null;
  90. expect( region2.element ).to.be.null;
  91. } );
  92. } );
  93. describe( 'register', () => {
  94. beforeEach( () => {
  95. setTestViewClass();
  96. setTestViewInstance();
  97. } );
  98. it( 'should throw when first argument is neither Region instance nor string', () => {
  99. expect( () => {
  100. view.register( new Date() );
  101. } ).to.throw( CKEditorError, /ui-view-register-wrongtype/ );
  102. } );
  103. it( 'should throw when missing the selector argument', () => {
  104. expect( () => {
  105. view.register( 'x' );
  106. } ).to.throw( CKEditorError, /ui-view-register-badselector/ );
  107. } );
  108. it( 'should throw when selector argument is of a wrong type', () => {
  109. expect( () => {
  110. view.register( 'x', new Date() );
  111. } ).to.throw( CKEditorError, /ui-view-register-badselector/ );
  112. expect( () => {
  113. view.register( 'x', false );
  114. } ).to.throw( CKEditorError, /ui-view-register-badselector/ );
  115. } );
  116. it( 'should throw when overriding an existing region but without override flag set', () => {
  117. expect( () => {
  118. view.register( 'x', true );
  119. view.register( new Region( 'x' ), true );
  120. } ).to.throw( CKEditorError, /ui-view-register-override/ );
  121. } );
  122. it( 'should register a new region with region name as a first argument', () => {
  123. view.register( 'x', true );
  124. expect( view.regions.get( 'x' ) ).to.be.an.instanceof( Region );
  125. } );
  126. it( 'should register a new region with Region instance as a first argument', () => {
  127. view.register( new Region( 'y' ), true );
  128. expect( view.regions.get( 'y' ) ).to.be.an.instanceof( Region );
  129. } );
  130. it( 'should override an existing region with override flag', () => {
  131. const region1 = new Region( 'x' );
  132. const region2 = new Region( 'x' );
  133. view.register( region1, true );
  134. view.register( region2, true, true );
  135. view.register( 'x', 'span', true );
  136. expect( view.regions.get( 'x' ) ).to.be.equal( region2 );
  137. expect( view._regionsSelectors.x ).to.be.equal( 'span' );
  138. } );
  139. it( 'should not override an existing region with the same region with override flag', () => {
  140. const region = new Region( 'x' );
  141. const spy = testUtils.sinon.spy( view.regions, 'remove' );
  142. view.register( region, true );
  143. view.register( region, true, true );
  144. sinon.assert.notCalled( spy );
  145. } );
  146. } );
  147. describe( 'el', () => {
  148. beforeEach( createViewInstanceWithTemplate );
  149. it( 'invokes out of #template', () => {
  150. setTestViewInstance( { a: 1 } );
  151. expect( view.element ).to.be.an.instanceof( HTMLElement );
  152. expect( view.element.nodeName ).to.be.equal( 'A' );
  153. } );
  154. it( 'can be explicitly declared', () => {
  155. class CustomView extends View {
  156. constructor() {
  157. super();
  158. this.element = document.createElement( 'span' );
  159. }
  160. }
  161. view = new CustomView();
  162. expect( view.element ).to.be.an.instanceof( HTMLElement );
  163. } );
  164. } );
  165. describe( 'bindToAttribute', () => {
  166. beforeEach( createViewInstanceWithTemplate );
  167. it( 'returns a function that passes arguments', () => {
  168. setTestViewInstance( { a: 'foo' } );
  169. const spy = testUtils.sinon.spy();
  170. const callback = view.bindToAttribute( 'a', spy );
  171. expect( spy.called ).to.be.false;
  172. callback( 'el', 'updater' );
  173. sinon.assert.calledOnce( spy );
  174. sinon.assert.calledWithExactly( spy, 'el', 'foo' );
  175. view.model.a = 'bar';
  176. sinon.assert.calledTwice( spy );
  177. expect( spy.secondCall.calledWithExactly( 'el', 'bar' ) ).to.be.true;
  178. } );
  179. it( 'allows binding attribute to the model', () => {
  180. setTestViewClass( function() {
  181. return {
  182. tag: 'p',
  183. attributes: {
  184. 'class': this.bindToAttribute( 'foo' )
  185. },
  186. children: [ 'abc' ]
  187. };
  188. } );
  189. setTestViewInstance( { foo: 'bar' } );
  190. expect( view.element.outerHTML ).to.be.equal( '<p class="bar">abc</p>' );
  191. view.model.foo = 'baz';
  192. expect( view.element.outerHTML ).to.be.equal( '<p class="baz">abc</p>' );
  193. } );
  194. it( 'allows binding "text" to the model', () => {
  195. setTestViewClass( function() {
  196. return {
  197. tag: 'p',
  198. children: [
  199. {
  200. text: this.bindToAttribute( 'foo' )
  201. },
  202. {
  203. tag: 'b',
  204. children: [ 'baz' ]
  205. }
  206. ]
  207. };
  208. } );
  209. setTestViewInstance( { foo: 'bar' } );
  210. expect( view.element.outerHTML ).to.be.equal( '<p>bar<b>baz</b></p>' );
  211. view.model.foo = 'qux';
  212. expect( view.element.outerHTML ).to.be.equal( '<p>qux<b>baz</b></p>' );
  213. } );
  214. it( 'allows binding to the model with value processing', () => {
  215. const callback = ( el, value ) =>
  216. ( value > 0 ? 'positive' : 'negative' );
  217. setTestViewClass( function() {
  218. return {
  219. tag: 'p',
  220. attributes: {
  221. 'class': this.bindToAttribute( 'foo', callback )
  222. },
  223. children: [
  224. { text: this.bindToAttribute( 'foo', callback ) }
  225. ]
  226. };
  227. } );
  228. setTestViewInstance( { foo: 3 } );
  229. expect( view.element.outerHTML ).to.be.equal( '<p class="positive">positive</p>' );
  230. view.model.foo = -7;
  231. expect( view.element.outerHTML ).to.be.equal( '<p class="negative">negative</p>' );
  232. } );
  233. it( 'allows binding to the model with custom callback', () => {
  234. setTestViewClass( function() {
  235. return {
  236. tag: 'p',
  237. attributes: {
  238. 'class': this.bindToAttribute( 'foo', ( el, value ) => {
  239. el.innerHTML = value;
  240. if ( value == 'changed' ) {
  241. return value;
  242. }
  243. } )
  244. },
  245. children: [ 'bar' ]
  246. };
  247. } );
  248. setTestViewInstance( { foo: 'moo' } );
  249. // Note: First the attribute binding sets innerHTML to 'moo',
  250. // then 'bar' TextNode child is added.
  251. expect( view.element.outerHTML ).to.be.equal( '<p>moobar</p>' );
  252. view.model.foo = 'changed';
  253. expect( view.element.outerHTML ).to.be.equal( '<p class="changed">changed</p>' );
  254. } );
  255. } );
  256. describe( 'on', () => {
  257. it( 'accepts plain binding', () => {
  258. const spy = testUtils.sinon.spy();
  259. setTestViewClass( function() {
  260. return {
  261. tag: 'p',
  262. on: {
  263. x: 'a',
  264. }
  265. };
  266. } );
  267. setTestViewInstance();
  268. view.on( 'a', spy );
  269. dispatchEvent( view.element, 'x' );
  270. sinon.assert.calledWithExactly( spy,
  271. sinon.match.has( 'name', 'a' ),
  272. sinon.match.has( 'target', view.element )
  273. );
  274. } );
  275. it( 'accepts an array of event bindings', () => {
  276. const spy1 = testUtils.sinon.spy();
  277. const spy2 = testUtils.sinon.spy();
  278. setTestViewClass( function() {
  279. return {
  280. tag: 'p',
  281. on: {
  282. x: [ 'a', 'b' ]
  283. }
  284. };
  285. } );
  286. setTestViewInstance();
  287. view.on( 'a', spy1 );
  288. view.on( 'b', spy2 );
  289. dispatchEvent( view.element, 'x' );
  290. sinon.assert.calledWithExactly( spy1,
  291. sinon.match.has( 'name', 'a' ),
  292. sinon.match.has( 'target', view.element )
  293. );
  294. sinon.assert.calledWithExactly( spy2,
  295. sinon.match.has( 'name', 'b' ),
  296. sinon.match.has( 'target', view.element )
  297. );
  298. } );
  299. it( 'accepts DOM selectors', () => {
  300. const spy1 = testUtils.sinon.spy();
  301. const spy2 = testUtils.sinon.spy();
  302. const spy3 = testUtils.sinon.spy();
  303. setTestViewClass( function() {
  304. return {
  305. tag: 'p',
  306. children: [
  307. {
  308. tag: 'span',
  309. attributes: {
  310. 'class': 'y',
  311. },
  312. on: {
  313. 'test@p': 'c'
  314. }
  315. },
  316. {
  317. tag: 'div',
  318. children: [
  319. {
  320. tag: 'span',
  321. attributes: {
  322. 'class': 'y',
  323. }
  324. }
  325. ],
  326. }
  327. ],
  328. on: {
  329. 'test@.y': 'a',
  330. 'test@div': 'b'
  331. }
  332. };
  333. } );
  334. setTestViewInstance();
  335. view.on( 'a', spy1 );
  336. view.on( 'b', spy2 );
  337. view.on( 'c', spy3 );
  338. // Test "test@p".
  339. dispatchEvent( view.element, 'test' );
  340. sinon.assert.callCount( spy1, 0 );
  341. sinon.assert.callCount( spy2, 0 );
  342. sinon.assert.callCount( spy3, 0 );
  343. // Test "test@.y".
  344. dispatchEvent( view.element.firstChild, 'test' );
  345. expect( spy1.firstCall.calledWithExactly(
  346. sinon.match.has( 'name', 'a' ),
  347. sinon.match.has( 'target', view.element.firstChild )
  348. ) ).to.be.true;
  349. sinon.assert.callCount( spy2, 0 );
  350. sinon.assert.callCount( spy3, 0 );
  351. // Test "test@div".
  352. dispatchEvent( view.element.lastChild, 'test' );
  353. sinon.assert.callCount( spy1, 1 );
  354. expect( spy2.firstCall.calledWithExactly(
  355. sinon.match.has( 'name', 'b' ),
  356. sinon.match.has( 'target', view.element.lastChild )
  357. ) ).to.be.true;
  358. sinon.assert.callCount( spy3, 0 );
  359. // Test "test@.y".
  360. dispatchEvent( view.element.lastChild.firstChild, 'test' );
  361. expect( spy1.secondCall.calledWithExactly(
  362. sinon.match.has( 'name', 'a' ),
  363. sinon.match.has( 'target', view.element.lastChild.firstChild )
  364. ) ).to.be.true;
  365. sinon.assert.callCount( spy2, 1 );
  366. sinon.assert.callCount( spy3, 0 );
  367. } );
  368. it( 'accepts function callbacks', () => {
  369. const spy1 = testUtils.sinon.spy();
  370. const spy2 = testUtils.sinon.spy();
  371. setTestViewClass( function() {
  372. return {
  373. tag: 'p',
  374. children: [
  375. {
  376. tag: 'span'
  377. }
  378. ],
  379. on: {
  380. x: spy1,
  381. 'y@span': [ spy2, 'c' ],
  382. }
  383. };
  384. } );
  385. setTestViewInstance();
  386. dispatchEvent( view.element, 'x' );
  387. dispatchEvent( view.element.firstChild, 'y' );
  388. sinon.assert.calledWithExactly( spy1,
  389. sinon.match.has( 'target', view.element )
  390. );
  391. sinon.assert.calledWithExactly( spy2,
  392. sinon.match.has( 'target', view.element.firstChild )
  393. );
  394. } );
  395. it( 'supports event delegation', () => {
  396. const spy = testUtils.sinon.spy();
  397. setTestViewClass( function() {
  398. return {
  399. tag: 'p',
  400. children: [
  401. {
  402. tag: 'span'
  403. }
  404. ],
  405. on: {
  406. x: 'a',
  407. }
  408. };
  409. } );
  410. setTestViewInstance();
  411. view.on( 'a', spy );
  412. dispatchEvent( view.element.firstChild, 'x' );
  413. sinon.assert.calledWithExactly( spy,
  414. sinon.match.has( 'name', 'a' ),
  415. sinon.match.has( 'target', view.element.firstChild )
  416. );
  417. } );
  418. it( 'works for future elements', () => {
  419. const spy = testUtils.sinon.spy();
  420. setTestViewClass( function() {
  421. return {
  422. tag: 'p',
  423. on: {
  424. 'test@div': 'a'
  425. }
  426. };
  427. } );
  428. setTestViewInstance();
  429. view.on( 'a', spy );
  430. const div = document.createElement( 'div' );
  431. view.element.appendChild( div );
  432. dispatchEvent( div, 'test' );
  433. sinon.assert.calledWithExactly( spy, sinon.match.has( 'name', 'a' ), sinon.match.has( 'target', div ) );
  434. } );
  435. } );
  436. describe( 'destroy', () => {
  437. beforeEach( createViewInstanceWithTemplate );
  438. it( 'should destroy the view', () => {
  439. view.destroy();
  440. expect( view.model ).to.be.null;
  441. expect( view.regions ).to.be.null;
  442. expect( view.template ).to.be.null;
  443. expect( view._regionsSelectors ).to.be.null;
  444. expect( view._element ).to.be.null;
  445. expect( view._template ).to.be.null;
  446. } );
  447. it( 'detaches the element from DOM', () => {
  448. const elRef = view.element;
  449. document.createElement( 'div' ).appendChild( view.element );
  450. view.destroy();
  451. expect( elRef.parentNode ).to.be.null;
  452. } );
  453. it( 'destroys child regions', () => {
  454. const region = new Region( 'x' );
  455. const spy = testUtils.sinon.spy( region, 'destroy' );
  456. const regionsRef = view.regions;
  457. const regionViewsRef = region.views;
  458. view.register( region, true );
  459. view.regions.get( 'x' ).views.add( new View() );
  460. view.destroy();
  461. expect( regionsRef.length ).to.be.equal( 0 );
  462. expect( regionViewsRef.length ).to.be.equal( 0 );
  463. expect( spy.calledOnce ).to.be.true;
  464. } );
  465. it( 'detaches bound model listeners', () => {
  466. setTestViewClass( function() {
  467. return {
  468. tag: 'p',
  469. children: [
  470. { text: this.bindToAttribute( 'foo' ) }
  471. ]
  472. };
  473. } );
  474. setTestViewInstance( { foo: 'bar' } );
  475. const modelRef = view.model;
  476. const elRef = view.element;
  477. expect( view.element.outerHTML ).to.be.equal( '<p>bar</p>' );
  478. modelRef.foo = 'baz';
  479. expect( view.element.outerHTML ).to.be.equal( '<p>baz</p>' );
  480. view.destroy();
  481. modelRef.foo = 'abc';
  482. expect( elRef.outerHTML ).to.be.equal( '<p>baz</p>' );
  483. } );
  484. it( 'destroy a template–less view', () => {
  485. view = new View();
  486. expect( () => {
  487. view.destroy();
  488. } ).to.not.throw();
  489. } );
  490. } );
  491. describe( 'applyTemplateToElement', () => {
  492. beforeEach( () => {
  493. setTestViewClass();
  494. setTestViewInstance( { a: 'foo', b: 42 } );
  495. } );
  496. it( 'should initialize attribute bindings', () => {
  497. const el = document.createElement( 'div' );
  498. view.applyTemplateToElement( el, {
  499. tag: 'div',
  500. attributes: {
  501. class: view.bindToAttribute( 'b' ),
  502. id: 'foo',
  503. checked: ''
  504. }
  505. } );
  506. expect( el.outerHTML ).to.be.equal( '<div class="42" id="foo" checked=""></div>' );
  507. view.model.b = 64;
  508. expect( el.outerHTML ).to.be.equal( '<div class="64" id="foo" checked=""></div>' );
  509. } );
  510. it( 'should initialize DOM listeners', () => {
  511. const el = document.createElement( 'div' );
  512. const spy = testUtils.sinon.spy();
  513. view.applyTemplateToElement( el, {
  514. tag: 'div',
  515. on: {
  516. click: spy
  517. }
  518. } );
  519. document.body.appendChild( el );
  520. dispatchEvent( el, 'click' );
  521. sinon.assert.calledOnce( spy );
  522. view.stopListening( el, 'click' );
  523. dispatchEvent( el, 'click' );
  524. sinon.assert.calledOnce( spy );
  525. } );
  526. it( 'should work for deep DOM structure', () => {
  527. const el = document.createElement( 'div' );
  528. const childA = document.createElement( 'a' );
  529. const childB = document.createElement( 'b' );
  530. childA.textContent = 'anchor';
  531. childB.textContent = 'bold';
  532. el.appendChild( childA );
  533. el.appendChild( childB );
  534. expect( el.outerHTML ).to.be.equal( '<div><a>anchor</a><b>bold</b></div>' );
  535. const spy1 = testUtils.sinon.spy();
  536. const spy2 = testUtils.sinon.spy();
  537. const spy3 = testUtils.sinon.spy();
  538. view.applyTemplateToElement( el, {
  539. tag: 'div',
  540. children: [
  541. {
  542. tag: 'a',
  543. on: {
  544. keyup: spy2
  545. },
  546. attributes: {
  547. class: view.bindToAttribute( 'b', ( el, b ) => 'applied-A-' + b ),
  548. id: 'applied-A'
  549. },
  550. children: [ 'Text applied to childA.' ]
  551. },
  552. {
  553. tag: 'b',
  554. on: {
  555. keydown: spy3
  556. },
  557. attributes: {
  558. class: view.bindToAttribute( 'b', ( el, b ) => 'applied-B-' + b ),
  559. id: 'applied-B'
  560. },
  561. children: [ 'Text applied to childB.' ]
  562. },
  563. 'Text which is not to be applied because it does NOT exist in original element.'
  564. ],
  565. on: {
  566. 'mouseover@a': spy1
  567. },
  568. attributes: {
  569. id: view.bindToAttribute( 'a', ( el, a ) => a.toUpperCase() ),
  570. class: view.bindToAttribute( 'b', ( el, b ) => 'applied-parent-' + b )
  571. }
  572. } );
  573. expect( el.outerHTML ).to.be.equal( '<div id="FOO" class="applied-parent-42">' +
  574. '<a class="applied-A-42" id="applied-A">Text applied to childA.</a>' +
  575. '<b class="applied-B-42" id="applied-B">Text applied to childB.</b>' +
  576. '</div>' );
  577. view.model.b = 16;
  578. expect( el.outerHTML ).to.be.equal( '<div id="FOO" class="applied-parent-16">' +
  579. '<a class="applied-A-16" id="applied-A">Text applied to childA.</a>' +
  580. '<b class="applied-B-16" id="applied-B">Text applied to childB.</b>' +
  581. '</div>' );
  582. document.body.appendChild( el );
  583. // Test "mouseover@a".
  584. dispatchEvent( el, 'mouseover' );
  585. dispatchEvent( childA, 'mouseover' );
  586. // Test "keyup".
  587. dispatchEvent( childA, 'keyup' );
  588. // Test "keydown".
  589. dispatchEvent( childB, 'keydown' );
  590. sinon.assert.calledOnce( spy1 );
  591. sinon.assert.calledOnce( spy2 );
  592. sinon.assert.calledOnce( spy3 );
  593. } );
  594. it( 're–uses a template definition object without redundant re–definition of "on" listener attachers', () => {
  595. const el1 = document.createElement( 'div' );
  596. const el2 = document.createElement( 'div' );
  597. const spy = testUtils.sinon.spy();
  598. const def = {
  599. tag: 'div',
  600. on: {
  601. click: spy
  602. }
  603. };
  604. view.applyTemplateToElement( el1, def );
  605. const attacherFn = def.on.click;
  606. view.applyTemplateToElement( el2, def );
  607. // Attacher function didn't change because it's still the same View instance.
  608. expect( attacherFn ).to.be.equal( def.on.click );
  609. expect( def.on._listenerView ).to.be.equal( view );
  610. document.body.appendChild( el1 );
  611. document.body.appendChild( el2 );
  612. dispatchEvent( el1, 'click' );
  613. sinon.assert.calledOnce( spy );
  614. dispatchEvent( el2, 'click' );
  615. sinon.assert.calledTwice( spy );
  616. } );
  617. it( 'shares a template definition between View instances', () => {
  618. const el = document.createElement( 'div' );
  619. const spy = testUtils.sinon.spy();
  620. const view1 = new View();
  621. const view2 = new View();
  622. const def = {
  623. tag: 'div',
  624. on: {
  625. click: spy
  626. }
  627. };
  628. document.body.appendChild( el );
  629. view1.applyTemplateToElement( el, def );
  630. expect( def.on._listenerView ).to.be.equal( view1 );
  631. dispatchEvent( el, 'click' );
  632. sinon.assert.calledOnce( spy );
  633. // Use another view1 to see if attachers are re–defined.
  634. view2.applyTemplateToElement( el, def );
  635. expect( def.on._listenerView ).to.be.equal( view2 );
  636. dispatchEvent( el, 'click' );
  637. // Spy was called for view1 (1st dispatch), then for view1 and view2 (2nd dispatch).
  638. sinon.assert.calledThrice( spy );
  639. } );
  640. } );
  641. } );
  642. function createViewInstanceWithTemplate() {
  643. setTestViewClass( () => ( { tag: 'a' } ) );
  644. setTestViewInstance();
  645. }
  646. function setTestViewClass( templateFn, regionsFn ) {
  647. TestView = class V extends View {
  648. constructor( model ) {
  649. super( model );
  650. if ( templateFn ) {
  651. this.template = templateFn.call( this );
  652. }
  653. if ( templateFn && regionsFn ) {
  654. regionsFn.call( this );
  655. }
  656. }
  657. };
  658. }
  659. function setTestViewInstance( model ) {
  660. view = new TestView( new Model( model ) );
  661. if ( view.template ) {
  662. document.body.appendChild( view.element );
  663. }
  664. }
  665. function dispatchEvent( el, domEvtName ) {
  666. if ( !el.parentNode ) {
  667. throw new Error( 'To dispatch an event, element must be in DOM. Otherwise #target is null.' );
  668. }
  669. el.dispatchEvent( new Event( domEvtName, {
  670. bubbles: true
  671. } ) );
  672. }