view.js 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171
  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( view.element ).to.be.null;
  36. } );
  37. } );
  38. describe( 'init', () => {
  39. beforeEach( () => {
  40. setTestViewClass( () => ( {
  41. tag: 'p',
  42. children: [
  43. { tag: 'span' },
  44. { tag: 'strong' }
  45. ]
  46. } ) );
  47. } );
  48. it( 'calls child regions #init', () => {
  49. setTestViewInstance();
  50. const region1 = new Region( 'x' );
  51. const region2 = new Region( 'y' );
  52. view.register( region1, el => el );
  53. view.register( region2, el => el );
  54. const spy1 = testUtils.sinon.spy( region1, 'init' );
  55. const spy2 = testUtils.sinon.spy( region2, 'init' );
  56. view.init();
  57. sinon.assert.calledOnce( spy1 );
  58. sinon.assert.calledOnce( spy2 );
  59. } );
  60. it( 'initializes view regions with string selector', () => {
  61. setTestViewInstance();
  62. const region1 = new Region( 'x' );
  63. const region2 = new Region( 'y' );
  64. view.register( region1, 'span' );
  65. view.register( region2, 'strong' );
  66. view.init();
  67. expect( region1.element ).to.be.equal( view.element.firstChild );
  68. expect( region2.element ).to.be.equal( view.element.lastChild );
  69. } );
  70. it( 'initializes view regions with function selector', () => {
  71. setTestViewInstance();
  72. const region1 = new Region( 'x' );
  73. const region2 = new Region( 'y' );
  74. view.register( region1, el => el.firstChild );
  75. view.register( region2, el => el.lastChild );
  76. view.init();
  77. expect( region1.element ).to.be.equal( view.element.firstChild );
  78. expect( region2.element ).to.be.equal( view.element.lastChild );
  79. } );
  80. it( 'initializes view regions with boolean selector', () => {
  81. setTestViewInstance();
  82. const region1 = new Region( 'x' );
  83. const region2 = new Region( 'y' );
  84. view.register( region1, true );
  85. view.register( region2, true );
  86. view.init();
  87. expect( region1.element ).to.be.null;
  88. expect( region2.element ).to.be.null;
  89. } );
  90. } );
  91. describe( 'register', () => {
  92. beforeEach( () => {
  93. setTestViewClass();
  94. setTestViewInstance();
  95. } );
  96. it( 'should throw when first argument is neither Region instance nor string', () => {
  97. expect( () => {
  98. view.register( new Date() );
  99. } ).to.throw( CKEditorError, /ui-view-register-wrongtype/ );
  100. } );
  101. it( 'should throw when missing the selector argument', () => {
  102. expect( () => {
  103. view.register( 'x' );
  104. } ).to.throw( CKEditorError, /ui-view-register-badselector/ );
  105. } );
  106. it( 'should throw when selector argument is of a wrong type', () => {
  107. expect( () => {
  108. view.register( 'x', new Date() );
  109. } ).to.throw( CKEditorError, /ui-view-register-badselector/ );
  110. expect( () => {
  111. view.register( 'x', false );
  112. } ).to.throw( CKEditorError, /ui-view-register-badselector/ );
  113. } );
  114. it( 'should throw when overriding an existing region but without override flag set', () => {
  115. expect( () => {
  116. view.register( 'x', true );
  117. view.register( new Region( 'x' ), true );
  118. } ).to.throw( CKEditorError, /ui-view-register-override/ );
  119. } );
  120. it( 'should register a new region with region name as a first argument', () => {
  121. view.register( 'x', true );
  122. expect( view.regions.get( 'x' ) ).to.be.an.instanceof( Region );
  123. } );
  124. it( 'should register a new region with Region instance as a first argument', () => {
  125. view.register( new Region( 'y' ), true );
  126. expect( view.regions.get( 'y' ) ).to.be.an.instanceof( Region );
  127. } );
  128. it( 'should override an existing region with override flag', () => {
  129. const region1 = new Region( 'x' );
  130. const region2 = new Region( 'x' );
  131. view.register( region1, true );
  132. view.register( region2, true, true );
  133. view.register( 'x', 'span', true );
  134. expect( view.regions.get( 'x' ) ).to.be.equal( region2 );
  135. expect( view._regionsSelectors.x ).to.be.equal( 'span' );
  136. } );
  137. it( 'should not override an existing region with the same region with override flag', () => {
  138. const region = new Region( 'x' );
  139. const spy = testUtils.sinon.spy( view.regions, 'remove' );
  140. view.register( region, true );
  141. view.register( region, true, true );
  142. sinon.assert.notCalled( spy );
  143. } );
  144. } );
  145. describe( 'el', () => {
  146. beforeEach( createViewInstanceWithTemplate );
  147. it( 'invokes out of #template', () => {
  148. setTestViewInstance( { a: 1 } );
  149. expect( view.element ).to.be.an.instanceof( HTMLElement );
  150. expect( view.element.nodeName ).to.be.equal( 'A' );
  151. } );
  152. it( 'can be explicitly declared', () => {
  153. class CustomView extends View {
  154. constructor() {
  155. super();
  156. this.element = document.createElement( 'span' );
  157. }
  158. }
  159. view = new CustomView();
  160. expect( view.element ).to.be.an.instanceof( HTMLElement );
  161. } );
  162. } );
  163. describe( 'attributeBinder', () => {
  164. it( 'provides "to" and "if" interface', () => {
  165. const bind = view.attributeBinder;
  166. expect( bind ).to.have.keys( 'to', 'if' );
  167. expect( typeof bind.to ).to.be.equal( 'function' );
  168. expect( typeof bind.if ).to.be.equal( 'function' );
  169. } );
  170. describe( 'to', () => {
  171. it( 'returns an object which describes the binding', () => {
  172. setTestViewInstance( { a: 1 } );
  173. const spy = testUtils.sinon.spy();
  174. const bind = view.attributeBinder;
  175. const binding = bind.to( 'a', spy );
  176. expect( spy.called ).to.be.false;
  177. expect( binding ).to.have.keys( [ 'type', 'model', 'attribute', 'callback' ] );
  178. expect( binding.model ).to.equal( view.model );
  179. expect( binding.callback ).to.equal( spy );
  180. expect( binding.attribute ).to.equal( 'a' );
  181. } );
  182. it( 'allows binding attribute to the model – simple (HTMLElement attribute)', () => {
  183. setTestViewClass( function() {
  184. const bind = this.attributeBinder;
  185. return {
  186. tag: 'p',
  187. attributes: {
  188. 'class': bind.to( 'foo' )
  189. },
  190. children: [ 'abc' ]
  191. };
  192. } );
  193. setTestViewInstance( { foo: 'bar' } );
  194. expect( view.element.outerHTML ).to.be.equal( '<p class="bar">abc</p>' );
  195. view.model.foo = 'baz';
  196. expect( view.element.outerHTML ).to.be.equal( '<p class="baz">abc</p>' );
  197. } );
  198. it( 'allows binding attribute to the model – simple (Text Node)', () => {
  199. setTestViewClass( function() {
  200. const bind = this.attributeBinder;
  201. return {
  202. tag: 'p',
  203. children: [
  204. {
  205. text: bind.to( 'foo' )
  206. }
  207. ]
  208. };
  209. } );
  210. setTestViewInstance( { foo: 'bar' } );
  211. expect( view.element.outerHTML ).to.be.equal( '<p>bar</p>' );
  212. view.model.foo = 'baz';
  213. expect( view.element.outerHTML ).to.be.equal( '<p>baz</p>' );
  214. } );
  215. it( 'allows binding attribute to the model – value processing', () => {
  216. setTestViewClass( function() {
  217. const bind = this.attributeBinder;
  218. const callback = value => value > 0 ? 'positive' : 'negative';
  219. return {
  220. tag: 'p',
  221. attributes: {
  222. 'class': bind.to( 'foo', callback )
  223. },
  224. children: [
  225. {
  226. text: bind.to( 'foo', callback )
  227. }
  228. ]
  229. };
  230. } );
  231. setTestViewInstance( { foo: 3 } );
  232. expect( view.element.outerHTML ).to.be.equal( '<p class="positive">positive</p>' );
  233. view.model.foo = -7;
  234. expect( view.element.outerHTML ).to.be.equal( '<p class="negative">negative</p>' );
  235. } );
  236. it( 'allows binding attribute to the model – value processing (use Node)', () => {
  237. setTestViewClass( function() {
  238. const bind = this.attributeBinder;
  239. const callback = ( value, node ) => {
  240. return ( !!node.tagName && value > 0 ) ? 'HTMLElement positive' : '';
  241. };
  242. return {
  243. tag: 'p',
  244. attributes: {
  245. 'class': bind.to( 'foo', callback )
  246. },
  247. children: [
  248. {
  249. text: bind.to( 'foo', callback )
  250. }
  251. ]
  252. };
  253. } );
  254. setTestViewInstance( { foo: 3 } );
  255. expect( view.element.outerHTML ).to.be.equal( '<p class="HTMLElement positive"></p>' );
  256. view.model.foo = -7;
  257. expect( view.element.outerHTML ).to.be.equal( '<p></p>' );
  258. } );
  259. it( 'allows binding attribute to the model – custom callback', () => {
  260. setTestViewClass( function() {
  261. const bind = this.attributeBinder;
  262. return {
  263. tag: 'p',
  264. attributes: {
  265. 'class': bind.to( 'foo', ( value, el ) => {
  266. el.innerHTML = value;
  267. if ( value == 'changed' ) {
  268. return value;
  269. }
  270. } )
  271. }
  272. };
  273. } );
  274. setTestViewInstance( { foo: 'moo' } );
  275. expect( view.element.outerHTML ).to.be.equal( '<p class="undefined">moo</p>' );
  276. view.model.foo = 'changed';
  277. expect( view.element.outerHTML ).to.be.equal( '<p class="changed">changed</p>' );
  278. } );
  279. it( 'allows binding attribute to the model – array of bindings (HTMLElement attribute)', () => {
  280. setTestViewClass( function() {
  281. const bind = this.attributeBinder;
  282. return {
  283. tag: 'p',
  284. attributes: {
  285. 'class': [
  286. 'ck-class',
  287. bind.to( 'foo' ),
  288. bind.to( 'bar' ),
  289. bind.to( 'foo', value => `foo-is-${value}` ),
  290. 'ck-end'
  291. ]
  292. },
  293. children: [ 'abc' ]
  294. };
  295. } );
  296. setTestViewInstance( { foo: 'a', bar: 'b' } );
  297. expect( view.element.outerHTML ).to.be.equal( '<p class="ck-class a b foo-is-a ck-end">abc</p>' );
  298. view.model.foo = 'c';
  299. view.model.bar = 'd';
  300. expect( view.element.outerHTML ).to.be.equal( '<p class="ck-class c d foo-is-c ck-end">abc</p>' );
  301. } );
  302. it( 'allows binding attribute to the model – array of bindings (Text Node)', () => {
  303. setTestViewClass( function() {
  304. const bind = this.attributeBinder;
  305. return {
  306. tag: 'p',
  307. attributes: {
  308. },
  309. children: [
  310. {
  311. text: [
  312. 'ck-class',
  313. bind.to( 'foo' ),
  314. bind.to( 'bar' ),
  315. bind.to( 'foo', value => `foo-is-${value}` ),
  316. 'ck-end'
  317. ]
  318. }
  319. ]
  320. };
  321. } );
  322. setTestViewInstance( { foo: 'a', bar: 'b' } );
  323. expect( view.element.outerHTML ).to.be.equal( '<p>ck-class a b foo-is-a ck-end</p>' );
  324. view.model.foo = 'c';
  325. view.model.bar = 'd';
  326. expect( view.element.outerHTML ).to.be.equal( '<p>ck-class c d foo-is-c ck-end</p>' );
  327. } );
  328. it( 'allows binding attribute to the model – falsy values', () => {
  329. setTestViewClass( function() {
  330. const bind = this.attributeBinder;
  331. return {
  332. tag: 'p',
  333. attributes: {
  334. 'class': bind.to( 'foo' )
  335. },
  336. children: [ 'abc' ]
  337. };
  338. } );
  339. setTestViewInstance( { foo: 'bar' } );
  340. expect( view.element.outerHTML ).to.be.equal( '<p class="bar">abc</p>' );
  341. view.model.foo = false;
  342. expect( view.element.outerHTML ).to.be.equal( '<p class="false">abc</p>' );
  343. view.model.foo = null;
  344. expect( view.element.outerHTML ).to.be.equal( '<p class="null">abc</p>' );
  345. view.model.foo = undefined;
  346. expect( view.element.outerHTML ).to.be.equal( '<p class="undefined">abc</p>' );
  347. view.model.foo = 0;
  348. expect( view.element.outerHTML ).to.be.equal( '<p class="0">abc</p>' );
  349. view.model.foo = '';
  350. expect( view.element.outerHTML ).to.be.equal( '<p>abc</p>' );
  351. } );
  352. } );
  353. describe( 'if', () => {
  354. it( 'returns an object which describes the binding', () => {
  355. setTestViewInstance( { a: 1 } );
  356. const spy = testUtils.sinon.spy();
  357. const bind = view.attributeBinder;
  358. const binding = bind.if( 'a', 'foo', spy );
  359. expect( spy.called ).to.be.false;
  360. expect( binding ).to.have.keys( [ 'type', 'model', 'attribute', 'callback', 'valueIfTrue' ] );
  361. expect( binding.model ).to.equal( view.model );
  362. expect( binding.callback ).to.equal( spy );
  363. expect( binding.attribute ).to.equal( 'a' );
  364. expect( binding.valueIfTrue ).to.equal( 'foo' );
  365. } );
  366. it( 'allows binding attribute to the model – presence of an attribute (HTMLElement attribute)', () => {
  367. setTestViewClass( function() {
  368. const bind = this.attributeBinder;
  369. return {
  370. tag: 'p',
  371. attributes: {
  372. 'class': bind.if( 'foo' )
  373. },
  374. children: [ 'abc' ]
  375. };
  376. } );
  377. setTestViewInstance( { foo: true } );
  378. expect( view.element.outerHTML ).to.be.equal( '<p class="">abc</p>' );
  379. view.model.foo = false;
  380. expect( view.element.outerHTML ).to.be.equal( '<p>abc</p>' );
  381. view.model.foo = 'bar';
  382. expect( view.element.outerHTML ).to.be.equal( '<p class="">abc</p>' );
  383. } );
  384. // TODO: Is this alright? It makes sense but it's pretty useless. Text Node cannot be
  385. // removed just like an attribute of some HTMLElement.
  386. it( 'allows binding attribute to the model – presence of an attribute (Text Node)', () => {
  387. setTestViewClass( function() {
  388. const bind = this.attributeBinder;
  389. return {
  390. tag: 'p',
  391. children: [
  392. {
  393. text: bind.if( 'foo' )
  394. }
  395. ]
  396. };
  397. } );
  398. setTestViewInstance( { foo: true } );
  399. expect( view.element.outerHTML ).to.be.equal( '<p></p>' );
  400. view.model.foo = false;
  401. expect( view.element.outerHTML ).to.be.equal( '<p></p>' );
  402. view.model.foo = 'bar';
  403. expect( view.element.outerHTML ).to.be.equal( '<p></p>' );
  404. } );
  405. it( 'allows binding attribute to the model – value of an attribute (HTMLElement attribute)', () => {
  406. setTestViewClass( function() {
  407. const bind = this.attributeBinder;
  408. return {
  409. tag: 'p',
  410. attributes: {
  411. 'class': bind.if( 'foo', 'bar' )
  412. },
  413. children: [ 'abc' ]
  414. };
  415. } );
  416. setTestViewInstance( { foo: 'bar' } );
  417. expect( view.element.outerHTML ).to.be.equal( '<p class="bar">abc</p>' );
  418. view.model.foo = false;
  419. expect( view.element.outerHTML ).to.be.equal( '<p>abc</p>' );
  420. view.model.foo = 64;
  421. expect( view.element.outerHTML ).to.be.equal( '<p class="bar">abc</p>' );
  422. } );
  423. it( 'allows binding attribute to the model – value of an attribute (Text Node)', () => {
  424. setTestViewClass( function() {
  425. const bind = this.attributeBinder;
  426. return {
  427. tag: 'p',
  428. children: [
  429. {
  430. text: bind.if( 'foo', 'bar' )
  431. }
  432. ]
  433. };
  434. } );
  435. setTestViewInstance( { foo: 'bar' } );
  436. expect( view.element.outerHTML ).to.be.equal( '<p>bar</p>' );
  437. view.model.foo = false;
  438. expect( view.element.outerHTML ).to.be.equal( '<p></p>' );
  439. view.model.foo = 64;
  440. expect( view.element.outerHTML ).to.be.equal( '<p>bar</p>' );
  441. } );
  442. it( 'allows binding attribute to the model – value of an attribute processed by a callback', () => {
  443. setTestViewClass( function() {
  444. const bind = this.attributeBinder;
  445. return {
  446. tag: 'p',
  447. attributes: {
  448. 'class': bind.if( 'foo', 'there–is–no–foo', value => !value )
  449. },
  450. children: [ 'abc' ]
  451. };
  452. } );
  453. setTestViewInstance( { foo: 'bar' } );
  454. expect( view.element.outerHTML ).to.be.equal( '<p>abc</p>' );
  455. view.model.foo = false;
  456. expect( view.element.outerHTML ).to.be.equal( '<p class="there–is–no–foo">abc</p>' );
  457. view.model.foo = 64;
  458. expect( view.element.outerHTML ).to.be.equal( '<p>abc</p>' );
  459. } );
  460. it( 'allows binding attribute to the model – value of an attribute processed by a callback (use Node)', () => {
  461. setTestViewClass( function() {
  462. const bind = this.attributeBinder;
  463. return {
  464. tag: 'p',
  465. attributes: {
  466. 'class': bind.if( 'foo', 'eqls-tag-name', ( value, el ) => el.tagName === value )
  467. },
  468. children: [ 'abc' ]
  469. };
  470. } );
  471. setTestViewInstance( { foo: 'bar' } );
  472. expect( view.element.outerHTML ).to.be.equal( '<p>abc</p>' );
  473. view.model.foo = 'P';
  474. expect( view.element.outerHTML ).to.be.equal( '<p class="eqls-tag-name">abc</p>' );
  475. view.model.foo = 64;
  476. expect( view.element.outerHTML ).to.be.equal( '<p>abc</p>' );
  477. } );
  478. it( 'allows binding attribute to the model – falsy values', () => {
  479. setTestViewClass( function() {
  480. const bind = this.attributeBinder;
  481. return {
  482. tag: 'p',
  483. attributes: {
  484. 'class': bind.if( 'foo', 'foo-is-set' )
  485. },
  486. children: [ 'abc' ]
  487. };
  488. } );
  489. setTestViewInstance( { foo: 'bar' } );
  490. expect( view.element.outerHTML ).to.be.equal( '<p class="foo-is-set">abc</p>' );
  491. view.model.foo = false;
  492. expect( view.element.outerHTML ).to.be.equal( '<p>abc</p>' );
  493. view.model.foo = null;
  494. expect( view.element.outerHTML ).to.be.equal( '<p>abc</p>' );
  495. view.model.foo = undefined;
  496. expect( view.element.outerHTML ).to.be.equal( '<p>abc</p>' );
  497. view.model.foo = '';
  498. expect( view.element.outerHTML ).to.be.equal( '<p>abc</p>' );
  499. view.model.foo = 0;
  500. expect( view.element.outerHTML ).to.be.equal( '<p>abc</p>' );
  501. } );
  502. } );
  503. } );
  504. describe( 'on', () => {
  505. it( 'accepts plain binding', () => {
  506. const spy = testUtils.sinon.spy();
  507. setTestViewClass( function() {
  508. return {
  509. tag: 'p',
  510. on: {
  511. x: 'a',
  512. }
  513. };
  514. } );
  515. setTestViewInstance();
  516. view.on( 'a', spy );
  517. dispatchEvent( view.element, 'x' );
  518. sinon.assert.calledWithExactly( spy,
  519. sinon.match.has( 'name', 'a' ),
  520. sinon.match.has( 'target', view.element )
  521. );
  522. } );
  523. it( 'accepts an array of event bindings', () => {
  524. const spy1 = testUtils.sinon.spy();
  525. const spy2 = testUtils.sinon.spy();
  526. setTestViewClass( function() {
  527. return {
  528. tag: 'p',
  529. on: {
  530. x: [ 'a', 'b' ]
  531. }
  532. };
  533. } );
  534. setTestViewInstance();
  535. view.on( 'a', spy1 );
  536. view.on( 'b', spy2 );
  537. dispatchEvent( view.element, 'x' );
  538. sinon.assert.calledWithExactly( spy1,
  539. sinon.match.has( 'name', 'a' ),
  540. sinon.match.has( 'target', view.element )
  541. );
  542. sinon.assert.calledWithExactly( spy2,
  543. sinon.match.has( 'name', 'b' ),
  544. sinon.match.has( 'target', view.element )
  545. );
  546. } );
  547. it( 'accepts DOM selectors', () => {
  548. const spy1 = testUtils.sinon.spy();
  549. const spy2 = testUtils.sinon.spy();
  550. const spy3 = testUtils.sinon.spy();
  551. setTestViewClass( function() {
  552. return {
  553. tag: 'p',
  554. children: [
  555. {
  556. tag: 'span',
  557. attributes: {
  558. 'class': 'y',
  559. },
  560. on: {
  561. 'test@p': 'c'
  562. }
  563. },
  564. {
  565. tag: 'div',
  566. children: [
  567. {
  568. tag: 'span',
  569. attributes: {
  570. 'class': 'y',
  571. }
  572. }
  573. ],
  574. }
  575. ],
  576. on: {
  577. 'test@.y': 'a',
  578. 'test@div': 'b'
  579. }
  580. };
  581. } );
  582. setTestViewInstance();
  583. view.on( 'a', spy1 );
  584. view.on( 'b', spy2 );
  585. view.on( 'c', spy3 );
  586. // Test "test@p".
  587. dispatchEvent( view.element, 'test' );
  588. sinon.assert.callCount( spy1, 0 );
  589. sinon.assert.callCount( spy2, 0 );
  590. sinon.assert.callCount( spy3, 0 );
  591. // Test "test@.y".
  592. dispatchEvent( view.element.firstChild, 'test' );
  593. expect( spy1.firstCall.calledWithExactly(
  594. sinon.match.has( 'name', 'a' ),
  595. sinon.match.has( 'target', view.element.firstChild )
  596. ) ).to.be.true;
  597. sinon.assert.callCount( spy2, 0 );
  598. sinon.assert.callCount( spy3, 0 );
  599. // Test "test@div".
  600. dispatchEvent( view.element.lastChild, 'test' );
  601. sinon.assert.callCount( spy1, 1 );
  602. expect( spy2.firstCall.calledWithExactly(
  603. sinon.match.has( 'name', 'b' ),
  604. sinon.match.has( 'target', view.element.lastChild )
  605. ) ).to.be.true;
  606. sinon.assert.callCount( spy3, 0 );
  607. // Test "test@.y".
  608. dispatchEvent( view.element.lastChild.firstChild, 'test' );
  609. expect( spy1.secondCall.calledWithExactly(
  610. sinon.match.has( 'name', 'a' ),
  611. sinon.match.has( 'target', view.element.lastChild.firstChild )
  612. ) ).to.be.true;
  613. sinon.assert.callCount( spy2, 1 );
  614. sinon.assert.callCount( spy3, 0 );
  615. } );
  616. it( 'accepts function callbacks', () => {
  617. const spy1 = testUtils.sinon.spy();
  618. const spy2 = testUtils.sinon.spy();
  619. setTestViewClass( function() {
  620. return {
  621. tag: 'p',
  622. children: [
  623. {
  624. tag: 'span'
  625. }
  626. ],
  627. on: {
  628. x: spy1,
  629. 'y@span': [ spy2, 'c' ],
  630. }
  631. };
  632. } );
  633. setTestViewInstance();
  634. dispatchEvent( view.element, 'x' );
  635. dispatchEvent( view.element.firstChild, 'y' );
  636. sinon.assert.calledWithExactly( spy1,
  637. sinon.match.has( 'target', view.element )
  638. );
  639. sinon.assert.calledWithExactly( spy2,
  640. sinon.match.has( 'target', view.element.firstChild )
  641. );
  642. } );
  643. it( 'supports event delegation', () => {
  644. const spy = testUtils.sinon.spy();
  645. setTestViewClass( function() {
  646. return {
  647. tag: 'p',
  648. children: [
  649. {
  650. tag: 'span'
  651. }
  652. ],
  653. on: {
  654. x: 'a',
  655. }
  656. };
  657. } );
  658. setTestViewInstance();
  659. view.on( 'a', spy );
  660. dispatchEvent( view.element.firstChild, 'x' );
  661. sinon.assert.calledWithExactly( spy,
  662. sinon.match.has( 'name', 'a' ),
  663. sinon.match.has( 'target', view.element.firstChild )
  664. );
  665. } );
  666. it( 'works for future elements', () => {
  667. const spy = testUtils.sinon.spy();
  668. setTestViewClass( function() {
  669. return {
  670. tag: 'p',
  671. on: {
  672. 'test@div': 'a'
  673. }
  674. };
  675. } );
  676. setTestViewInstance();
  677. view.on( 'a', spy );
  678. const div = document.createElement( 'div' );
  679. view.element.appendChild( div );
  680. dispatchEvent( div, 'test' );
  681. sinon.assert.calledWithExactly( spy, sinon.match.has( 'name', 'a' ), sinon.match.has( 'target', div ) );
  682. } );
  683. } );
  684. describe( 'destroy', () => {
  685. beforeEach( createViewInstanceWithTemplate );
  686. it( 'should destroy the view', () => {
  687. view.destroy();
  688. expect( view.model ).to.be.null;
  689. expect( view.regions ).to.be.null;
  690. expect( view.template ).to.be.null;
  691. expect( view._regionsSelectors ).to.be.null;
  692. expect( view._element ).to.be.null;
  693. expect( view._template ).to.be.null;
  694. } );
  695. it( 'detaches the element from DOM', () => {
  696. const elRef = view.element;
  697. document.createElement( 'div' ).appendChild( view.element );
  698. view.destroy();
  699. expect( elRef.parentNode ).to.be.null;
  700. } );
  701. it( 'destroys child regions', () => {
  702. const region = new Region( 'x' );
  703. const spy = testUtils.sinon.spy( region, 'destroy' );
  704. const regionsRef = view.regions;
  705. const regionViewsRef = region.views;
  706. view.register( region, true );
  707. view.regions.get( 'x' ).views.add( new View() );
  708. view.destroy();
  709. expect( regionsRef.length ).to.be.equal( 0 );
  710. expect( regionViewsRef.length ).to.be.equal( 0 );
  711. expect( spy.calledOnce ).to.be.true;
  712. } );
  713. it( 'detaches bound model listeners', () => {
  714. setTestViewClass( function() {
  715. const bind = this.attributeBinder;
  716. return {
  717. tag: 'p',
  718. children: [
  719. { text: bind.to( 'foo' ) }
  720. ]
  721. };
  722. } );
  723. setTestViewInstance( { foo: 'bar' } );
  724. const modelRef = view.model;
  725. const elRef = view.element;
  726. expect( view.element.outerHTML ).to.be.equal( '<p>bar</p>' );
  727. modelRef.foo = 'baz';
  728. expect( view.element.outerHTML ).to.be.equal( '<p>baz</p>' );
  729. view.destroy();
  730. modelRef.foo = 'abc';
  731. expect( elRef.outerHTML ).to.be.equal( '<p>baz</p>' );
  732. } );
  733. it( 'destroy a template–less view', () => {
  734. view = new View();
  735. expect( () => {
  736. view.destroy();
  737. } ).to.not.throw();
  738. } );
  739. } );
  740. describe( 'applyTemplateToElement', () => {
  741. beforeEach( () => {
  742. setTestViewClass();
  743. setTestViewInstance( { a: 'foo', b: 42 } );
  744. } );
  745. it( 'should initialize attribute bindings', () => {
  746. const el = document.createElement( 'div' );
  747. const bind = view.attributeBinder;
  748. view.applyTemplateToElement( el, {
  749. tag: 'div',
  750. attributes: {
  751. class: bind.to( 'b' ),
  752. id: 'foo',
  753. checked: 'checked'
  754. }
  755. } );
  756. expect( el.outerHTML ).to.be.equal( '<div class="42" id="foo" checked="checked"></div>' );
  757. view.model.b = 64;
  758. expect( el.outerHTML ).to.be.equal( '<div class="64" id="foo" checked="checked"></div>' );
  759. } );
  760. it( 'should initialize DOM listeners', () => {
  761. const el = document.createElement( 'div' );
  762. const spy = testUtils.sinon.spy();
  763. view.applyTemplateToElement( el, {
  764. tag: 'div',
  765. on: {
  766. click: spy
  767. }
  768. } );
  769. document.body.appendChild( el );
  770. dispatchEvent( el, 'click' );
  771. sinon.assert.calledOnce( spy );
  772. view.stopListening( el, 'click' );
  773. dispatchEvent( el, 'click' );
  774. sinon.assert.calledOnce( spy );
  775. } );
  776. it( 'should work for deep DOM structure', () => {
  777. const el = document.createElement( 'div' );
  778. const childA = document.createElement( 'a' );
  779. const childB = document.createElement( 'b' );
  780. childA.textContent = 'anchor';
  781. childB.textContent = 'bold';
  782. el.appendChild( childA );
  783. el.appendChild( childB );
  784. expect( el.outerHTML ).to.be.equal( '<div><a>anchor</a><b>bold</b></div>' );
  785. const spy1 = testUtils.sinon.spy();
  786. const spy2 = testUtils.sinon.spy();
  787. const spy3 = testUtils.sinon.spy();
  788. const bind = view.attributeBinder;
  789. view.applyTemplateToElement( el, {
  790. tag: 'div',
  791. children: [
  792. {
  793. tag: 'a',
  794. on: {
  795. keyup: spy2
  796. },
  797. attributes: {
  798. class: bind.to( 'b', b => 'applied-A-' + b ),
  799. id: 'applied-A'
  800. },
  801. children: [ 'Text applied to childA.' ]
  802. },
  803. {
  804. tag: 'b',
  805. on: {
  806. keydown: spy3
  807. },
  808. attributes: {
  809. class: bind.to( 'b', b => 'applied-B-' + b ),
  810. id: 'applied-B'
  811. },
  812. children: [ 'Text applied to childB.' ]
  813. },
  814. 'Text which is not to be applied because it does NOT exist in original element.'
  815. ],
  816. on: {
  817. 'mouseover@a': spy1
  818. },
  819. attributes: {
  820. id: bind.to( 'a', a => a.toUpperCase() ),
  821. class: bind.to( 'b', b => 'applied-parent-' + b )
  822. }
  823. } );
  824. expect( el.outerHTML ).to.be.equal( '<div id="FOO" class="applied-parent-42">' +
  825. '<a class="applied-A-42" id="applied-A">Text applied to childA.</a>' +
  826. '<b class="applied-B-42" id="applied-B">Text applied to childB.</b>' +
  827. '</div>' );
  828. view.model.b = 16;
  829. expect( el.outerHTML ).to.be.equal( '<div id="FOO" class="applied-parent-16">' +
  830. '<a class="applied-A-16" id="applied-A">Text applied to childA.</a>' +
  831. '<b class="applied-B-16" id="applied-B">Text applied to childB.</b>' +
  832. '</div>' );
  833. document.body.appendChild( el );
  834. // Test "mouseover@a".
  835. dispatchEvent( el, 'mouseover' );
  836. dispatchEvent( childA, 'mouseover' );
  837. // Test "keyup".
  838. dispatchEvent( childA, 'keyup' );
  839. // Test "keydown".
  840. dispatchEvent( childB, 'keydown' );
  841. sinon.assert.calledOnce( spy1 );
  842. sinon.assert.calledOnce( spy2 );
  843. sinon.assert.calledOnce( spy3 );
  844. } );
  845. it( 're–uses a template definition object without redundant re–definition of "on" listener attachers', () => {
  846. const el1 = document.createElement( 'div' );
  847. const el2 = document.createElement( 'div' );
  848. const spy = testUtils.sinon.spy();
  849. const def = {
  850. tag: 'div',
  851. on: {
  852. click: spy
  853. }
  854. };
  855. view.applyTemplateToElement( el1, def );
  856. const attacherFn = def.on.click;
  857. view.applyTemplateToElement( el2, def );
  858. // Attacher function didn't change because it's still the same View instance.
  859. expect( attacherFn ).to.be.equal( def.on.click );
  860. expect( def.on._listenerView ).to.be.equal( view );
  861. document.body.appendChild( el1 );
  862. document.body.appendChild( el2 );
  863. dispatchEvent( el1, 'click' );
  864. sinon.assert.calledOnce( spy );
  865. dispatchEvent( el2, 'click' );
  866. sinon.assert.calledTwice( spy );
  867. } );
  868. it( 'shares a template definition between View instances – event listeners', () => {
  869. const el = document.createElement( 'div' );
  870. const spy = testUtils.sinon.spy();
  871. const view1 = new View();
  872. const view2 = new View();
  873. const def = {
  874. tag: 'div',
  875. on: {
  876. click: spy
  877. }
  878. };
  879. document.body.appendChild( el );
  880. view1.applyTemplateToElement( el, def );
  881. expect( def.on._listenerView ).to.be.equal( view1 );
  882. dispatchEvent( el, 'click' );
  883. sinon.assert.calledOnce( spy );
  884. // Use another view1 to see if attachers are re–defined.
  885. view2.applyTemplateToElement( el, def );
  886. expect( def.on._listenerView ).to.be.equal( view2 );
  887. dispatchEvent( el, 'click' );
  888. // Spy was called for view1 (1st dispatch), then for view1 and view2 (2nd dispatch).
  889. sinon.assert.calledThrice( spy );
  890. } );
  891. } );
  892. } );
  893. function createViewInstanceWithTemplate() {
  894. setTestViewClass( () => ( { tag: 'a' } ) );
  895. setTestViewInstance();
  896. }
  897. function setTestViewClass( templateFn, regionsFn ) {
  898. TestView = class V extends View {
  899. constructor( model ) {
  900. super( model );
  901. if ( templateFn ) {
  902. this.template = templateFn.call( this );
  903. }
  904. if ( templateFn && regionsFn ) {
  905. regionsFn.call( this );
  906. }
  907. }
  908. };
  909. }
  910. function setTestViewInstance( model ) {
  911. view = new TestView( new Model( model ) );
  912. if ( view.template ) {
  913. document.body.appendChild( view.element );
  914. }
  915. }
  916. function dispatchEvent( el, domEvtName ) {
  917. if ( !el.parentNode ) {
  918. throw new Error( 'To dispatch an event, element must be in DOM. Otherwise #target is null.' );
  919. }
  920. el.dispatchEvent( new Event( domEvtName, {
  921. bubbles: true
  922. } ) );
  923. }