/** * @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 ModelTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/modeltesteditor'; import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph'; import { getData, setData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model'; import { assertEqualMarkup } from '@ckeditor/ckeditor5-utils/tests/_utils/utils'; import TableEditing from '../../src/tableediting'; import { modelTable } from '../_utils/utils'; import InsertTableCommand from '../../src/commands/inserttablecommand'; describe( 'InsertTableCommand', () => { let editor, model, command; beforeEach( () => { return ModelTestEditor .create( { plugins: [ Paragraph, TableEditing ] } ) .then( newEditor => { editor = newEditor; model = editor.model; command = new InsertTableCommand( editor ); } ); } ); afterEach( () => { return editor.destroy(); } ); describe( 'isEnabled', () => { describe( 'when selection is collapsed', () => { it( 'should be true if in paragraph', () => { setData( model, 'foo[]' ); expect( command.isEnabled ).to.be.true; } ); it( 'should be false if in table', () => { setData( model, 'foo[]
' ); expect( command.isEnabled ).to.be.false; } ); } ); } ); describe( 'execute()', () => { it( 'should create a single batch', () => { setData( model, 'foo[]' ); const spy = sinon.spy(); model.document.on( 'change', spy ); command.execute( { rows: 3, columns: 4 } ); sinon.assert.calledOnce( spy ); } ); describe( 'collapsed selection', () => { it( 'should insert table in empty root', () => { setData( model, '[]' ); command.execute(); assertEqualMarkup( getData( model ), modelTable( [ [ '[]', '' ], [ '', '' ] ] ) ); } ); it( 'should insert table with two rows and two columns after non-empty paragraph if selection is at the end', () => { setData( model, 'foo[]' ); command.execute(); assertEqualMarkup( getData( model ), 'foo' + modelTable( [ [ '[]', '' ], [ '', '' ] ] ) ); } ); it( 'should insert table with given rows and columns after non-empty paragraph', () => { setData( model, 'foo[]' ); command.execute( { rows: 3, columns: 4 } ); assertEqualMarkup( getData( model ), 'foo' + modelTable( [ [ '[]', '', '', '' ], [ '', '', '', '' ], [ '', '', '', '' ] ] ) ); } ); it( 'should insert table with given heading rows and heading columns after non-empty paragraph', () => { setData( model, 'foo[]' ); command.execute( { rows: 3, columns: 4, headingRows: 1, headingColumns: 2 } ); assertEqualMarkup( getData( model ), 'foo' + modelTable( [ [ '[]', '', '', '' ], [ '', '', '', '' ], [ '', '', '', '' ] ], { headingRows: 1, headingColumns: 2 } ) ); } ); it( 'should insert table before after non-empty paragraph if selection is inside', () => { setData( model, 'f[]oo' ); command.execute(); assertEqualMarkup( getData( model ), modelTable( [ [ '[]', '' ], [ '', '' ] ] ) + 'foo' ); } ); it( 'should replace empty paragraph with table', () => { setData( model, '[]' ); command.execute( { rows: 3, columns: 4 } ); assertEqualMarkup( getData( model ), modelTable( [ [ '[]', '', '', '' ], [ '', '', '', '' ], [ '', '', '', '' ] ] ) ); } ); } ); } ); } );