mediaembedui.js 6.2 KB

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