textalternativeformview.js 5.1 KB

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