heading.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /**
  2. * @license Copyright (c) 2003-2017, 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 Heading from '../src/heading';
  8. import HeadingEngine from '../src/headingengine';
  9. import DropdownView from '@ckeditor/ckeditor5-ui/src/dropdown/dropdownview';
  10. import Locale from '@ckeditor/ckeditor5-utils/src/locale';
  11. import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
  12. testUtils.createSinonSandbox();
  13. describe( 'Heading', () => {
  14. let editor, dropdown;
  15. beforeEach( () => {
  16. const editorElement = document.createElement( 'div' );
  17. document.body.appendChild( editorElement );
  18. return ClassicTestEditor.create( editorElement, {
  19. plugins: [ Heading ],
  20. toolbar: [ 'heading' ]
  21. } )
  22. .then( newEditor => {
  23. editor = newEditor;
  24. dropdown = editor.ui.componentFactory.create( 'headings' );
  25. } );
  26. } );
  27. afterEach( () => {
  28. return editor.destroy();
  29. } );
  30. it( 'should be loaded', () => {
  31. expect( editor.plugins.get( Heading ) ).to.be.instanceOf( Heading );
  32. } );
  33. it( 'should load HeadingEngine', () => {
  34. expect( editor.plugins.get( HeadingEngine ) ).to.be.instanceOf( HeadingEngine );
  35. } );
  36. describe( 'init()', () => {
  37. it( 'should register formats feature component', () => {
  38. const dropdown = editor.ui.componentFactory.create( 'headings' );
  39. expect( dropdown ).to.be.instanceOf( DropdownView );
  40. expect( dropdown.buttonView.isEnabled ).to.be.true;
  41. expect( dropdown.buttonView.isOn ).to.be.undefined;
  42. expect( dropdown.buttonView.label ).to.equal( 'Paragraph' );
  43. } );
  44. it( 'should execute format command on model execute event', () => {
  45. const executeSpy = testUtils.sinon.spy( editor, 'execute' );
  46. const dropdown = editor.ui.componentFactory.create( 'headings' );
  47. dropdown.formatId = 'foo';
  48. dropdown.fire( 'execute' );
  49. sinon.assert.calledOnce( executeSpy );
  50. sinon.assert.calledWithExactly( executeSpy, 'heading', { formatId: 'foo' } );
  51. } );
  52. it( 'should focus view after command execution', () => {
  53. const focusSpy = testUtils.sinon.spy( editor.editing.view, 'focus' );
  54. const dropdown = editor.ui.componentFactory.create( 'headings' );
  55. dropdown.fire( 'execute' );
  56. sinon.assert.calledOnce( focusSpy );
  57. } );
  58. describe( 'model to command binding', () => {
  59. let command;
  60. beforeEach( () => {
  61. command = editor.commands.get( 'heading' );
  62. } );
  63. it( 'isEnabled', () => {
  64. expect( dropdown.buttonView.isEnabled ).to.be.true;
  65. command.isEnabled = false;
  66. expect( dropdown.buttonView.isEnabled ).to.be.false;
  67. } );
  68. it( 'label', () => {
  69. expect( dropdown.buttonView.label ).to.equal( 'Paragraph' );
  70. command.value = command.formats[ 1 ];
  71. expect( dropdown.buttonView.label ).to.equal( 'Heading 1' );
  72. } );
  73. } );
  74. describe( 'localization', () => {
  75. let command;
  76. beforeEach( () => {
  77. const editorElement = document.createElement( 'div' );
  78. const spy = testUtils.sinon.stub( Locale.prototype, '_t' ).returns( 'foo' );
  79. spy.withArgs( 'Paragraph' ).returns( 'Akapit' );
  80. spy.withArgs( 'Heading 1' ).returns( 'Nagłówek 1' );
  81. spy.withArgs( 'Heading 2' ).returns( 'Nagłówek 2' );
  82. return ClassicTestEditor.create( editorElement, {
  83. plugins: [ Heading ],
  84. toolbar: [ 'heading' ],
  85. heading: {
  86. formats: [
  87. { id: 'paragraph', element: 'p', label: 'Paragraph' },
  88. { id: 'heading1', element: 'h2', label: 'Heading 1' },
  89. { id: 'heading2', element: 'h3', label: 'Not automatically localized' }
  90. ]
  91. }
  92. } )
  93. .then( newEditor => {
  94. editor = newEditor;
  95. dropdown = editor.ui.componentFactory.create( 'headings' );
  96. command = editor.commands.get( 'heading' );
  97. } );
  98. } );
  99. it( 'does not alter the original config', () => {
  100. expect( editor.config.get( 'heading.formats' ) ).to.deep.equal( [
  101. { id: 'paragraph', element: 'p', label: 'Paragraph' },
  102. { id: 'heading1', element: 'h2', label: 'Heading 1' },
  103. { id: 'heading2', element: 'h3', label: 'Not automatically localized' }
  104. ] );
  105. } );
  106. it( 'works for the #buttonView', () => {
  107. const buttonView = dropdown.buttonView;
  108. expect( buttonView.label ).to.equal( 'Akapit' );
  109. command.value = command.formats[ 1 ];
  110. expect( buttonView.label ).to.equal( 'Nagłówek 1' );
  111. } );
  112. it( 'works for the listView#items in the panel', () => {
  113. const listView = dropdown.listView;
  114. expect( listView.items.map( item => item.label ) ).to.deep.equal( [
  115. 'Akapit',
  116. 'Nagłówek 1',
  117. 'Not automatically localized'
  118. ] );
  119. } );
  120. } );
  121. } );
  122. } );