linkactionsview.js 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. /**
  2. * @license Copyright (c) 2003-2020, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
  4. */
  5. import LinkActionsView from '../../src/ui/linkactionsview';
  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. describe( 'LinkActionsView', () => {
  14. let view;
  15. testUtils.createSinonSandbox();
  16. beforeEach( () => {
  17. view = new LinkActionsView( { t: val => val } );
  18. view.render();
  19. } );
  20. describe( 'constructor()', () => {
  21. it( 'should create element from template', () => {
  22. expect( view.element.classList.contains( 'ck' ) ).to.true;
  23. expect( view.element.classList.contains( 'ck-link-actions' ) ).to.true;
  24. expect( view.element.classList.contains( 'ck-responsive-form' ) ).to.true;
  25. expect( view.element.getAttribute( 'tabindex' ) ).to.equal( '-1' );
  26. } );
  27. it( 'should create child views', () => {
  28. expect( view.previewButtonView ).to.be.instanceOf( View );
  29. expect( view.unlinkButtonView ).to.be.instanceOf( View );
  30. expect( view.editButtonView ).to.be.instanceOf( View );
  31. expect( view._unboundChildren.get( 0 ) ).to.equal( view.previewButtonView );
  32. expect( view._unboundChildren.get( 1 ) ).to.equal( view.editButtonView );
  33. expect( view._unboundChildren.get( 2 ) ).to.equal( view.unlinkButtonView );
  34. } );
  35. it( 'should create #focusTracker instance', () => {
  36. expect( view.focusTracker ).to.be.instanceOf( FocusTracker );
  37. } );
  38. it( 'should create #keystrokes instance', () => {
  39. expect( view.keystrokes ).to.be.instanceOf( KeystrokeHandler );
  40. } );
  41. it( 'should create #_focusCycler instance', () => {
  42. expect( view._focusCycler ).to.be.instanceOf( FocusCycler );
  43. } );
  44. it( 'should create #_focusables view collection', () => {
  45. expect( view._focusables ).to.be.instanceOf( ViewCollection );
  46. } );
  47. it( 'should fire `edit` event on editButtonView#execute', () => {
  48. const spy = sinon.spy();
  49. view.on( 'edit', spy );
  50. view.editButtonView.fire( 'execute' );
  51. expect( spy.calledOnce ).to.true;
  52. } );
  53. it( 'should fire `unlink` event on unlinkButtonView#execute', () => {
  54. const spy = sinon.spy();
  55. view.on( 'unlink', spy );
  56. view.unlinkButtonView.fire( 'execute' );
  57. expect( spy.calledOnce ).to.true;
  58. } );
  59. describe( 'preview button view', () => {
  60. it( 'is an anchor', () => {
  61. expect( view.previewButtonView.element.tagName.toLowerCase() ).to.equal( 'a' );
  62. } );
  63. it( 'has a CSS class', () => {
  64. expect( view.previewButtonView.element.classList.contains( 'ck-link-actions__preview' ) ).to.be.true;
  65. } );
  66. it( 'has a "target" attribute', () => {
  67. expect( view.previewButtonView.element.getAttribute( 'target' ) ).to.equal( '_blank' );
  68. } );
  69. it( 'has a "rel" attribute', () => {
  70. expect( view.previewButtonView.element.getAttribute( 'rel' ) ).to.equal( 'noopener noreferrer' );
  71. } );
  72. describe( '<a> bindings', () => {
  73. it( 'binds href DOM attribute to view#href', () => {
  74. expect( view.previewButtonView.element.getAttribute( 'href' ) ).to.be.null;
  75. view.href = 'foo';
  76. expect( view.previewButtonView.element.getAttribute( 'href' ) ).to.equal( 'foo' );
  77. } );
  78. it( 'does not render unsafe view#href', () => {
  79. view.href = 'javascript:alert(1)';
  80. expect( view.previewButtonView.element.getAttribute( 'href' ) ).to.equal( '#' );
  81. } );
  82. it( 'binds #isEnabled to view#href', () => {
  83. expect( view.previewButtonView.isEnabled ).to.be.false;
  84. view.href = 'foo';
  85. expect( view.previewButtonView.isEnabled ).to.be.true;
  86. } );
  87. } );
  88. } );
  89. describe( 'template', () => {
  90. it( 'has child views', () => {
  91. expect( view.template.children[ 0 ] ).to.equal( view.previewButtonView );
  92. expect( view.template.children[ 1 ] ).to.equal( view.editButtonView );
  93. expect( view.template.children[ 2 ] ).to.equal( view.unlinkButtonView );
  94. } );
  95. } );
  96. } );
  97. describe( 'render()', () => {
  98. it( 'should register child views in #_focusables', () => {
  99. expect( view._focusables.map( f => f ) ).to.have.members( [
  100. view.previewButtonView,
  101. view.editButtonView,
  102. view.unlinkButtonView
  103. ] );
  104. } );
  105. it( 'should register child views\' #element in #focusTracker', () => {
  106. const spy = testUtils.sinon.spy( FocusTracker.prototype, 'add' );
  107. view = new LinkActionsView( { t: () => {} } );
  108. view.render();
  109. sinon.assert.calledWithExactly( spy.getCall( 0 ), view.previewButtonView.element );
  110. sinon.assert.calledWithExactly( spy.getCall( 1 ), view.editButtonView.element );
  111. sinon.assert.calledWithExactly( spy.getCall( 2 ), view.unlinkButtonView.element );
  112. } );
  113. it( 'starts listening for #keystrokes coming from #element', () => {
  114. view = new LinkActionsView( { t: () => {} } );
  115. const spy = sinon.spy( view.keystrokes, 'listenTo' );
  116. view.render();
  117. sinon.assert.calledOnce( spy );
  118. sinon.assert.calledWithExactly( spy, view.element );
  119. } );
  120. describe( 'activates keyboard navigation for the toolbar', () => {
  121. it( 'so "tab" focuses the next focusable item', () => {
  122. const keyEvtData = {
  123. keyCode: keyCodes.tab,
  124. preventDefault: sinon.spy(),
  125. stopPropagation: sinon.spy()
  126. };
  127. // Mock the preview button is focused.
  128. view.focusTracker.isFocused = true;
  129. view.focusTracker.focusedElement = view.previewButtonView.element;
  130. const spy = sinon.spy( view.editButtonView, '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. it( 'so "shift + tab" focuses the previous focusable item', () => {
  137. const keyEvtData = {
  138. keyCode: keyCodes.tab,
  139. shiftKey: true,
  140. preventDefault: sinon.spy(),
  141. stopPropagation: sinon.spy()
  142. };
  143. // Mock the edit button is focused.
  144. view.focusTracker.isFocused = true;
  145. view.focusTracker.focusedElement = view.editButtonView.element;
  146. const spy = sinon.spy( view.previewButtonView, 'focus' );
  147. view.keystrokes.press( keyEvtData );
  148. sinon.assert.calledOnce( keyEvtData.preventDefault );
  149. sinon.assert.calledOnce( keyEvtData.stopPropagation );
  150. sinon.assert.calledOnce( spy );
  151. } );
  152. } );
  153. } );
  154. describe( 'focus()', () => {
  155. it( 'focuses the #previewButtonView', () => {
  156. const spy = sinon.spy( view.previewButtonView, 'focus' );
  157. view.focus();
  158. sinon.assert.calledOnce( spy );
  159. } );
  160. } );
  161. } );