linkformview.js 6.2 KB

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