mediaembedui.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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. expect( function createSecondInstance() {
  49. editor.ui.componentFactory.create( 'mediaEmbed' );
  50. } ).not.to.throw();
  51. } );
  52. describe( 'dropdown', () => {
  53. it( 'should bind #isEnabled to the command', () => {
  54. const command = editor.commands.get( 'mediaEmbed' );
  55. expect( dropdown.isEnabled ).to.be.true;
  56. command.isEnabled = false;
  57. expect( dropdown.isEnabled ).to.be.false;
  58. } );
  59. it( 'should add a form to the panelView#children collection', () => {
  60. expect( dropdown.panelView.children.length ).to.equal( 1 );
  61. expect( dropdown.panelView.children.get( 0 ) ).to.be.instanceOf( MediaFormView );
  62. } );
  63. describe( 'button', () => {
  64. it( 'should set a #label of the #buttonView', () => {
  65. expect( dropdown.buttonView.label ).to.equal( 'Insert media' );
  66. } );
  67. it( 'should set an #icon of the #buttonView', () => {
  68. expect( dropdown.buttonView.icon ).to.equal( mediaIcon );
  69. } );
  70. it( 'should enable tooltips for the #buttonView', () => {
  71. expect( dropdown.buttonView.tooltip ).to.be.true;
  72. } );
  73. describe( '#open event', () => {
  74. it( 'executes the actions with the "low" priority', () => {
  75. const spy = sinon.spy();
  76. const selectSpy = sinon.spy( form.urlInputView.fieldView, 'select' );
  77. button.on( 'open', () => {
  78. spy();
  79. } );
  80. button.fire( 'open' );
  81. sinon.assert.callOrder( spy, selectSpy );
  82. } );
  83. it( 'should update form\'s #url', () => {
  84. const command = editor.commands.get( 'mediaEmbed' );
  85. button.fire( 'open' );
  86. expect( form.url ).to.equal( '' );
  87. command.value = 'foo';
  88. button.fire( 'open' );
  89. expect( form.url ).to.equal( 'foo' );
  90. } );
  91. it( 'should select the content of the input', () => {
  92. const spy = sinon.spy( form.urlInputView.fieldView, 'select' );
  93. button.fire( 'open' );
  94. sinon.assert.calledOnce( spy );
  95. } );
  96. it( 'should focus the form', () => {
  97. const spy = sinon.spy( form, 'focus' );
  98. button.fire( 'open' );
  99. sinon.assert.calledOnce( spy );
  100. } );
  101. } );
  102. } );
  103. describe( '#submit event', () => {
  104. it( 'checks if the form is valid', () => {
  105. const spy = sinon.spy( form, 'isValid' );
  106. dropdown.fire( 'submit' );
  107. sinon.assert.calledOnce( spy );
  108. } );
  109. it( 'executes the command and closes the UI (if the form is valid)', () => {
  110. const viewFocusSpy = sinon.spy( editor.editing.view, 'focus' );
  111. const commandSpy = sinon.spy( editor.commands.get( 'mediaEmbed' ), 'execute' );
  112. // The form is invalid.
  113. form.url = 'https://invalid/url';
  114. dropdown.isOpen = true;
  115. dropdown.fire( 'submit' );
  116. sinon.assert.notCalled( commandSpy );
  117. sinon.assert.notCalled( viewFocusSpy );
  118. expect( dropdown.isOpen ).to.be.true;
  119. // The form is valid.
  120. form.url = 'https://valid/url';
  121. dropdown.fire( 'submit' );
  122. sinon.assert.calledOnce( commandSpy );
  123. sinon.assert.calledWithExactly( commandSpy, 'https://valid/url' );
  124. sinon.assert.calledOnce( viewFocusSpy );
  125. expect( dropdown.isOpen ).to.be.false;
  126. } );
  127. } );
  128. describe( '#change:isOpen event', () => {
  129. it( 'resets form status', () => {
  130. const spy = sinon.spy( form, 'resetFormStatus' );
  131. dropdown.fire( 'change:isOpen' );
  132. sinon.assert.calledOnce( spy );
  133. } );
  134. } );
  135. describe( '#cancel event', () => {
  136. it( 'closes the UI', () => {
  137. const viewFocusSpy = sinon.spy( editor.editing.view, 'focus' );
  138. dropdown.isOpen = true;
  139. dropdown.fire( 'cancel' );
  140. sinon.assert.calledOnce( viewFocusSpy );
  141. expect( dropdown.isOpen ).to.be.false;
  142. } );
  143. } );
  144. } );
  145. describe( 'form', () => {
  146. it( 'delegates #submit to the dropdown', done => {
  147. dropdown.once( 'submit', () => done() );
  148. form.fire( 'submit' );
  149. } );
  150. it( 'delegates #cancel to the dropdown', done => {
  151. dropdown.once( 'submit', () => done() );
  152. form.fire( 'submit' );
  153. } );
  154. it( 'binds urlInputView#isReadOnly to command#isEnabled', () => {
  155. const command = editor.commands.get( 'mediaEmbed' );
  156. expect( form.urlInputView.isReadOnly ).to.be.false;
  157. command.isEnabled = false;
  158. expect( form.urlInputView.isReadOnly ).to.be.true;
  159. } );
  160. it( 'binds saveButtonView#isEnabled to command#isEnabled', () => {
  161. const command = editor.commands.get( 'mediaEmbed' );
  162. expect( form.saveButtonView.isEnabled ).to.be.true;
  163. command.isEnabled = false;
  164. expect( form.saveButtonView.isEnabled ).to.be.false;
  165. } );
  166. describe( 'validators', () => {
  167. it( 'check the empty URL', () => {
  168. form.url = '';
  169. expect( form.isValid() ).to.be.false;
  170. form.url = 'https://valid/url';
  171. expect( form.isValid() ).to.be.true;
  172. } );
  173. it( 'check the supported media', () => {
  174. form.url = 'https://invalid/url';
  175. expect( form.isValid() ).to.be.false;
  176. form.url = 'https://valid/url';
  177. expect( form.isValid() ).to.be.true;
  178. } );
  179. } );
  180. } );
  181. } );