linkformview.js 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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. /* globals Event */
  6. import LinkFormView from '../../src/ui/linkformview';
  7. import View from '@ckeditor/ckeditor5-ui/src/view';
  8. import { keyCodes } from '@ckeditor/ckeditor5-utils/src/keyboard';
  9. import KeystrokeHandler from '@ckeditor/ckeditor5-utils/src/keystrokehandler';
  10. import FocusTracker from '@ckeditor/ckeditor5-utils/src/focustracker';
  11. import FocusCycler from '@ckeditor/ckeditor5-ui/src/focuscycler';
  12. import ViewCollection from '@ckeditor/ckeditor5-ui/src/viewcollection';
  13. import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
  14. import ManualDecorator from '../../src/utils/manualdecorator';
  15. import Collection from '@ckeditor/ckeditor5-utils/src/collection';
  16. describe( 'LinkFormView', () => {
  17. let view;
  18. testUtils.createSinonSandbox();
  19. beforeEach( () => {
  20. view = new LinkFormView( { t: val => val }, [] );
  21. view.render();
  22. } );
  23. afterEach( () => {
  24. view.destroy();
  25. } );
  26. describe( 'constructor()', () => {
  27. it( 'should create element from template', () => {
  28. expect( view.element.classList.contains( 'ck' ) ).to.true;
  29. expect( view.element.classList.contains( 'ck-link-form' ) ).to.true;
  30. expect( view.element.getAttribute( 'tabindex' ) ).to.equal( '-1' );
  31. } );
  32. it( 'should create child views', () => {
  33. expect( view.urlInputView ).to.be.instanceOf( View );
  34. expect( view.saveButtonView ).to.be.instanceOf( View );
  35. expect( view.cancelButtonView ).to.be.instanceOf( View );
  36. expect( view.saveButtonView.element.classList.contains( 'ck-button-save' ) ).to.be.true;
  37. expect( view.cancelButtonView.element.classList.contains( 'ck-button-cancel' ) ).to.be.true;
  38. expect( view.children.get( 0 ) ).to.equal( view.urlInputView );
  39. expect( view.children.get( 1 ) ).to.equal( view.saveButtonView );
  40. expect( view.children.get( 2 ) ).to.equal( view.cancelButtonView );
  41. } );
  42. it( 'should create #focusTracker instance', () => {
  43. expect( view.focusTracker ).to.be.instanceOf( FocusTracker );
  44. } );
  45. it( 'should create #keystrokes instance', () => {
  46. expect( view.keystrokes ).to.be.instanceOf( KeystrokeHandler );
  47. } );
  48. it( 'should create #_focusCycler instance', () => {
  49. expect( view._focusCycler ).to.be.instanceOf( FocusCycler );
  50. } );
  51. it( 'should create #_focusables view collection', () => {
  52. expect( view._focusables ).to.be.instanceOf( ViewCollection );
  53. } );
  54. it( 'should fire `cancel` event on cancelButtonView#execute', () => {
  55. const spy = sinon.spy();
  56. view.on( 'cancel', spy );
  57. view.cancelButtonView.fire( 'execute' );
  58. expect( spy.calledOnce ).to.true;
  59. } );
  60. describe( 'url input view', () => {
  61. it( 'has placeholder', () => {
  62. expect( view.urlInputView.inputView.placeholder ).to.equal( 'https://example.com' );
  63. } );
  64. } );
  65. describe( 'template', () => {
  66. it( 'has url input view', () => {
  67. expect( view.template.children[ 0 ].get( 0 ) ).to.equal( view.urlInputView );
  68. } );
  69. it( 'has button views', () => {
  70. expect( view.template.children[ 0 ].get( 1 ) ).to.equal( view.saveButtonView );
  71. expect( view.template.children[ 0 ].get( 2 ) ).to.equal( view.cancelButtonView );
  72. } );
  73. } );
  74. } );
  75. describe( 'render()', () => {
  76. it( 'should register child views in #_focusables', () => {
  77. expect( view._focusables.map( f => f ) ).to.have.members( [
  78. view.urlInputView,
  79. view.saveButtonView,
  80. view.cancelButtonView,
  81. ] );
  82. } );
  83. it( 'should register child views\' #element in #focusTracker', () => {
  84. const spy = testUtils.sinon.spy( FocusTracker.prototype, 'add' );
  85. view = new LinkFormView( { t: () => {} }, [] );
  86. view.render();
  87. sinon.assert.calledWithExactly( spy.getCall( 0 ), view.urlInputView.element );
  88. sinon.assert.calledWithExactly( spy.getCall( 1 ), view.saveButtonView.element );
  89. sinon.assert.calledWithExactly( spy.getCall( 2 ), view.cancelButtonView.element );
  90. } );
  91. it( 'starts listening for #keystrokes coming from #element', () => {
  92. view = new LinkFormView( { t: () => {} }, [] );
  93. const spy = sinon.spy( view.keystrokes, 'listenTo' );
  94. view.render();
  95. sinon.assert.calledOnce( spy );
  96. sinon.assert.calledWithExactly( spy, view.element );
  97. } );
  98. describe( 'activates keyboard navigation for the toolbar', () => {
  99. it( 'so "tab" focuses 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. describe( 'customAttributes', () => {
  150. let view, collection;
  151. beforeEach( () => {
  152. collection = new Collection();
  153. collection.add( new ManualDecorator( {
  154. id: 'decorator1',
  155. label: 'Foo',
  156. attributes: {
  157. foo: 'bar'
  158. }
  159. } ) );
  160. collection.add( new ManualDecorator( {
  161. id: 'decorator2',
  162. label: 'Download',
  163. attributes: {
  164. download: 'download'
  165. }
  166. } ) );
  167. collection.add( new ManualDecorator( {
  168. id: 'decorator3',
  169. label: 'Multi',
  170. attributes: {
  171. class: 'fancy-class',
  172. target: '_blank',
  173. rel: 'noopener noreferrer'
  174. }
  175. } ) );
  176. view = new LinkFormView( { t: val => val }, collection );
  177. view.render();
  178. } );
  179. afterEach( () => {
  180. view.destroy();
  181. collection.clear();
  182. } );
  183. it( 'switch buttons reflects state of customAttributes', () => {
  184. expect( view.customAttributesView.length ).to.equal( 3 );
  185. expect( view.customAttributesView.get( 0 ) ).to.deep.include( {
  186. name: 'decorator1',
  187. label: 'Foo'
  188. } );
  189. expect( view.customAttributesView.get( 1 ) ).to.deep.include( {
  190. name: 'decorator2',
  191. label: 'Download'
  192. } );
  193. expect( view.customAttributesView.get( 2 ) ).to.deep.include( {
  194. name: 'decorator3',
  195. label: 'Multi'
  196. } );
  197. } );
  198. it( 'reacts on switch button changes', () => {
  199. const modelItem = collection.first;
  200. const viewItem = view.customAttributesView.first;
  201. expect( modelItem.value ).to.be.undefined;
  202. expect( viewItem.isOn ).to.be.undefined;
  203. viewItem.element.dispatchEvent( new Event( 'click' ) );
  204. expect( modelItem.value ).to.be.true;
  205. expect( viewItem.isOn ).to.be.true;
  206. viewItem.element.dispatchEvent( new Event( 'click' ) );
  207. expect( modelItem.value ).to.be.false;
  208. expect( viewItem.isOn ).to.be.false;
  209. } );
  210. } );
  211. } );