/** * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved. * For licensing, see LICENSE.md. */ 'use strict'; import VirtualTestEditor from '/tests/ckeditor5/_utils/virtualtesteditor.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', default: true }, { id: 'heading1', viewElement: 'h2' }, { id: 'heading2', viewElement: 'h3' }, { id: 'heading3', viewElement: 'h4' } ]; describe( 'FormatsCommand', () => { let editor, document, command, root; beforeEach( () => { return VirtualTestEditor.create() .then( newEditor => { editor = newEditor; document = editor.document; command = new FormatsCommand( editor, formats ); const schema = document.schema; for ( let format of formats ) { schema.registerItem( format.id, '$block' ); } root = document.getRoot(); } ); } ); 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` ); const element = root.getChild( 0 ); document.selection.addRange( Range.createFromParentsAndOffsets( element, 3, element, 3 ) ); expect( command.format ).to.equal( format ); } ); } } ); 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` ); command._doExecute( to.id ); expect( getData( document ) ).to.equal( `<${ to.id }>foobar` ); } ); } } ); } );