buttonview.js 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  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 not initially set', () => {
  64. expect( view.tooltipView ).to.be.undefined;
  65. expect( view.element.childNodes ).to.have.length( 1 );
  66. } );
  67. it( 'is not initially set (despite #label and #keystroke)', () => {
  68. view.label = 'foo';
  69. view.keystroke = 'A';
  70. view.init();
  71. expect( view.tooltipView ).to.be.undefined;
  72. } );
  73. it( 'is not set if neither `true`, String or Function', () => {
  74. view.label = 'foo';
  75. view.keystroke = 'A';
  76. view.tooltip = false;
  77. view.init();
  78. expect( view.tooltipView ).to.be.undefined;
  79. view.tooltip = 3;
  80. expect( view.tooltipView ).to.be.undefined;
  81. view.tooltip = new Date();
  82. expect( view.tooltipView ).to.be.undefined;
  83. } );
  84. it( 'when set, is added to the DOM', () => {
  85. view.tooltip = 'foo';
  86. view.icon = 'bar';
  87. view.init();
  88. expect( view.element.childNodes ).to.have.length( 3 );
  89. expect( view.element.childNodes[ 2 ] ).to.equal( view.tooltipView.element );
  90. expect( view.tooltipView ).to.instanceOf( TooltipView );
  91. } );
  92. it( 'when set, is destroyed along with the view', () => {
  93. view.tooltip = 'foo';
  94. view.init();
  95. const spy = sinon.spy( view.tooltipView, 'destroy' );
  96. view.destroy();
  97. sinon.assert.calledOnce( spy );
  98. } );
  99. describe( 'defined as a Boolean', () => {
  100. it( 'renders tooltip text out of #label and #keystroke', () => {
  101. view.tooltip = true;
  102. view.label = 'bar';
  103. view.keystroke = 'A';
  104. view.init();
  105. expect( view.tooltipView.text ).to.equal( 'bar (A)' );
  106. } );
  107. it( 'reacts to changes in #label and #keystroke', () => {
  108. view.tooltip = true;
  109. view.label = 'foo';
  110. view.keystroke = 'B';
  111. view.init();
  112. expect( view.tooltipView.text ).to.equal( 'foo (B)' );
  113. view.label = 'baz';
  114. view.keystroke = false;
  115. expect( view.tooltipView.text ).to.equal( 'baz' );
  116. } );
  117. } );
  118. describe( 'defined as a String', () => {
  119. it( 'renders as a plain text', () => {
  120. view.tooltip = 'bar';
  121. view.label = 'foo';
  122. view.keystroke = 'A';
  123. view.init();
  124. expect( view.tooltipView.text ).to.equal( 'bar' );
  125. } );
  126. it( 'reacts to changes of #tooltip', () => {
  127. view.tooltip = 'bar';
  128. view.init();
  129. expect( view.tooltipView.text ).to.equal( 'bar' );
  130. view.tooltip = 'foo';
  131. expect( view.tooltipView.text ).to.equal( 'foo' );
  132. } );
  133. } );
  134. describe( 'defined as a Function', () => {
  135. it( 'generates a tooltip text when passed #label and #keystroke', () => {
  136. view.tooltip = ( l, k ) => `${ l } - ${ k }`;
  137. view.label = 'foo';
  138. view.keystroke = 'A';
  139. view.init();
  140. expect( view.tooltipView.text ).to.equal( 'foo - A' );
  141. } );
  142. it( 'reacts to changes of #label and #keystroke', () => {
  143. view.tooltip = ( l, k ) => `${ l } - ${ k }`;
  144. view.label = 'foo';
  145. view.keystroke = 'A';
  146. view.init();
  147. expect( view.tooltipView.text ).to.equal( 'foo - A' );
  148. view.label = 'bar';
  149. view.keystroke = 'B';
  150. expect( view.tooltipView.text ).to.equal( 'bar - B' );
  151. } );
  152. } );
  153. } );
  154. describe( 'text', () => {
  155. it( 'is not initially set ', () => {
  156. expect( view.element.textContent ).to.equal( '' );
  157. } );
  158. it( 'reacts on view#label', () => {
  159. view.label = 'bar';
  160. expect( view.element.textContent ).to.equal( 'bar' );
  161. } );
  162. } );
  163. describe( 'tabindex', () => {
  164. it( 'is initially set ', () => {
  165. expect( view.element.attributes.tabindex.value ).to.equal( '-1' );
  166. } );
  167. it( 'reacts on view#tabindex', () => {
  168. view.tabindex = 3;
  169. expect( view.element.attributes.tabindex.value ).to.equal( '3' );
  170. } );
  171. } );
  172. describe( 'mousedown event', () => {
  173. it( 'should be prevented', () => {
  174. const ret = view.element.dispatchEvent( new Event( 'mousedown', { cancelable: true } ) );
  175. expect( ret ).to.false;
  176. } );
  177. } );
  178. describe( 'execute event', () => {
  179. it( 'triggers view#execute event if button is not disabled', () => {
  180. const spy = sinon.spy();
  181. view.on( 'execute', spy );
  182. view.set( 'isEnabled', true );
  183. view.element.dispatchEvent( new Event( 'click' ) );
  184. sinon.assert.callCount( spy, 1 );
  185. view.isEnabled = false;
  186. view.element.dispatchEvent( new Event( 'click' ) );
  187. sinon.assert.callCount( spy, 1 );
  188. } );
  189. } );
  190. } );
  191. describe( 'icon', () => {
  192. it( 'is not initially set', () => {
  193. expect( view.element.childNodes ).to.have.length( 1 );
  194. expect( view.iconView ).to.be.undefined;
  195. } );
  196. it( 'is set when view#icon is defined', () => {
  197. view = new ButtonView( locale );
  198. view.icon = 'foo';
  199. view.init();
  200. expect( view.element.childNodes ).to.have.length( 2 );
  201. expect( view.element.childNodes[ 0 ] ).to.equal( view.iconView.element );
  202. expect( view.iconView ).to.instanceOf( IconView );
  203. expect( view.iconView.content ).to.equal( 'foo' );
  204. view.icon = 'bar';
  205. expect( view.iconView.content ).to.equal( 'bar' );
  206. } );
  207. it( 'is destroyed with the view', () => {
  208. view = new ButtonView( locale );
  209. view.icon = 'foo';
  210. view.init();
  211. const spy = sinon.spy( view.iconView, 'destroy' );
  212. view.destroy();
  213. sinon.assert.calledOnce( spy );
  214. } );
  215. } );
  216. describe( 'focus()', () => {
  217. it( 'focuses the button in DOM', () => {
  218. const spy = sinon.spy( view.element, 'focus' );
  219. view.focus();
  220. sinon.assert.calledOnce( spy );
  221. } );
  222. } );
  223. } );