imageinsertpanelview.js 11 KB

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