imagestyle.js 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. /**
  2. * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. import ClassicTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/classictesteditor';
  6. import ImageStyle from '../src/imagestyle';
  7. import ImageStyleEngine from '../src/imagestyle/imagestyleengine';
  8. import ButtonView from '@ckeditor/ckeditor5-ui/src/button/buttonview';
  9. import global from '@ckeditor/ckeditor5-utils/src/dom/global';
  10. describe( 'ImageStyle', () => {
  11. let editor;
  12. const styles = [
  13. { name: 'style 1', title: 'Style 1 title', icon: 'style1-icon', value: null },
  14. { name: 'style 2', title: 'Style 2 title', icon: 'style2-icon', value: 'style2', cssClass: 'style2-class' },
  15. { name: 'style 3', title: 'Style 3 title', icon: 'style3-icon', value: 'style3', cssClass: 'style3-class' }
  16. ];
  17. beforeEach( () => {
  18. const editorElement = global.document.createElement( 'div' );
  19. global.document.body.appendChild( editorElement );
  20. return ClassicTestEditor.create( editorElement, {
  21. plugins: [ ImageStyle ],
  22. image: {
  23. styles
  24. }
  25. } )
  26. .then( newEditor => {
  27. editor = newEditor;
  28. } );
  29. } );
  30. afterEach( () => {
  31. return editor.destroy();
  32. } );
  33. it( 'should be loaded', () => {
  34. expect( editor.plugins.get( ImageStyle ) ).to.be.instanceOf( ImageStyle );
  35. } );
  36. it( 'should load ImageStyleEngine plugin', () => {
  37. expect( editor.plugins.get( ImageStyleEngine ) ).to.be.instanceOf( ImageStyleEngine );
  38. } );
  39. it( 'should register buttons for each style', () => {
  40. const spy = sinon.spy( editor, 'execute' );
  41. for ( const style of styles ) {
  42. const command = editor.commands.get( style.name );
  43. const buttonView = editor.ui.componentFactory.create( style.name );
  44. expect( buttonView ).to.be.instanceOf( ButtonView );
  45. expect( buttonView.label ).to.equal( style.title );
  46. expect( buttonView.icon ).to.equal( style.icon );
  47. command.isEnabled = true;
  48. expect( buttonView.isEnabled ).to.be.true;
  49. command.isEnabled = false;
  50. expect( buttonView.isEnabled ).to.be.false;
  51. buttonView.fire( 'execute' );
  52. sinon.assert.calledWithExactly( editor.execute, style.name );
  53. spy.reset();
  54. }
  55. } );
  56. it( 'should not add buttons to image toolbar if configuration is present', () => {
  57. const editorElement = global.document.createElement( 'div' );
  58. global.document.body.appendChild( editorElement );
  59. return ClassicTestEditor.create( editorElement, {
  60. plugins: [ ImageStyle ],
  61. image: {
  62. styles,
  63. toolbar: [ 'foo', 'bar' ]
  64. }
  65. } )
  66. .then( newEditor => {
  67. expect( newEditor.config.get( 'image.toolbar' ) ).to.eql( [ 'foo', 'bar' ] );
  68. newEditor.destroy();
  69. } );
  70. } );
  71. } );