8
0

linkformview.js 6.0 KB

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