splitbuttonview.js 6.6 KB

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