mediaformview.js 9.1 KB

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