textalternativeformview.js 4.7 KB

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