imageresizebuttons.js 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  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 ImageResizeButtons from '../../src/imageresize/imageresizebuttons';
  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. import { expectToThrowCKEditorError } from '@ckeditor/ckeditor5-utils/tests/_utils/utils';
  18. import iconSmall from '@ckeditor/ckeditor5-core/theme/icons/object-size-small.svg';
  19. import iconMedium from '@ckeditor/ckeditor5-core/theme/icons/object-size-medium.svg';
  20. import iconLarge from '@ckeditor/ckeditor5-core/theme/icons/object-size-large.svg';
  21. import iconFull from '@ckeditor/ckeditor5-core/theme/icons/object-size-full.svg';
  22. describe( 'ImageResizeButtons', () => {
  23. let plugin, command, editor, editorElement;
  24. const resizeOptions = [ {
  25. name: 'imageResize:original',
  26. value: null
  27. },
  28. {
  29. name: 'imageResize:25',
  30. value: '25'
  31. },
  32. {
  33. name: 'imageResize:50',
  34. value: '50'
  35. },
  36. {
  37. name: 'imageResize:75',
  38. value: '75'
  39. } ];
  40. testUtils.createSinonSandbox();
  41. beforeEach( async () => {
  42. editorElement = document.createElement( 'div' );
  43. document.body.appendChild( editorElement );
  44. editor = await ClassicTestEditor
  45. .create( editorElement, {
  46. plugins: [ Image, ImageStyle, Paragraph, Undo, Table, ImageResizeButtons ],
  47. image: {
  48. resizeOptions
  49. }
  50. } );
  51. command = editor.commands.get( 'imageResize' );
  52. plugin = editor.plugins.get( 'ImageResizeButtons' );
  53. } );
  54. afterEach( async () => {
  55. if ( editorElement ) {
  56. editorElement.remove();
  57. }
  58. if ( editor ) {
  59. await editor.destroy();
  60. }
  61. } );
  62. describe( 'plugin', () => {
  63. it( 'should be named', () => {
  64. expect( ImageResizeButtons.pluginName ).to.equal( 'ImageResizeButtons' );
  65. } );
  66. } );
  67. describe( 'constructor()', () => {
  68. it( 'should create `_resizeUnit` with default value of `%`', () => {
  69. const unit = plugin._resizeUnit;
  70. expect( unit ).to.equal( '%' );
  71. } );
  72. } );
  73. describe( 'init()', () => {
  74. it( 'should be disabled when command is disabled', () => {
  75. command.isEnabled = true;
  76. expect( plugin.isEnabled ).to.be.true;
  77. command.isEnabled = false;
  78. expect( plugin.isEnabled ).to.be.false;
  79. } );
  80. } );
  81. describe( 'resize options dropdown', () => {
  82. it( 'should be disabled when plugin is disabled', () => {
  83. const dropdownView = editor.ui.componentFactory.create( 'imageResize' );
  84. plugin.isEnabled = true;
  85. expect( dropdownView.isEnabled ).to.be.true;
  86. plugin.isEnabled = false;
  87. expect( dropdownView.isEnabled ).to.be.false;
  88. } );
  89. it( 'should be an instance of `DropdownView` if component is created without a value suffix', () => {
  90. expect( editor.ui.componentFactory.create( 'imageResize' ) ).to.be.instanceof( DropdownView );
  91. } );
  92. it( 'should have 4 resize options in the `imageResize` dropdown', () => {
  93. const dropdownView = editor.ui.componentFactory.create( 'imageResize' );
  94. expect( dropdownView.listView.items.length ).to.equal( 4 );
  95. expect( dropdownView.listView.items.first.element.textContent ).to.equal( 'Original' );
  96. expect( dropdownView.listView.items._items[ 1 ].element.textContent ).to.equal( '25%' );
  97. expect( dropdownView.listView.items.last.element.textContent ).to.equal( '75%' );
  98. } );
  99. it( 'should be created with a proper tooltip', () => {
  100. const dropdownView = editor.ui.componentFactory.create( 'imageResize' );
  101. expect( dropdownView.buttonView.tooltip ).to.equal( 'Resize image' );
  102. } );
  103. it( 'should execute resize command with a proper value', () => {
  104. const dropdownView = editor.ui.componentFactory.create( 'imageResize' );
  105. const commandSpy = sinon.spy( command, 'execute' );
  106. const resizeBy50Percent = dropdownView.listView.items._items[ 1 ].children._items[ 0 ];
  107. command.isEnabled = true;
  108. resizeBy50Percent.fire( 'execute' );
  109. sinon.assert.calledOnce( commandSpy );
  110. expect( command.value.width ).to.equal( '25%' );
  111. } );
  112. } );
  113. describe( 'resize option button', () => {
  114. let editor, plugin;
  115. beforeEach( async () => {
  116. editor = await ClassicTestEditor
  117. .create( editorElement, {
  118. plugins: [ Image, ImageStyle, Paragraph, Undo, Table, ImageResizeButtons ],
  119. image: {
  120. resizeUnit: '%',
  121. resizeOptions: [ {
  122. name: 'imageResize:original',
  123. value: null,
  124. icon: 'original'
  125. },
  126. {
  127. name: 'imageResize:25',
  128. value: '25',
  129. icon: 'small'
  130. },
  131. {
  132. name: 'imageResize:50',
  133. value: '50',
  134. icon: 'medium'
  135. },
  136. {
  137. name: 'imageResize:75',
  138. value: '75',
  139. icon: 'large'
  140. } ],
  141. toolbar: [ 'imageResize:original', 'imageResize:25', 'imageResize:50', 'imageResize:75' ]
  142. }
  143. } );
  144. plugin = editor.plugins.get( 'ImageResizeButtons' );
  145. } );
  146. afterEach( async () => {
  147. if ( editorElement ) {
  148. editorElement.remove();
  149. }
  150. if ( editor ) {
  151. await editor.destroy();
  152. }
  153. } );
  154. it( 'should be bound to `#isEnabled`', () => {
  155. const buttonView = editor.ui.componentFactory.create( 'imageResize:50' );
  156. plugin.isEnabled = true;
  157. expect( buttonView.isEnabled ).to.be.true;
  158. plugin.isEnabled = false;
  159. expect( buttonView.isEnabled ).to.be.false;
  160. } );
  161. it( 'should be an instance of `ButtonView` if component is created with a value suffix', () => {
  162. expect( editor.ui.componentFactory.create( 'imageResize:50' ) ).to.be.instanceof( ButtonView );
  163. } );
  164. it( 'should be created with invisible "Resize image: 30%" label when is provided', async () => {
  165. const editor = await ClassicTestEditor
  166. .create( editorElement, {
  167. plugins: [ Image, ImageStyle, Paragraph, Undo, Table, ImageResizeButtons ],
  168. image: {
  169. resizeUnit: '%',
  170. resizeOptions: [ {
  171. name: 'imageResize:30',
  172. value: '30',
  173. label: 'Resize image: 30%',
  174. icon: 'small'
  175. } ],
  176. toolbar: [ 'imageResize:30' ]
  177. }
  178. } );
  179. const buttonView = editor.ui.componentFactory.create( 'imageResize:30' );
  180. buttonView.render();
  181. expect( buttonView.withText ).to.be.false;
  182. expect( buttonView.label ).to.equal( 'Resize image: 30%' );
  183. expect( buttonView.labelView ).to.be.instanceOf( View );
  184. editor.destroy();
  185. } );
  186. it( 'should be created with invisible "Resize image to 50%" label when is not provided', async () => {
  187. const buttonView = editor.ui.componentFactory.create( 'imageResize:50' );
  188. buttonView.render();
  189. expect( buttonView.withText ).to.be.false;
  190. expect( buttonView.label ).to.equal( 'Resize image to 50%' );
  191. expect( buttonView.labelView ).to.be.instanceOf( View );
  192. editor.destroy();
  193. } );
  194. it( 'should be created with a proper tooltip, depends on the set value', () => {
  195. const buttonViewOriginal = editor.ui.componentFactory.create( 'imageResize:original' );
  196. const buttonView50 = editor.ui.componentFactory.create( 'imageResize:50' );
  197. buttonViewOriginal.render();
  198. buttonView50.render();
  199. expect( buttonViewOriginal.tooltip ).to.equal( 'Resize image to the original size' );
  200. expect( buttonView50.tooltip ).to.equal( 'Resize image to 50%' );
  201. } );
  202. it( 'should execute `imageResize` command with "50%" value', () => {
  203. const buttonView = editor.ui.componentFactory.create( 'imageResize:50' );
  204. const command = editor.commands.get( 'imageResize' );
  205. const commandSpy = sinon.spy( command, 'execute' );
  206. command.isEnabled = true;
  207. buttonView.fire( 'execute' );
  208. sinon.assert.calledOnce( commandSpy );
  209. expect( command.value.width ).to.equal( '50%' );
  210. } );
  211. it( 'should have set a proper icon', () => {
  212. const buttonOriginal = editor.ui.componentFactory.create( 'imageResize:original' );
  213. const button25 = editor.ui.componentFactory.create( 'imageResize:25' );
  214. const button50 = editor.ui.componentFactory.create( 'imageResize:50' );
  215. const button75 = editor.ui.componentFactory.create( 'imageResize:75' );
  216. expect( buttonOriginal.icon ).to.deep.equal( iconFull );
  217. expect( button25.icon ).to.deep.equal( iconSmall );
  218. expect( button50.icon ).to.deep.equal( iconMedium );
  219. expect( button75.icon ).to.deep.equal( iconLarge );
  220. } );
  221. it( 'should throw the CKEditorError if no `icon` is provided', async () => {
  222. const editor = await ClassicTestEditor
  223. .create( editorElement, {
  224. plugins: [ Image, ImageStyle, Paragraph, Undo, Table, ImageResizeButtons ],
  225. image: {
  226. resizeUnit: '%',
  227. resizeOptions: [ {
  228. name: 'imageResize:noicon',
  229. value: '100'
  230. } ],
  231. toolbar: [ 'imageResize:noicon' ]
  232. }
  233. } );
  234. const errMsg = 'The resize option "imageResize:noicon" misses the "icon" property ' +
  235. 'or the property value doesn\'t match any of the available icons.';
  236. expectToThrowCKEditorError( () => {
  237. editor.ui.componentFactory.create( 'imageResize:noicon' );
  238. }, errMsg, editor );
  239. editor.destroy();
  240. } );
  241. } );
  242. } );