splitbuttonview.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. /**
  2. * @license Copyright (c) 2003-2019, 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. describe( 'activates keyboard navigation for the toolbar', () => {
  55. it( 'so "arrowright" on view#arrowView does nothing', () => {
  56. const keyEvtData = {
  57. keyCode: keyCodes.arrowright,
  58. preventDefault: sinon.spy(),
  59. stopPropagation: sinon.spy()
  60. };
  61. view.focusTracker.isFocused = true;
  62. view.focusTracker.focusedElement = view.arrowView.element;
  63. const spy = sinon.spy( view.actionView, 'focus' );
  64. view.keystrokes.press( keyEvtData );
  65. sinon.assert.notCalled( spy );
  66. sinon.assert.notCalled( keyEvtData.preventDefault );
  67. sinon.assert.notCalled( keyEvtData.stopPropagation );
  68. } );
  69. it( 'so "arrowleft" on view#arrowView focuses view#actionView', () => {
  70. const keyEvtData = {
  71. keyCode: keyCodes.arrowleft,
  72. preventDefault: sinon.spy(),
  73. stopPropagation: sinon.spy()
  74. };
  75. view.focusTracker.isFocused = true;
  76. view.focusTracker.focusedElement = view.arrowView.element;
  77. const spy = sinon.spy( view.actionView, 'focus' );
  78. view.keystrokes.press( keyEvtData );
  79. sinon.assert.calledOnce( spy );
  80. sinon.assert.calledOnce( keyEvtData.preventDefault );
  81. sinon.assert.calledOnce( keyEvtData.stopPropagation );
  82. } );
  83. it( 'so "arrowright" on view#actionView focuses view#arrowView', () => {
  84. const keyEvtData = {
  85. keyCode: keyCodes.arrowright,
  86. preventDefault: sinon.spy(),
  87. stopPropagation: sinon.spy()
  88. };
  89. view.focusTracker.isFocused = true;
  90. view.focusTracker.focusedElement = view.actionView.element;
  91. const spy = sinon.spy( view.arrowView, 'focus' );
  92. view.keystrokes.press( keyEvtData );
  93. sinon.assert.calledOnce( spy );
  94. sinon.assert.calledOnce( keyEvtData.preventDefault );
  95. sinon.assert.calledOnce( keyEvtData.stopPropagation );
  96. } );
  97. it( 'so "arrowleft" on view#actionsView does nothing', () => {
  98. const keyEvtData = {
  99. keyCode: keyCodes.arrowleft,
  100. preventDefault: sinon.spy(),
  101. stopPropagation: sinon.spy()
  102. };
  103. view.focusTracker.isFocused = true;
  104. view.focusTracker.focusedElement = view.actionView.element;
  105. const spy = sinon.spy( view.arrowView, 'focus' );
  106. view.keystrokes.press( keyEvtData );
  107. sinon.assert.notCalled( spy );
  108. sinon.assert.notCalled( keyEvtData.preventDefault );
  109. sinon.assert.notCalled( keyEvtData.stopPropagation );
  110. } );
  111. } );
  112. } );
  113. describe( 'bindings', () => {
  114. it( 'delegates actionView#execute to view#execute', () => {
  115. const spy = sinon.spy();
  116. view.on( 'execute', spy );
  117. view.actionView.fire( 'execute' );
  118. sinon.assert.calledOnce( spy );
  119. } );
  120. it( 'binds actionView#icon to view', () => {
  121. expect( view.actionView.icon ).to.be.undefined;
  122. view.icon = 'foo';
  123. expect( view.actionView.icon ).to.equal( 'foo' );
  124. } );
  125. it( 'binds actionView#isEnabled to view', () => {
  126. expect( view.actionView.isEnabled ).to.be.true;
  127. view.isEnabled = false;
  128. expect( view.actionView.isEnabled ).to.be.false;
  129. } );
  130. it( 'binds actionView#label to view', () => {
  131. expect( view.actionView.label ).to.be.undefined;
  132. view.label = 'foo';
  133. expect( view.actionView.label ).to.equal( 'foo' );
  134. } );
  135. it( 'delegates arrowView#execute to view#open', () => {
  136. const spy = sinon.spy();
  137. view.on( 'open', spy );
  138. view.arrowView.fire( 'execute' );
  139. sinon.assert.calledOnce( spy );
  140. } );
  141. it( 'binds arrowView#isEnabled to view', () => {
  142. expect( view.arrowView.isEnabled ).to.be.true;
  143. view.isEnabled = false;
  144. expect( view.arrowView.isEnabled ).to.be.false;
  145. } );
  146. it( 'binds actionView#tabindex to view', () => {
  147. expect( view.actionView.tabindex ).to.equal( -1 );
  148. view.tabindex = 1;
  149. expect( view.actionView.tabindex ).to.equal( 1 );
  150. } );
  151. // Makes little sense for split button but the Button interface specifies it, so let's support it.
  152. it( 'binds actionView#type to view', () => {
  153. expect( view.actionView.type ).to.equal( 'button' );
  154. view.type = 'submit';
  155. expect( view.actionView.type ).to.equal( 'submit' );
  156. } );
  157. it( 'binds actionView#withText to view', () => {
  158. expect( view.actionView.withText ).to.equal( false );
  159. view.withText = true;
  160. expect( view.actionView.withText ).to.equal( true );
  161. } );
  162. it( 'binds actionView#tooltipPosition to view', () => {
  163. expect( view.actionView.tooltipPosition ).to.equal( 's' );
  164. view.tooltipPosition = 'n';
  165. expect( view.actionView.tooltipPosition ).to.equal( 'n' );
  166. } );
  167. } );
  168. describe( 'focus()', () => {
  169. it( 'focuses the actionButton', () => {
  170. const spy = sinon.spy( view.actionView, 'focus' );
  171. view.focus();
  172. sinon.assert.calledOnce( spy );
  173. } );
  174. } );
  175. } );