headingbuttonsui.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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', 'heading4', 'heading5', 'heading6' ],
  23. heading: {
  24. options: [
  25. { model: 'paragraph', title: 'Paragraph', class: 'ck-heading_paragraph' },
  26. { model: 'heading1', view: 'h2', title: 'Heading 1', class: 'ck-heading_heading1' },
  27. { model: 'heading2', view: 'h3', title: 'Heading 2', class: 'ck-heading_heading2' },
  28. { model: 'heading3', view: 'h4', title: 'Heading 3', class: 'ck-heading_heading3' },
  29. { model: 'heading4', view: 'h5', title: 'Heading 4', class: 'ck-heading_heading4' },
  30. { model: 'heading5', view: 'h6', title: 'Heading 5', class: 'ck-heading_heading5' },
  31. { model: 'heading6', view: 'p', title: 'Heading 6', class: 'ck-heading_heading6' }
  32. ]
  33. }
  34. } )
  35. .then( newEditor => {
  36. editor = newEditor;
  37. // Set data so the commands will be enabled.
  38. setData( editor.model, '<heading1>f{}oo</heading1>' );
  39. } );
  40. } );
  41. afterEach( () => {
  42. editorElement.remove();
  43. return editor.destroy();
  44. } );
  45. it( 'should define default buttons', () => {
  46. const factory = editor.ui.componentFactory;
  47. expect( factory.create( 'heading1' ) ).to.be.instanceOf( ButtonView );
  48. expect( factory.create( 'heading2' ) ).to.be.instanceOf( ButtonView );
  49. expect( factory.create( 'heading3' ) ).to.be.instanceOf( ButtonView );
  50. expect( factory.create( 'heading4' ) ).to.be.instanceOf( ButtonView );
  51. expect( factory.create( 'heading5' ) ).to.be.instanceOf( ButtonView );
  52. expect( factory.create( 'heading6' ) ).to.be.instanceOf( ButtonView );
  53. } );
  54. it( 'should initialize buttons with correct localized data', () => {
  55. const localizedOptions = getLocalizedOptions( editor ).filter( option => option.model == 'heading2' )[ 0 ];
  56. const heading2Button = editor.ui.componentFactory.create( 'heading2' );
  57. expect( heading2Button.label ).to.equal( localizedOptions.title );
  58. expect( heading2Button.icon ).to.equal( iconHeading2 );
  59. expect( heading2Button.tooltip ).to.equal( true );
  60. } );
  61. it( 'should bind buttons to correct commands', () => {
  62. const headingButton = editor.ui.componentFactory.create( 'heading1' );
  63. const headingCommand = editor.commands.get( 'heading' );
  64. expect( headingCommand.isEnabled ).to.be.true;
  65. expect( headingButton.isEnabled ).to.be.true;
  66. headingCommand.isEnabled = false;
  67. expect( headingButton.isEnabled ).to.be.false;
  68. expect( headingCommand.value ).to.equal( 'heading1' );
  69. expect( headingButton.isOn ).to.be.true;
  70. setData( editor.model, '<heading2>f{}oo</heading2>' );
  71. expect( headingCommand.value ).to.equal( 'heading2' );
  72. expect( headingButton.isOn ).to.be.false;
  73. } );
  74. it( 'should bind button execute to command execute', () => {
  75. const headingButton = editor.ui.componentFactory.create( 'heading1' );
  76. const executeCommandSpy = sinon.spy( editor, 'execute' );
  77. headingButton.fire( 'execute' );
  78. sinon.assert.calledOnce( executeCommandSpy );
  79. sinon.assert.calledWithExactly( executeCommandSpy, 'heading', { value: 'heading1' } );
  80. } );
  81. } );
  82. describe( 'custom config', () => {
  83. const customIcon = '<svg></svg>';
  84. beforeEach( () => {
  85. editorElement = document.createElement( 'div' );
  86. document.body.appendChild( editorElement );
  87. return ClassicTestEditor
  88. .create( editorElement, {
  89. heading: {
  90. options: [
  91. { model: 'paragraph' },
  92. { model: 'heading1', view: 'h2', icon: customIcon },
  93. ]
  94. },
  95. plugins: [ HeadingButtonsUI, HeadingEditing ],
  96. toolbar: [ 'heading1', 'heading2', 'heading3' ]
  97. } )
  98. .then( newEditor => {
  99. editor = newEditor;
  100. // Set data so the commands will be enabled.
  101. setData( editor.model, '<heading1>f{}oo</heading1>' );
  102. } );
  103. } );
  104. afterEach( () => {
  105. editorElement.remove();
  106. return editor.destroy();
  107. } );
  108. it( 'should allow to pass custom image to the configuration', () => {
  109. const headingButton = editor.ui.componentFactory.create( 'heading1' );
  110. expect( headingButton.icon ).to.equal( customIcon );
  111. } );
  112. } );
  113. } );