imageresizeui.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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. /* global document */
  6. import ClassicTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/classictesteditor';
  7. import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
  8. import Image from '../../../src/image';
  9. import DropdownView from '@ckeditor/ckeditor5-ui/src/dropdown/dropdownview';
  10. import ButtonView from '@ckeditor/ckeditor5-ui/src/button/buttonview';
  11. import View from '@ckeditor/ckeditor5-ui/src/view';
  12. import ImageResizeUI from '../../../src/imageresize/ui/imageresizeui';
  13. import ImageStyle from '../../../src/imagestyle';
  14. import Undo from '@ckeditor/ckeditor5-undo/src/undo';
  15. import Table from '@ckeditor/ckeditor5-table/src/table';
  16. import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
  17. describe( 'ImageResizeUI', () => {
  18. let plugin, command, editor, editorElement;
  19. const resizeOptions = [ {
  20. name: 'imageResize:original',
  21. label: 'Original size',
  22. value: null
  23. },
  24. {
  25. name: 'imageResize:50',
  26. label: '50%',
  27. value: '50'
  28. },
  29. {
  30. name: 'imageResize:75',
  31. label: '75%',
  32. value: '75'
  33. } ];
  34. testUtils.createSinonSandbox();
  35. beforeEach( async () => {
  36. editorElement = document.createElement( 'div' );
  37. document.body.appendChild( editorElement );
  38. editor = await ClassicTestEditor
  39. .create( editorElement, {
  40. plugins: [ Image, ImageStyle, Paragraph, Undo, Table, ImageResizeUI ],
  41. image: {
  42. resizeUnit: '%',
  43. resizeOptions
  44. }
  45. } );
  46. command = editor.commands.get( 'imageResize' );
  47. plugin = editor.plugins.get( 'ImageResizeUI' );
  48. } );
  49. afterEach( async () => {
  50. if ( editorElement ) {
  51. editorElement.remove();
  52. }
  53. if ( editor ) {
  54. await editor.destroy();
  55. }
  56. } );
  57. describe( 'plugin', () => {
  58. it( 'should be named', () => {
  59. expect( ImageResizeUI.pluginName ).to.equal( 'ImageResizeUI' );
  60. } );
  61. it( 'should be disabled when command is disabled', () => {
  62. command.isEnabled = true;
  63. expect( plugin.isEnabled ).to.be.true;
  64. command.isEnabled = false;
  65. expect( plugin.isEnabled ).to.be.false;
  66. } );
  67. } );
  68. describe( 'init()', () => {
  69. it( 'should have set "%" resize unit', () => {
  70. const unit = editor.config.get( 'image.resizeUnit' );
  71. expect( unit ).to.equal( '%' );
  72. } );
  73. it( 'should have set "%" resize unit if not defined', async () => {
  74. const editor = await ClassicTestEditor
  75. .create( editorElement, {
  76. plugins: [ Image, ImageStyle, Paragraph, Undo, Table, ImageResizeUI ],
  77. image: {
  78. resizeOptions
  79. }
  80. } );
  81. const button = editor.ui.componentFactory.create( 'imageResize:50' );
  82. const command = editor.commands.get( 'imageResize' );
  83. command.isEnabled = true;
  84. button.fire( 'execute' );
  85. expect( command.value.width.includes( '%' ) ).to.be.true;
  86. await editor.destroy();
  87. } );
  88. it( 'should have set "px" resize unit', async () => {
  89. const editor = await ClassicTestEditor
  90. .create( editorElement, {
  91. plugins: [ Image, ImageStyle, Paragraph, Undo, Table, ImageResizeUI ],
  92. image: {
  93. resizeUnit: 'px',
  94. resizeOptions
  95. }
  96. } );
  97. const button = editor.ui.componentFactory.create( 'imageResize:50' );
  98. const command = editor.commands.get( 'imageResize' );
  99. command.isEnabled = true;
  100. button.fire( 'execute' );
  101. expect( command.value.width.includes( 'px' ) ).to.be.true;
  102. await editor.destroy();
  103. } );
  104. it( 'should not register a dropdown or buttons if no resize options passed', async () => {
  105. const editor = await ClassicTestEditor
  106. .create( editorElement, {
  107. plugins: [ Image, ImageStyle, Paragraph, Undo, Table, ImageResizeUI ],
  108. image: {
  109. resizeUnit: 'px'
  110. }
  111. } );
  112. const resizeOptions = editor.config.get( 'image.resizeOptions' );
  113. expect( resizeOptions ).to.be.undefined;
  114. expect( editor.ui.componentFactory.has( 'imageResize' ) ).to.be.false;
  115. await editor.destroy();
  116. } );
  117. } );
  118. describe( 'resize options dropdown', () => {
  119. it( 'should be disabled when plugin is disabled', () => {
  120. const dropdownView = editor.ui.componentFactory.create( 'imageResize' );
  121. plugin.isEnabled = true;
  122. expect( dropdownView.isEnabled ).to.be.true;
  123. plugin.isEnabled = false;
  124. expect( dropdownView.isEnabled ).to.be.false;
  125. } );
  126. it( 'should be an instance of `DropdownView` if component is created without a value suffix', () => {
  127. expect( editor.ui.componentFactory.create( 'imageResize' ) ).to.be.instanceof( DropdownView );
  128. } );
  129. it( 'should have 3 resize options in the `imageResize` dropdown', () => {
  130. const dropdownView = editor.ui.componentFactory.create( 'imageResize' );
  131. expect( dropdownView.listView.items.length ).to.equal( 3 );
  132. expect( dropdownView.listView.items.first.element.textContent ).to.equal( 'Original size' );
  133. expect( dropdownView.listView.items._items[ 1 ].element.textContent ).to.equal( '50%' );
  134. expect( dropdownView.listView.items.last.element.textContent ).to.equal( '75%' );
  135. } );
  136. it( 'should execute resize command with a proper value', () => {
  137. const dropdownView = editor.ui.componentFactory.create( 'imageResize' );
  138. const commandSpy = sinon.spy( command, 'execute' );
  139. const resizeBy50Percent = dropdownView.listView.items._items[ 1 ].children._items[ 0 ];
  140. command.isEnabled = true;
  141. resizeBy50Percent.fire( 'execute' );
  142. sinon.assert.calledOnce( commandSpy );
  143. expect( command.value.width ).to.equal( '50%' );
  144. } );
  145. } );
  146. describe( 'resize option button', () => {
  147. it( 'should be bound to `#isEnabled`', () => {
  148. const buttonView = editor.ui.componentFactory.create( 'imageResize:50' );
  149. plugin.isEnabled = true;
  150. expect( buttonView.isEnabled ).to.be.true;
  151. plugin.isEnabled = false;
  152. expect( buttonView.isEnabled ).to.be.false;
  153. } );
  154. it( 'should be an instance of `ButtonView` if component is created with a value suffix', () => {
  155. expect( editor.ui.componentFactory.create( 'imageResize:50' ) ).to.be.instanceof( ButtonView );
  156. } );
  157. it( 'should be created with visible "50%" label', () => {
  158. const buttonView = editor.ui.componentFactory.create( 'imageResize:50' );
  159. buttonView.render();
  160. expect( buttonView.withText ).to.be.true;
  161. expect( buttonView.label ).to.equal( '50%' );
  162. expect( buttonView.labelView ).to.be.instanceOf( View );
  163. } );
  164. it( 'should have `commandValue` equal "50%"', () => {
  165. const buttonView = editor.ui.componentFactory.create( 'imageResize:50' );
  166. expect( buttonView.commandValue ).to.equal( '50%' );
  167. } );
  168. it( 'should execute `imageResize` command with "50%" value', () => {
  169. const buttonView = editor.ui.componentFactory.create( 'imageResize:50' );
  170. const command = editor.commands.get( 'imageResize' );
  171. const commandSpy = sinon.spy( command, 'execute' );
  172. command.isEnabled = true;
  173. buttonView.fire( 'execute' );
  174. sinon.assert.calledOnce( commandSpy );
  175. expect( command.value.width ).to.equal( '50%' );
  176. } );
  177. } );
  178. } );