textalternativeformview.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /**
  2. * @license Copyright (c) 2003-2020, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
  4. */
  5. /* global Event */
  6. import { keyCodes } from '@ckeditor/ckeditor5-utils/src/keyboard';
  7. import TextAlternativeFormView from '../../../src/imagetextalternative/ui/textalternativeformview';
  8. import View from '@ckeditor/ckeditor5-ui/src/view';
  9. import KeystrokeHandler from '@ckeditor/ckeditor5-utils/src/keystrokehandler';
  10. import FocusTracker from '@ckeditor/ckeditor5-utils/src/focustracker';
  11. import FocusCycler from '@ckeditor/ckeditor5-ui/src/focuscycler';
  12. import ViewCollection from '@ckeditor/ckeditor5-ui/src/viewcollection';
  13. import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
  14. describe( 'TextAlternativeFormView', () => {
  15. let view;
  16. testUtils.createSinonSandbox();
  17. beforeEach( () => {
  18. view = new TextAlternativeFormView( { t: () => {} } );
  19. } );
  20. describe( 'constructor()', () => {
  21. it( 'should create element from template', () => {
  22. view.render();
  23. expect( view.element.classList.contains( 'ck' ) ).to.be.true;
  24. expect( view.element.classList.contains( 'ck-text-alternative-form' ) ).to.be.true;
  25. expect( view.element.classList.contains( 'ck-responsive-form' ) ).to.be.true;
  26. expect( view.element.getAttribute( 'tabindex' ) ).to.equal( '-1' );
  27. } );
  28. it( 'should create #focusTracker instance', () => {
  29. expect( view.focusTracker ).to.be.instanceOf( FocusTracker );
  30. } );
  31. it( 'should create #keystrokes instance', () => {
  32. expect( view.keystrokes ).to.be.instanceOf( KeystrokeHandler );
  33. } );
  34. it( 'should create child views', () => {
  35. expect( view.labeledInput ).to.be.instanceOf( View );
  36. expect( view.saveButtonView ).to.be.instanceOf( View );
  37. expect( view.cancelButtonView ).to.be.instanceOf( View );
  38. view.render();
  39. expect( view.saveButtonView.element.classList.contains( 'ck-button-save' ) ).to.be.true;
  40. expect( view.cancelButtonView.element.classList.contains( 'ck-button-cancel' ) ).to.be.true;
  41. } );
  42. it( 'should create #_focusCycler instance', () => {
  43. expect( view._focusCycler ).to.be.instanceOf( FocusCycler );
  44. } );
  45. it( 'should create #_focusables view collection', () => {
  46. expect( view._focusables ).to.be.instanceOf( ViewCollection );
  47. } );
  48. it( 'should fire `cancel` event on cancelButtonView#execute', () => {
  49. const spy = sinon.spy();
  50. view.on( 'cancel', spy );
  51. view.cancelButtonView.fire( 'execute' );
  52. sinon.assert.calledOnce( spy );
  53. } );
  54. } );
  55. describe( 'render()', () => {
  56. it( 'starts listening for #keystrokes coming from #element', () => {
  57. const spy = sinon.spy( view.keystrokes, 'listenTo' );
  58. view.render();
  59. sinon.assert.calledOnce( spy );
  60. sinon.assert.calledWithExactly( spy, view.element );
  61. } );
  62. describe( 'focus cycling and management', () => {
  63. it( 'should register child views in #_focusables', () => {
  64. view.render();
  65. expect( view._focusables.map( f => f ) ).to.have.members( [
  66. view.labeledInput,
  67. view.saveButtonView,
  68. view.cancelButtonView
  69. ] );
  70. } );
  71. it( 'should register child views\' #element in #focusTracker', () => {
  72. const spy = testUtils.sinon.spy( FocusTracker.prototype, 'add' );
  73. view.render();
  74. sinon.assert.calledWithExactly( spy.getCall( 0 ), view.labeledInput.element );
  75. sinon.assert.calledWithExactly( spy.getCall( 1 ), view.saveButtonView.element );
  76. sinon.assert.calledWithExactly( spy.getCall( 2 ), view.cancelButtonView.element );
  77. } );
  78. describe( 'activates keyboard navigation in the form', () => {
  79. beforeEach( () => {
  80. view.render();
  81. } );
  82. it( 'so "tab" focuses the next focusable item', () => {
  83. const keyEvtData = {
  84. keyCode: keyCodes.tab,
  85. preventDefault: sinon.spy(),
  86. stopPropagation: sinon.spy()
  87. };
  88. // Mock the url input is focused.
  89. view.focusTracker.isFocused = true;
  90. view.focusTracker.focusedElement = view.labeledInput.element;
  91. const spy = sinon.spy( view.saveButtonView, 'focus' );
  92. view.keystrokes.press( keyEvtData );
  93. sinon.assert.calledOnce( keyEvtData.preventDefault );
  94. sinon.assert.calledOnce( keyEvtData.stopPropagation );
  95. sinon.assert.calledOnce( spy );
  96. } );
  97. it( 'so "shift + tab" focuses the previous focusable item', () => {
  98. const keyEvtData = {
  99. keyCode: keyCodes.tab,
  100. shiftKey: true,
  101. preventDefault: sinon.spy(),
  102. stopPropagation: sinon.spy()
  103. };
  104. // Mock the cancel button is focused.
  105. view.focusTracker.isFocused = true;
  106. view.focusTracker.focusedElement = view.cancelButtonView.element;
  107. const spy = sinon.spy( view.saveButtonView, 'focus' );
  108. view.keystrokes.press( keyEvtData );
  109. sinon.assert.calledOnce( keyEvtData.preventDefault );
  110. sinon.assert.calledOnce( keyEvtData.stopPropagation );
  111. sinon.assert.calledOnce( spy );
  112. } );
  113. } );
  114. } );
  115. } );
  116. describe( 'DOM bindings', () => {
  117. describe( 'submit event', () => {
  118. it( 'should trigger submit event', () => {
  119. const spy = sinon.spy();
  120. view.render();
  121. view.on( 'submit', spy );
  122. view.element.dispatchEvent( new Event( 'submit' ) );
  123. expect( spy.calledOnce ).to.true;
  124. } );
  125. } );
  126. } );
  127. } );