8
0

imagestyle.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /**
  2. * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. import Plugin from '@ckeditor/ckeditor5-core/src/plugin';
  6. import ClassicTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/classictesteditor';
  7. import ImageToolbar from '../src/imagetoolbar';
  8. import ImageStyle from '../src/imagestyle';
  9. import ImageStyleEngine from '../src/imagestyle/imagestyleengine';
  10. import ButtonView from '@ckeditor/ckeditor5-ui/src/button/buttonview';
  11. import global from '@ckeditor/ckeditor5-utils/src/dom/global';
  12. describe( 'ImageStyle', () => {
  13. let editor;
  14. const styles = [
  15. { name: 'style 1', title: 'Style 1 title', icon: 'style1-icon', value: null },
  16. { name: 'style 2', title: 'Style 2 title', icon: 'style2-icon', value: 'style2', cssClass: 'style2-class' },
  17. { name: 'style 3', title: 'Style 3 title', icon: 'style3-icon', value: 'style3', cssClass: 'style3-class' }
  18. ];
  19. beforeEach( () => {
  20. const editorElement = global.document.createElement( 'div' );
  21. global.document.body.appendChild( editorElement );
  22. return ClassicTestEditor.create( editorElement, {
  23. plugins: [ ImageStyle ],
  24. image: {
  25. styles
  26. }
  27. } )
  28. .then( newEditor => {
  29. editor = newEditor;
  30. } );
  31. } );
  32. afterEach( () => {
  33. return editor.destroy();
  34. } );
  35. it( 'should be loaded', () => {
  36. expect( editor.plugins.get( ImageStyle ) ).to.be.instanceOf( ImageStyle );
  37. } );
  38. it( 'should load ImageStyleEngine plugin', () => {
  39. expect( editor.plugins.get( ImageStyleEngine ) ).to.be.instanceOf( ImageStyleEngine );
  40. } );
  41. it( 'should register buttons for each style', () => {
  42. const spy = sinon.spy( editor, 'execute' );
  43. for ( let style of styles ) {
  44. const command = editor.commands.get( style.name );
  45. const buttonView = editor.ui.componentFactory.create( style.name );
  46. expect( buttonView ).to.be.instanceOf( ButtonView );
  47. expect( buttonView.label ).to.equal( style.title );
  48. expect( buttonView.icon ).to.equal( style.icon );
  49. command.isEnabled = true;
  50. expect( buttonView.isEnabled ).to.be.true;
  51. command.isEnabled = false;
  52. expect( buttonView.isEnabled ).to.be.false;
  53. buttonView.fire( 'execute' );
  54. sinon.assert.calledWithExactly( editor.execute, style.name );
  55. spy.reset();
  56. }
  57. } );
  58. it( 'should not add buttons to default image toolbar if image toolbar is not present', () => {
  59. expect( editor.config.get( 'image.defaultToolbar' ) ).to.be.undefined;
  60. } );
  61. it( 'should add buttons to default image toolbar if toolbar is present', () => {
  62. const editorElement = global.document.createElement( 'div' );
  63. global.document.body.appendChild( editorElement );
  64. return ClassicTestEditor.create( editorElement, {
  65. plugins: [ ImageStyle, ImageToolbar ]
  66. } )
  67. .then( newEditor => {
  68. expect( newEditor.config.get( 'image.defaultToolbar' ) ).to.eql( [ 'imageStyleFull', 'imageStyleSide' ] );
  69. newEditor.destroy();
  70. } );
  71. } );
  72. it( 'should add separator before the button if toolbar is present and already has items', () => {
  73. const editorElement = global.document.createElement( 'div' );
  74. global.document.body.appendChild( editorElement );
  75. const FooPlugin = class extends Plugin {
  76. init() {
  77. this.editor.ui.componentFactory.add( 'foo', () => new ButtonView() );
  78. this.editor.config.get( 'image.defaultToolbar' ).push( 'foo' );
  79. }
  80. };
  81. return ClassicTestEditor.create( editorElement, {
  82. plugins: [ FooPlugin, ImageStyle, ImageToolbar ]
  83. } )
  84. .then( newEditor => {
  85. expect( newEditor.config.get( 'image.defaultToolbar' ) ).to.eql( [ 'foo', '|', 'imageStyleFull', 'imageStyleSide' ] );
  86. newEditor.destroy();
  87. } );
  88. } );
  89. it( 'should not add buttons to image toolbar if configuration is present', () => {
  90. const editorElement = global.document.createElement( 'div' );
  91. global.document.body.appendChild( editorElement );
  92. return ClassicTestEditor.create( editorElement, {
  93. plugins: [ ImageStyle ],
  94. image: {
  95. styles,
  96. toolbar: [ 'foo', 'bar' ]
  97. }
  98. } )
  99. .then( newEditor => {
  100. expect( newEditor.config.get( 'image.toolbar' ) ).to.eql( [ 'foo', 'bar' ] );
  101. newEditor.destroy();
  102. } );
  103. } );
  104. } );