8
0

textalternativeformview.js 4.9 KB

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