textalternativeformview.js 4.8 KB

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