8
0

linkactionsview.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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. 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.getAttribute( 'tabindex' ) ).to.equal( '-1' );
  25. } );
  26. it( 'should create child views', () => {
  27. expect( view.previewButtonView ).to.be.instanceOf( View );
  28. expect( view.unlinkButtonView ).to.be.instanceOf( View );
  29. expect( view.editButtonView ).to.be.instanceOf( View );
  30. expect( view._unboundChildren.get( 0 ) ).to.equal( view.previewButtonView );
  31. expect( view._unboundChildren.get( 1 ) ).to.equal( view.editButtonView );
  32. expect( view._unboundChildren.get( 2 ) ).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 fire `edit` event on editButtonView#execute', () => {
  47. const spy = sinon.spy();
  48. view.on( 'edit', spy );
  49. view.editButtonView.fire( 'execute' );
  50. expect( spy.calledOnce ).to.true;
  51. } );
  52. it( 'should fire `unlink` event on unlinkButtonView#execute', () => {
  53. const spy = sinon.spy();
  54. view.on( 'unlink', spy );
  55. view.unlinkButtonView.fire( 'execute' );
  56. expect( spy.calledOnce ).to.true;
  57. } );
  58. describe( 'preview button view', () => {
  59. it( 'is an anchor', () => {
  60. expect( view.previewButtonView.element.tagName.toLowerCase() ).to.equal( 'a' );
  61. } );
  62. it( 'has a CSS class', () => {
  63. expect( view.previewButtonView.element.classList.contains( 'ck-link-actions__preview' ) ).to.be.true;
  64. } );
  65. it( 'has a target attribute', () => {
  66. expect( view.previewButtonView.element.getAttribute( 'target' ) ).to.equal( '_blank' );
  67. } );
  68. describe( '<a> bindings', () => {
  69. it( 'binds href DOM attribute to view#href', () => {
  70. expect( view.previewButtonView.element.getAttribute( 'href' ) ).to.be.null;
  71. view.href = 'foo';
  72. expect( view.previewButtonView.element.getAttribute( 'href' ) ).to.equal( 'foo' );
  73. } );
  74. it( 'does not render unsafe view#href', () => {
  75. view.href = 'javascript:alert(1)';
  76. expect( view.previewButtonView.element.getAttribute( 'href' ) ).to.equal( '#' );
  77. } );
  78. it( 'binds #isEnabled to view#href', () => {
  79. expect( view.previewButtonView.isEnabled ).to.be.false;
  80. view.href = 'foo';
  81. expect( view.previewButtonView.isEnabled ).to.be.true;
  82. } );
  83. } );
  84. } );
  85. describe( 'template', () => {
  86. it( 'has child views', () => {
  87. expect( view.template.children[ 0 ] ).to.equal( view.previewButtonView );
  88. expect( view.template.children[ 1 ] ).to.equal( view.editButtonView );
  89. expect( view.template.children[ 2 ] ).to.equal( view.unlinkButtonView );
  90. } );
  91. } );
  92. } );
  93. describe( 'render()', () => {
  94. it( 'should register child views in #_focusables', () => {
  95. expect( view._focusables.map( f => f ) ).to.have.members( [
  96. view.previewButtonView,
  97. view.editButtonView,
  98. view.unlinkButtonView
  99. ] );
  100. } );
  101. it( 'should register child views\' #element in #focusTracker', () => {
  102. const spy = testUtils.sinon.spy( FocusTracker.prototype, 'add' );
  103. view = new LinkActionsView( { t: () => {} } );
  104. view.render();
  105. sinon.assert.calledWithExactly( spy.getCall( 0 ), view.previewButtonView.element );
  106. sinon.assert.calledWithExactly( spy.getCall( 1 ), view.editButtonView.element );
  107. sinon.assert.calledWithExactly( spy.getCall( 2 ), view.unlinkButtonView.element );
  108. } );
  109. it( 'starts listening for #keystrokes coming from #element', () => {
  110. view = new LinkActionsView( { t: () => {} } );
  111. const spy = sinon.spy( view.keystrokes, 'listenTo' );
  112. view.render();
  113. sinon.assert.calledOnce( spy );
  114. sinon.assert.calledWithExactly( spy, view.element );
  115. } );
  116. describe( 'activates keyboard navigation for the toolbar', () => {
  117. it( 'so "tab" focuses the next focusable item', () => {
  118. const keyEvtData = {
  119. keyCode: keyCodes.tab,
  120. preventDefault: sinon.spy(),
  121. stopPropagation: sinon.spy()
  122. };
  123. // Mock the preview button is focused.
  124. view.focusTracker.isFocused = true;
  125. view.focusTracker.focusedElement = view.previewButtonView.element;
  126. const spy = sinon.spy( view.editButtonView, 'focus' );
  127. view.keystrokes.press( keyEvtData );
  128. sinon.assert.calledOnce( keyEvtData.preventDefault );
  129. sinon.assert.calledOnce( keyEvtData.stopPropagation );
  130. sinon.assert.calledOnce( spy );
  131. } );
  132. it( 'so "shift + tab" focuses the previous focusable item', () => {
  133. const keyEvtData = {
  134. keyCode: keyCodes.tab,
  135. shiftKey: true,
  136. preventDefault: sinon.spy(),
  137. stopPropagation: sinon.spy()
  138. };
  139. // Mock the edit button is focused.
  140. view.focusTracker.isFocused = true;
  141. view.focusTracker.focusedElement = view.editButtonView.element;
  142. const spy = sinon.spy( view.previewButtonView, 'focus' );
  143. view.keystrokes.press( keyEvtData );
  144. sinon.assert.calledOnce( keyEvtData.preventDefault );
  145. sinon.assert.calledOnce( keyEvtData.stopPropagation );
  146. sinon.assert.calledOnce( spy );
  147. } );
  148. } );
  149. } );
  150. describe( 'focus()', () => {
  151. it( 'focuses the #previewButtonView', () => {
  152. const spy = sinon.spy( view.previewButtonView, 'focus' );
  153. view.focus();
  154. sinon.assert.calledOnce( spy );
  155. } );
  156. } );
  157. } );