imagestylecommand.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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 ImageStyleCommand from '../../src/imagestyle/imagestylecommand';
  7. import { setData, getData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  8. describe( 'ImageStyleCommand', () => {
  9. const defaultStyle = { name: 'defaultStyle', title: 'foo bar', icon: 'icon-1', isDefault: true };
  10. const otherStyle = { name: 'otherStyle', title: 'baz', icon: 'icon-2', className: 'other-class-name' };
  11. let model, command;
  12. beforeEach( () => {
  13. return ModelTestEditor.create()
  14. .then( newEditor => {
  15. model = newEditor.model;
  16. command = new ImageStyleCommand( newEditor, [ defaultStyle, otherStyle ] );
  17. model.schema.register( 'p', { inheritAllFrom: '$block' } );
  18. model.schema.register( 'image', {
  19. isObject: true,
  20. isBlock: true,
  21. allowWhere: '$block',
  22. allowAttributes: 'imageStyle'
  23. } );
  24. } );
  25. } );
  26. it( 'should have defaultStyle property correctly set', () => {
  27. expect( command.defaultStyle ).to.equal( 'defaultStyle' );
  28. } );
  29. it( 'command value should be false if no image is selected', () => {
  30. setData( model, '<p>[]</p><image></image>' );
  31. expect( command.value ).to.be.false;
  32. } );
  33. it( 'should match default style if no imageStyle attribute is present', () => {
  34. setData( model, '[<image></image>]' );
  35. expect( command.value ).to.equal( 'defaultStyle' );
  36. } );
  37. it( 'proper command should have true value when imageStyle attribute is present', () => {
  38. setData( model, '[<image imageStyle="otherStyle"></image>]' );
  39. expect( command.value ).to.equal( 'otherStyle' );
  40. } );
  41. it( 'should have false value if style does not match', () => {
  42. setData( model, '[<image imageStyle="foo"></image>]' );
  43. expect( command.value ).to.be.false;
  44. } );
  45. it( 'should set proper value when executed', () => {
  46. setData( model, '[<image></image>]' );
  47. command.execute( { value: 'otherStyle' } );
  48. expect( getData( model ) ).to.equal( '[<image imageStyle="otherStyle"></image>]' );
  49. } );
  50. it( 'should do nothing when attribute already present', () => {
  51. setData( model, '[<image imageStyle="otherStyle"></image>]' );
  52. command.execute( { value: 'otherStyle' } );
  53. expect( getData( model ) ).to.equal( '[<image imageStyle="otherStyle"></image>]' );
  54. } );
  55. it( 'should use parent batch', () => {
  56. setData( model, '[<image></image>]' );
  57. model.change( writer => {
  58. expect( writer.batch.operations ).to.length( 0 );
  59. command.execute( { value: 'otherStyle' } );
  60. expect( writer.batch.operations ).to.length.above( 0 );
  61. } );
  62. } );
  63. it( 'should be enabled on image element', () => {
  64. setData( model, '[<image></image>]' );
  65. expect( command.isEnabled ).to.be.true;
  66. } );
  67. it( 'should be disabled when not placed on image', () => {
  68. setData( model, '[<p></p>]' );
  69. expect( command.isEnabled ).to.be.false;
  70. } );
  71. it( 'should be disabled when not placed directly on image', () => {
  72. setData( model, '[<p></p><image></image>]' );
  73. expect( command.isEnabled ).to.be.false;
  74. } );
  75. it( 'default style should be active after executing it after another style', () => {
  76. setData( model, '[<image></image>]' );
  77. expect( command.value ).to.equal( 'defaultStyle' );
  78. command.execute( { value: 'otherStyle' } );
  79. expect( getData( model ) ).to.equal( '[<image imageStyle="otherStyle"></image>]' );
  80. command.execute( { value: 'defaultStyle' } );
  81. expect( getData( model ) ).to.equal( '[<image></image>]' );
  82. expect( command.value ).to.equal( 'defaultStyle' );
  83. } );
  84. } );