/** * @license Copyright (c) 2003-2019, CKSource - Frederico Knabben. All rights reserved. * For licensing, see LICENSE.md. */ import AlignmentCommand from '../src/alignmentcommand'; import { getData as getModelData, setData as setModelData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model'; import Command from '@ckeditor/ckeditor5-core/src/command'; import ModelTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/modeltesteditor'; describe( 'AlignmentCommand', () => { let editor, model, command; beforeEach( () => { return ModelTestEditor.create() .then( newEditor => { model = newEditor.model; command = new AlignmentCommand( newEditor ); editor = newEditor; editor.commands.add( 'alignment', command ); model.schema.register( 'paragraph', { inheritAllFrom: '$block' } ); model.schema.register( 'div', { inheritAllFrom: '$block' } ); model.schema.extend( 'paragraph', { allowAttributes: 'alignment' } ); } ); } ); afterEach( () => { editor.destroy(); } ); it( 'is a command', () => { expect( AlignmentCommand.prototype ).to.be.instanceOf( Command ); expect( command ).to.be.instanceOf( Command ); } ); describe( 'value', () => { it( 'is set to block alignment when selection is in block that has alignment attribute set', () => { setModelData( model, 'x[]x' ); expect( command ).to.have.property( 'value', 'center' ); } ); it( 'is set to default alignment when selection is in block with default alignment', () => { setModelData( model, 'x[]x' ); expect( command ).to.have.property( 'value', 'left' ); } ); } ); describe( 'isEnabled', () => { it( 'is true when selection is in a block which can have added alignment', () => { setModelData( model, 'x[]x' ); expect( command ).to.have.property( 'isEnabled', true ); } ); it( 'is false when selection is in a block which can not be aligned', () => { setModelData( model, '
x[]x
' ); expect( command ).to.have.property( 'isEnabled', false ); } ); } ); describe( 'execute()', () => { describe( 'applying alignment', () => { it( 'adds alignment to block element', () => { setModelData( model, 'x[]x' ); editor.execute( 'alignment', { value: 'center' } ); expect( getModelData( model ) ).to.equal( 'x[]x' ); } ); it( 'should remove alignment from single block element if already has one', () => { setModelData( model, 'x[]x' ); editor.execute( 'alignment', { value: 'left' } ); expect( getModelData( model ) ).to.equal( 'x[]x' ); } ); it( 'adds alignment to all selected blocks', () => { setModelData( model, 'x[xxxx]x' ); editor.execute( 'alignment', { value: 'center' } ); expect( getModelData( model ) ).to.equal( 'x[x' + 'xx' + 'x]x' ); } ); it( 'sets alignment on all selected blocks as first block', () => { setModelData( model, 'x[x' + 'xx' + 'x]x' ); editor.execute( 'alignment', { value: 'center' } ); expect( getModelData( model ) ).to.equal( 'x[x' + 'xx' + 'x]x' ); } ); it( 'should remove alignment if block has the same alignment set', () => { setModelData( model, 'x[]x' ); editor.execute( 'alignment', { value: 'center' } ); expect( getModelData( model ) ).to.equal( 'x[]x' ); } ); } ); describe( 'applying default alignment', () => { it( 'removes alignment from block element when passed as value', () => { setModelData( model, 'x[]x' ); editor.execute( 'alignment', { value: 'left' } ); expect( getModelData( model ) ).to.equal( 'x[]x' ); } ); it( 'removes alignment from block element when no value is passed', () => { setModelData( model, 'x[]x' ); editor.execute( 'alignment' ); expect( getModelData( model ) ).to.equal( 'x[]x' ); } ); it( 'removes alignment from all selected blocks', () => { setModelData( model, 'x[x' + 'xx' + 'x]x' ); editor.execute( 'alignment', { value: 'left' } ); expect( getModelData( model ) ).to.equal( 'x[xxxx]x' ); } ); it( 'removes alignment from all selected blocks even if one has no alignment defined', () => { setModelData( model, 'x[x' + 'xx' + 'x]x' ); editor.execute( 'alignment', { value: 'left' } ); expect( getModelData( model ) ).to.equal( 'x[xxxx]x' ); } ); } ); } ); } );