mediaembedui.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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. import ClassicTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/classictesteditor';
  6. import MediaEmbed from '../src/mediaembed';
  7. import MediaEmbedUI from '../src/mediaembedui';
  8. import MediaFormView from '../src/ui/mediaformview';
  9. import DropdownView from '@ckeditor/ckeditor5-ui/src/dropdown/dropdownview';
  10. import global from '@ckeditor/ckeditor5-utils/src/dom/global';
  11. import mediaIcon from '../theme/icons/media.svg';
  12. describe( 'MediaEmbedUI', () => {
  13. let editorElement, editor, dropdown, button, form;
  14. beforeEach( () => {
  15. editorElement = global.document.createElement( 'div' );
  16. global.document.body.appendChild( editorElement );
  17. return ClassicTestEditor
  18. .create( editorElement, {
  19. plugins: [ MediaEmbed ],
  20. mediaEmbed: {
  21. providers: [
  22. {
  23. name: 'valid-media',
  24. url: /^https:\/\/valid\/(.*)/,
  25. html: id => `<iframe src="${ id }"></iframe>`
  26. }
  27. ]
  28. }
  29. } )
  30. .then( newEditor => {
  31. editor = newEditor;
  32. dropdown = editor.ui.componentFactory.create( 'mediaEmbed' );
  33. button = dropdown.buttonView;
  34. form = dropdown.panelView.children.get( 0 );
  35. } );
  36. } );
  37. afterEach( () => {
  38. editorElement.remove();
  39. return editor.destroy();
  40. } );
  41. it( 'should be named', () => {
  42. expect( MediaEmbedUI.pluginName ).to.equal( 'MediaEmbedUI' );
  43. } );
  44. it( 'should add the "mediaEmbed" component to the factory', () => {
  45. expect( dropdown ).to.be.instanceOf( DropdownView );
  46. } );
  47. it( 'should allow creating two instances', () => {
  48. let secondInstance;
  49. expect( function createSecondInstance() {
  50. secondInstance = editor.ui.componentFactory.create( 'mediaEmbed' );
  51. } ).not.to.throw();
  52. expect( dropdown ).to.not.equal( secondInstance );
  53. } );
  54. describe( 'dropdown', () => {
  55. it( 'should bind #isEnabled to the command', () => {
  56. const command = editor.commands.get( 'mediaEmbed' );
  57. expect( dropdown.isEnabled ).to.be.true;
  58. command.isEnabled = false;
  59. expect( dropdown.isEnabled ).to.be.false;
  60. } );
  61. it( 'should add a form to the panelView#children collection', () => {
  62. expect( dropdown.panelView.children.length ).to.equal( 1 );
  63. expect( dropdown.panelView.children.get( 0 ) ).to.be.instanceOf( MediaFormView );
  64. } );
  65. describe( 'button', () => {
  66. it( 'should set a #label of the #buttonView', () => {
  67. expect( dropdown.buttonView.label ).to.equal( 'Insert media' );
  68. } );
  69. it( 'should set an #icon of the #buttonView', () => {
  70. expect( dropdown.buttonView.icon ).to.equal( mediaIcon );
  71. } );
  72. it( 'should enable tooltips for the #buttonView', () => {
  73. expect( dropdown.buttonView.tooltip ).to.be.true;
  74. } );
  75. describe( '#open event', () => {
  76. it( 'executes the actions with the "low" priority', () => {
  77. const spy = sinon.spy();
  78. const selectSpy = sinon.spy( form.urlInputView.fieldView, 'select' );
  79. button.on( 'open', () => {
  80. spy();
  81. } );
  82. button.fire( 'open' );
  83. sinon.assert.callOrder( spy, selectSpy );
  84. } );
  85. it( 'should update form\'s #url', () => {
  86. const command = editor.commands.get( 'mediaEmbed' );
  87. button.fire( 'open' );
  88. expect( form.url ).to.equal( '' );
  89. command.value = 'foo';
  90. button.fire( 'open' );
  91. expect( form.url ).to.equal( 'foo' );
  92. } );
  93. it( 'should select the content of the input', () => {
  94. const spy = sinon.spy( form.urlInputView.fieldView, 'select' );
  95. button.fire( 'open' );
  96. sinon.assert.calledOnce( spy );
  97. } );
  98. it( 'should focus the form', () => {
  99. const spy = sinon.spy( form, 'focus' );
  100. button.fire( 'open' );
  101. sinon.assert.calledOnce( spy );
  102. } );
  103. it( 'should disable CSS transitions to avoid unnecessary animations (and then enable them again)', () => {
  104. const disableCssTransitionsSpy = sinon.spy( form, 'disableCssTransitions' );
  105. const enableCssTransitionsSpy = sinon.spy( form, 'enableCssTransitions' );
  106. const selectSpy = sinon.spy( form.urlInputView.fieldView, 'select' );
  107. button.fire( 'open' );
  108. sinon.assert.callOrder( disableCssTransitionsSpy, selectSpy, enableCssTransitionsSpy );
  109. } );
  110. } );
  111. } );
  112. describe( '#submit event', () => {
  113. it( 'checks if the form is valid', () => {
  114. const spy = sinon.spy( form, 'isValid' );
  115. dropdown.fire( 'submit' );
  116. sinon.assert.calledOnce( spy );
  117. } );
  118. it( 'executes the command and closes the UI (if the form is valid)', () => {
  119. const viewFocusSpy = sinon.spy( editor.editing.view, 'focus' );
  120. const commandSpy = sinon.spy( editor.commands.get( 'mediaEmbed' ), 'execute' );
  121. // The form is invalid.
  122. form.url = 'https://invalid/url';
  123. dropdown.isOpen = true;
  124. dropdown.fire( 'submit' );
  125. sinon.assert.notCalled( commandSpy );
  126. sinon.assert.notCalled( viewFocusSpy );
  127. expect( dropdown.isOpen ).to.be.true;
  128. // The form is valid.
  129. form.url = 'https://valid/url';
  130. dropdown.fire( 'submit' );
  131. sinon.assert.calledOnce( commandSpy );
  132. sinon.assert.calledWithExactly( commandSpy, 'https://valid/url' );
  133. sinon.assert.calledOnce( viewFocusSpy );
  134. expect( dropdown.isOpen ).to.be.false;
  135. } );
  136. } );
  137. describe( '#change:isOpen event', () => {
  138. it( 'resets form status', () => {
  139. const spy = sinon.spy( form, 'resetFormStatus' );
  140. dropdown.fire( 'change:isOpen' );
  141. sinon.assert.calledOnce( spy );
  142. } );
  143. } );
  144. describe( '#cancel event', () => {
  145. it( 'closes the UI', () => {
  146. const viewFocusSpy = sinon.spy( editor.editing.view, 'focus' );
  147. dropdown.isOpen = true;
  148. dropdown.fire( 'cancel' );
  149. sinon.assert.calledOnce( viewFocusSpy );
  150. expect( dropdown.isOpen ).to.be.false;
  151. } );
  152. } );
  153. } );
  154. describe( 'form', () => {
  155. it( 'delegates #submit to the dropdown', done => {
  156. dropdown.once( 'submit', () => done() );
  157. form.fire( 'submit' );
  158. } );
  159. it( 'delegates #cancel to the dropdown', done => {
  160. dropdown.once( 'submit', () => done() );
  161. form.fire( 'submit' );
  162. } );
  163. it( 'binds urlInputView#isReadOnly to command#isEnabled', () => {
  164. const command = editor.commands.get( 'mediaEmbed' );
  165. expect( form.urlInputView.isReadOnly ).to.be.false;
  166. command.isEnabled = false;
  167. expect( form.urlInputView.isReadOnly ).to.be.true;
  168. } );
  169. it( 'should trim URL input value', () => {
  170. form.urlInputView.fieldView.element.value = ' ';
  171. form.urlInputView.fieldView.fire( 'input' );
  172. expect( form.mediaURLInputValue ).to.equal( '' );
  173. form.urlInputView.fieldView.element.value = ' test ';
  174. form.urlInputView.fieldView.fire( 'input' );
  175. expect( form.mediaURLInputValue ).to.equal( 'test' );
  176. } );
  177. it( 'binds saveButtonView#isEnabled to trimmed URL input value', () => {
  178. form.urlInputView.fieldView.fire( 'input' );
  179. expect( form.saveButtonView.isEnabled ).to.be.false;
  180. form.urlInputView.fieldView.element.value = 'test';
  181. form.urlInputView.fieldView.fire( 'input' );
  182. expect( form.saveButtonView.isEnabled ).to.be.true;
  183. } );
  184. describe( 'validators', () => {
  185. it( 'check the empty URL', () => {
  186. form.url = '';
  187. expect( form.isValid() ).to.be.false;
  188. form.url = 'https://valid/url';
  189. expect( form.isValid() ).to.be.true;
  190. } );
  191. it( 'check the supported media', () => {
  192. form.url = 'https://invalid/url';
  193. expect( form.isValid() ).to.be.false;
  194. form.url = 'https://valid/url';
  195. expect( form.isValid() ).to.be.true;
  196. } );
  197. } );
  198. } );
  199. } );