imagestyle.js 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /**
  2. * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* global document */
  6. import ClassicTestEditor from 'tests/core/_utils/classictesteditor.js';
  7. import ImageStyle from 'ckeditor5/image/imagestyle/imagestyle.js';
  8. import ImageStyleEngine from 'ckeditor5/image/imagestyle/imagestyleengine.js';
  9. import ButtonView from 'ckeditor5/ui/button/buttonview.js';
  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 = document.createElement( 'div' );
  19. 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 command = editor.commands.get( 'imagestyle' );
  41. const spy = sinon.spy( editor, 'execute' );
  42. for ( let style of styles ) {
  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, 'imagestyle', { value: style.value } );
  53. spy.reset();
  54. }
  55. } );
  56. it( 'should add buttons to image toolbar if there is no default configuration', () => {
  57. const toolbarConfig = editor.config.get( 'image.toolbar' );
  58. expect( toolbarConfig ).to.eql( styles.map( style => style.name ) );
  59. } );
  60. it( 'should not add buttons to image toolbar if configuration is present', () => {
  61. const editorElement = document.createElement( 'div' );
  62. document.body.appendChild( editorElement );
  63. return ClassicTestEditor.create( editorElement, {
  64. plugins: [ ImageStyle ],
  65. image: {
  66. styles,
  67. toolbar: [ 'foo', 'bar' ]
  68. }
  69. } )
  70. .then( newEditor => {
  71. expect( newEditor.config.get( 'image.toolbar' ) ).to.eql( [ 'foo', 'bar' ] );
  72. } );
  73. } );
  74. } );