imagestylecommand.js 3.2 KB

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