8
0

splitbuttonview.js 6.2 KB

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