8
0

buttonview.js 7.3 KB

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