view.js 29 KB

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