imagestylecommand.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /**
  2. * @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  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( 'command value should be false if no image is selected', () => {
  27. setData( model, '[]<image></image>' );
  28. expect( command.value ).to.be.false;
  29. } );
  30. it( 'should match default style if no imageStyle attribute is present', () => {
  31. setData( model, '[<image></image>]' );
  32. expect( command.value ).to.equal( 'defaultStyle' );
  33. } );
  34. it( 'proper command should have true value when imageStyle attribute is present', () => {
  35. setData( model, '[<image imageStyle="otherStyle"></image>]' );
  36. expect( command.value ).to.equal( 'otherStyle' );
  37. } );
  38. it( 'should have false value if style does not match', () => {
  39. setData( model, '[<image imageStyle="foo"></image>]' );
  40. expect( command.value ).to.be.false;
  41. } );
  42. it( 'should set proper value when executed', () => {
  43. setData( model, '[<image></image>]' );
  44. command.execute( { value: 'otherStyle' } );
  45. expect( getData( model ) ).to.equal( '[<image imageStyle="otherStyle"></image>]' );
  46. } );
  47. it( 'should do nothing when attribute already present', () => {
  48. setData( model, '[<image imageStyle="otherStyle"></image>]' );
  49. command.execute( { value: 'otherStyle' } );
  50. expect( getData( model ) ).to.equal( '[<image imageStyle="otherStyle"></image>]' );
  51. } );
  52. it( 'should use parent batch', () => {
  53. setData( model, '[<image></image>]' );
  54. model.change( writer => {
  55. expect( writer.batch.operations ).to.length( 0 );
  56. command.execute( { value: 'otherStyle' } );
  57. expect( writer.batch.operations ).to.length.above( 0 );
  58. } );
  59. } );
  60. it( 'should be enabled on image element', () => {
  61. setData( model, '[<image></image>]' );
  62. expect( command.isEnabled ).to.be.true;
  63. } );
  64. it( 'should be disabled when not placed on image', () => {
  65. setData( model, '[<p></p>]' );
  66. expect( command.isEnabled ).to.be.false;
  67. } );
  68. it( 'should be disabled when not placed directly on image', () => {
  69. setData( model, '[<p></p><image></image>]' );
  70. expect( command.isEnabled ).to.be.false;
  71. } );
  72. it( 'default style should be active after executing it after another style', () => {
  73. setData( model, '[<image></image>]' );
  74. expect( command.value ).to.equal( 'defaultStyle' );
  75. command.execute( { value: 'otherStyle' } );
  76. expect( getData( model ) ).to.equal( '[<image imageStyle="otherStyle"></image>]' );
  77. command.execute( { value: 'defaultStyle' } );
  78. expect( getData( model ) ).to.equal( '[<image></image>]' );
  79. expect( command.value ).to.equal( 'defaultStyle' );
  80. } );
  81. } );