8
0

buttonview.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  1. /**
  2. * @license Copyright (c) 2003-2020, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
  4. */
  5. /* globals Event */
  6. import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
  7. import ButtonView from '../../src/button/buttonview';
  8. import IconView from '../../src/icon/iconview';
  9. import TooltipView from '../../src/tooltip/tooltipview';
  10. import View from '../../src/view';
  11. import ViewCollection from '../../src/viewcollection';
  12. import env from '@ckeditor/ckeditor5-utils/src/env';
  13. describe( 'ButtonView', () => {
  14. let locale, view;
  15. testUtils.createSinonSandbox();
  16. beforeEach( () => {
  17. locale = { t() {} };
  18. view = new ButtonView( locale );
  19. view.render();
  20. } );
  21. afterEach( () => {
  22. view.destroy();
  23. } );
  24. describe( 'constructor()', () => {
  25. it( 'creates view#children collection', () => {
  26. expect( view.children ).to.be.instanceOf( ViewCollection );
  27. } );
  28. it( 'creates #tooltipView', () => {
  29. expect( view.tooltipView ).to.be.instanceOf( TooltipView );
  30. } );
  31. it( 'creates #labelView', () => {
  32. expect( view.labelView ).to.be.instanceOf( View );
  33. expect( view.labelView.element.classList.contains( 'ck' ) ).to.be.true;
  34. expect( view.labelView.element.classList.contains( 'ck-button__label' ) ).to.be.true;
  35. } );
  36. it( 'creates #keystrokeView', () => {
  37. expect( view.keystrokeView ).to.be.instanceOf( View );
  38. } );
  39. it( 'creates #iconView', () => {
  40. expect( view.iconView ).to.be.instanceOf( IconView );
  41. } );
  42. } );
  43. describe( '<button> bindings', () => {
  44. describe( 'class', () => {
  45. it( 'is set initially', () => {
  46. expect( view.element.classList ).to.have.length( 3 );
  47. expect( view.element.classList.contains( 'ck' ) ).to.true;
  48. expect( view.element.classList.contains( 'ck-button' ) ).to.true;
  49. expect( view.element.classList.contains( 'ck-disabled' ) ).to.false;
  50. expect( view.element.classList.contains( 'ck-off' ) ).to.true;
  51. } );
  52. it( 'reacts on view#isEnabled', () => {
  53. view.isEnabled = true;
  54. expect( view.element.classList.contains( 'ck-disabled' ) ).to.false;
  55. view.isEnabled = false;
  56. expect( view.element.classList.contains( 'ck-disabled' ) ).to.true;
  57. } );
  58. it( 'reacts on view#isOn', () => {
  59. view.isOn = true;
  60. expect( view.element.classList.contains( 'ck-on' ) ).to.true;
  61. view.isOn = false;
  62. expect( view.element.classList.contains( 'ck-on' ) ).to.false;
  63. } );
  64. it( 'reacts on view#isVisible', () => {
  65. view.isVisible = true;
  66. expect( view.element.classList.contains( 'ck-hidden' ) ).to.be.false;
  67. view.isVisible = false;
  68. expect( view.element.classList.contains( 'ck-hidden' ) ).to.be.true;
  69. } );
  70. it( 'reacts on view#withText', () => {
  71. view.withText = true;
  72. expect( view.element.classList.contains( 'ck-button_with-text' ) ).to.true;
  73. view.withText = false;
  74. expect( view.element.classList.contains( 'ck-button_with-text' ) ).to.false;
  75. } );
  76. it( 'reacts on view#withKeystroke', () => {
  77. view.withKeystroke = true;
  78. expect( view.element.classList.contains( 'ck-button_with-keystroke' ) ).to.true;
  79. view.withKeystroke = false;
  80. expect( view.element.classList.contains( 'ck-button_with-keystroke' ) ).to.false;
  81. } );
  82. it( 'reacts on view#type', () => {
  83. // Default value.
  84. expect( view.element.getAttribute( 'type' ) ).to.equal( 'button' );
  85. view.type = 'submit';
  86. expect( view.element.getAttribute( 'type' ) ).to.equal( 'submit' );
  87. // Default value.
  88. view.type = null;
  89. expect( view.element.getAttribute( 'type' ) ).to.equal( 'button' );
  90. } );
  91. it( 'reacts on view#class', () => {
  92. view.set( 'class', 'foo' );
  93. expect( view.element.classList.contains( 'foo' ) ).to.be.true;
  94. } );
  95. } );
  96. describe( 'labelView', () => {
  97. it( 'reacts on view#labelStyle', () => {
  98. expect( view.labelView.element.attributes.getNamedItem( 'style' ) ).to.be.null;
  99. view.labelStyle = 'color: red';
  100. expect( view.labelView.element.attributes.getNamedItem( 'style' ).value ).to.equal( 'color: red' );
  101. } );
  102. } );
  103. describe( 'tooltip', () => {
  104. it( 'is initially set', () => {
  105. expect( view.children.getIndex( view.tooltipView ) ).to.equal( 0 );
  106. } );
  107. it( 'it reacts to #tooltipPosition attribute', () => {
  108. view.tooltip = 'foo';
  109. view.icon = '<svg></svg>';
  110. expect( view.tooltipPosition ).to.equal( 's' );
  111. expect( view.tooltipView.position ).to.equal( 's' );
  112. view.tooltipPosition = 'n';
  113. expect( view.tooltipView.position ).to.equal( 'n' );
  114. } );
  115. describe( 'defined as a Boolean', () => {
  116. it( 'renders tooltip text out of #label and #keystroke', () => {
  117. view.tooltip = true;
  118. view.label = 'bar';
  119. view.keystroke = 'A';
  120. expect( view.tooltipView.text ).to.equal( 'bar (A)' );
  121. } );
  122. it( 'not render tooltip text when #tooltip value is false', () => {
  123. view.tooltip = false;
  124. view.label = 'bar';
  125. view.keystroke = 'A';
  126. expect( view.tooltipView.text ).to.equal( '' );
  127. } );
  128. it( 'reacts to changes in #label and #keystroke', () => {
  129. view.tooltip = true;
  130. view.label = 'foo';
  131. view.keystroke = 'B';
  132. expect( view.tooltipView.text ).to.equal( 'foo (B)' );
  133. view.label = 'baz';
  134. view.keystroke = false;
  135. expect( view.tooltipView.text ).to.equal( 'baz' );
  136. } );
  137. } );
  138. describe( 'defined as a String', () => {
  139. it( 'renders as a plain text', () => {
  140. view.tooltip = 'bar';
  141. view.label = 'foo';
  142. view.keystroke = 'A';
  143. expect( view.tooltipView.text ).to.equal( 'bar' );
  144. } );
  145. it( 'reacts to changes of #tooltip', () => {
  146. view.tooltip = 'bar';
  147. expect( view.tooltipView.text ).to.equal( 'bar' );
  148. view.tooltip = 'foo';
  149. expect( view.tooltipView.text ).to.equal( 'foo' );
  150. } );
  151. } );
  152. describe( 'defined as a Function', () => {
  153. it( 'generates a tooltip text when passed #label and #keystroke', () => {
  154. view.tooltip = ( l, k ) => `${ l } - ${ k }`;
  155. view.label = 'foo';
  156. view.keystroke = 'A';
  157. expect( view.tooltipView.text ).to.equal( 'foo - A' );
  158. } );
  159. it( 'reacts to changes of #label and #keystroke', () => {
  160. view.tooltip = ( l, k ) => `${ l } - ${ k }`;
  161. view.label = 'foo';
  162. view.keystroke = 'A';
  163. expect( view.tooltipView.text ).to.equal( 'foo - A' );
  164. view.label = 'bar';
  165. view.keystroke = 'B';
  166. expect( view.tooltipView.text ).to.equal( 'bar - B' );
  167. } );
  168. } );
  169. } );
  170. describe( 'text', () => {
  171. it( 'is not initially set ', () => {
  172. expect( view.element.textContent ).to.equal( '' );
  173. } );
  174. it( 'reacts on view#label', () => {
  175. view.label = 'bar';
  176. expect( view.element.textContent ).to.equal( 'bar' );
  177. } );
  178. } );
  179. describe( 'tabindex', () => {
  180. it( 'is initially set ', () => {
  181. expect( view.element.attributes.tabindex.value ).to.equal( '-1' );
  182. } );
  183. it( 'reacts on view#tabindex', () => {
  184. view.tabindex = 3;
  185. expect( view.element.attributes.tabindex.value ).to.equal( '3' );
  186. } );
  187. } );
  188. describe( 'aria', () => {
  189. it( '-labelledby is set', () => {
  190. expect( view.element.attributes[ 'aria-labelledby' ].value )
  191. .to.equal( view.element.lastChild.id )
  192. .to.match( /^ck-editor__aria-label_\w+$/ );
  193. } );
  194. it( '-disabled reacts to #isEnabled', () => {
  195. view.isEnabled = true;
  196. expect( view.element.attributes[ 'aria-disabled' ] ).to.be.undefined;
  197. view.isEnabled = false;
  198. expect( view.element.attributes[ 'aria-disabled' ].value ).to.equal( 'true' );
  199. } );
  200. it( '-pressed reacts to #isOn', () => {
  201. view.isToggleable = true;
  202. view.isOn = true;
  203. expect( view.element.attributes[ 'aria-pressed' ].value ).to.equal( 'true' );
  204. view.isOn = false;
  205. expect( view.element.attributes[ 'aria-pressed' ].value ).to.equal( 'false' );
  206. } );
  207. it( '-pressed is not present for non–toggleable button', () => {
  208. view.isOn = true;
  209. expect( view.element.hasAttribute( 'aria-pressed' ) ).to.be.false;
  210. view.isOn = false;
  211. expect( view.element.hasAttribute( 'aria-pressed' ) ).to.be.false;
  212. } );
  213. } );
  214. describe( 'mousedown event', () => {
  215. it( 'should be prevented', () => {
  216. const ret = view.element.dispatchEvent( new Event( 'mousedown', { cancelable: true } ) );
  217. expect( ret ).to.false;
  218. } );
  219. } );
  220. describe( 'execute event', () => {
  221. it( 'triggers view#execute event if button is not disabled', () => {
  222. const spy = sinon.spy();
  223. view.on( 'execute', spy );
  224. view.set( 'isEnabled', true );
  225. view.element.dispatchEvent( new Event( 'click' ) );
  226. sinon.assert.callCount( spy, 1 );
  227. view.isEnabled = false;
  228. view.element.dispatchEvent( new Event( 'click' ) );
  229. sinon.assert.callCount( spy, 1 );
  230. } );
  231. } );
  232. } );
  233. describe( '#iconView', () => {
  234. it( 'is omited in #children when view#icon is not defined', () => {
  235. view = new ButtonView( locale );
  236. view.render();
  237. expect( view.element.childNodes ).to.have.length( 2 );
  238. expect( view.iconView.element ).to.be.null;
  239. } );
  240. it( 'is added to the #children when view#icon is defined', () => {
  241. view = new ButtonView( locale );
  242. view.icon = '<svg></svg>';
  243. view.render();
  244. expect( view.element.childNodes ).to.have.length( 3 );
  245. expect( view.element.childNodes[ 0 ] ).to.equal( view.iconView.element );
  246. expect( view.iconView ).to.instanceOf( IconView );
  247. expect( view.iconView.content ).to.equal( '<svg></svg>' );
  248. expect( view.iconView.element.classList.contains( 'ck-button__icon' ) ).to.be.true;
  249. view.icon = '<svg>bar</svg>';
  250. expect( view.iconView.content ).to.equal( '<svg>bar</svg>' );
  251. } );
  252. it( 'is destroyed with the view', () => {
  253. view = new ButtonView( locale );
  254. view.icon = '<svg></svg>';
  255. view.render();
  256. const spy = sinon.spy( view.iconView, 'destroy' );
  257. view.destroy();
  258. sinon.assert.calledOnce( spy );
  259. } );
  260. } );
  261. describe( '#keystrokeView', () => {
  262. it( 'is omited in #children when view#icon is not defined', () => {
  263. view = new ButtonView( locale );
  264. view.render();
  265. expect( view.element.childNodes ).to.have.length( 2 );
  266. expect( view.keystrokeView.element ).to.be.null;
  267. } );
  268. it( 'is added to the #children when view#withKeystroke is true', () => {
  269. testUtils.sinon.stub( env, 'isMac' ).value( false );
  270. view = new ButtonView( locale );
  271. view.keystroke = 'Ctrl+A';
  272. view.withKeystroke = true;
  273. view.render();
  274. expect( view.element.childNodes ).to.have.length( 3 );
  275. expect( view.element.childNodes[ 2 ] ).to.equal( view.keystrokeView.element );
  276. expect( view.keystrokeView.element.classList.contains( 'ck' ) ).to.be.true;
  277. expect( view.keystrokeView.element.classList.contains( 'ck-button__keystroke' ) ).to.be.true;
  278. expect( view.keystrokeView ).to.instanceOf( View );
  279. expect( view.keystrokeView.element.textContent ).to.equal( 'Ctrl+A' );
  280. } );
  281. it( 'usese fancy kesytroke preview on Mac', () => {
  282. testUtils.sinon.stub( env, 'isMac' ).value( true );
  283. view = new ButtonView( locale );
  284. view.keystroke = 'Ctrl+A';
  285. view.withKeystroke = true;
  286. view.render();
  287. expect( view.keystrokeView.element.textContent ).to.equal( '⌘A' );
  288. } );
  289. it( 'is destroyed with the view', () => {
  290. view = new ButtonView( locale );
  291. view.keystroke = 'Ctrl+A';
  292. view.withKeystroke = true;
  293. view.render();
  294. const spy = sinon.spy( view.keystrokeView, 'destroy' );
  295. view.destroy();
  296. sinon.assert.calledOnce( spy );
  297. } );
  298. } );
  299. describe( 'focus()', () => {
  300. it( 'focuses the button in DOM', () => {
  301. const spy = sinon.spy( view.element, 'focus' );
  302. view.focus();
  303. sinon.assert.calledOnce( spy );
  304. } );
  305. } );
  306. } );