buttonview.js 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  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. testUtils.createSinonSandbox();
  13. describe( 'ButtonView', () => {
  14. let locale, view;
  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. } );
  79. describe( 'tooltip', () => {
  80. it( 'is initially set', () => {
  81. expect( view.children.getIndex( view.tooltipView ) ).to.equal( 0 );
  82. } );
  83. it( 'it reacts to #tooltipPosition attribute', () => {
  84. view.tooltip = 'foo';
  85. view.icon = '<svg></svg>';
  86. expect( view.tooltipPosition ).to.equal( 's' );
  87. expect( view.tooltipView.position ).to.equal( 's' );
  88. view.tooltipPosition = 'n';
  89. expect( view.tooltipView.position ).to.equal( 'n' );
  90. } );
  91. describe( 'defined as a Boolean', () => {
  92. it( 'renders tooltip text out of #label and #keystroke', () => {
  93. view.tooltip = true;
  94. view.label = 'bar';
  95. view.keystroke = 'A';
  96. expect( view.tooltipView.text ).to.equal( 'bar (A)' );
  97. } );
  98. it( 'not render tooltip text when #tooltip value is false', () => {
  99. view.tooltip = false;
  100. view.label = 'bar';
  101. view.keystroke = 'A';
  102. expect( view.tooltipView.text ).to.equal( '' );
  103. } );
  104. it( 'reacts to changes in #label and #keystroke', () => {
  105. view.tooltip = true;
  106. view.label = 'foo';
  107. view.keystroke = 'B';
  108. expect( view.tooltipView.text ).to.equal( 'foo (B)' );
  109. view.label = 'baz';
  110. view.keystroke = false;
  111. expect( view.tooltipView.text ).to.equal( 'baz' );
  112. } );
  113. } );
  114. describe( 'defined as a String', () => {
  115. it( 'renders as a plain text', () => {
  116. view.tooltip = 'bar';
  117. view.label = 'foo';
  118. view.keystroke = 'A';
  119. expect( view.tooltipView.text ).to.equal( 'bar' );
  120. } );
  121. it( 'reacts to changes of #tooltip', () => {
  122. view.tooltip = 'bar';
  123. expect( view.tooltipView.text ).to.equal( 'bar' );
  124. view.tooltip = 'foo';
  125. expect( view.tooltipView.text ).to.equal( 'foo' );
  126. } );
  127. } );
  128. describe( 'defined as a Function', () => {
  129. it( 'generates a tooltip text when passed #label and #keystroke', () => {
  130. view.tooltip = ( l, k ) => `${ l } - ${ k }`;
  131. view.label = 'foo';
  132. view.keystroke = 'A';
  133. expect( view.tooltipView.text ).to.equal( 'foo - A' );
  134. } );
  135. it( 'reacts to changes of #label and #keystroke', () => {
  136. view.tooltip = ( l, k ) => `${ l } - ${ k }`;
  137. view.label = 'foo';
  138. view.keystroke = 'A';
  139. expect( view.tooltipView.text ).to.equal( 'foo - A' );
  140. view.label = 'bar';
  141. view.keystroke = 'B';
  142. expect( view.tooltipView.text ).to.equal( 'bar - B' );
  143. } );
  144. } );
  145. } );
  146. describe( 'text', () => {
  147. it( 'is not initially set ', () => {
  148. expect( view.element.textContent ).to.equal( '' );
  149. } );
  150. it( 'reacts on view#label', () => {
  151. view.label = 'bar';
  152. expect( view.element.textContent ).to.equal( 'bar' );
  153. } );
  154. } );
  155. describe( 'tabindex', () => {
  156. it( 'is initially set ', () => {
  157. expect( view.element.attributes.tabindex.value ).to.equal( '-1' );
  158. } );
  159. it( 'reacts on view#tabindex', () => {
  160. view.tabindex = 3;
  161. expect( view.element.attributes.tabindex.value ).to.equal( '3' );
  162. } );
  163. } );
  164. describe( 'aria', () => {
  165. it( '-labelledby is set', () => {
  166. expect( view.element.attributes[ 'aria-labelledby' ].value )
  167. .to.equal( view.element.lastChild.id )
  168. .to.match( /^ck-editor__aria-label_\w+$/ );
  169. } );
  170. it( '-disabled reacts to #isEnabled', () => {
  171. view.isEnabled = true;
  172. expect( view.element.attributes[ 'aria-disabled' ] ).to.be.undefined;
  173. view.isEnabled = false;
  174. expect( view.element.attributes[ 'aria-disabled' ].value ).to.equal( 'true' );
  175. } );
  176. it( '-pressed reacts to #isOn', () => {
  177. view.isOn = true;
  178. expect( view.element.attributes[ 'aria-pressed' ].value ).to.equal( 'true' );
  179. view.isOn = false;
  180. expect( view.element.attributes[ 'aria-pressed' ] ).to.be.undefined;
  181. } );
  182. } );
  183. describe( 'mousedown event', () => {
  184. it( 'should be prevented', () => {
  185. const ret = view.element.dispatchEvent( new Event( 'mousedown', { cancelable: true } ) );
  186. expect( ret ).to.false;
  187. } );
  188. } );
  189. describe( 'execute event', () => {
  190. it( 'triggers view#execute event if button is not disabled', () => {
  191. const spy = sinon.spy();
  192. view.on( 'execute', spy );
  193. view.set( 'isEnabled', true );
  194. view.element.dispatchEvent( new Event( 'click' ) );
  195. sinon.assert.callCount( spy, 1 );
  196. view.isEnabled = false;
  197. view.element.dispatchEvent( new Event( 'click' ) );
  198. sinon.assert.callCount( spy, 1 );
  199. } );
  200. } );
  201. } );
  202. describe( 'icon', () => {
  203. it( 'is omited in #children when view#icon is not defined', () => {
  204. view = new ButtonView( locale );
  205. view.render();
  206. expect( view.element.childNodes ).to.have.length( 2 );
  207. expect( view.iconView.element ).to.be.null;
  208. } );
  209. it( 'is added to the #children when view#icon is defined', () => {
  210. view = new ButtonView( locale );
  211. view.icon = '<svg></svg>';
  212. view.render();
  213. expect( view.element.childNodes ).to.have.length( 3 );
  214. expect( view.element.childNodes[ 0 ] ).to.equal( view.iconView.element );
  215. expect( view.iconView ).to.instanceOf( IconView );
  216. expect( view.iconView.content ).to.equal( '<svg></svg>' );
  217. expect( view.iconView.element.classList.contains( 'ck-button__icon' ) ).to.be.true;
  218. view.icon = '<svg>bar</svg>';
  219. expect( view.iconView.content ).to.equal( '<svg>bar</svg>' );
  220. } );
  221. it( 'is destroyed with the view', () => {
  222. view = new ButtonView( locale );
  223. view.icon = '<svg></svg>';
  224. view.render();
  225. const spy = sinon.spy( view.iconView, 'destroy' );
  226. view.destroy();
  227. sinon.assert.calledOnce( spy );
  228. } );
  229. } );
  230. describe( 'focus()', () => {
  231. it( 'focuses the button in DOM', () => {
  232. const spy = sinon.spy( view.element, 'focus' );
  233. view.focus();
  234. sinon.assert.calledOnce( spy );
  235. } );
  236. } );
  237. } );