imagestyle.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /**
  2. * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. import ClassicTestEditor from 'ckeditor5-core/tests/_utils/classictesteditor';
  6. import ImageToolbar from 'ckeditor5-image/src/imagetoolbar';
  7. import ImageStyle from 'ckeditor5-image/src/imagestyle/imagestyle';
  8. import ImageStyleEngine from 'ckeditor5-image/src/imagestyle/imagestyleengine';
  9. import ButtonView from 'ckeditor5-ui/src/button/buttonview';
  10. import global from 'ckeditor5-utils/src/dom/global';
  11. describe( 'ImageStyle', () => {
  12. let editor;
  13. const styles = [
  14. { name: 'style 1', title: 'Style 1 title', icon: 'style1-icon', value: null },
  15. { name: 'style 2', title: 'Style 2 title', icon: 'style2-icon', value: 'style2', cssClass: 'style2-class' },
  16. { name: 'style 3', title: 'Style 3 title', icon: 'style3-icon', value: 'style3', cssClass: 'style3-class' }
  17. ];
  18. beforeEach( () => {
  19. const editorElement = global.document.createElement( 'div' );
  20. global.document.body.appendChild( editorElement );
  21. return ClassicTestEditor.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 ( let 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 default image toolbar if image toolbar is not present', () => {
  58. expect( editor.config.get( 'image.defaultToolbar' ) ).to.be.undefined;
  59. } );
  60. it( 'should add buttons to default image toolbar if toolbar is present', () => {
  61. const editorElement = global.document.createElement( 'div' );
  62. global.document.body.appendChild( editorElement );
  63. return ClassicTestEditor.create( editorElement, {
  64. plugins: [ ImageStyle, ImageToolbar ]
  65. } )
  66. .then( newEditor => {
  67. expect( newEditor.config.get( 'image.defaultToolbar' ) ).to.eql( [ 'imageStyleFull', 'imageStyleSide' ] );
  68. newEditor.destroy();
  69. } );
  70. } );
  71. it( 'should not add buttons to image toolbar if configuration is present', () => {
  72. const editorElement = global.document.createElement( 'div' );
  73. global.document.body.appendChild( editorElement );
  74. return ClassicTestEditor.create( editorElement, {
  75. plugins: [ ImageStyle ],
  76. image: {
  77. styles,
  78. toolbar: [ 'foo', 'bar' ]
  79. }
  80. } )
  81. .then( newEditor => {
  82. expect( newEditor.config.get( 'image.toolbar' ) ).to.eql( [ 'foo', 'bar' ] );
  83. newEditor.destroy();
  84. } );
  85. } );
  86. } );