imageuploadpanelview.js 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  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. /* globals Event */
  6. import DropdownView from '@ckeditor/ckeditor5-ui/src/dropdown/dropdownview';
  7. import LabeledFieldView from '@ckeditor/ckeditor5-ui/src/labeledfield/labeledfieldview';
  8. import ImageUploadPanelView from '../../../src/imageupload/ui/imageuploadpanelview';
  9. import ImageUploadFormRowView from '../../../src/imageupload/ui/imageuploadformrowview';
  10. import ButtonView from '@ckeditor/ckeditor5-ui/src/button/buttonview';
  11. import SplitButtonView from '@ckeditor/ckeditor5-ui/src/dropdown/button/splitbuttonview';
  12. import Collection from '@ckeditor/ckeditor5-utils/src/collection';
  13. import { keyCodes } from '@ckeditor/ckeditor5-utils/src/keyboard';
  14. import KeystrokeHandler from '@ckeditor/ckeditor5-utils/src/keystrokehandler';
  15. import FocusTracker from '@ckeditor/ckeditor5-utils/src/focustracker';
  16. import FocusCycler from '@ckeditor/ckeditor5-ui/src/focuscycler';
  17. import ViewCollection from '@ckeditor/ckeditor5-ui/src/viewcollection';
  18. import View from '@ckeditor/ckeditor5-ui/src/view';
  19. import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
  20. describe( 'ImageUploadPanelView', () => {
  21. let view;
  22. beforeEach( () => {
  23. view = new ImageUploadPanelView( { t: val => val } );
  24. view.render();
  25. } );
  26. describe( 'constructor()', () => {
  27. it( 'should contain instance of LabeledInputView as #labeledInputView', () => {
  28. expect( view.labeledInputView ).to.be.instanceOf( LabeledFieldView );
  29. } );
  30. it( 'should contain instance of ButtonView as #insertButtonView', () => {
  31. expect( view.insertButtonView ).to.be.instanceOf( ButtonView );
  32. expect( view.insertButtonView.label ).to.equal( 'Insert' );
  33. } );
  34. it( 'should contain instance of ButtonView as #cancelButtonView', () => {
  35. expect( view.cancelButtonView ).to.be.instanceOf( ButtonView );
  36. expect( view.cancelButtonView.label ).to.equal( 'Cancel' );
  37. } );
  38. it( 'should contain instance of DropdownView as #dropdownView', () => {
  39. expect( view.dropdownView ).to.be.instanceOf( DropdownView );
  40. } );
  41. it( 'should contain instance of SplitButtonView for the #dropdownView button', () => {
  42. expect( view.dropdownView ).to.be.instanceOf( DropdownView );
  43. expect( view.dropdownView.buttonView ).to.be.instanceOf( SplitButtonView );
  44. } );
  45. it( 'should contain #imageURLInputValue', () => {
  46. expect( view.imageURLInputValue ).to.equal( '' );
  47. } );
  48. it( 'should contain #_integrations as an instance of Collection', () => {
  49. expect( view._integrations ).to.be.instanceOf( Collection );
  50. } );
  51. describe( 'integrations', () => {
  52. it( 'should contain 2 integrations when they were passed to the ImageUloadPanelView as options.integrations', () => {
  53. const view = new ImageUploadPanelView( { t: val => val }, {
  54. integrations: {
  55. 'integration1': new View(),
  56. 'integration2': new ButtonView()
  57. }
  58. } );
  59. expect( view._integrations ).to.be.instanceOf( Collection );
  60. expect( view._integrations.length ).to.equal( 2 );
  61. } );
  62. it( 'should contain insertImageViaUrl view when "insertImageViaUrl" token is passed via options.integrations', () => {
  63. const view = new ImageUploadPanelView( { t: val => val }, {
  64. integrations: {
  65. 'insertImageViaUrl': 'insertImageViaUrl',
  66. 'integration1': new View(),
  67. 'integration2': new ButtonView()
  68. }
  69. } );
  70. expect( view._integrations ).to.be.instanceOf( Collection );
  71. expect( view._integrations.length ).to.equal( 3 );
  72. expect( view._integrations.first ).to.be.instanceOf( LabeledFieldView );
  73. } );
  74. } );
  75. it( 'should create #focusTracker instance', () => {
  76. expect( view.focusTracker ).to.be.instanceOf( FocusTracker );
  77. } );
  78. it( 'should create #keystrokes instance', () => {
  79. expect( view.keystrokes ).to.be.instanceOf( KeystrokeHandler );
  80. } );
  81. it( 'should create #_focusCycler instance', () => {
  82. expect( view._focusCycler ).to.be.instanceOf( FocusCycler );
  83. } );
  84. it( 'should create #_focusables view collection', () => {
  85. expect( view._focusables ).to.be.instanceOf( ViewCollection );
  86. } );
  87. describe( 'events', () => {
  88. it( 'should fire "submit" event on insertButtonView#execute', () => {
  89. const spy = sinon.spy();
  90. view.on( 'submit', spy );
  91. view.insertButtonView.fire( 'execute' );
  92. expect( spy.calledOnce ).to.true;
  93. } );
  94. it( 'should fire "cancel" event on cancelButtonView#execute', () => {
  95. const spy = sinon.spy();
  96. view.on( 'cancel', spy );
  97. view.cancelButtonView.fire( 'execute' );
  98. expect( spy.calledOnce ).to.true;
  99. } );
  100. } );
  101. } );
  102. describe( 'image URL input view', () => {
  103. it( 'should have placeholder', () => {
  104. expect( view.labeledInputView.fieldView.placeholder ).to.equal( 'https://example.com/src/image.png' );
  105. } );
  106. it( 'should have info text', () => {
  107. expect( view.labeledInputView.infoText ).to.match( /^Paste the image source URL/ );
  108. } );
  109. } );
  110. describe( 'template', () => {
  111. it( 'should create element from the template', () => {
  112. expect( view.element.classList.contains( 'ck' ) ).to.true;
  113. expect( view.element.classList.contains( 'ck-image-upload-form' ) ).to.true;
  114. expect( view.element.getAttribute( 'tabindex' ) ).to.equal( '-1' );
  115. } );
  116. it( 'should have URL input view', () => {
  117. expect( view.template.children[ 0 ] ).to.equal( view.labeledInputView );
  118. } );
  119. it( 'should have form row view with buttons', () => {
  120. expect( view.template.children[ 1 ] ).to.be.instanceOf( ImageUploadFormRowView );
  121. expect( view.template.children[ 1 ].children.first ).to.equal( view.insertButtonView );
  122. expect( view.template.children[ 1 ].children.last ).to.equal( view.cancelButtonView );
  123. } );
  124. } );
  125. describe( 'render()', () => {
  126. it( 'should register child views in #_focusables', () => {
  127. expect( view._focusables.map( f => f ) ).to.have.members( [
  128. view.labeledInputView,
  129. view.insertButtonView,
  130. view.cancelButtonView
  131. ] );
  132. } );
  133. it( 'should register child views\' #element in #focusTracker', () => {
  134. const spy = testUtils.sinon.spy( FocusTracker.prototype, 'add' );
  135. view = new ImageUploadPanelView( { t: () => {} } );
  136. view.render();
  137. sinon.assert.calledWithExactly( spy.getCall( 0 ), view.labeledInputView.element );
  138. sinon.assert.calledWithExactly( spy.getCall( 1 ), view.insertButtonView.element );
  139. sinon.assert.calledWithExactly( spy.getCall( 2 ), view.cancelButtonView.element );
  140. } );
  141. it( 'starts listening for #keystrokes coming from #element', () => {
  142. view = new ImageUploadPanelView( { t: () => {} } );
  143. const spy = sinon.spy( view.keystrokes, 'listenTo' );
  144. view.render();
  145. sinon.assert.calledOnce( spy );
  146. sinon.assert.calledWithExactly( spy, view.element );
  147. } );
  148. it( 'intercepts the arrow* events and overrides the default toolbar behavior', () => {
  149. const keyEvtData = {
  150. stopPropagation: sinon.spy()
  151. };
  152. keyEvtData.keyCode = keyCodes.arrowdown;
  153. view.keystrokes.press( keyEvtData );
  154. sinon.assert.calledOnce( keyEvtData.stopPropagation );
  155. keyEvtData.keyCode = keyCodes.arrowup;
  156. view.keystrokes.press( keyEvtData );
  157. sinon.assert.calledTwice( keyEvtData.stopPropagation );
  158. keyEvtData.keyCode = keyCodes.arrowleft;
  159. view.keystrokes.press( keyEvtData );
  160. sinon.assert.calledThrice( keyEvtData.stopPropagation );
  161. keyEvtData.keyCode = keyCodes.arrowright;
  162. view.keystrokes.press( keyEvtData );
  163. sinon.assert.callCount( keyEvtData.stopPropagation, 4 );
  164. } );
  165. it( 'intercepts the "selectstart" event of the #labeledInputView with the high priority', () => {
  166. const spy = sinon.spy();
  167. const event = new Event( 'selectstart', {
  168. bubbles: true,
  169. cancelable: true
  170. } );
  171. event.stopPropagation = spy;
  172. view.labeledInputView.element.dispatchEvent( event );
  173. sinon.assert.calledOnce( spy );
  174. } );
  175. describe( 'activates keyboard navigation for the toolbar', () => {
  176. it( 'so "tab" focuses the next focusable item', () => {
  177. const keyEvtData = {
  178. keyCode: keyCodes.tab,
  179. preventDefault: sinon.spy(),
  180. stopPropagation: sinon.spy()
  181. };
  182. // Mock the url input is focused.
  183. view.focusTracker.isFocused = true;
  184. view.focusTracker.focusedElement = view.labeledInputView.element;
  185. const spy = sinon.spy( view.insertButtonView, 'focus' );
  186. view.keystrokes.press( keyEvtData );
  187. sinon.assert.calledOnce( keyEvtData.preventDefault );
  188. sinon.assert.calledOnce( keyEvtData.stopPropagation );
  189. sinon.assert.calledOnce( spy );
  190. } );
  191. it( 'so "shift + tab" focuses the previous focusable item', () => {
  192. const keyEvtData = {
  193. keyCode: keyCodes.tab,
  194. shiftKey: true,
  195. preventDefault: sinon.spy(),
  196. stopPropagation: sinon.spy()
  197. };
  198. // Mock the cancel button is focused.
  199. view.focusTracker.isFocused = true;
  200. view.focusTracker.focusedElement = view.cancelButtonView.element;
  201. const spy = sinon.spy( view.insertButtonView, 'focus' );
  202. view.keystrokes.press( keyEvtData );
  203. sinon.assert.calledOnce( keyEvtData.preventDefault );
  204. sinon.assert.calledOnce( keyEvtData.stopPropagation );
  205. sinon.assert.calledOnce( spy );
  206. } );
  207. } );
  208. } );
  209. describe( 'focus()', () => {
  210. it( 'should focus on the #labeledInputView', () => {
  211. const spy = sinon.spy( view.labeledInputView, 'focus' );
  212. view.focus();
  213. sinon.assert.calledOnce( spy );
  214. } );
  215. } );
  216. describe( 'URL input', () => {
  217. it( 'should be bound with #imageURLInputValue', () => {
  218. view.labeledInputView.fieldView.element.value = 'abc';
  219. view.labeledInputView.fieldView.fire( 'input' );
  220. expect( view.imageURLInputValue ).to.equal( 'abc' );
  221. view.labeledInputView.fieldView.element.value = 'xyz';
  222. view.labeledInputView.fieldView.fire( 'input' );
  223. expect( view.imageURLInputValue ).to.equal( 'xyz' );
  224. } );
  225. } );
  226. } );