8
0

splitbuttonview.js 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. /**
  2. * @license Copyright (c) 2003-2020, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
  4. */
  5. import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
  6. import { keyCodes } from '@ckeditor/ckeditor5-utils/src/keyboard';
  7. import ButtonView from '../../../src/button/buttonview';
  8. import SplitButtonView from '../../../src/dropdown/button/splitbuttonview';
  9. describe( 'SplitButtonView', () => {
  10. let locale, view;
  11. testUtils.createSinonSandbox();
  12. beforeEach( () => {
  13. locale = { t() {} };
  14. view = new SplitButtonView( locale );
  15. view.render();
  16. } );
  17. describe( 'constructor()', () => {
  18. it( 'sets view#locale', () => {
  19. expect( view.locale ).to.equal( locale );
  20. } );
  21. it( 'creates view#actionView', () => {
  22. expect( view.actionView ).to.be.instanceOf( ButtonView );
  23. expect( view.actionView.element.classList.contains( 'ck-splitbutton__action' ) ).to.be.true;
  24. } );
  25. it( 'adds isToggleable to view#actionView', () => {
  26. expect( view.actionView.isToggleable ).to.be.false;
  27. view.isToggleable = true;
  28. expect( view.actionView.isToggleable ).to.be.true;
  29. } );
  30. it( 'creates view#arrowView', () => {
  31. expect( view.arrowView ).to.be.instanceOf( ButtonView );
  32. expect( view.arrowView.element.classList.contains( 'ck-splitbutton__arrow' ) ).to.be.true;
  33. expect( view.arrowView.element.attributes[ 'aria-haspopup' ].value ).to.equal( 'true' );
  34. expect( view.arrowView.icon ).to.be.not.undefined;
  35. } );
  36. it( 'creates element from template', () => {
  37. expect( view.element.tagName ).to.equal( 'DIV' );
  38. expect( view.element.classList.contains( 'ck' ) ).to.be.true;
  39. expect( view.element.classList.contains( 'ck-splitbutton' ) ).to.be.true;
  40. } );
  41. it( 'binds #isVisible to the template', () => {
  42. expect( view.element.classList.contains( 'ck-hidden' ) ).to.be.false;
  43. view.isVisible = false;
  44. expect( view.element.classList.contains( 'ck-hidden' ) ).to.be.true;
  45. // There should be no binding to the action view. Only the entire split button should react.
  46. expect( view.actionView.element.classList.contains( 'ck-hidden' ) ).to.be.false;
  47. } );
  48. it( 'binds arrowView#isOn to the template', () => {
  49. view.arrowView.isOn = true;
  50. expect( view.element.classList.contains( 'ck-splitbutton_open' ) ).to.be.true;
  51. view.arrowView.isOn = false;
  52. expect( view.element.classList.contains( 'ck-splitbutton_open' ) ).to.be.false;
  53. } );
  54. it( 'binds arrowView aria-expanded attribute to #isOn', () => {
  55. view.arrowView.isOn = true;
  56. expect( view.arrowView.element.getAttribute( 'aria-expanded' ) ).to.equal( 'true' );
  57. view.arrowView.isOn = false;
  58. expect( view.arrowView.element.getAttribute( 'aria-expanded' ) ).to.equal( 'false' );
  59. } );
  60. describe( 'activates keyboard navigation for the toolbar', () => {
  61. it( 'so "arrowright" on view#arrowView does nothing', () => {
  62. const keyEvtData = {
  63. keyCode: keyCodes.arrowright,
  64. preventDefault: sinon.spy(),
  65. stopPropagation: sinon.spy()
  66. };
  67. view.focusTracker.isFocused = true;
  68. view.focusTracker.focusedElement = view.arrowView.element;
  69. const spy = sinon.spy( view.actionView, 'focus' );
  70. view.keystrokes.press( keyEvtData );
  71. sinon.assert.notCalled( spy );
  72. sinon.assert.notCalled( keyEvtData.preventDefault );
  73. sinon.assert.notCalled( keyEvtData.stopPropagation );
  74. } );
  75. it( 'so "arrowleft" on view#arrowView focuses view#actionView', () => {
  76. const keyEvtData = {
  77. keyCode: keyCodes.arrowleft,
  78. preventDefault: sinon.spy(),
  79. stopPropagation: sinon.spy()
  80. };
  81. view.focusTracker.isFocused = true;
  82. view.focusTracker.focusedElement = view.arrowView.element;
  83. const spy = sinon.spy( view.actionView, 'focus' );
  84. view.keystrokes.press( keyEvtData );
  85. sinon.assert.calledOnce( spy );
  86. sinon.assert.calledOnce( keyEvtData.preventDefault );
  87. sinon.assert.calledOnce( keyEvtData.stopPropagation );
  88. } );
  89. it( 'so "arrowright" on view#actionView focuses view#arrowView', () => {
  90. const keyEvtData = {
  91. keyCode: keyCodes.arrowright,
  92. preventDefault: sinon.spy(),
  93. stopPropagation: sinon.spy()
  94. };
  95. view.focusTracker.isFocused = true;
  96. view.focusTracker.focusedElement = view.actionView.element;
  97. const spy = sinon.spy( view.arrowView, 'focus' );
  98. view.keystrokes.press( keyEvtData );
  99. sinon.assert.calledOnce( spy );
  100. sinon.assert.calledOnce( keyEvtData.preventDefault );
  101. sinon.assert.calledOnce( keyEvtData.stopPropagation );
  102. } );
  103. it( 'so "arrowleft" on view#actionsView does nothing', () => {
  104. const keyEvtData = {
  105. keyCode: keyCodes.arrowleft,
  106. preventDefault: sinon.spy(),
  107. stopPropagation: sinon.spy()
  108. };
  109. view.focusTracker.isFocused = true;
  110. view.focusTracker.focusedElement = view.actionView.element;
  111. const spy = sinon.spy( view.arrowView, 'focus' );
  112. view.keystrokes.press( keyEvtData );
  113. sinon.assert.notCalled( spy );
  114. sinon.assert.notCalled( keyEvtData.preventDefault );
  115. sinon.assert.notCalled( keyEvtData.stopPropagation );
  116. } );
  117. } );
  118. } );
  119. describe( 'bindings', () => {
  120. it( 'delegates actionView#execute to view#execute', () => {
  121. const spy = sinon.spy();
  122. view.on( 'execute', spy );
  123. view.actionView.fire( 'execute' );
  124. sinon.assert.calledOnce( spy );
  125. } );
  126. it( 'binds actionView#icon to view', () => {
  127. expect( view.actionView.icon ).to.be.undefined;
  128. view.icon = 'foo';
  129. expect( view.actionView.icon ).to.equal( 'foo' );
  130. } );
  131. it( 'binds actionView#isEnabled to view', () => {
  132. expect( view.actionView.isEnabled ).to.be.true;
  133. view.isEnabled = false;
  134. expect( view.actionView.isEnabled ).to.be.false;
  135. } );
  136. it( 'binds actionView#label to view', () => {
  137. expect( view.actionView.label ).to.be.undefined;
  138. view.label = 'foo';
  139. expect( view.actionView.label ).to.equal( 'foo' );
  140. } );
  141. it( 'delegates arrowView#execute to view#open', () => {
  142. const spy = sinon.spy();
  143. view.on( 'open', spy );
  144. view.arrowView.fire( 'execute' );
  145. sinon.assert.calledOnce( spy );
  146. } );
  147. it( 'binds arrowView#isEnabled to view', () => {
  148. expect( view.arrowView.isEnabled ).to.be.true;
  149. view.isEnabled = false;
  150. expect( view.arrowView.isEnabled ).to.be.false;
  151. } );
  152. it( 'binds actionView#tabindex to view', () => {
  153. expect( view.actionView.tabindex ).to.equal( -1 );
  154. view.tabindex = 1;
  155. expect( view.actionView.tabindex ).to.equal( 1 );
  156. } );
  157. // Makes little sense for split button but the Button interface specifies it, so let's support it.
  158. it( 'binds actionView#type to view', () => {
  159. expect( view.actionView.type ).to.equal( 'button' );
  160. view.type = 'submit';
  161. expect( view.actionView.type ).to.equal( 'submit' );
  162. } );
  163. it( 'binds actionView#withText to view', () => {
  164. expect( view.actionView.withText ).to.equal( false );
  165. view.withText = true;
  166. expect( view.actionView.withText ).to.equal( true );
  167. } );
  168. it( 'binds actionView#tooltipPosition to view', () => {
  169. expect( view.actionView.tooltipPosition ).to.equal( 's' );
  170. view.tooltipPosition = 'n';
  171. expect( view.actionView.tooltipPosition ).to.equal( 'n' );
  172. } );
  173. } );
  174. describe( 'focus()', () => {
  175. it( 'focuses the actionButton', () => {
  176. const spy = sinon.spy( view.actionView, 'focus' );
  177. view.focus();
  178. sinon.assert.calledOnce( spy );
  179. } );
  180. } );
  181. } );