mediaformview.js 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  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 MediaFormView from '../../src/ui/mediaformview';
  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. describe( 'MediaFormView', () => {
  15. let view;
  16. testUtils.createSinonSandbox();
  17. beforeEach( () => {
  18. view = new MediaFormView( [], { t: val => val } );
  19. view.render();
  20. } );
  21. describe( 'constructor()', () => {
  22. it( 'accepts validators', () => {
  23. const validators = [];
  24. const view = new MediaFormView( validators, { t: val => val } );
  25. expect( view._validators ).to.equal( validators );
  26. } );
  27. it( 'should create element from template', () => {
  28. expect( view.element.classList.contains( 'ck' ) ).to.true;
  29. expect( view.element.classList.contains( 'ck-media-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._unboundChildren.get( 0 ) ).to.equal( view.urlInputView );
  39. expect( view._unboundChildren.get( 1 ) ).to.equal( view.saveButtonView );
  40. expect( view._unboundChildren.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 info text', () => {
  62. expect( view.urlInputView.infoText ).to.match( /^Paste the media URL/ );
  63. } );
  64. it( 'displays the tip upon #input when the field has a value', () => {
  65. view.urlInputView.fieldView.element.value = 'foo';
  66. view.urlInputView.fieldView.fire( 'input' );
  67. expect( view.urlInputView.infoText ).to.match( /^Tip: Paste the URL into/ );
  68. view.urlInputView.fieldView.element.value = '';
  69. view.urlInputView.fieldView.fire( 'input' );
  70. expect( view.urlInputView.infoText ).to.match( /^Paste the media URL/ );
  71. } );
  72. } );
  73. describe( 'template', () => {
  74. it( 'has url input view', () => {
  75. expect( view.template.children[ 0 ] ).to.equal( view.urlInputView );
  76. } );
  77. it( 'has button views', () => {
  78. expect( view.template.children[ 1 ] ).to.equal( view.saveButtonView );
  79. expect( view.template.children[ 2 ] ).to.equal( view.cancelButtonView );
  80. } );
  81. } );
  82. } );
  83. describe( 'render()', () => {
  84. it( 'should register child views in #_focusables', () => {
  85. expect( view._focusables.map( f => f ) ).to.have.members( [
  86. view.urlInputView,
  87. view.saveButtonView,
  88. view.cancelButtonView
  89. ] );
  90. } );
  91. it( 'should register child views\' #element in #focusTracker', () => {
  92. view = new MediaFormView( [], { t: () => {} } );
  93. const spy = testUtils.sinon.spy( view.focusTracker, 'add' );
  94. view.render();
  95. sinon.assert.calledWithExactly( spy.getCall( 0 ), view.urlInputView.element );
  96. sinon.assert.calledWithExactly( spy.getCall( 1 ), view.saveButtonView.element );
  97. sinon.assert.calledWithExactly( spy.getCall( 2 ), view.cancelButtonView.element );
  98. } );
  99. it( 'starts listening for #keystrokes coming from #element', () => {
  100. view = new MediaFormView( [], { t: () => {} } );
  101. const spy = sinon.spy( view.keystrokes, 'listenTo' );
  102. view.render();
  103. sinon.assert.calledOnce( spy );
  104. sinon.assert.calledWithExactly( spy, view.element );
  105. } );
  106. describe( 'activates keyboard navigation for the toolbar', () => {
  107. it( 'so "tab" focuses the next focusable item', () => {
  108. const keyEvtData = {
  109. keyCode: keyCodes.tab,
  110. preventDefault: sinon.spy(),
  111. stopPropagation: sinon.spy()
  112. };
  113. // Mock the url input is focused.
  114. view.focusTracker.isFocused = true;
  115. view.focusTracker.focusedElement = view.urlInputView.element;
  116. const spy = sinon.spy( view.saveButtonView, 'focus' );
  117. view.keystrokes.press( keyEvtData );
  118. sinon.assert.calledOnce( keyEvtData.preventDefault );
  119. sinon.assert.calledOnce( keyEvtData.stopPropagation );
  120. sinon.assert.calledOnce( spy );
  121. } );
  122. it( 'so "shift + tab" focuses the previous focusable item', () => {
  123. const keyEvtData = {
  124. keyCode: keyCodes.tab,
  125. shiftKey: true,
  126. preventDefault: sinon.spy(),
  127. stopPropagation: sinon.spy()
  128. };
  129. // Mock the cancel button is focused.
  130. view.focusTracker.isFocused = true;
  131. view.focusTracker.focusedElement = view.cancelButtonView.element;
  132. const spy = sinon.spy( view.saveButtonView, 'focus' );
  133. view.keystrokes.press( keyEvtData );
  134. sinon.assert.calledOnce( keyEvtData.preventDefault );
  135. sinon.assert.calledOnce( keyEvtData.stopPropagation );
  136. sinon.assert.calledOnce( spy );
  137. } );
  138. } );
  139. it( 'intercepts the arrow* events and overrides the default toolbar behavior', () => {
  140. const keyEvtData = {
  141. stopPropagation: sinon.spy()
  142. };
  143. keyEvtData.keyCode = keyCodes.arrowdown;
  144. view.keystrokes.press( keyEvtData );
  145. sinon.assert.calledOnce( keyEvtData.stopPropagation );
  146. keyEvtData.keyCode = keyCodes.arrowup;
  147. view.keystrokes.press( keyEvtData );
  148. sinon.assert.calledTwice( keyEvtData.stopPropagation );
  149. keyEvtData.keyCode = keyCodes.arrowleft;
  150. view.keystrokes.press( keyEvtData );
  151. sinon.assert.calledThrice( keyEvtData.stopPropagation );
  152. keyEvtData.keyCode = keyCodes.arrowright;
  153. view.keystrokes.press( keyEvtData );
  154. sinon.assert.callCount( keyEvtData.stopPropagation, 4 );
  155. } );
  156. it( 'intercepts the "selectstart" event of the #urlInputView with the high priority', () => {
  157. const spy = sinon.spy();
  158. const event = new Event( 'selectstart', {
  159. bubbles: true,
  160. cancelable: true
  161. } );
  162. event.stopPropagation = spy;
  163. view.urlInputView.element.dispatchEvent( event );
  164. sinon.assert.calledOnce( spy );
  165. } );
  166. } );
  167. describe( 'DOM bindings', () => {
  168. describe( 'submit event', () => {
  169. it( 'should trigger submit event', () => {
  170. const spy = sinon.spy();
  171. view.on( 'submit', spy );
  172. view.element.dispatchEvent( new Event( 'submit' ) );
  173. expect( spy.calledOnce ).to.true;
  174. } );
  175. } );
  176. } );
  177. describe( 'focus()', () => {
  178. it( 'focuses the #urlInputView', () => {
  179. const spy = sinon.spy( view.urlInputView, 'focus' );
  180. view.focus();
  181. sinon.assert.calledOnce( spy );
  182. } );
  183. } );
  184. describe( 'url()', () => {
  185. it( 'returns the #inputView DOM value', () => {
  186. view.urlInputView.fieldView.element.value = 'foo';
  187. expect( view.url ).to.equal( 'foo' );
  188. } );
  189. it( 'sets the #inputView DOM value', () => {
  190. view.urlInputView.fieldView.element.value = 'bar';
  191. view.url = 'foo';
  192. expect( view.urlInputView.fieldView.element.value ).to.equal( 'foo' );
  193. view.url = ' baz ';
  194. expect( view.urlInputView.fieldView.element.value ).to.equal( 'baz' );
  195. } );
  196. } );
  197. describe( 'isValid()', () => {
  198. it( 'calls resetFormStatus()', () => {
  199. const spy = sinon.spy( view, 'resetFormStatus' );
  200. view.isValid();
  201. sinon.assert.calledOnce( spy );
  202. } );
  203. it( 'returns false when at least one validator has failed', () => {
  204. const val1 = sinon.stub().returns( 'some error' );
  205. const val2 = sinon.stub().returns( false );
  206. const validators = [ val1, val2 ];
  207. const view = new MediaFormView( validators, { t: val => val } );
  208. expect( view.isValid() ).to.be.false;
  209. sinon.assert.calledOnce( val1 );
  210. sinon.assert.notCalled( val2 );
  211. expect( view.urlInputView.errorText ).to.equal( 'some error' );
  212. } );
  213. it( 'returns true when all validators passed', () => {
  214. const val1 = sinon.stub().returns( false );
  215. const val2 = sinon.stub().returns( false );
  216. const validators = [ val1, val2 ];
  217. const view = new MediaFormView( validators, { t: val => val } );
  218. expect( view.isValid() ).to.be.true;
  219. sinon.assert.calledOnce( val1 );
  220. sinon.assert.calledOnce( val2 );
  221. expect( view.urlInputView.errorText ).to.be.null;
  222. } );
  223. } );
  224. describe( 'resetFormStatus()', () => {
  225. it( 'resets urlInputView#errorText', () => {
  226. view.urlInputView.errorText = 'foo';
  227. view.resetFormStatus();
  228. expect( view.urlInputView.errorText ).to.be.null;
  229. } );
  230. it( 'resets urlInputView#infoText', () => {
  231. view.urlInputView.infoText = 'foo';
  232. view.resetFormStatus();
  233. expect( view.urlInputView.infoText ).to.match( /^Paste the media URL/ );
  234. } );
  235. } );
  236. } );