8
0

textalternativeformview.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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.getAttribute( 'tabindex' ) ).to.equal( '-1' );
  26. } );
  27. it( 'should create #focusTracker instance', () => {
  28. expect( view.focusTracker ).to.be.instanceOf( FocusTracker );
  29. } );
  30. it( 'should create #keystrokes instance', () => {
  31. expect( view.keystrokes ).to.be.instanceOf( KeystrokeHandler );
  32. } );
  33. it( 'should create child views', () => {
  34. expect( view.labeledInput ).to.be.instanceOf( View );
  35. expect( view.saveButtonView ).to.be.instanceOf( View );
  36. expect( view.cancelButtonView ).to.be.instanceOf( View );
  37. view.render();
  38. expect( view.saveButtonView.element.classList.contains( 'ck-button-save' ) ).to.be.true;
  39. expect( view.cancelButtonView.element.classList.contains( 'ck-button-cancel' ) ).to.be.true;
  40. } );
  41. it( 'should create #_focusCycler instance', () => {
  42. expect( view._focusCycler ).to.be.instanceOf( FocusCycler );
  43. } );
  44. it( 'should create #_focusables view collection', () => {
  45. expect( view._focusables ).to.be.instanceOf( ViewCollection );
  46. } );
  47. it( 'should fire `cancel` event on cancelButtonView#execute', () => {
  48. const spy = sinon.spy();
  49. view.on( 'cancel', spy );
  50. view.cancelButtonView.fire( 'execute' );
  51. sinon.assert.calledOnce( spy );
  52. } );
  53. } );
  54. describe( 'render()', () => {
  55. it( 'starts listening for #keystrokes coming from #element', () => {
  56. const spy = sinon.spy( view.keystrokes, 'listenTo' );
  57. view.render();
  58. sinon.assert.calledOnce( spy );
  59. sinon.assert.calledWithExactly( spy, view.element );
  60. } );
  61. describe( 'focus cycling and management', () => {
  62. it( 'should register child views in #_focusables', () => {
  63. view.render();
  64. expect( view._focusables.map( f => f ) ).to.have.members( [
  65. view.labeledInput,
  66. view.saveButtonView,
  67. view.cancelButtonView
  68. ] );
  69. } );
  70. it( 'should register child views\' #element in #focusTracker', () => {
  71. const spy = testUtils.sinon.spy( FocusTracker.prototype, 'add' );
  72. view.render();
  73. sinon.assert.calledWithExactly( spy.getCall( 0 ), view.labeledInput.element );
  74. sinon.assert.calledWithExactly( spy.getCall( 1 ), view.saveButtonView.element );
  75. sinon.assert.calledWithExactly( spy.getCall( 2 ), view.cancelButtonView.element );
  76. } );
  77. describe( 'activates keyboard navigation in the form', () => {
  78. beforeEach( () => {
  79. view.render();
  80. } );
  81. it( 'so "tab" focuses the next focusable item', () => {
  82. const keyEvtData = {
  83. keyCode: keyCodes.tab,
  84. preventDefault: sinon.spy(),
  85. stopPropagation: sinon.spy()
  86. };
  87. // Mock the url input is focused.
  88. view.focusTracker.isFocused = true;
  89. view.focusTracker.focusedElement = view.labeledInput.element;
  90. const spy = sinon.spy( view.saveButtonView, 'focus' );
  91. view.keystrokes.press( keyEvtData );
  92. sinon.assert.calledOnce( keyEvtData.preventDefault );
  93. sinon.assert.calledOnce( keyEvtData.stopPropagation );
  94. sinon.assert.calledOnce( spy );
  95. } );
  96. it( 'so "shift + tab" focuses the previous focusable item', () => {
  97. const keyEvtData = {
  98. keyCode: keyCodes.tab,
  99. shiftKey: true,
  100. preventDefault: sinon.spy(),
  101. stopPropagation: sinon.spy()
  102. };
  103. // Mock the cancel button is focused.
  104. view.focusTracker.isFocused = true;
  105. view.focusTracker.focusedElement = view.cancelButtonView.element;
  106. const spy = sinon.spy( view.saveButtonView, 'focus' );
  107. view.keystrokes.press( keyEvtData );
  108. sinon.assert.calledOnce( keyEvtData.preventDefault );
  109. sinon.assert.calledOnce( keyEvtData.stopPropagation );
  110. sinon.assert.calledOnce( spy );
  111. } );
  112. } );
  113. } );
  114. } );
  115. describe( 'DOM bindings', () => {
  116. describe( 'submit event', () => {
  117. it( 'should trigger submit event', () => {
  118. const spy = sinon.spy();
  119. view.render();
  120. view.on( 'submit', spy );
  121. view.element.dispatchEvent( new Event( 'submit' ) );
  122. expect( spy.calledOnce ).to.true;
  123. } );
  124. } );
  125. } );
  126. } );