headingengine.js 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /**
  2. * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. import HeadingEngine from 'ckeditor5-heading/src/headingengine';
  6. import Paragraph from 'ckeditor5-paragraph/src/paragraph';
  7. import VirtualTestEditor from 'ckeditor5-core/tests/_utils/virtualtesteditor';
  8. import HeadingCommand from 'ckeditor5-heading/src/headingcommand';
  9. import Enter from 'ckeditor5-enter/src/enter';
  10. import { getData } from 'ckeditor5-engine/src/dev-utils/model';
  11. describe( 'HeadingEngine', () => {
  12. let editor, document;
  13. beforeEach( () => {
  14. return VirtualTestEditor.create( {
  15. plugins: [ Enter, HeadingEngine ]
  16. } )
  17. .then( newEditor => {
  18. editor = newEditor;
  19. document = editor.document;
  20. } );
  21. } );
  22. it( 'should be loaded', () => {
  23. expect( editor.plugins.get( HeadingEngine ) ).to.be.instanceOf( HeadingEngine );
  24. } );
  25. it( 'should load paragraph feature', () => {
  26. expect( editor.plugins.get( Paragraph ) ).to.be.instanceOf( Paragraph );
  27. } );
  28. it( 'should set proper schema rules', () => {
  29. expect( document.schema.hasItem( 'heading1' ) ).to.be.true;
  30. expect( document.schema.hasItem( 'heading2' ) ).to.be.true;
  31. expect( document.schema.hasItem( 'heading3' ) ).to.be.true;
  32. expect( document.schema.check( { name: 'heading1', inside: '$root' } ) ).to.be.true;
  33. expect( document.schema.check( { name: '$inline', inside: 'heading1' } ) ).to.be.true;
  34. expect( document.schema.check( { name: 'heading2', inside: '$root' } ) ).to.be.true;
  35. expect( document.schema.check( { name: '$inline', inside: 'heading2' } ) ).to.be.true;
  36. expect( document.schema.check( { name: 'heading3', inside: '$root' } ) ).to.be.true;
  37. expect( document.schema.check( { name: '$inline', inside: 'heading3' } ) ).to.be.true;
  38. } );
  39. it( 'should register format command', () => {
  40. expect( editor.commands.has( 'heading' ) ).to.be.true;
  41. const command = editor.commands.get( 'heading' );
  42. expect( command ).to.be.instanceOf( HeadingCommand );
  43. } );
  44. it( 'should convert heading1', () => {
  45. editor.setData( '<h2>foobar</h2>' );
  46. expect( getData( document, { withoutSelection: true } ) ).to.equal( '<heading1>foobar</heading1>' );
  47. expect( editor.getData() ).to.equal( '<h2>foobar</h2>' );
  48. } );
  49. it( 'should convert heading2', () => {
  50. editor.setData( '<h3>foobar</h3>' );
  51. expect( getData( document, { withoutSelection: true } ) ).to.equal( '<heading2>foobar</heading2>' );
  52. expect( editor.getData() ).to.equal( '<h3>foobar</h3>' );
  53. } );
  54. it( 'should convert heading3', () => {
  55. editor.setData( '<h4>foobar</h4>' );
  56. expect( getData( document, { withoutSelection: true } ) ).to.equal( '<heading3>foobar</heading3>' );
  57. expect( editor.getData() ).to.equal( '<h4>foobar</h4>' );
  58. } );
  59. it( 'should make enter command insert a defaultFormat block if selection ended at the end of heading block', () => {
  60. editor.setData( '<h2>foobar</h2>' );
  61. document.selection.collapse( document.getRoot().getChild( 0 ), 'end' );
  62. editor.execute( 'enter' );
  63. expect( getData( document ) ).to.equal( '<heading1>foobar</heading1><paragraph>[]</paragraph>' );
  64. } );
  65. it( 'should not alter enter command if selection not ended at the end of heading block', () => {
  66. // This test is to fill code coverage.
  67. editor.setData( '<h2>foobar</h2>' );
  68. document.selection.collapse( document.getRoot().getChild( 0 ), 3 );
  69. editor.execute( 'enter' );
  70. expect( getData( document ) ).to.equal( '<heading1>foo</heading1><heading1>[]bar</heading1>' );
  71. } );
  72. } );