textalternativeformview.js 5.2 KB

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