8
0

mediaembedui.js 6.0 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. providers: [
  21. {
  22. name: 'valid-media',
  23. url: /^https:\/\/valid\/(.*)/,
  24. html: id => `<iframe src="${ id }"></iframe>`
  25. }
  26. ]
  27. }
  28. } )
  29. .then( newEditor => {
  30. editor = newEditor;
  31. dropdown = editor.ui.componentFactory.create( 'mediaEmbed' );
  32. button = dropdown.buttonView;
  33. form = dropdown.panelView.children.get( 0 );
  34. } );
  35. } );
  36. afterEach( () => {
  37. editorElement.remove();
  38. return editor.destroy();
  39. } );
  40. it( 'should add the "mediaEmbed" component to the factory', () => {
  41. expect( dropdown ).to.be.instanceOf( DropdownView );
  42. } );
  43. describe( 'dropdown', () => {
  44. it( 'should bind #isEnabled to the command', () => {
  45. const command = editor.commands.get( 'mediaEmbed' );
  46. expect( dropdown.isEnabled ).to.be.true;
  47. command.isEnabled = false;
  48. expect( dropdown.isEnabled ).to.be.false;
  49. } );
  50. it( 'should add a form to the panelView#children collection', () => {
  51. expect( dropdown.panelView.children.length ).to.equal( 1 );
  52. expect( dropdown.panelView.children.get( 0 ) ).to.be.instanceOf( MediaFormView );
  53. } );
  54. describe( 'button', () => {
  55. it( 'should set a #label of the #buttonView', () => {
  56. expect( dropdown.buttonView.label ).to.equal( 'Insert media' );
  57. } );
  58. it( 'should set an #icon of the #buttonView', () => {
  59. expect( dropdown.buttonView.icon ).to.equal( mediaIcon );
  60. } );
  61. it( 'should enable tooltips for the #buttonView', () => {
  62. expect( dropdown.buttonView.tooltip ).to.be.true;
  63. } );
  64. describe( '#open event', () => {
  65. it( 'executes the actions with the "low" priority', () => {
  66. const spy = sinon.spy();
  67. const selectSpy = sinon.spy( form.urlInputView, 'select' );
  68. button.on( 'open', () => {
  69. spy();
  70. } );
  71. button.fire( 'open' );
  72. sinon.assert.callOrder( spy, selectSpy );
  73. } );
  74. it( 'should update form\'s #url', () => {
  75. const command = editor.commands.get( 'mediaEmbed' );
  76. button.fire( 'open' );
  77. expect( form.url ).to.equal( '' );
  78. command.value = 'foo';
  79. button.fire( 'open' );
  80. expect( form.url ).to.equal( 'foo' );
  81. } );
  82. it( 'should select the content of the input', () => {
  83. const spy = sinon.spy( form.urlInputView, 'select' );
  84. button.fire( 'open' );
  85. sinon.assert.calledOnce( spy );
  86. } );
  87. it( 'should focus the form', () => {
  88. const spy = sinon.spy( form, 'focus' );
  89. button.fire( 'open' );
  90. sinon.assert.calledOnce( spy );
  91. } );
  92. } );
  93. } );
  94. describe( '#submit event', () => {
  95. it( 'checks if the form is valid', () => {
  96. const spy = sinon.spy( form, 'isValid' );
  97. dropdown.fire( 'submit' );
  98. sinon.assert.calledOnce( spy );
  99. } );
  100. it( 'executes the command and closes the UI (if the form is valid)', () => {
  101. const viewFocusSpy = sinon.spy( editor.editing.view, 'focus' );
  102. const commandSpy = sinon.spy( editor.commands.get( 'mediaEmbed' ), 'execute' );
  103. // The form is invalid.
  104. form.url = 'https://invalid/url';
  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. form.url = 'https://valid/url';
  112. dropdown.fire( 'submit' );
  113. sinon.assert.calledOnce( commandSpy );
  114. sinon.assert.calledWithExactly( commandSpy, 'https://valid/url' );
  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( 'mediaEmbed' );
  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( 'mediaEmbed' );
  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. } );