mediaformview.js 9.6 KB

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