/** * @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 CodeBlockEditing from '../src/codeblockediting'; import IndentCodeBlockCommand from '../src/indentcodeblockcommand'; import AlignmentEditing from '@ckeditor/ckeditor5-alignment/src/alignmentediting'; import BoldEditing from '@ckeditor/ckeditor5-basic-styles/src/bold/boldediting'; import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph'; import BlockQuoteEditing from '@ckeditor/ckeditor5-block-quote/src/blockquoteediting'; import ModelTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/modeltesteditor'; import { getData as getModelData, setData as setModelData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model'; describe( 'IndentCodeBlockCommand', () => { let editor, model, indentCommand; beforeEach( () => { return ModelTestEditor .create( { plugins: [ CodeBlockEditing, Paragraph, BlockQuoteEditing, AlignmentEditing, BoldEditing ] } ) .then( newEditor => { editor = newEditor; model = editor.model; indentCommand = new IndentCodeBlockCommand( editor, 'forward' ); } ); } ); afterEach( () => { return editor.destroy(); } ); describe( '#isEnabled', () => { it( 'should be true when the first selected block is a codeBlock #1', () => { setModelData( model, 'f[]oo' ); expect( indentCommand.isEnabled ).to.be.true; } ); it( 'should be true when the first selected block is a codeBlock #2', () => { setModelData( model, 'f[ooba]r' ); expect( indentCommand.isEnabled ).to.be.true; } ); it( 'should be false when there is no code block in the selection', () => { setModelData( model, 'foo[]' ); expect( indentCommand.isEnabled ).to.be.false; } ); it( 'should be false when the selection is not anchored in the code block', () => { setModelData( model, 'f[oobarba]z' ); expect( indentCommand.isEnabled ).to.be.false; } ); describe( 'config.codeBlock.indentSequence', () => { it( 'should disable the command when the config is not set', () => { return ModelTestEditor .create( { plugins: [ CodeBlockEditing, Paragraph, BlockQuoteEditing, AlignmentEditing, BoldEditing ], codeBlock: { indentSequence: false } } ) .then( newEditor => { const editor = newEditor; const model = editor.model; const indentCommand = new IndentCodeBlockCommand( editor, 'forward' ); setModelData( model, '[]foo' ); expect( indentCommand.isEnabled ).to.be.false; return editor.destroy(); } ); } ); } ); } ); describe( 'execute()', () => { it( 'should indent when a selection is collapsed in an empty code block', () => { setModelData( model, '[]' ); indentCommand.execute(); expect( getModelData( model ) ).to.equal( ' []' ); } ); it( 'should indent when a selection is collapsed', () => { setModelData( model, 'f[]oo' ); indentCommand.execute(); expect( getModelData( model ) ).to.equal( 'f []oo' ); } ); it( 'should indent a whole line when a selection is expanded', () => { setModelData( model, 'f[o]o' ); indentCommand.execute(); expect( getModelData( model ) ).to.equal( ' f[o]o' ); } ); it( 'should indent multiple lines when a selection is expanded', () => { setModelData( model, 'f[oob]ar' ); indentCommand.execute(); expect( getModelData( model ) ).to.equal( ' f[oo b]ar' ); } ); it( 'should append the indentation to the line\'s leading white spaces (#1)', () => { setModelData( model, '[]foo' ); // []foo model.change( writer => { writer.insertText( ' ', model.document.getRoot().getChild( 0 ) ); } ); indentCommand.execute(); expect( getModelData( model ) ).to.equal( ' []foo' ); } ); it( 'should append the indentation to the line\'s leading white spaces (#2)', () => { setModelData( model, 'f[oob]ar' ); // f[oo b]ar model.change( writer => { writer.insertText( ' ', model.document.getRoot().getChild( 0 ), 4 ); writer.insertText( ' ', model.document.getRoot().getChild( 0 ), 0 ); } ); indentCommand.execute(); expect( getModelData( model ) ).to.equal( ' f[oo b]ar' ); } ); describe( 'config.codeBlock.indentSequence', () => { it( 'should be used when indenting', () => { return ModelTestEditor .create( { plugins: [ CodeBlockEditing, Paragraph, BlockQuoteEditing, AlignmentEditing, BoldEditing ], codeBlock: { indentSequence: ' ' } } ) .then( newEditor => { const editor = newEditor; const model = editor.model; const indentCommand = new IndentCodeBlockCommand( editor, 'forward' ); setModelData( model, 'f[o]o' ); indentCommand.execute(); expect( getModelData( model ) ).to.equal( ' f[o]o' ); return editor.destroy(); } ); } ); } ); } ); } );