heading.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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 options 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.modelElement = 'paragraph';
  53. dropdown.fire( 'execute' );
  54. sinon.assert.calledOnce( executeSpy );
  55. sinon.assert.calledWithExactly( executeSpy, 'paragraph' );
  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.modelElement = 'paragraph';
  61. dropdown.fire( 'execute' );
  62. sinon.assert.calledOnce( focusSpy );
  63. } );
  64. describe( 'model to command binding', () => {
  65. let commands;
  66. beforeEach( () => {
  67. commands = editor.plugins.get( HeadingEngine ).commands;
  68. } );
  69. it( 'isEnabled', () => {
  70. for ( let command of commands ) {
  71. command.isEnabled = false;
  72. }
  73. expect( dropdown.buttonView.isEnabled ).to.be.false;
  74. commands.get( 'heading2' ).isEnabled = true;
  75. expect( dropdown.buttonView.isEnabled ).to.be.true;
  76. } );
  77. it( 'label', () => {
  78. for ( let command of commands ) {
  79. command.value = false;
  80. }
  81. expect( dropdown.buttonView.label ).to.equal( 'Paragraph' );
  82. commands.get( 'heading2' ).value = true;
  83. expect( dropdown.buttonView.label ).to.equal( 'Heading 2' );
  84. } );
  85. } );
  86. describe( 'localization', () => {
  87. let commands;
  88. beforeEach( () => {
  89. const editorElement = document.createElement( 'div' );
  90. return ClassicTestEditor.create( editorElement, {
  91. plugins: [ Heading ],
  92. toolbar: [ 'heading' ],
  93. lang: 'pl',
  94. heading: {
  95. options: [
  96. { modelElement: 'paragraph', viewElement: 'p', title: 'Paragraph' },
  97. { modelElement: 'heading1', viewElement: 'h2', title: 'Heading 1' },
  98. { modelElement: 'heading2', viewElement: 'h3', title: 'Not automatically localized' }
  99. ]
  100. }
  101. } )
  102. .then( newEditor => {
  103. editor = newEditor;
  104. dropdown = editor.ui.componentFactory.create( 'headings' );
  105. commands = editor.plugins.get( HeadingEngine ).commands;
  106. } );
  107. } );
  108. it( 'does not alter the original config', () => {
  109. expect( editor.config.get( 'heading.options' ) ).to.deep.equal( [
  110. { modelElement: 'paragraph', viewElement: 'p', title: 'Paragraph' },
  111. { modelElement: 'heading1', viewElement: 'h2', title: 'Heading 1' },
  112. { modelElement: 'heading2', viewElement: 'h3', title: 'Not automatically localized' }
  113. ] );
  114. } );
  115. it( 'works for the #buttonView', () => {
  116. const buttonView = dropdown.buttonView;
  117. expect( buttonView.label ).to.equal( 'Akapit' );
  118. commands.get( 'heading1' ).value = true;
  119. expect( buttonView.label ).to.equal( 'Nagłówek 1' );
  120. } );
  121. it( 'works for the listView#items in the panel', () => {
  122. const listView = dropdown.listView;
  123. expect( listView.items.map( item => item.label ) ).to.deep.equal( [
  124. 'Akapit',
  125. 'Nagłówek 1',
  126. 'Not automatically localized'
  127. ] );
  128. } );
  129. } );
  130. } );
  131. } );