8
0

buttonview.js 7.6 KB

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