linkactionsview.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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' ) ).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( '<button> 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( 'binds #isEnabled to view#href', () => {
  75. expect( view.previewButtonView.isEnabled ).to.be.false;
  76. view.href = 'foo';
  77. expect( view.previewButtonView.isEnabled ).to.be.true;
  78. } );
  79. } );
  80. } );
  81. describe( 'template', () => {
  82. it( 'has child views', () => {
  83. expect( view.template.children[ 0 ] ).to.equal( view.previewButtonView );
  84. expect( view.template.children[ 1 ] ).to.equal( view.editButtonView );
  85. expect( view.template.children[ 2 ] ).to.equal( view.unlinkButtonView );
  86. } );
  87. } );
  88. } );
  89. describe( 'render()', () => {
  90. it( 'should register child views in #_focusables', () => {
  91. expect( view._focusables.map( f => f ) ).to.have.members( [
  92. view.previewButtonView,
  93. view.editButtonView,
  94. view.unlinkButtonView
  95. ] );
  96. } );
  97. it( 'should register child views\' #element in #focusTracker', () => {
  98. const spy = testUtils.sinon.spy( FocusTracker.prototype, 'add' );
  99. view = new LinkActionsView( { t: () => {} } );
  100. view.render();
  101. sinon.assert.calledWithExactly( spy.getCall( 0 ), view.previewButtonView.element );
  102. sinon.assert.calledWithExactly( spy.getCall( 1 ), view.editButtonView.element );
  103. sinon.assert.calledWithExactly( spy.getCall( 2 ), view.unlinkButtonView.element );
  104. } );
  105. it( 'starts listening for #keystrokes coming from #element', () => {
  106. view = new LinkActionsView( { t: () => {} } );
  107. const spy = sinon.spy( view.keystrokes, 'listenTo' );
  108. view.render();
  109. sinon.assert.calledOnce( spy );
  110. sinon.assert.calledWithExactly( spy, view.element );
  111. } );
  112. describe( 'activates keyboard navigation for the toolbar', () => {
  113. it( 'so "tab" focuses the next focusable item', () => {
  114. const keyEvtData = {
  115. keyCode: keyCodes.tab,
  116. preventDefault: sinon.spy(),
  117. stopPropagation: sinon.spy()
  118. };
  119. // Mock the preview button is focused.
  120. view.focusTracker.isFocused = true;
  121. view.focusTracker.focusedElement = view.previewButtonView.element;
  122. const spy = sinon.spy( view.editButtonView, 'focus' );
  123. view.keystrokes.press( keyEvtData );
  124. sinon.assert.calledOnce( keyEvtData.preventDefault );
  125. sinon.assert.calledOnce( keyEvtData.stopPropagation );
  126. sinon.assert.calledOnce( spy );
  127. } );
  128. it( 'so "shift + tab" focuses the previous focusable item', () => {
  129. const keyEvtData = {
  130. keyCode: keyCodes.tab,
  131. shiftKey: true,
  132. preventDefault: sinon.spy(),
  133. stopPropagation: sinon.spy()
  134. };
  135. // Mock the edit button is focused.
  136. view.focusTracker.isFocused = true;
  137. view.focusTracker.focusedElement = view.editButtonView.element;
  138. const spy = sinon.spy( view.previewButtonView, 'focus' );
  139. view.keystrokes.press( keyEvtData );
  140. sinon.assert.calledOnce( keyEvtData.preventDefault );
  141. sinon.assert.calledOnce( keyEvtData.stopPropagation );
  142. sinon.assert.calledOnce( spy );
  143. } );
  144. } );
  145. } );
  146. describe( 'focus()', () => {
  147. it( 'focuses the #previewButtonView', () => {
  148. const spy = sinon.spy( view.previewButtonView, 'focus' );
  149. view.focus();
  150. sinon.assert.calledOnce( spy );
  151. } );
  152. } );
  153. } );