textalternativeformview.js 4.8 KB

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