mediaformview.js 8.8 KB

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