buttonview.js 8.0 KB

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