mediaformview.js 9.7 KB

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