heading.js 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /**
  2. * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* globals document */
  6. import ClassicTestEditor from 'ckeditor5-core/tests/_utils/classictesteditor';
  7. import Heading from 'ckeditor5-heading/src/heading';
  8. import HeadingEngine from 'ckeditor5-heading/src/headingengine';
  9. import DropdownView from 'ckeditor5-ui/src/dropdown/dropdownview';
  10. import testUtils from 'ckeditor5-core/tests/_utils/utils';
  11. testUtils.createSinonSandbox();
  12. describe( 'Heading', () => {
  13. let editor, dropdown;
  14. beforeEach( () => {
  15. const editorElement = document.createElement( 'div' );
  16. document.body.appendChild( editorElement );
  17. return ClassicTestEditor.create( editorElement, {
  18. plugins: [ Heading ],
  19. toolbar: [ 'heading' ]
  20. } )
  21. .then( newEditor => {
  22. editor = newEditor;
  23. dropdown = editor.ui.componentFactory.create( 'headings' );
  24. } );
  25. } );
  26. afterEach( () => {
  27. return editor.destroy();
  28. } );
  29. it( 'should be loaded', () => {
  30. expect( editor.plugins.get( Heading ) ).to.be.instanceOf( Heading );
  31. } );
  32. it( 'should load HeadingEngine', () => {
  33. expect( editor.plugins.get( HeadingEngine ) ).to.be.instanceOf( HeadingEngine );
  34. } );
  35. describe( 'init()', () => {
  36. it( 'should register formats feature component', () => {
  37. const dropdown = editor.ui.componentFactory.create( 'headings' );
  38. expect( dropdown ).to.be.instanceOf( DropdownView );
  39. expect( dropdown.buttonView.isEnabled ).to.be.true;
  40. expect( dropdown.buttonView.isOn ).to.be.undefined;
  41. expect( dropdown.buttonView.label ).to.equal( 'Paragraph' );
  42. } );
  43. it( 'should execute format command on model execute event', () => {
  44. const executeSpy = testUtils.sinon.spy( editor, 'execute' );
  45. const dropdown = editor.ui.componentFactory.create( 'headings' );
  46. dropdown.formatId = 'foo';
  47. dropdown.fire( 'execute' );
  48. sinon.assert.calledOnce( executeSpy );
  49. sinon.assert.calledWithExactly( executeSpy, 'heading', { formatId: 'foo' } );
  50. } );
  51. it( 'should focus view after command execution', () => {
  52. const focusSpy = testUtils.sinon.spy( editor.editing.view, 'focus' );
  53. const dropdown = editor.ui.componentFactory.create( 'headings' );
  54. dropdown.fire( 'execute' );
  55. sinon.assert.calledOnce( focusSpy );
  56. } );
  57. describe( 'model to command binding', () => {
  58. let command;
  59. beforeEach( () => {
  60. command = editor.commands.get( 'heading' );
  61. } );
  62. it( 'isEnabled', () => {
  63. expect( dropdown.buttonView.isEnabled ).to.be.true;
  64. command.isEnabled = false;
  65. expect( dropdown.buttonView.isEnabled ).to.be.false;
  66. } );
  67. it( 'label', () => {
  68. expect( dropdown.buttonView.label ).to.equal( 'Paragraph' );
  69. command.value = command.formats[ 1 ];
  70. expect( dropdown.buttonView.label ).to.equal( 'Heading 1' );
  71. } );
  72. } );
  73. } );
  74. } );