headingbuttonsui.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /**
  2. * @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* globals document */
  6. import ClassicTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/classictesteditor';
  7. import HeadingEditing from '../src/headingediting';
  8. import HeadingButtonsUI from '../src/headingbuttonsui';
  9. import { setData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  10. import ButtonView from '@ckeditor/ckeditor5-ui/src/button/buttonview';
  11. import { getLocalizedOptions } from '../src/utils';
  12. import iconHeading2 from '../theme/icons/heading2.svg';
  13. describe( 'HeadingButtonUI', () => {
  14. let editorElement, editor;
  15. describe( 'default config', () => {
  16. beforeEach( () => {
  17. editorElement = document.createElement( 'div' );
  18. document.body.appendChild( editorElement );
  19. return ClassicTestEditor
  20. .create( editorElement, {
  21. plugins: [ HeadingButtonsUI, HeadingEditing ],
  22. toolbar: [ 'heading1', 'heading2', 'heading3' ]
  23. } )
  24. .then( newEditor => {
  25. editor = newEditor;
  26. // Set data so the commands will be enabled.
  27. setData( editor.model, '<heading1>f{}oo</heading1>' );
  28. } );
  29. } );
  30. afterEach( () => {
  31. editorElement.remove();
  32. return editor.destroy();
  33. } );
  34. it( 'should define default buttons', () => {
  35. const factory = editor.ui.componentFactory;
  36. expect( factory.create( 'heading1' ) ).to.be.instanceOf( ButtonView );
  37. expect( factory.create( 'heading2' ) ).to.be.instanceOf( ButtonView );
  38. expect( factory.create( 'heading3' ) ).to.be.instanceOf( ButtonView );
  39. } );
  40. it( 'should intialize buttons with correct localized data', () => {
  41. const localizedOptions = getLocalizedOptions( editor ).filter( option => option.model == 'heading2' )[ 0 ];
  42. const heading2Button = editor.ui.componentFactory.create( 'heading2' );
  43. expect( heading2Button.label ).to.equal( localizedOptions.title );
  44. expect( heading2Button.icon ).to.equal( iconHeading2 );
  45. expect( heading2Button.tooltip ).to.equal( true );
  46. } );
  47. it( 'should bind buttons to correct commands', () => {
  48. const headingButton = editor.ui.componentFactory.create( 'heading1' );
  49. const headingCommand = editor.commands.get( 'heading' );
  50. expect( headingCommand.isEnabled ).to.be.true;
  51. expect( headingButton.isEnabled ).to.be.true;
  52. headingCommand.isEnabled = false;
  53. expect( headingButton.isEnabled ).to.be.false;
  54. expect( headingCommand.value ).to.equal( 'heading1' );
  55. expect( headingButton.isOn ).to.be.true;
  56. setData( editor.model, '<heading2>f{}oo</heading2>' );
  57. expect( headingCommand.value ).to.equal( 'heading2' );
  58. expect( headingButton.isOn ).to.be.false;
  59. } );
  60. it( 'should bind button execute to command execute', () => {
  61. const headingButton = editor.ui.componentFactory.create( 'heading1' );
  62. const executeCommandSpy = sinon.spy( editor, 'execute' );
  63. headingButton.fire( 'execute' );
  64. sinon.assert.calledOnce( executeCommandSpy );
  65. sinon.assert.calledWithExactly( executeCommandSpy, 'heading', { value: 'heading1' } );
  66. } );
  67. } );
  68. describe( 'custom config', () => {
  69. const customIcon = '<svg></svg>';
  70. beforeEach( () => {
  71. editorElement = document.createElement( 'div' );
  72. document.body.appendChild( editorElement );
  73. return ClassicTestEditor
  74. .create( editorElement, {
  75. heading: {
  76. options: [
  77. { model: 'paragraph' },
  78. { model: 'heading1', view: 'h2', icon: customIcon },
  79. ]
  80. },
  81. plugins: [ HeadingButtonsUI, HeadingEditing ],
  82. toolbar: [ 'heading1', 'heading2', 'heading3' ]
  83. } )
  84. .then( newEditor => {
  85. editor = newEditor;
  86. // Set data so the commands will be enabled.
  87. setData( editor.model, '<heading1>f{}oo</heading1>' );
  88. } );
  89. } );
  90. afterEach( () => {
  91. editorElement.remove();
  92. return editor.destroy();
  93. } );
  94. it( 'should allow to pass custom image to the configuration', () => {
  95. const headingButton = editor.ui.componentFactory.create( 'heading1' );
  96. expect( headingButton.icon ).to.equal( customIcon );
  97. } );
  98. } );
  99. } );