imageresizecommand.js 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. import ModelTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/modeltesteditor';
  6. import ImageResizeCommand from '../../src/imageresize/imageresizecommand';
  7. import { setData, getData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  8. describe( 'ImageStyleCommand', () => {
  9. let model, command;
  10. beforeEach( () => {
  11. return ModelTestEditor.create()
  12. .then( newEditor => {
  13. model = newEditor.model;
  14. command = new ImageResizeCommand( newEditor );
  15. model.schema.register( 'p', { inheritAllFrom: '$block' } );
  16. model.schema.register( 'image', {
  17. isObject: true,
  18. isBlock: true,
  19. allowWhere: '$block',
  20. allowAttributes: 'width'
  21. } );
  22. } );
  23. } );
  24. describe( '#isEnabled', () => {
  25. it( 'is true when image is selected', () => {
  26. setData( model, '<p>x</p>[<image width="50px"></image>]<p>x</p>' );
  27. expect( command ).to.have.property( 'isEnabled', true );
  28. } );
  29. it( 'is false when image is not selected', () => {
  30. setData( model, '<p>x[]</p><image width="50px"></image>' );
  31. expect( command ).to.have.property( 'isEnabled', false );
  32. } );
  33. it( 'is false when more than one image is selected', () => {
  34. setData( model, '<p>x</p>[<image width="50px"></image><image width="50px"></image>]' );
  35. expect( command ).to.have.property( 'isEnabled', false );
  36. } );
  37. } );
  38. describe( '#value', () => {
  39. it( 'is null when image is not selected', () => {
  40. setData( model, '<p>x[]</p><image width="50px"></image>' );
  41. expect( command ).to.have.property( 'value', null );
  42. } );
  43. it( 'is set to an object with a width property (and height set to null)', () => {
  44. setData( model, '<p>x</p>[<image width="50px"></image>]<p>x</p>' );
  45. expect( command ).to.have.deep.property( 'value', { width: '50px', height: null } );
  46. } );
  47. it( 'is set to null if image does not have the width set', () => {
  48. setData( model, '<p>x</p>[<image></image>]<p>x</p>' );
  49. expect( command ).to.have.property( 'value', null );
  50. } );
  51. } );
  52. describe( 'execute()', () => {
  53. it( 'sets image width', () => {
  54. setData( model, '[<image width="50px"></image>]' );
  55. command.execute( { width: '100%' } );
  56. expect( getData( model ) ).to.equal( '[<image width="100%"></image>]' );
  57. } );
  58. it( 'removes image width when null passed', () => {
  59. setData( model, '[<image width="50px"></image>]' );
  60. command.execute( { width: null } );
  61. expect( getData( model ) ).to.equal( '[<image></image>]' );
  62. expect( model.document.getRoot().getChild( 0 ).hasAttribute( 'width' ) ).to.be.false;
  63. } );
  64. } );
  65. } );