splitbuttonview.js 6.7 KB

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