buttonview.js 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. /**
  2. * @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  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. describe( 'ButtonView', () => {
  13. let locale, view;
  14. testUtils.createSinonSandbox();
  15. beforeEach( () => {
  16. locale = { t() {} };
  17. view = new ButtonView( locale );
  18. view.render();
  19. } );
  20. describe( 'constructor()', () => {
  21. it( 'creates view#children collection', () => {
  22. expect( view.children ).to.be.instanceOf( ViewCollection );
  23. } );
  24. it( 'creates #tooltipView', () => {
  25. expect( view.tooltipView ).to.be.instanceOf( TooltipView );
  26. } );
  27. it( 'creates #labelView', () => {
  28. expect( view.labelView ).to.be.instanceOf( View );
  29. expect( view.labelView.element.classList.contains( 'ck' ) ).to.be.true;
  30. expect( view.labelView.element.classList.contains( 'ck-button__label' ) ).to.be.true;
  31. } );
  32. it( 'creates #iconView', () => {
  33. expect( view.iconView ).to.be.instanceOf( IconView );
  34. } );
  35. } );
  36. describe( '<button> bindings', () => {
  37. describe( 'class', () => {
  38. it( 'is set initially', () => {
  39. expect( view.element.classList ).to.have.length( 3 );
  40. expect( view.element.classList.contains( 'ck' ) ).to.true;
  41. expect( view.element.classList.contains( 'ck-button' ) ).to.true;
  42. expect( view.element.classList.contains( 'ck-disabled' ) ).to.false;
  43. expect( view.element.classList.contains( 'ck-off' ) ).to.true;
  44. } );
  45. it( 'reacts on view#isEnabled', () => {
  46. view.isEnabled = true;
  47. expect( view.element.classList.contains( 'ck-disabled' ) ).to.false;
  48. view.isEnabled = false;
  49. expect( view.element.classList.contains( 'ck-disabled' ) ).to.true;
  50. } );
  51. it( 'reacts on view#isOn', () => {
  52. view.isOn = true;
  53. expect( view.element.classList.contains( 'ck-on' ) ).to.true;
  54. view.isOn = false;
  55. expect( view.element.classList.contains( 'ck-on' ) ).to.false;
  56. } );
  57. it( 'reacts on view#isVisible', () => {
  58. view.isVisible = true;
  59. expect( view.element.classList.contains( 'ck-hidden' ) ).to.be.false;
  60. view.isVisible = false;
  61. expect( view.element.classList.contains( 'ck-hidden' ) ).to.be.true;
  62. } );
  63. it( 'reacts on view#withText', () => {
  64. view.withText = true;
  65. expect( view.element.classList.contains( 'ck-button_with-text' ) ).to.true;
  66. view.withText = false;
  67. expect( view.element.classList.contains( 'ck-button_with-text' ) ).to.false;
  68. } );
  69. it( 'reacts on view#type', () => {
  70. // Default value.
  71. expect( view.element.getAttribute( 'type' ) ).to.equal( 'button' );
  72. view.type = 'submit';
  73. expect( view.element.getAttribute( 'type' ) ).to.equal( 'submit' );
  74. // Default value.
  75. view.type = null;
  76. expect( view.element.getAttribute( 'type' ) ).to.equal( 'button' );
  77. } );
  78. it( 'reacts on view#class', () => {
  79. view.set( 'class', 'foo' );
  80. expect( view.element.classList.contains( 'foo' ) ).to.be.true;
  81. } );
  82. } );
  83. describe( 'labelView', () => {
  84. it( 'reacts on view#labelStyle', () => {
  85. expect( view.labelView.element.attributes.getNamedItem( 'style' ) ).to.be.null;
  86. view.labelStyle = 'color: red';
  87. expect( view.labelView.element.attributes.getNamedItem( 'style' ).value ).to.equal( 'color: red' );
  88. } );
  89. } );
  90. describe( 'tooltip', () => {
  91. it( 'is initially set', () => {
  92. expect( view.children.getIndex( view.tooltipView ) ).to.equal( 0 );
  93. } );
  94. it( 'it reacts to #tooltipPosition attribute', () => {
  95. view.tooltip = 'foo';
  96. view.icon = '<svg></svg>';
  97. expect( view.tooltipPosition ).to.equal( 's' );
  98. expect( view.tooltipView.position ).to.equal( 's' );
  99. view.tooltipPosition = 'n';
  100. expect( view.tooltipView.position ).to.equal( 'n' );
  101. } );
  102. describe( 'defined as a Boolean', () => {
  103. it( 'renders tooltip text out of #label and #keystroke', () => {
  104. view.tooltip = true;
  105. view.label = 'bar';
  106. view.keystroke = 'A';
  107. expect( view.tooltipView.text ).to.equal( 'bar (A)' );
  108. } );
  109. it( 'not render tooltip text when #tooltip value is false', () => {
  110. view.tooltip = false;
  111. view.label = 'bar';
  112. view.keystroke = 'A';
  113. expect( view.tooltipView.text ).to.equal( '' );
  114. } );
  115. it( 'reacts to changes in #label and #keystroke', () => {
  116. view.tooltip = true;
  117. view.label = 'foo';
  118. view.keystroke = 'B';
  119. expect( view.tooltipView.text ).to.equal( 'foo (B)' );
  120. view.label = 'baz';
  121. view.keystroke = false;
  122. expect( view.tooltipView.text ).to.equal( 'baz' );
  123. } );
  124. } );
  125. describe( 'defined as a String', () => {
  126. it( 'renders as a plain text', () => {
  127. view.tooltip = 'bar';
  128. view.label = 'foo';
  129. view.keystroke = 'A';
  130. expect( view.tooltipView.text ).to.equal( 'bar' );
  131. } );
  132. it( 'reacts to changes of #tooltip', () => {
  133. view.tooltip = 'bar';
  134. expect( view.tooltipView.text ).to.equal( 'bar' );
  135. view.tooltip = 'foo';
  136. expect( view.tooltipView.text ).to.equal( 'foo' );
  137. } );
  138. } );
  139. describe( 'defined as a Function', () => {
  140. it( 'generates a tooltip text when passed #label and #keystroke', () => {
  141. view.tooltip = ( l, k ) => `${ l } - ${ k }`;
  142. view.label = 'foo';
  143. view.keystroke = 'A';
  144. expect( view.tooltipView.text ).to.equal( 'foo - A' );
  145. } );
  146. it( 'reacts to changes of #label and #keystroke', () => {
  147. view.tooltip = ( l, k ) => `${ l } - ${ k }`;
  148. view.label = 'foo';
  149. view.keystroke = 'A';
  150. expect( view.tooltipView.text ).to.equal( 'foo - A' );
  151. view.label = 'bar';
  152. view.keystroke = 'B';
  153. expect( view.tooltipView.text ).to.equal( 'bar - B' );
  154. } );
  155. } );
  156. } );
  157. describe( 'text', () => {
  158. it( 'is not initially set ', () => {
  159. expect( view.element.textContent ).to.equal( '' );
  160. } );
  161. it( 'reacts on view#label', () => {
  162. view.label = 'bar';
  163. expect( view.element.textContent ).to.equal( 'bar' );
  164. } );
  165. } );
  166. describe( 'tabindex', () => {
  167. it( 'is initially set ', () => {
  168. expect( view.element.attributes.tabindex.value ).to.equal( '-1' );
  169. } );
  170. it( 'reacts on view#tabindex', () => {
  171. view.tabindex = 3;
  172. expect( view.element.attributes.tabindex.value ).to.equal( '3' );
  173. } );
  174. } );
  175. describe( 'aria', () => {
  176. it( '-labelledby is set', () => {
  177. expect( view.element.attributes[ 'aria-labelledby' ].value )
  178. .to.equal( view.element.lastChild.id )
  179. .to.match( /^ck-editor__aria-label_\w+$/ );
  180. } );
  181. it( '-disabled reacts to #isEnabled', () => {
  182. view.isEnabled = true;
  183. expect( view.element.attributes[ 'aria-disabled' ] ).to.be.undefined;
  184. view.isEnabled = false;
  185. expect( view.element.attributes[ 'aria-disabled' ].value ).to.equal( 'true' );
  186. } );
  187. it( '-pressed reacts to #isOn', () => {
  188. view.isOn = true;
  189. expect( view.element.attributes[ 'aria-pressed' ].value ).to.equal( 'true' );
  190. view.isOn = false;
  191. expect( view.element.attributes[ 'aria-pressed' ] ).to.be.undefined;
  192. } );
  193. } );
  194. describe( 'mousedown event', () => {
  195. it( 'should be prevented', () => {
  196. const ret = view.element.dispatchEvent( new Event( 'mousedown', { cancelable: true } ) );
  197. expect( ret ).to.false;
  198. } );
  199. } );
  200. describe( 'execute event', () => {
  201. it( 'triggers view#execute event if button is not disabled', () => {
  202. const spy = sinon.spy();
  203. view.on( 'execute', spy );
  204. view.set( 'isEnabled', true );
  205. view.element.dispatchEvent( new Event( 'click' ) );
  206. sinon.assert.callCount( spy, 1 );
  207. view.isEnabled = false;
  208. view.element.dispatchEvent( new Event( 'click' ) );
  209. sinon.assert.callCount( spy, 1 );
  210. } );
  211. } );
  212. } );
  213. describe( 'icon', () => {
  214. it( 'is omited in #children when view#icon is not defined', () => {
  215. view = new ButtonView( locale );
  216. view.render();
  217. expect( view.element.childNodes ).to.have.length( 2 );
  218. expect( view.iconView.element ).to.be.null;
  219. } );
  220. it( 'is added to the #children when view#icon is defined', () => {
  221. view = new ButtonView( locale );
  222. view.icon = '<svg></svg>';
  223. view.render();
  224. expect( view.element.childNodes ).to.have.length( 3 );
  225. expect( view.element.childNodes[ 0 ] ).to.equal( view.iconView.element );
  226. expect( view.iconView ).to.instanceOf( IconView );
  227. expect( view.iconView.content ).to.equal( '<svg></svg>' );
  228. expect( view.iconView.element.classList.contains( 'ck-button__icon' ) ).to.be.true;
  229. view.icon = '<svg>bar</svg>';
  230. expect( view.iconView.content ).to.equal( '<svg>bar</svg>' );
  231. } );
  232. it( 'is destroyed with the view', () => {
  233. view = new ButtonView( locale );
  234. view.icon = '<svg></svg>';
  235. view.render();
  236. const spy = sinon.spy( view.iconView, 'destroy' );
  237. view.destroy();
  238. sinon.assert.calledOnce( spy );
  239. } );
  240. } );
  241. describe( 'focus()', () => {
  242. it( 'focuses the button in DOM', () => {
  243. const spy = sinon.spy( view.element, 'focus' );
  244. view.focus();
  245. sinon.assert.calledOnce( spy );
  246. } );
  247. } );
  248. } );