8
0

splitbuttonview.js 6.3 KB

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