splitbuttonview.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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. describe( 'activates keyboard navigation for the toolbar', () => {
  34. it( 'so "arrowright" on view#arrowView does nothing', () => {
  35. const keyEvtData = {
  36. keyCode: keyCodes.arrowright,
  37. preventDefault: sinon.spy(),
  38. stopPropagation: sinon.spy()
  39. };
  40. view.focusTracker.isFocused = true;
  41. view.focusTracker.focusedElement = view.arrowView.element;
  42. const spy = sinon.spy( view.actionView, 'focus' );
  43. view.keystrokes.press( keyEvtData );
  44. sinon.assert.notCalled( spy );
  45. sinon.assert.notCalled( keyEvtData.preventDefault );
  46. sinon.assert.notCalled( keyEvtData.stopPropagation );
  47. } );
  48. it( 'so "arrowleft" on view#arrowView focuses view#actionView', () => {
  49. const keyEvtData = {
  50. keyCode: keyCodes.arrowleft,
  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.calledOnce( spy );
  59. sinon.assert.calledOnce( keyEvtData.preventDefault );
  60. sinon.assert.calledOnce( keyEvtData.stopPropagation );
  61. } );
  62. it( 'so "arrowright" on view#actionView focuses view#arrowView', () => {
  63. const keyEvtData = {
  64. keyCode: keyCodes.arrowright,
  65. preventDefault: sinon.spy(),
  66. stopPropagation: sinon.spy()
  67. };
  68. view.focusTracker.isFocused = true;
  69. view.focusTracker.focusedElement = view.actionView.element;
  70. const spy = sinon.spy( view.arrowView, '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 "arrowleft" on view#actionsView does nothing', () => {
  77. const keyEvtData = {
  78. keyCode: keyCodes.arrowleft,
  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.notCalled( spy );
  87. sinon.assert.notCalled( keyEvtData.preventDefault );
  88. sinon.assert.notCalled( keyEvtData.stopPropagation );
  89. } );
  90. } );
  91. } );
  92. describe( 'bindings', () => {
  93. it( 'delegates actionView#execute to view#execute', () => {
  94. const spy = sinon.spy();
  95. view.on( 'execute', spy );
  96. view.actionView.fire( 'execute' );
  97. sinon.assert.calledOnce( spy );
  98. } );
  99. it( 'binds actionView#icon to view', () => {
  100. expect( view.actionView.icon ).to.be.undefined;
  101. view.icon = 'foo';
  102. expect( view.actionView.icon ).to.equal( 'foo' );
  103. } );
  104. it( 'binds actionView#isEnabled to view', () => {
  105. expect( view.actionView.isEnabled ).to.be.true;
  106. view.isEnabled = false;
  107. expect( view.actionView.isEnabled ).to.be.false;
  108. } );
  109. it( 'binds actionView#label to view', () => {
  110. expect( view.actionView.label ).to.be.undefined;
  111. view.label = 'foo';
  112. expect( view.actionView.label ).to.equal( 'foo' );
  113. } );
  114. it( 'delegates arrowView#execute to view#select', () => {
  115. const spy = sinon.spy();
  116. view.on( 'select', spy );
  117. view.arrowView.fire( 'execute' );
  118. sinon.assert.calledOnce( spy );
  119. } );
  120. it( 'binds arrowView#isEnabled to view', () => {
  121. expect( view.arrowView.isEnabled ).to.be.true;
  122. view.isEnabled = false;
  123. expect( view.arrowView.isEnabled ).to.be.false;
  124. } );
  125. } );
  126. describe( 'focus()', () => {
  127. it( 'focuses the actionButton', () => {
  128. const spy = sinon.spy( view.actionView, 'focus' );
  129. view.focus();
  130. sinon.assert.calledOnce( spy );
  131. } );
  132. } );
  133. } );