imagestyle.js 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. /**
  2. * @license Copyright (c) 2003-2018, 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', isDefault: true },
  14. { name: 'style 2', title: 'Style 2 title', icon: 'style2-icon', cssClass: 'style2-class' },
  15. { name: 'style 3', title: 'Style 3 title', icon: 'style3-icon', cssClass: 'style3-class' }
  16. ];
  17. beforeEach( () => {
  18. const editorElement = global.document.createElement( 'div' );
  19. global.document.body.appendChild( editorElement );
  20. return ClassicTestEditor
  21. .create( editorElement, {
  22. plugins: [ ImageStyle ],
  23. image: {
  24. styles
  25. }
  26. } )
  27. .then( newEditor => {
  28. editor = newEditor;
  29. } );
  30. } );
  31. afterEach( () => {
  32. return editor.destroy();
  33. } );
  34. it( 'should be loaded', () => {
  35. expect( editor.plugins.get( ImageStyle ) ).to.be.instanceOf( ImageStyle );
  36. } );
  37. it( 'should load ImageStyleEngine plugin', () => {
  38. expect( editor.plugins.get( ImageStyleEngine ) ).to.be.instanceOf( ImageStyleEngine );
  39. } );
  40. it( 'should register buttons for each style', () => {
  41. const spy = sinon.spy( editor, 'execute' );
  42. for ( const style of styles ) {
  43. const command = editor.commands.get( style.name );
  44. const buttonView = editor.ui.componentFactory.create( style.name );
  45. expect( buttonView ).to.be.instanceOf( ButtonView );
  46. expect( buttonView.label ).to.equal( style.title );
  47. expect( buttonView.icon ).to.equal( style.icon );
  48. command.isEnabled = true;
  49. expect( buttonView.isEnabled ).to.be.true;
  50. command.isEnabled = false;
  51. expect( buttonView.isEnabled ).to.be.false;
  52. buttonView.fire( 'execute' );
  53. sinon.assert.calledWithExactly( editor.execute, style.name );
  54. spy.reset();
  55. }
  56. } );
  57. it( 'should not add buttons to image toolbar if configuration is present', () => {
  58. const editorElement = global.document.createElement( 'div' );
  59. global.document.body.appendChild( editorElement );
  60. return ClassicTestEditor
  61. .create( editorElement, {
  62. plugins: [ ImageStyle ],
  63. image: {
  64. styles,
  65. toolbar: [ 'foo', 'bar' ]
  66. }
  67. } )
  68. .then( newEditor => {
  69. expect( newEditor.config.get( 'image.toolbar' ) ).to.eql( [ 'foo', 'bar' ] );
  70. newEditor.destroy();
  71. } );
  72. } );
  73. } );