heading.js 6.4 KB

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