mediaembedui.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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. } );
  104. } );
  105. describe( '#submit event', () => {
  106. it( 'checks if the form is valid', () => {
  107. const spy = sinon.spy( form, 'isValid' );
  108. dropdown.fire( 'submit' );
  109. sinon.assert.calledOnce( spy );
  110. } );
  111. it( 'executes the command and closes the UI (if the form is valid)', () => {
  112. const viewFocusSpy = sinon.spy( editor.editing.view, 'focus' );
  113. const commandSpy = sinon.spy( editor.commands.get( 'mediaEmbed' ), 'execute' );
  114. // The form is invalid.
  115. form.url = 'https://invalid/url';
  116. dropdown.isOpen = true;
  117. dropdown.fire( 'submit' );
  118. sinon.assert.notCalled( commandSpy );
  119. sinon.assert.notCalled( viewFocusSpy );
  120. expect( dropdown.isOpen ).to.be.true;
  121. // The form is valid.
  122. form.url = 'https://valid/url';
  123. dropdown.fire( 'submit' );
  124. sinon.assert.calledOnce( commandSpy );
  125. sinon.assert.calledWithExactly( commandSpy, 'https://valid/url' );
  126. sinon.assert.calledOnce( viewFocusSpy );
  127. expect( dropdown.isOpen ).to.be.false;
  128. } );
  129. } );
  130. describe( '#change:isOpen event', () => {
  131. it( 'resets form status', () => {
  132. const spy = sinon.spy( form, 'resetFormStatus' );
  133. dropdown.fire( 'change:isOpen' );
  134. sinon.assert.calledOnce( spy );
  135. } );
  136. } );
  137. describe( '#cancel event', () => {
  138. it( 'closes the UI', () => {
  139. const viewFocusSpy = sinon.spy( editor.editing.view, 'focus' );
  140. dropdown.isOpen = true;
  141. dropdown.fire( 'cancel' );
  142. sinon.assert.calledOnce( viewFocusSpy );
  143. expect( dropdown.isOpen ).to.be.false;
  144. } );
  145. } );
  146. } );
  147. describe( 'form', () => {
  148. it( 'delegates #submit to the dropdown', done => {
  149. dropdown.once( 'submit', () => done() );
  150. form.fire( 'submit' );
  151. } );
  152. it( 'delegates #cancel to the dropdown', done => {
  153. dropdown.once( 'submit', () => done() );
  154. form.fire( 'submit' );
  155. } );
  156. it( 'binds urlInputView#isReadOnly to command#isEnabled', () => {
  157. const command = editor.commands.get( 'mediaEmbed' );
  158. expect( form.urlInputView.isReadOnly ).to.be.false;
  159. command.isEnabled = false;
  160. expect( form.urlInputView.isReadOnly ).to.be.true;
  161. } );
  162. it( 'should trim URL input value', () => {
  163. form.urlInputView.fieldView.element.value = ' ';
  164. form.urlInputView.fieldView.fire( 'input' );
  165. expect( form.mediaURLInputValue ).to.equal( '' );
  166. form.urlInputView.fieldView.element.value = ' test ';
  167. form.urlInputView.fieldView.fire( 'input' );
  168. expect( form.mediaURLInputValue ).to.equal( 'test' );
  169. } );
  170. it( 'binds saveButtonView#isEnabled to trimmed URL input value', () => {
  171. form.urlInputView.fieldView.fire( 'input' );
  172. expect( form.saveButtonView.isEnabled ).to.be.false;
  173. form.urlInputView.fieldView.element.value = 'test';
  174. form.urlInputView.fieldView.fire( 'input' );
  175. expect( form.saveButtonView.isEnabled ).to.be.true;
  176. } );
  177. describe( 'validators', () => {
  178. it( 'check the empty URL', () => {
  179. form.url = '';
  180. expect( form.isValid() ).to.be.false;
  181. form.url = 'https://valid/url';
  182. expect( form.isValid() ).to.be.true;
  183. } );
  184. it( 'check the supported media', () => {
  185. form.url = 'https://invalid/url';
  186. expect( form.isValid() ).to.be.false;
  187. form.url = 'https://valid/url';
  188. expect( form.isValid() ).to.be.true;
  189. } );
  190. } );
  191. } );
  192. } );