/**
* @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
* For licensing, see LICENSE.md.
*/
'use strict';
import StandardEditor from '/ckeditor5/editor/standardeditor.js';
import FormatsCommand from '/ckeditor5/formats/formatscommand.js';
import Range from '/ckeditor5/engine/model/range.js';
import { setData, getData } from '/tests/engine/_utils/model.js';
const formats = [
{ id: 'paragraph', viewElement: 'p' },
{ id: 'heading1', viewElement: 'h2' },
{ id: 'heading2', viewElement: 'h3' },
{ id: 'heading3', viewElement: 'h4' }
];
describe( 'FormatsCommand', () => {
let editor, document, command, root;
beforeEach( () => {
editor = new StandardEditor( null );
document = editor.document;
command = new FormatsCommand( editor, formats );
const schema = document.schema;
for ( let format of formats ) {
schema.registerItem( format.id, '$block' );
}
root = document.createRoot();
} );
afterEach( () => {
command.destroy();
} );
describe( 'value', () => {
for ( let format of formats ) {
test( format );
}
function test( format ) {
it( `equals ${ format.id } when collapsed selection is placed inside ${ format.id } element`, () => {
setData( document, `<${ format.id }>foobar${ format.id }>` );
const element = root.getChild( 0 );
document.selection.addRange( Range.createFromParentsAndOffsets( element, 3, element, 3 ) );
expect( command.value ).to.equal( format.id );
} );
}
} );
describe( '_doExecute', () => {
let convertTo = formats[ formats.length - 1 ];
for ( let format of formats ) {
test( format, convertTo );
convertTo = format;
}
it( 'uses paragraph as default value', () => {
setData( document, 'foobar' );
command._doExecute();
expect( getData( document ) ).to.equal( 'foobar' );
} );
it( 'converts to default format when executed with already applied format', () => {
setData( document, 'foobar' );
command._doExecute( 'heading1' );
expect( getData( document ) ).to.equal( 'foobar' );
} );
function test( from, to ) {
it( `converts ${ from.id } to ${ to.id } on collapsed selection`, () => {
setData( document, `<${ from.id }>foobar${ from.id }>` );
command._doExecute( to.id );
expect( getData( document ) ).to.equal( `<${ to.id }>foobar${ to.id }>` );
} );
}
} );
} );