heading.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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.commandName = '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.commandName = 'paragraph';
  61. dropdown.fire( 'execute' );
  62. sinon.assert.calledOnce( focusSpy );
  63. } );
  64. describe( 'model to command binding', () => {
  65. let commands;
  66. beforeEach( () => {
  67. commands = {};
  68. editor.config.get( 'heading.options' ).forEach( ( { modelElement } ) => {
  69. commands[ modelElement ] = editor.commands.get( modelElement );
  70. } );
  71. } );
  72. it( 'isEnabled', () => {
  73. for ( let name in commands ) {
  74. commands[ name ].isEnabled = false;
  75. }
  76. expect( dropdown.buttonView.isEnabled ).to.be.false;
  77. commands.heading2.isEnabled = true;
  78. expect( dropdown.buttonView.isEnabled ).to.be.true;
  79. } );
  80. it( 'label', () => {
  81. for ( let name in commands ) {
  82. commands[ name ].value = false;
  83. }
  84. expect( dropdown.buttonView.label ).to.equal( 'Paragraph' );
  85. commands.heading2.value = true;
  86. expect( dropdown.buttonView.label ).to.equal( 'Heading 2' );
  87. } );
  88. } );
  89. describe( 'localization', () => {
  90. let commands;
  91. beforeEach( () => {
  92. return localizedEditor( [
  93. { modelElement: 'paragraph', title: 'Paragraph' },
  94. { modelElement: 'heading1', viewElement: 'h2', title: 'Heading 1' },
  95. { modelElement: 'heading2', viewElement: 'h3', title: 'Heading 2' }
  96. ] );
  97. } );
  98. it( 'does not alter the original config', () => {
  99. expect( editor.config.get( 'heading.options' ) ).to.deep.equal( [
  100. { modelElement: 'paragraph', title: 'Paragraph' },
  101. { modelElement: 'heading1', viewElement: 'h2', title: 'Heading 1' },
  102. { modelElement: 'heading2', viewElement: 'h3', title: 'Heading 2' }
  103. ] );
  104. } );
  105. it( 'works for the #buttonView', () => {
  106. const buttonView = dropdown.buttonView;
  107. expect( buttonView.label ).to.equal( 'Akapit' );
  108. commands.heading1.value = true;
  109. expect( buttonView.label ).to.equal( 'Nagłówek 1' );
  110. } );
  111. it( 'works for the listView#items in the panel', () => {
  112. const listView = dropdown.listView;
  113. expect( listView.items.map( item => item.label ) ).to.deep.equal( [
  114. 'Akapit',
  115. 'Nagłówek 1',
  116. 'Nagłówek 2'
  117. ] );
  118. } );
  119. it( 'allows custom titles', () => {
  120. return localizedEditor( [
  121. { modelElement: 'paragraph', title: 'Custom paragraph title' },
  122. { modelElement: 'heading1', title: 'Custom heading1 title' }
  123. ] ).then( () => {
  124. const listView = dropdown.listView;
  125. expect( listView.items.map( item => item.label ) ).to.deep.equal( [
  126. 'Custom paragraph title',
  127. 'Custom heading1 title',
  128. ] );
  129. } );
  130. } );
  131. it( 'translates default using the the locale', () => {
  132. return localizedEditor( [
  133. { modelElement: 'paragraph', title: 'Paragraph' }
  134. ] ).then( () => {
  135. const listView = dropdown.listView;
  136. expect( listView.items.map( item => item.label ) ).to.deep.equal( [
  137. 'Akapit'
  138. ] );
  139. } );
  140. } );
  141. function localizedEditor( options ) {
  142. const editorElement = document.createElement( 'div' );
  143. return ClassicTestEditor.create( editorElement, {
  144. plugins: [ Heading ],
  145. toolbar: [ 'heading' ],
  146. lang: 'pl',
  147. heading: {
  148. options: options
  149. }
  150. } )
  151. .then( newEditor => {
  152. editor = newEditor;
  153. dropdown = editor.ui.componentFactory.create( 'headings' );
  154. commands = {};
  155. editor.config.get( 'heading.options' ).forEach( ( { modelElement } ) => {
  156. commands[ modelElement ] = editor.commands.get( modelElement );
  157. } );
  158. } );
  159. }
  160. } );
  161. describe( 'class', () => {
  162. it( 'is set for the listView#items in the panel', () => {
  163. const listView = dropdown.listView;
  164. expect( listView.items.map( item => item.class ) ).to.deep.equal( [
  165. 'ck-heading_paragraph',
  166. 'ck-heading_heading1',
  167. 'ck-heading_heading2',
  168. 'ck-heading_heading3'
  169. ] );
  170. } );
  171. it( 'reflects the #value of the commands', () => {
  172. const listView = dropdown.listView;
  173. editor.commands.get( 'heading2' ).value = true;
  174. expect( listView.items.map( item => item.isActive ) ).to.deep.equal( [
  175. false,
  176. false,
  177. true,
  178. false
  179. ] );
  180. } );
  181. } );
  182. } );
  183. } );