8
0

buttonview.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. /**
  2. * @license Copyright (c) 2003-2017, 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. testUtils.createSinonSandbox();
  11. describe( 'ButtonView', () => {
  12. let locale, view;
  13. beforeEach( () => {
  14. locale = { t() {} };
  15. return ( view = new ButtonView( locale ) ).init();
  16. } );
  17. describe( '<button> bindings', () => {
  18. describe( 'class', () => {
  19. it( 'is set initially', () => {
  20. expect( view.element.classList ).to.have.length( 3 );
  21. expect( view.element.classList.contains( 'ck-button' ) ).to.true;
  22. expect( view.element.classList.contains( 'ck-enabled' ) ).to.true;
  23. expect( view.element.classList.contains( 'ck-off' ) ).to.true;
  24. } );
  25. it( 'reacts on view#isEnabled', () => {
  26. view.isEnabled = true;
  27. expect( view.element.classList.contains( 'ck-disabled' ) ).to.false;
  28. view.isEnabled = false;
  29. expect( view.element.classList.contains( 'ck-disabled' ) ).to.true;
  30. } );
  31. it( 'reacts on view#isOn', () => {
  32. view.isOn = true;
  33. expect( view.element.classList.contains( 'ck-on' ) ).to.true;
  34. view.isOn = false;
  35. expect( view.element.classList.contains( 'ck-on' ) ).to.false;
  36. } );
  37. it( 'reacts on view#isVisible', () => {
  38. view.isVisible = true;
  39. expect( view.element.classList.contains( 'ck-hidden' ) ).to.be.false;
  40. view.isVisible = false;
  41. expect( view.element.classList.contains( 'ck-hidden' ) ).to.be.true;
  42. } );
  43. it( 'reacts on view#withText', () => {
  44. view.withText = true;
  45. expect( view.element.classList.contains( 'ck-button_with-text' ) ).to.true;
  46. view.withText = false;
  47. expect( view.element.classList.contains( 'ck-button_with-text' ) ).to.false;
  48. } );
  49. it( 'reacts on view#type', () => {
  50. // Default value.
  51. expect( view.element.getAttribute( 'type' ) ).to.equal( 'button' );
  52. view.type = 'submit';
  53. expect( view.element.getAttribute( 'type' ) ).to.equal( 'submit' );
  54. // Default value.
  55. view.type = null;
  56. expect( view.element.getAttribute( 'type' ) ).to.equal( 'button' );
  57. } );
  58. } );
  59. describe( 'tooltip', () => {
  60. beforeEach( () => {
  61. view = new ButtonView( locale );
  62. } );
  63. it( 'is initially set', () => {
  64. expect( view.tooltipView ).to.be.instanceof( TooltipView );
  65. expect( Array.from( view.template.children ) ).to.include( view.tooltipView );
  66. } );
  67. it( 'it reacts to #tooltipPosition attribute', () => {
  68. view.tooltip = 'foo';
  69. view.icon = 'bar';
  70. view.init();
  71. expect( view.tooltipPosition ).to.equal( 's' );
  72. expect( view.tooltipView.position ).to.equal( 's' );
  73. view.tooltipPosition = 'n';
  74. expect( view.tooltipView.position ).to.equal( 'n' );
  75. } );
  76. describe( 'defined as a Boolean', () => {
  77. it( 'renders tooltip text out of #label and #keystroke', () => {
  78. view.tooltip = true;
  79. view.label = 'bar';
  80. view.keystroke = 'A';
  81. view.init();
  82. expect( view.tooltipView.text ).to.equal( 'bar (A)' );
  83. } );
  84. it( 'not render tooltip #tooltip value is false', () => {
  85. view.tooltip = false;
  86. view.label = 'bar';
  87. view.keystroke = 'A';
  88. view.init();
  89. expect( view.tooltipView.text ).to.equal( '' );
  90. } );
  91. it( 'reacts to changes in #label and #keystroke', () => {
  92. view.tooltip = true;
  93. view.label = 'foo';
  94. view.keystroke = 'B';
  95. view.init();
  96. expect( view.tooltipView.text ).to.equal( 'foo (B)' );
  97. view.label = 'baz';
  98. view.keystroke = false;
  99. expect( view.tooltipView.text ).to.equal( 'baz' );
  100. } );
  101. } );
  102. describe( 'defined as a String', () => {
  103. it( 'renders as a plain text', () => {
  104. view.tooltip = 'bar';
  105. view.label = 'foo';
  106. view.keystroke = 'A';
  107. view.init();
  108. expect( view.tooltipView.text ).to.equal( 'bar' );
  109. } );
  110. it( 'reacts to changes of #tooltip', () => {
  111. view.tooltip = 'bar';
  112. view.init();
  113. expect( view.tooltipView.text ).to.equal( 'bar' );
  114. view.tooltip = 'foo';
  115. expect( view.tooltipView.text ).to.equal( 'foo' );
  116. } );
  117. } );
  118. describe( 'defined as a Function', () => {
  119. it( 'generates a tooltip text when passed #label and #keystroke', () => {
  120. view.tooltip = ( l, k ) => `${ l } - ${ k }`;
  121. view.label = 'foo';
  122. view.keystroke = 'A';
  123. view.init();
  124. expect( view.tooltipView.text ).to.equal( 'foo - A' );
  125. } );
  126. it( 'reacts to changes of #label and #keystroke', () => {
  127. view.tooltip = ( l, k ) => `${ l } - ${ k }`;
  128. view.label = 'foo';
  129. view.keystroke = 'A';
  130. view.init();
  131. expect( view.tooltipView.text ).to.equal( 'foo - A' );
  132. view.label = 'bar';
  133. view.keystroke = 'B';
  134. expect( view.tooltipView.text ).to.equal( 'bar - B' );
  135. } );
  136. } );
  137. } );
  138. describe( 'text', () => {
  139. it( 'is not initially set ', () => {
  140. expect( view.element.textContent ).to.equal( '' );
  141. } );
  142. it( 'reacts on view#label', () => {
  143. view.label = 'bar';
  144. expect( view.element.textContent ).to.equal( 'bar' );
  145. } );
  146. } );
  147. describe( 'tabindex', () => {
  148. it( 'is initially set ', () => {
  149. expect( view.element.attributes.tabindex.value ).to.equal( '-1' );
  150. } );
  151. it( 'reacts on view#tabindex', () => {
  152. view.tabindex = 3;
  153. expect( view.element.attributes.tabindex.value ).to.equal( '3' );
  154. } );
  155. } );
  156. describe( 'mousedown event', () => {
  157. it( 'should be prevented', () => {
  158. const ret = view.element.dispatchEvent( new Event( 'mousedown', { cancelable: true } ) );
  159. expect( ret ).to.false;
  160. } );
  161. } );
  162. describe( 'execute event', () => {
  163. it( 'triggers view#execute event if button is not disabled', () => {
  164. const spy = sinon.spy();
  165. view.on( 'execute', spy );
  166. view.set( 'isEnabled', true );
  167. view.element.dispatchEvent( new Event( 'click' ) );
  168. sinon.assert.callCount( spy, 1 );
  169. view.isEnabled = false;
  170. view.element.dispatchEvent( new Event( 'click' ) );
  171. sinon.assert.callCount( spy, 1 );
  172. } );
  173. } );
  174. } );
  175. describe( 'icon', () => {
  176. it( 'is not initially set', () => {
  177. expect( view.element.childNodes ).to.have.length( 2 );
  178. expect( view.iconView ).to.be.undefined;
  179. } );
  180. it( 'is set when view#icon is defined', () => {
  181. view = new ButtonView( locale );
  182. view.icon = 'foo';
  183. view.init();
  184. expect( view.element.childNodes ).to.have.length( 3 );
  185. expect( view.element.childNodes[ 0 ] ).to.equal( view.iconView.element );
  186. expect( view.iconView ).to.instanceOf( IconView );
  187. expect( view.iconView.content ).to.equal( 'foo' );
  188. view.icon = 'bar';
  189. expect( view.iconView.content ).to.equal( 'bar' );
  190. } );
  191. it( 'is destroyed with the view', () => {
  192. view = new ButtonView( locale );
  193. view.icon = 'foo';
  194. view.init();
  195. const spy = sinon.spy( view.iconView, 'destroy' );
  196. view.destroy();
  197. sinon.assert.calledOnce( spy );
  198. } );
  199. } );
  200. describe( 'focus()', () => {
  201. it( 'focuses the button in DOM', () => {
  202. const spy = sinon.spy( view.element, 'focus' );
  203. view.focus();
  204. sinon.assert.calledOnce( spy );
  205. } );
  206. } );
  207. } );