8
0

imageresizeui.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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/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( () => {
  36. editorElement = document.createElement( 'div' );
  37. document.body.appendChild( editorElement );
  38. return ClassicTestEditor
  39. .create( editorElement, {
  40. plugins: [ Image, ImageStyle, Paragraph, Undo, Table, ImageResizeUI ],
  41. image: {
  42. resizeUnit: '%',
  43. imageResizeOptions: resizeOptions
  44. }
  45. } )
  46. .then( newEditor => {
  47. editor = newEditor;
  48. command = editor.commands.get( 'imageResize' );
  49. plugin = editor.plugins.get( 'ImageResizeUI' );
  50. } );
  51. } );
  52. afterEach( () => {
  53. if ( editorElement ) {
  54. editorElement.remove();
  55. }
  56. if ( editor ) {
  57. return editor.destroy();
  58. }
  59. } );
  60. describe( 'plugin', () => {
  61. it( 'should be named', () => {
  62. expect( ImageResizeUI.pluginName ).to.equal( 'ImageResizeUI' );
  63. } );
  64. it( 'should be bound to `imageResize#isEnabled`', () => {
  65. command.isEnabled = true;
  66. expect( plugin.isEnabled ).to.be.true;
  67. command.isEnabled = false;
  68. expect( plugin.isEnabled ).to.be.false;
  69. } );
  70. } );
  71. describe( 'init()', () => {
  72. it( 'should have set "%" resize unit', () => {
  73. expect( plugin._resizeUnit ).to.equal( '%' );
  74. } );
  75. it( 'should have set "%" resize unit if not defined', () => {
  76. return ClassicTestEditor
  77. .create( editorElement, {
  78. plugins: [ Image, ImageStyle, Paragraph, Undo, Table, ImageResizeUI ],
  79. image: {
  80. imageResizeOptions: resizeOptions
  81. }
  82. } )
  83. .then( newEditor => {
  84. const plugin = newEditor.plugins.get( 'ImageResizeUI' );
  85. expect( plugin._resizeUnit ).to.equal( '%' );
  86. newEditor.destroy();
  87. } );
  88. } );
  89. it( 'should have set "px" resize unit', () => {
  90. return ClassicTestEditor
  91. .create( editorElement, {
  92. plugins: [ Image, ImageStyle, Paragraph, Undo, Table, ImageResizeUI ],
  93. image: {
  94. resizeUnit: 'px',
  95. imageResizeOptions: resizeOptions
  96. }
  97. } )
  98. .then( newEditor => {
  99. const plugin = newEditor.plugins.get( 'ImageResizeUI' );
  100. expect( plugin._resizeUnit ).to.equal( 'px' );
  101. newEditor.destroy();
  102. } );
  103. } );
  104. it( 'should have configured resize options', () => {
  105. const imageResizeOptions = editor.config.get( 'image.imageResizeOptions' );
  106. expect( imageResizeOptions.length ).to.equal( 3 );
  107. } );
  108. } );
  109. describe( 'resize options dropdown', () => {
  110. it( 'should be bound to `#isEnabled`', () => {
  111. const dropdownView = editor.ui.componentFactory.create( 'imageResize' );
  112. plugin.isEnabled = true;
  113. expect( dropdownView.isEnabled ).to.be.true;
  114. plugin.isEnabled = false;
  115. expect( dropdownView.isEnabled ).to.be.false;
  116. } );
  117. it( 'should be an instance of `DropdownView` if component is created without a value suffix', () => {
  118. expect( editor.ui.componentFactory.create( 'imageResize' ) ).to.be.instanceof( DropdownView );
  119. } );
  120. it( 'should have 3 resize options in the `imageResize` dropdown', () => {
  121. const dropdownView = editor.ui.componentFactory.create( 'imageResize' );
  122. expect( dropdownView.listView.items.length ).to.equal( 3 );
  123. expect( dropdownView.listView.items.first.element.textContent ).to.equal( 'Original size' );
  124. expect( dropdownView.listView.items._items[ 1 ].element.textContent ).to.equal( '50%' );
  125. expect( dropdownView.listView.items.last.element.textContent ).to.equal( '75%' );
  126. } );
  127. it( 'should execute resize command with a proper value', () => {
  128. const dropdownView = editor.ui.componentFactory.create( 'imageResize' );
  129. const commandSpy = sinon.spy( command, 'execute' );
  130. const resizeBy50Percent = dropdownView.listView.items._items[ 1 ].children._items[ 0 ];
  131. // TODO
  132. command.value = { width: resizeBy50Percent.commandValue };
  133. resizeBy50Percent.fire( 'execute' );
  134. sinon.assert.calledOnce( commandSpy );
  135. expect( command.value.width ).to.equal( '50%' );
  136. } );
  137. } );
  138. describe( 'resize option button', () => {
  139. it( 'should be bound to `#isEnabled`', () => {
  140. const buttonView = editor.ui.componentFactory.create( 'imageResize:50' );
  141. plugin.isEnabled = true;
  142. expect( buttonView.isEnabled ).to.be.true;
  143. plugin.isEnabled = false;
  144. expect( buttonView.isEnabled ).to.be.false;
  145. } );
  146. it( 'should be an instance of `ButtonView` if component is created with a value suffix', () => {
  147. expect( editor.ui.componentFactory.create( 'imageResize:50' ) ).to.be.instanceof( ButtonView );
  148. } );
  149. it( 'should be created with visible "50%" label', () => {
  150. const buttonView = editor.ui.componentFactory.create( 'imageResize:50' );
  151. buttonView.render();
  152. expect( buttonView.withText ).to.be.true;
  153. expect( buttonView.label ).to.equal( '50%' );
  154. expect( buttonView.labelView ).to.be.instanceOf( View );
  155. } );
  156. it( 'should have `commandValue` equal "50%"', () => {
  157. const buttonView = editor.ui.componentFactory.create( 'imageResize:50' );
  158. expect( buttonView.commandValue ).to.equal( '50%' );
  159. } );
  160. it( 'should execute `imageResize` command with "50%" value', () => {
  161. const buttonView = editor.ui.componentFactory.create( 'imageResize:50' );
  162. const command = editor.commands.get( 'imageResize' );
  163. const commandSpy = sinon.spy( command, 'execute' );
  164. // TODO
  165. command.value = { width: buttonView.commandValue };
  166. buttonView.fire( 'execute' );
  167. sinon.assert.calledOnce( commandSpy );
  168. expect( command.value.width ).to.equal( '50%' );
  169. } );
  170. } );
  171. } );