imagestylecommand.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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 do nothing attribute is not known', () => {
  53. setData( model, '[<image imageStyle="otherStyle"></image>]' );
  54. command.execute( { value: 'someNotExistingStyle' } );
  55. expect( getData( model ) ).to.equal( '[<image imageStyle="otherStyle"></image>]' );
  56. } );
  57. it( 'should do nothing when value is not passed', () => {
  58. setData( model, '[<image imageStyle="otherStyle"></image>]' );
  59. command.execute();
  60. expect( getData( model ) ).to.equal( '[<image imageStyle="otherStyle"></image>]' );
  61. } );
  62. it( 'should use parent batch', () => {
  63. setData( model, '[<image></image>]' );
  64. model.change( writer => {
  65. expect( writer.batch.deltas ).to.length( 0 );
  66. command.execute( { value: 'otherStyle' } );
  67. expect( writer.batch.deltas ).to.length.above( 0 );
  68. } );
  69. } );
  70. it( 'should be enabled on image element', () => {
  71. setData( model, '[<image></image>]' );
  72. expect( command.isEnabled ).to.be.true;
  73. } );
  74. it( 'should be disabled when not placed on image', () => {
  75. setData( model, '[<p></p>]' );
  76. expect( command.isEnabled ).to.be.false;
  77. } );
  78. it( 'should be disabled when not placed directly on image', () => {
  79. setData( model, '[<p></p><image></image>]' );
  80. expect( command.isEnabled ).to.be.false;
  81. } );
  82. it( 'default style should be active after executing it after another style', () => {
  83. setData( model, '[<image></image>]' );
  84. expect( command.value ).to.equal( 'defaultStyle' );
  85. command.execute( { value: 'otherStyle' } );
  86. expect( getData( model ) ).to.equal( '[<image imageStyle="otherStyle"></image>]' );
  87. command.execute( { value: 'defaultStyle' } );
  88. expect( getData( model ) ).to.equal( '[<image></image>]' );
  89. expect( command.value ).to.equal( 'defaultStyle' );
  90. } );
  91. } );