/**
* @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
* For licensing, see LICENSE.md.
*/
import ModelTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/modeltesteditor';
import ParagraphCommand from '../src/paragraphcommand';
import Selection from '@ckeditor/ckeditor5-engine/src/model/selection';
import Range from '@ckeditor/ckeditor5-engine/src/model/range';
import { setData, getData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
describe( 'HeadingCommand', () => {
let editor, document, command, root, schema;
beforeEach( () => {
return ModelTestEditor.create().then( ( newEditor ) => {
editor = newEditor;
document = editor.document;
schema = document.schema;
command = new ParagraphCommand( editor );
root = document.getRoot();
editor.commands.set( 'paragraph', command );
schema.registerItem( 'paragraph', '$block' );
schema.registerItem( 'heading1', '$block' );
} );
} );
afterEach( () => {
command.destroy();
} );
describe( 'value', () => {
it( 'has default value', () => {
setData( document, '' );
expect( command.value ).to.be.false;
} );
it( 'responds to changes in selection (collapsed selection)', () => {
setData( document, 'foo[]bar' );
expect( command.value ).to.be.false;
setData( document, 'foo[]bar' );
expect( command.value ).to.be.true;
} );
it( 'responds to changes in selection (non–collapsed selection)', () => {
setData( document, '[foo]bar' );
expect( command.value ).to.be.false;
setData( document, '[foobar]' );
expect( command.value ).to.be.false;
setData( document, 'foo[bar]' );
expect( command.value ).to.be.true;
setData( document, 'foo[bar]' );
expect( command.value ).to.be.true;
setData( document, '[barfoo]' );
expect( command.value ).to.be.true;
} );
} );
describe( '_doExecute', () => {
it( 'should update value after execution', () => {
setData( document, '[]' );
command._doExecute();
expect( getData( document ) ).to.equal( '[]' );
expect( command.value ).to.be.true;
} );
describe( 'custom options', () => {
it( 'should use provided batch', () => {
const batch = editor.document.batch();
setData( document, 'foo[]bar' );
expect( batch.deltas.length ).to.equal( 0 );
command._doExecute( { batch } );
expect( batch.deltas.length ).to.be.above( 0 );
} );
it( 'should use provided selection', () => {
setData( document, 'foo[]barbazqux' );
const secondTolastHeading = root.getChild( 1 );
const lastHeading = root.getChild( 2 );
const selection = new Selection();
selection.addRange( Range.createFromParentsAndOffsets( secondTolastHeading, 0, lastHeading, 0 ) );
command._doExecute( { selection } );
expect( getData( document ) ).to.equal( 'foo[]barbazqux' );
} );
} );
describe( 'collapsed selection', () => {
it( 'does nothing when executed with already applied', () => {
setData( document, 'foo[]bar' );
command._doExecute();
expect( getData( document ) ).to.equal( 'foo[]bar' );
} );
it( 'converts topmost blocks', () => {
schema.registerItem( 'inlineImage', '$inline' );
schema.allow( { name: '$text', inside: 'inlineImage' } );
setData( document, 'foo[]bar' );
command._doExecute();
expect( getData( document ) ).to.equal( 'foo[]bar' );
} );
} );
describe( 'non-collapsed selection', () => {
it( 'converts all elements where selection is applied', () => {
schema.registerItem( 'heading2', '$block' );
setData( document, 'foo[bar]baz' );
command._doExecute();
expect( getData( document ) ).to.equal(
'foo[bar]baz'
);
} );
} );
} );
} );