8
0

splitbuttonview.js 6.6 KB

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