8
0

imagestylecommand.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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, defaultStyleCommand, otherStyleCommand;
  12. beforeEach( () => {
  13. return ModelTestEditor.create()
  14. .then( newEditor => {
  15. model = newEditor.model;
  16. defaultStyleCommand = new ImageStyleCommand( newEditor, defaultStyle );
  17. otherStyleCommand = new ImageStyleCommand( newEditor, otherStyle );
  18. model.schema.register( 'p', { inheritAllFrom: '$block' } );
  19. model.schema.register( 'image', {
  20. isObject: true,
  21. isBlock: true,
  22. allowWhere: '$block',
  23. allowAttributes: 'imageStyle'
  24. } );
  25. } );
  26. } );
  27. it( 'command value should be false if no image is selected', () => {
  28. setData( model, '[]<image></image>' );
  29. expect( defaultStyleCommand.value ).to.be.false;
  30. expect( otherStyleCommand.value ).to.be.false;
  31. } );
  32. it( 'should match default style if no imageStyle attribute is present', () => {
  33. setData( model, '[<image></image>]' );
  34. expect( defaultStyleCommand.value ).to.be.true;
  35. expect( otherStyleCommand.value ).to.be.false;
  36. } );
  37. it( 'proper command should have true value when imageStyle attribute is present', () => {
  38. setData( model, '[<image imageStyle="otherStyle"></image>]' );
  39. expect( defaultStyleCommand.value ).to.be.false;
  40. expect( otherStyleCommand.value ).to.be.true;
  41. } );
  42. it( 'should have false value if style does not match', () => {
  43. setData( model, '[<image imageStyle="foo"></image>]' );
  44. expect( defaultStyleCommand.value ).to.be.false;
  45. expect( otherStyleCommand.value ).to.be.false;
  46. } );
  47. it( 'should set proper value when executed', () => {
  48. setData( model, '[<image></image>]' );
  49. otherStyleCommand.execute();
  50. expect( getData( model ) ).to.equal( '[<image imageStyle="otherStyle"></image>]' );
  51. } );
  52. it( 'should do nothing when attribute already present', () => {
  53. setData( model, '[<image imageStyle="otherStyle"></image>]' );
  54. otherStyleCommand.execute();
  55. expect( getData( model ) ).to.equal( '[<image imageStyle="otherStyle"></image>]' );
  56. } );
  57. it( 'should use parent batch', () => {
  58. setData( model, '[<image></image>]' );
  59. model.change( writer => {
  60. expect( writer.batch.deltas ).to.length( 0 );
  61. otherStyleCommand.execute();
  62. expect( writer.batch.deltas ).to.length.above( 0 );
  63. } );
  64. } );
  65. it( 'should be enabled on image element', () => {
  66. setData( model, '[<image></image>]' );
  67. expect( defaultStyleCommand.isEnabled ).to.be.true;
  68. expect( otherStyleCommand.isEnabled ).to.be.true;
  69. } );
  70. it( 'should be disabled when not placed on image', () => {
  71. setData( model, '[<p></p>]' );
  72. expect( defaultStyleCommand.isEnabled ).to.be.false;
  73. expect( otherStyleCommand.isEnabled ).to.be.false;
  74. } );
  75. it( 'should be disabled when not placed directly on image', () => {
  76. setData( model, '[<p></p><image></image>]' );
  77. expect( defaultStyleCommand.isEnabled ).to.be.false;
  78. expect( otherStyleCommand.isEnabled ).to.be.false;
  79. } );
  80. it( 'default style should be active after executing it after another style', () => {
  81. setData( model, '[<image></image>]' );
  82. expect( defaultStyleCommand.value ).to.be.true;
  83. expect( otherStyleCommand.value ).to.be.false;
  84. otherStyleCommand.execute();
  85. expect( getData( model ) ).to.equal( '[<image imageStyle="otherStyle"></image>]' );
  86. defaultStyleCommand.execute();
  87. expect( getData( model ) ).to.equal( '[<image></image>]' );
  88. expect( defaultStyleCommand.value ).to.be.true;
  89. expect( otherStyleCommand.value ).to.be.false;
  90. } );
  91. } );