textalternativeformview.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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. it( 'should implement the CSS transition disabling feature', () => {
  55. expect( view.disableCssTransitions ).to.be.a( 'function' );
  56. } );
  57. } );
  58. describe( 'render()', () => {
  59. it( 'starts listening for #keystrokes coming from #element', () => {
  60. const spy = sinon.spy( view.keystrokes, 'listenTo' );
  61. view.render();
  62. sinon.assert.calledOnce( spy );
  63. sinon.assert.calledWithExactly( spy, view.element );
  64. } );
  65. describe( 'focus cycling and management', () => {
  66. it( 'should register child views in #_focusables', () => {
  67. view.render();
  68. expect( view._focusables.map( f => f ) ).to.have.members( [
  69. view.labeledInput,
  70. view.saveButtonView,
  71. view.cancelButtonView
  72. ] );
  73. } );
  74. it( 'should register child views\' #element in #focusTracker', () => {
  75. const spy = testUtils.sinon.spy( view.focusTracker, 'add' );
  76. view.render();
  77. sinon.assert.calledWithExactly( spy.getCall( 0 ), view.labeledInput.element );
  78. sinon.assert.calledWithExactly( spy.getCall( 1 ), view.saveButtonView.element );
  79. sinon.assert.calledWithExactly( spy.getCall( 2 ), view.cancelButtonView.element );
  80. } );
  81. describe( 'activates keyboard navigation in the form', () => {
  82. beforeEach( () => {
  83. view.render();
  84. } );
  85. it( 'so "tab" focuses the next focusable item', () => {
  86. const keyEvtData = {
  87. keyCode: keyCodes.tab,
  88. preventDefault: sinon.spy(),
  89. stopPropagation: sinon.spy()
  90. };
  91. // Mock the url input is focused.
  92. view.focusTracker.isFocused = true;
  93. view.focusTracker.focusedElement = view.labeledInput.element;
  94. const spy = sinon.spy( view.saveButtonView, 'focus' );
  95. view.keystrokes.press( keyEvtData );
  96. sinon.assert.calledOnce( keyEvtData.preventDefault );
  97. sinon.assert.calledOnce( keyEvtData.stopPropagation );
  98. sinon.assert.calledOnce( spy );
  99. } );
  100. it( 'so "shift + tab" focuses the previous focusable item', () => {
  101. const keyEvtData = {
  102. keyCode: keyCodes.tab,
  103. shiftKey: true,
  104. preventDefault: sinon.spy(),
  105. stopPropagation: sinon.spy()
  106. };
  107. // Mock the cancel button is focused.
  108. view.focusTracker.isFocused = true;
  109. view.focusTracker.focusedElement = view.cancelButtonView.element;
  110. const spy = sinon.spy( view.saveButtonView, 'focus' );
  111. view.keystrokes.press( keyEvtData );
  112. sinon.assert.calledOnce( keyEvtData.preventDefault );
  113. sinon.assert.calledOnce( keyEvtData.stopPropagation );
  114. sinon.assert.calledOnce( spy );
  115. } );
  116. } );
  117. } );
  118. } );
  119. describe( 'DOM bindings', () => {
  120. describe( 'submit event', () => {
  121. it( 'should trigger submit event', () => {
  122. const spy = sinon.spy();
  123. view.render();
  124. view.on( 'submit', spy );
  125. view.element.dispatchEvent( new Event( 'submit' ) );
  126. expect( spy.calledOnce ).to.true;
  127. } );
  128. } );
  129. } );
  130. } );