8
0

heading.js 4.5 KB

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