/**
* @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 { setData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
import { assertTableStyle, modelTable } from '../../_utils/utils';
import TablePropertiesEditing from '../../../src/tableproperties/tablepropertiesediting';
import TableWidthCommand from '../../../src/tableproperties/commands/tablewidthcommand';
describe( 'table properties', () => {
describe( 'commands', () => {
describe( 'TableWidthCommand', () => {
let editor, model, command;
beforeEach( async () => {
editor = await ModelTestEditor.create( {
plugins: [ Paragraph, TablePropertiesEditing ]
} );
model = editor.model;
command = new TableWidthCommand( editor );
} );
afterEach( () => {
return editor.destroy();
} );
describe( 'isEnabled', () => {
describe( 'collapsed selection', () => {
it( 'should be false if selection does not have table', () => {
setData( model, 'foo[]' );
expect( command.isEnabled ).to.be.false;
} );
it( 'should be true is selection has table', () => {
setData( model, modelTable( [ [ '[]foo' ] ] ) );
expect( command.isEnabled ).to.be.true;
} );
} );
describe( 'non-collapsed selection', () => {
it( 'should be false if selection does not have table', () => {
setData( model, 'f[oo]' );
expect( command.isEnabled ).to.be.false;
} );
it( 'should be true is selection has table', () => {
setData( model, modelTable( [ [ 'f[o]o' ] ] ) );
expect( command.isEnabled ).to.be.true;
} );
} );
} );
describe( 'value', () => {
describe( 'collapsed selection', () => {
it( 'should be undefined if selected table has no width property', () => {
setData( model, modelTable( [ [ '[]foo' ] ] ) );
expect( command.value ).to.be.undefined;
} );
it( 'should be set if selected table has width property', () => {
setData( model, modelTable( [ [ '[]foo' ] ], { width: '100px' } ) );
expect( command.value ).to.equal( '100px' );
} );
} );
describe( 'non-collapsed selection', () => {
it( 'should be false if selection does not have table', () => {
setData( model, 'f[oo]' );
expect( command.value ).to.be.undefined;
} );
it( 'should be true is selection has table', () => {
setData( model, modelTable( [ [ 'f[o]o' ] ], { width: '100px' } ) );
expect( command.value ).to.equal( '100px' );
} );
} );
} );
describe( 'execute()', () => {
it( 'should use provided batch', () => {
setData( model, modelTable( [ [ 'foo[]' ] ] ) );
const batch = model.createBatch();
const spy = sinon.spy( model, 'enqueueChange' );
command.execute( { value: '25px', batch } );
sinon.assert.calledWith( spy, batch );
} );
it( 'should add default unit for numeric values (number passed)', () => {
setData( model, modelTable( [ [ 'foo[]' ] ] ) );
command.execute( { value: 25 } );
assertTableStyle( editor, null, 'width:25px;' );
} );
it( 'should add default unit for numeric values (string passed)', () => {
setData( model, modelTable( [ [ 'foo[]' ] ] ) );
command.execute( { value: 25 } );
assertTableStyle( editor, null, 'width:25px;' );
} );
it( 'should not add default unit for numeric values with unit', () => {
setData( model, modelTable( [ [ 'foo[]' ] ] ) );
command.execute( { value: '25pt' } );
assertTableStyle( editor, null, 'width:25pt;' );
} );
it( 'should add default unit to floats (number passed)', () => {
setData( model, modelTable( [ [ 'foo[]' ] ] ) );
command.execute( { value: 25.1 } );
assertTableStyle( editor, null, 'width:25.1px;' );
} );
it( 'should add default unit to floats (string passed)', () => {
setData( model, modelTable( [ [ 'foo[]' ] ] ) );
command.execute( { value: '0.1' } );
assertTableStyle( editor, null, 'width:0.1px;' );
} );
it( 'should pass invalid values', () => {
setData( model, modelTable( [ [ 'foo[]' ] ] ) );
command.execute( { value: 'bar' } );
assertTableStyle( editor, null, 'width:bar;' );
} );
it( 'should pass invalid value (string passed, CSS float without leading 0)', () => {
setData( model, modelTable( [ [ 'foo[]' ] ] ) );
command.execute( { value: '.2' } );
assertTableStyle( editor, null, 'width:.2;' );
} );
describe( 'collapsed selection', () => {
it( 'should set selected table width to a passed value', () => {
setData( model, modelTable( [ [ 'foo[]' ] ] ) );
command.execute( { value: '25px' } );
assertTableStyle( editor, null, 'width:25px;' );
} );
it( 'should change selected table width to a passed value', () => {
setData( model, modelTable( [ [ '[]foo' ] ], { width: '100px' } ) );
command.execute( { value: '25px' } );
assertTableStyle( editor, null, 'width:25px;' );
} );
it( 'should remove width from a selected table if no value is passed', () => {
setData( model, modelTable( [ [ '[]foo' ] ], { width: '100px' } ) );
command.execute();
assertTableStyle( editor, '' );
} );
} );
describe( 'non-collapsed selection', () => {
it( 'should set selected table width to a passed value', () => {
setData( model, modelTable( [ [ '[foo]' ] ] ) );
command.execute( { value: '25px' } );
assertTableStyle( editor, null, 'width:25px;' );
} );
it( 'should change selected table width to a passed value', () => {
setData( model, modelTable( [ [ '[foo]' ] ] ) );
command.execute( { value: '25px' } );
assertTableStyle( editor, null, 'width:25px;' );
} );
it( 'should remove width from a selected table if no value is passed', () => {
setData( model, modelTable( [ [ '[foo]' ] ] ) );
command.execute();
assertTableStyle( editor, '' );
} );
} );
} );
} );
} );
} );