linkactionsview.js 6.2 KB

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