linkformview.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. /**
  2. * @license Copyright (c) 2003-2019, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
  4. */
  5. /* globals Event */
  6. import LinkFormView from '../../src/ui/linkformview';
  7. import View from '@ckeditor/ckeditor5-ui/src/view';
  8. import { keyCodes } from '@ckeditor/ckeditor5-utils/src/keyboard';
  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. describe( 'LinkFormView', () => {
  15. let view;
  16. testUtils.createSinonSandbox();
  17. beforeEach( () => {
  18. view = new LinkFormView( { t: val => val } );
  19. view.render();
  20. } );
  21. describe( 'constructor()', () => {
  22. it( 'should create element from template', () => {
  23. expect( view.element.classList.contains( 'ck' ) ).to.true;
  24. expect( view.element.classList.contains( 'ck-link-form' ) ).to.true;
  25. expect( view.element.getAttribute( 'tabindex' ) ).to.equal( '-1' );
  26. } );
  27. it( 'should create child views', () => {
  28. expect( view.urlInputView ).to.be.instanceOf( View );
  29. expect( view.saveButtonView ).to.be.instanceOf( View );
  30. expect( view.cancelButtonView ).to.be.instanceOf( View );
  31. expect( view.saveButtonView.element.classList.contains( 'ck-button-save' ) ).to.be.true;
  32. expect( view.cancelButtonView.element.classList.contains( 'ck-button-cancel' ) ).to.be.true;
  33. expect( view._unboundChildren.get( 0 ) ).to.equal( view.urlInputView );
  34. expect( view._unboundChildren.get( 1 ) ).to.equal( view.saveButtonView );
  35. expect( view._unboundChildren.get( 2 ) ).to.equal( view.cancelButtonView );
  36. } );
  37. it( 'should create #focusTracker instance', () => {
  38. expect( view.focusTracker ).to.be.instanceOf( FocusTracker );
  39. } );
  40. it( 'should create #keystrokes instance', () => {
  41. expect( view.keystrokes ).to.be.instanceOf( KeystrokeHandler );
  42. } );
  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 fire `cancel` event on cancelButtonView#execute', () => {
  50. const spy = sinon.spy();
  51. view.on( 'cancel', spy );
  52. view.cancelButtonView.fire( 'execute' );
  53. expect( spy.calledOnce ).to.true;
  54. } );
  55. describe( 'url input view', () => {
  56. it( 'has placeholder', () => {
  57. expect( view.urlInputView.inputView.placeholder ).to.equal( 'https://example.com' );
  58. } );
  59. } );
  60. describe( 'template', () => {
  61. it( 'has url input view', () => {
  62. expect( view.template.children[ 0 ] ).to.equal( view.urlInputView );
  63. } );
  64. it( 'has button views', () => {
  65. expect( view.template.children[ 1 ] ).to.equal( view.saveButtonView );
  66. expect( view.template.children[ 2 ] ).to.equal( view.cancelButtonView );
  67. } );
  68. } );
  69. } );
  70. describe( 'render()', () => {
  71. it( 'should register child views in #_focusables', () => {
  72. expect( view._focusables.map( f => f ) ).to.have.members( [
  73. view.urlInputView,
  74. view.saveButtonView,
  75. view.cancelButtonView,
  76. ] );
  77. } );
  78. it( 'should register child views\' #element in #focusTracker', () => {
  79. const spy = testUtils.sinon.spy( FocusTracker.prototype, 'add' );
  80. view = new LinkFormView( { t: () => {} } );
  81. view.render();
  82. sinon.assert.calledWithExactly( spy.getCall( 0 ), view.urlInputView.element );
  83. sinon.assert.calledWithExactly( spy.getCall( 1 ), view.saveButtonView.element );
  84. sinon.assert.calledWithExactly( spy.getCall( 2 ), view.cancelButtonView.element );
  85. } );
  86. it( 'starts listening for #keystrokes coming from #element', () => {
  87. view = new LinkFormView( { t: () => {} } );
  88. const spy = sinon.spy( view.keystrokes, 'listenTo' );
  89. view.render();
  90. sinon.assert.calledOnce( spy );
  91. sinon.assert.calledWithExactly( spy, view.element );
  92. } );
  93. describe( 'activates keyboard navigation for the toolbar', () => {
  94. it( 'so "tab" focuses the next focusable item', () => {
  95. const keyEvtData = {
  96. keyCode: keyCodes.tab,
  97. preventDefault: sinon.spy(),
  98. stopPropagation: sinon.spy()
  99. };
  100. // Mock the url input is focused.
  101. view.focusTracker.isFocused = true;
  102. view.focusTracker.focusedElement = view.urlInputView.element;
  103. const spy = sinon.spy( view.saveButtonView, 'focus' );
  104. view.keystrokes.press( keyEvtData );
  105. sinon.assert.calledOnce( keyEvtData.preventDefault );
  106. sinon.assert.calledOnce( keyEvtData.stopPropagation );
  107. sinon.assert.calledOnce( spy );
  108. } );
  109. it( 'so "shift + tab" focuses the previous focusable item', () => {
  110. const keyEvtData = {
  111. keyCode: keyCodes.tab,
  112. shiftKey: true,
  113. preventDefault: sinon.spy(),
  114. stopPropagation: sinon.spy()
  115. };
  116. // Mock the cancel button is focused.
  117. view.focusTracker.isFocused = true;
  118. view.focusTracker.focusedElement = view.cancelButtonView.element;
  119. const spy = sinon.spy( view.saveButtonView, 'focus' );
  120. view.keystrokes.press( keyEvtData );
  121. sinon.assert.calledOnce( keyEvtData.preventDefault );
  122. sinon.assert.calledOnce( keyEvtData.stopPropagation );
  123. sinon.assert.calledOnce( spy );
  124. } );
  125. } );
  126. } );
  127. describe( 'DOM bindings', () => {
  128. describe( 'submit event', () => {
  129. it( 'should trigger submit event', () => {
  130. const spy = sinon.spy();
  131. view.on( 'submit', spy );
  132. view.element.dispatchEvent( new Event( 'submit' ) );
  133. expect( spy.calledOnce ).to.true;
  134. } );
  135. } );
  136. } );
  137. describe( 'focus()', () => {
  138. it( 'focuses the #urlInputView', () => {
  139. const spy = sinon.spy( view.urlInputView, 'focus' );
  140. view.focus();
  141. sinon.assert.calledOnce( spy );
  142. } );
  143. } );
  144. } );