/**
* @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 { getData, setData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
import InsertTableCommand from '../../src/commands/inserttablecommand';
import TableUtils from '../../src/tableutils';
import { defaultConversion, defaultSchema, modelTable } from '../_utils/utils';
import { assertEqualMarkup } from '@ckeditor/ckeditor5-utils/tests/_utils/utils';
describe( 'InsertTableCommand', () => {
let editor, model, command;
beforeEach( () => {
return ModelTestEditor
.create( {
plugins: [ TableUtils ]
} )
.then( newEditor => {
editor = newEditor;
model = editor.model;
command = new InsertTableCommand( editor );
defaultSchema( model.schema );
defaultConversion( editor.conversion );
} );
} );
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, '
' );
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 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( [
[ '[]', '', '', '' ],
[ '', '', '', '' ],
[ '', '', '', '' ]
] )
);
} );
} );
} );
} );