mediaembedui.js 6.1 KB

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