imagestylecommand.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /**
  2. * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. import ModelTestEditor from 'ckeditor5-core/tests/_utils/modeltesteditor';
  6. import ImageStyleCommand from 'ckeditor5-image/src/imagestyle/imagestylecommand';
  7. import { setData, getData } from 'ckeditor5-engine/src/dev-utils/model';
  8. describe( 'ImageStyleCommand', () => {
  9. const defaultStyle = { name: 'defaultStyle', title: 'foo bar', icon: 'icon-1', value: null };
  10. const otherStyle = { name: 'otherStyle', title: 'baz', icon: 'icon-2', value: 'other', className: 'other-class-name' };
  11. let document, defaultStyleCommand, otherStyleCommand;
  12. beforeEach( () => {
  13. return ModelTestEditor.create()
  14. .then( newEditor => {
  15. document = newEditor.document;
  16. defaultStyleCommand = new ImageStyleCommand( newEditor, defaultStyle );
  17. otherStyleCommand = new ImageStyleCommand( newEditor, otherStyle );
  18. document.schema.registerItem( 'p', '$block' );
  19. document.schema.registerItem( 'image' );
  20. document.schema.objects.add( 'image' );
  21. document.schema.allow( { name: 'image', inside: '$root' } );
  22. document.schema.allow( { name: 'image', inside: '$root', attributes: [ 'imageStyle' ] } );
  23. } );
  24. } );
  25. it( 'command value should be false if no image is selected', () => {
  26. setData( document, '[]<image></image>' );
  27. expect( defaultStyleCommand.value ).to.be.false;
  28. expect( otherStyleCommand.value ).to.be.false;
  29. } );
  30. it( 'should match default style if no imageStyle attribute is present', () => {
  31. setData( document, '[<image></image>]' );
  32. expect( defaultStyleCommand.value ).to.be.true;
  33. expect( otherStyleCommand.value ).to.be.false;
  34. } );
  35. it( 'proper command should have true value when imageStyle attribute is present', () => {
  36. setData( document, '[<image imageStyle="other"></image>]' );
  37. expect( defaultStyleCommand.value ).to.be.false;
  38. expect( otherStyleCommand.value ).to.be.true;
  39. } );
  40. it( 'should have false value if style does not match', () => {
  41. setData( document, '[<image imageStyle="foo"></image>]' );
  42. expect( defaultStyleCommand.value ).to.be.false;
  43. expect( otherStyleCommand.value ).to.be.false;
  44. } );
  45. it( 'should set proper value when executed', () => {
  46. setData( document, '[<image></image>]' );
  47. otherStyleCommand._doExecute();
  48. expect( getData( document ) ).to.equal( '[<image imageStyle="other"></image>]' );
  49. } );
  50. it( 'should do nothing when attribute already present', () => {
  51. setData( document, '[<image imageStyle="other"></image>]' );
  52. otherStyleCommand._doExecute();
  53. expect( getData( document ) ).to.equal( '[<image imageStyle="other"></image>]' );
  54. } );
  55. it( 'should allow to provide batch instance', () => {
  56. const batch = document.batch();
  57. const spy = sinon.spy( batch, 'setAttribute' );
  58. setData( document, '[<image></image>]' );
  59. otherStyleCommand._doExecute( { batch } );
  60. expect( getData( document ) ).to.equal( '[<image imageStyle="other"></image>]' );
  61. sinon.assert.calledOnce( spy );
  62. } );
  63. it( 'should be enabled on image element', () => {
  64. setData( document, '[<image></image>]' );
  65. expect( defaultStyleCommand.isEnabled ).to.be.true;
  66. expect( otherStyleCommand.isEnabled ).to.be.true;
  67. } );
  68. it( 'should be disabled when not placed on image', () => {
  69. setData( document, '[<p></p>]' );
  70. expect( defaultStyleCommand.isEnabled ).to.be.false;
  71. expect( otherStyleCommand.isEnabled ).to.be.false;
  72. } );
  73. it( 'should be disabled when not placed directly on image', () => {
  74. setData( document, '[<p></p><image></image>]' );
  75. expect( defaultStyleCommand.isEnabled ).to.be.false;
  76. expect( otherStyleCommand.isEnabled ).to.be.false;
  77. } );
  78. } );