linkformview.js 5.5 KB

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