/** * @license Copyright (c) 2003-2020, CKSource - Frederico Knabben. All rights reserved. * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license */ import RemoveFormatCommand from '../src/removeformatcommand'; import Command from '@ckeditor/ckeditor5-core/src/command'; import ModelTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/modeltesteditor'; import { getData, setData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model'; describe( 'RemoveFormatCommand', () => { let editor, model, command; beforeEach( () => { return ModelTestEditor.create() .then( newEditor => { editor = newEditor; model = editor.model; command = new RemoveFormatCommand( newEditor ); editor.commands.add( 'removeFormat', command ); model.schema.register( 'p', { inheritAllFrom: '$block' } ); model.schema.addAttributeCheck( ( ctx, attributeName ) => { // Bold will be used as an example formatting attribute. if ( ctx.endsWith( 'p $text' ) && attributeName == 'bold' ) { return true; } } ); model.schema.addAttributeCheck( ( ctx, attributeName ) => { // Text attribtue "irrelevant" will be used to make sure that non-formatting // is note being removed. if ( ctx.endsWith( 'p $text' ) && attributeName == 'irrelevant' ) { return true; } } ); model.schema.setAttributeProperties( 'bold', { isFormatting: true } ); } ); } ); afterEach( () => { editor.destroy(); } ); it( 'is a command', () => { expect( RemoveFormatCommand.prototype ).to.be.instanceOf( Command ); expect( command ).to.be.instanceOf( Command ); } ); describe( 'isEnabled', () => { const expectEnabledPropertyToBe = expectedValue => expect( command ).to.have.property( 'isEnabled', expectedValue ); const cases = { 'state when in non-formatting markup': { input: '
fo[]o
', assert: () => expectEnabledPropertyToBe( false ) }, 'state with collapsed selection in formatting markup': { input: 'f<$text bold="true">o[]o$text>
', assert: () => expectEnabledPropertyToBe( true ) }, 'state with selection containing formatting in the middle': { input: 'f[oo <$text bold="true">bar$text> ba]z
', assert: () => expectEnabledPropertyToBe( true ) }, 'state with partially selected formatting at the start': { input: '<$text bold="true">b[ar$text> ba]z
', assert: () => expectEnabledPropertyToBe( true ) }, 'state with partially selected formatting at the end': { input: 'f[oo <$text bold="true">ba]z$text>
', assert: () => expectEnabledPropertyToBe( true ) }, 'state with formatted selection alone': { input: 'fo[]o
', setDataOptions: { selectionAttributes: { bold: true, irrelevant: true } }, assert: () => expectEnabledPropertyToBe( true ) } }; generateTypicalUseCases( cases ); } ); describe( 'execute()', () => { const expectModelToBeEqual = expectedValue => expect( getData( model ) ).to.equal( expectedValue ); const cases = { 'state when in non-formatting markup': { input: 'fo[]o
', assert: () => expectModelToBeEqual( 'fo[]o
' ) }, 'state with collapsed selection in formatting markup': { input: 'f<$text bold="true">o[]o$text>
', assert: () => expectModelToBeEqual( 'f<$text bold="true">o$text>[]<$text bold="true">o$text>
' ) }, 'state with selection containing formatting in the middle': { input: 'f[oo <$text bold="true">bar$text> ba]z
', assert: () => expectModelToBeEqual( 'f[oo bar ba]z
' ) }, 'state with partially selected formatting at the start': { input: '<$text bold="true">b[ar$text> ba]z
', assert: () => expectModelToBeEqual( '<$text bold="true">b$text>[ar ba]z
' ) }, 'state with partially selected formatting at the end': { input: 'f[oo <$text bold="true">ba]z$text>
', assert: () => expectModelToBeEqual( 'f[oo ba]<$text bold="true">z$text>
' ) }, 'state with formatted selection alone': { input: 'fo[]o
', setDataOptions: { selectionAttributes: { bold: true, irrelevant: true } }, assert: () => { expect( model.document.selection.hasAttribute( 'bold' ) ).to.equal( false ); expect( model.document.selection.hasAttribute( 'irrelevant' ) ).to.equal( true ); } } }; generateTypicalUseCases( cases, { beforeAssert: () => command.execute() } ); } ); function generateTypicalUseCases( useCases, options ) { for ( const [ key, testConfig ] of Object.entries( useCases ) ) { it( key, () => { setData( model, testConfig.input, testConfig.setDataOptions ); if ( options && options.beforeAssert ) { options.beforeAssert(); } testConfig.assert(); } ); } } } );