浏览代码

Add table background color command tests.

Maciej Gołaszewski 5 年之前
父节点
当前提交
f054486a48

+ 4 - 4
packages/ckeditor5-table/src/tableproperties/commands/tablebackgroundcolorcommand.js

@@ -39,10 +39,10 @@ export default class TableBackgroundColorCommand extends Command {
 		const editor = this.editor;
 		const selection = editor.model.document.selection;
 
-		const table = Array.from( selection.getSelectedBlocks() ).find( block => block.is( 'table' ) );
+		const table = findAncestor( 'table', selection.getFirstPosition() );
 
 		this.isEnabled = !!table;
-		this.value = table && this._getValue( table );
+		this.value = this._getValue( table );
 	}
 
 	_getValue( table ) {
@@ -65,12 +65,12 @@ export default class TableBackgroundColorCommand extends Command {
 		const model = this.editor.model;
 		const selection = model.document.selection;
 
-		const { value } = options;
+		const { value, batch } = options;
 
 		const tables = Array.from( selection.getSelectedBlocks() )
 			.map( element => findAncestor( 'table', model.createPositionAt( element, 0 ) ) );
 
-		model.change( writer => {
+		model.enqueueChange( batch || 'default', writer => {
 			if ( value ) {
 				tables.forEach( table => writer.setAttribute( this.attributeName, value, table ) );
 			} else {

+ 14 - 14
packages/ckeditor5-table/tests/tableproperties/commands/tablealignmentcommand.js

@@ -32,24 +32,24 @@ describe( 'table properties', () => {
 
 			describe( 'isEnabled', () => {
 				describe( 'collapsed selection', () => {
-					it( 'should be false if selection does not have table cell', () => {
+					it( 'should be false if selection does not have table', () => {
 						setData( model, '<paragraph>foo[]</paragraph>' );
 						expect( command.isEnabled ).to.be.false;
 					} );
 
-					it( 'should be true is selection has table cell', () => {
+					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 cell', () => {
+					it( 'should be false if selection does not have table', () => {
 						setData( model, '<paragraph>f[oo]</paragraph>' );
 						expect( command.isEnabled ).to.be.false;
 					} );
 
-					it( 'should be true is selection has table cell', () => {
+					it( 'should be true is selection has table', () => {
 						setData( model, modelTable( [ [ 'f[o]o' ] ] ) );
 						expect( command.isEnabled ).to.be.true;
 					} );
@@ -58,13 +58,13 @@ describe( 'table properties', () => {
 
 			describe( 'value', () => {
 				describe( 'collapsed selection', () => {
-					it( 'should be undefined if selected table cell has no alignment property', () => {
+					it( 'should be undefined if selected table has no alignment property', () => {
 						setData( model, modelTable( [ [ '[]foo' ] ] ) );
 
 						expect( command.value ).to.be.undefined;
 					} );
 
-					it( 'should be set if selected table cell has alignment property', () => {
+					it( 'should be set if selected table has alignment property', () => {
 						setData( model, modelTable( [ [ '[]foo' ] ], { alignment: 'center' } ) );
 
 						expect( command.value ).to.equal( 'center' );
@@ -72,13 +72,13 @@ describe( 'table properties', () => {
 				} );
 
 				describe( 'non-collapsed selection', () => {
-					it( 'should be false if selection does not have table cell', () => {
+					it( 'should be false if selection does not have table', () => {
 						setData( model, '<paragraph>f[oo]</paragraph>' );
 
 						expect( command.value ).to.be.undefined;
 					} );
 
-					it( 'should be true is selection has table cell', () => {
+					it( 'should be true is selection has table', () => {
 						setData( model, modelTable( [ [ 'f[o]o' ] ], { alignment: 'center' } ) );
 
 						expect( command.value ).to.equal( 'center' );
@@ -97,7 +97,7 @@ describe( 'table properties', () => {
 				} );
 
 				describe( 'collapsed selection', () => {
-					it( 'should set selected table cell alignment to a passed value', () => {
+					it( 'should set selected table alignment to a passed value', () => {
 						setData( model, modelTable( [ [ 'foo[]' ] ] ) );
 
 						command.execute( { value: 'right' } );
@@ -105,7 +105,7 @@ describe( 'table properties', () => {
 						assertTableStyle( editor, 'margin-left:auto;margin-right:0;' );
 					} );
 
-					it( 'should change selected table cell alignment to a passed value', () => {
+					it( 'should change selected table alignment to a passed value', () => {
 						setData( model, modelTable( [ [ '[]foo' ] ], { alignment: 'center' } ) );
 
 						command.execute( { value: 'right' } );
@@ -113,7 +113,7 @@ describe( 'table properties', () => {
 						assertTableStyle( editor, 'margin-left:auto;margin-right:0;' );
 					} );
 
-					it( 'should remove alignment from a selected table cell if no value is passed', () => {
+					it( 'should remove alignment from a selected table if no value is passed', () => {
 						setData( model, modelTable( [ [ '[]foo' ] ], { alignment: 'center' } ) );
 
 						command.execute();
@@ -123,7 +123,7 @@ describe( 'table properties', () => {
 				} );
 
 				describe( 'non-collapsed selection', () => {
-					it( 'should set selected table cell alignment to a passed value', () => {
+					it( 'should set selected table alignment to a passed value', () => {
 						setData( model, modelTable( [ [ '[foo]' ] ] ) );
 
 						command.execute( { value: 'right' } );
@@ -131,7 +131,7 @@ describe( 'table properties', () => {
 						assertTableStyle( editor, 'margin-left:auto;margin-right:0;' );
 					} );
 
-					it( 'should change selected table cell alignment to a passed value', () => {
+					it( 'should change selected table alignment to a passed value', () => {
 						setData( model, modelTable( [ [ '[foo]' ] ] ) );
 
 						command.execute( { value: 'right' } );
@@ -139,7 +139,7 @@ describe( 'table properties', () => {
 						assertTableStyle( editor, 'margin-left:auto;margin-right:0;' );
 					} );
 
-					it( 'should remove alignment from a selected table cell if no value is passed', () => {
+					it( 'should remove alignment from a selected table if no value is passed', () => {
 						setData( model, modelTable( [ [ '[foo]' ] ] ) );
 
 						command.execute();

+ 153 - 0
packages/ckeditor5-table/tests/tableproperties/commands/tablebackgroundcolorcommand.js

@@ -0,0 +1,153 @@
+/**
+ * @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 TableBackgroundColorCommand from '../../../src/tableproperties/commands/tablebackgroundcolorcommand';
+
+describe( 'table properties', () => {
+	describe( 'commands', () => {
+		describe( 'TableBackgroundColorCommand', () => {
+			let editor, model, command;
+
+			beforeEach( async () => {
+				editor = await ModelTestEditor.create( {
+					plugins: [ Paragraph, TablePropertiesEditing ]
+				} );
+
+				model = editor.model;
+				command = new TableBackgroundColorCommand( editor );
+			} );
+
+			afterEach( () => {
+				return editor.destroy();
+			} );
+
+			describe( 'isEnabled', () => {
+				describe( 'collapsed selection', () => {
+					it( 'should be false if selection does not have table', () => {
+						setData( model, '<paragraph>foo[]</paragraph>' );
+						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, '<paragraph>f[oo]</paragraph>' );
+						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 backgroundColor property', () => {
+						setData( model, modelTable( [ [ '[]foo' ] ] ) );
+
+						expect( command.value ).to.be.undefined;
+					} );
+
+					it( 'should be set if selected table has backgroundColor property', () => {
+						setData( model, modelTable( [ [ '[]foo' ] ], { backgroundColor: 'blue' } ) );
+
+						expect( command.value ).to.equal( 'blue' );
+					} );
+				} );
+
+				describe( 'non-collapsed selection', () => {
+					it( 'should be false if selection does not have table', () => {
+						setData( model, '<paragraph>f[oo]</paragraph>' );
+
+						expect( command.value ).to.be.undefined;
+					} );
+
+					it( 'should be true is selection has table', () => {
+						setData( model, modelTable( [ [ 'f[o]o' ] ], { backgroundColor: 'blue' } ) );
+
+						expect( command.value ).to.equal( 'blue' );
+					} );
+				} );
+			} );
+
+			describe( 'execute()', () => {
+				it( 'should use provided batch', () => {
+					setData( model, modelTable( [ [ 'foo[]' ] ] ) );
+					const batch = model.createBatch();
+					const spy = sinon.spy( model, 'enqueueChange' );
+
+					command.execute( { value: '#f00', batch } );
+					sinon.assert.calledWith( spy, batch );
+				} );
+
+				describe( 'collapsed selection', () => {
+					it( 'should set selected table backgroundColor to a passed value', () => {
+						setData( model, modelTable( [ [ 'foo[]' ] ] ) );
+
+						command.execute( { value: '#f00' } );
+
+						assertTableStyle( editor, 'background-color:#f00;' );
+					} );
+
+					it( 'should change selected table backgroundColor to a passed value', () => {
+						setData( model, modelTable( [ [ '[]foo' ] ], { backgroundColor: 'blue' } ) );
+
+						command.execute( { value: '#f00' } );
+
+						assertTableStyle( editor, 'background-color:#f00;' );
+					} );
+
+					it( 'should remove backgroundColor from a selected table if no value is passed', () => {
+						setData( model, modelTable( [ [ '[]foo' ] ], { backgroundColor: 'blue' } ) );
+
+						command.execute();
+
+						assertTableStyle( editor, '' );
+					} );
+				} );
+
+				describe( 'non-collapsed selection', () => {
+					it( 'should set selected table backgroundColor to a passed value', () => {
+						setData( model, modelTable( [ [ '[foo]' ] ] ) );
+
+						command.execute( { value: '#f00' } );
+
+						assertTableStyle( editor, 'background-color:#f00;' );
+					} );
+
+					it( 'should change selected table backgroundColor to a passed value', () => {
+						setData( model, modelTable( [ [ '[foo]' ] ] ) );
+
+						command.execute( { value: '#f00' } );
+
+						assertTableStyle( editor, 'background-color:#f00;' );
+					} );
+
+					it( 'should remove backgroundColor from a selected table if no value is passed', () => {
+						setData( model, modelTable( [ [ '[foo]' ] ] ) );
+
+						command.execute();
+
+						assertTableStyle( editor, '' );
+					} );
+				} );
+			} );
+		} );
+	} );
+} );

+ 14 - 14
packages/ckeditor5-table/tests/tableproperties/commands/tableheightcommand.js

@@ -32,24 +32,24 @@ describe( 'table properties', () => {
 
 			describe( 'isEnabled', () => {
 				describe( 'collapsed selection', () => {
-					it( 'should be false if selection does not have table cell', () => {
+					it( 'should be false if selection does not have table', () => {
 						setData( model, '<paragraph>foo[]</paragraph>' );
 						expect( command.isEnabled ).to.be.false;
 					} );
 
-					it( 'should be true is selection has table cell', () => {
+					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 cell', () => {
+					it( 'should be false if selection does not have table', () => {
 						setData( model, '<paragraph>f[oo]</paragraph>' );
 						expect( command.isEnabled ).to.be.false;
 					} );
 
-					it( 'should be true is selection has table cell', () => {
+					it( 'should be true is selection has table', () => {
 						setData( model, modelTable( [ [ 'f[o]o' ] ] ) );
 						expect( command.isEnabled ).to.be.true;
 					} );
@@ -58,13 +58,13 @@ describe( 'table properties', () => {
 
 			describe( 'value', () => {
 				describe( 'collapsed selection', () => {
-					it( 'should be undefined if selected table cell has no height property', () => {
+					it( 'should be undefined if selected table has no height property', () => {
 						setData( model, modelTable( [ [ '[]foo' ] ] ) );
 
 						expect( command.value ).to.be.undefined;
 					} );
 
-					it( 'should be set if selected table cell has height property', () => {
+					it( 'should be set if selected table has height property', () => {
 						setData( model, modelTable( [ [ '[]foo' ] ], { height: '100px' } ) );
 
 						expect( command.value ).to.equal( '100px' );
@@ -72,13 +72,13 @@ describe( 'table properties', () => {
 				} );
 
 				describe( 'non-collapsed selection', () => {
-					it( 'should be false if selection does not have table cell', () => {
+					it( 'should be false if selection does not have table', () => {
 						setData( model, '<paragraph>f[oo]</paragraph>' );
 
 						expect( command.value ).to.be.undefined;
 					} );
 
-					it( 'should be true is selection has table cell', () => {
+					it( 'should be true is selection has table', () => {
 						setData( model, modelTable( [ [ 'f[o]o' ] ], { height: '100px' } ) );
 
 						expect( command.value ).to.equal( '100px' );
@@ -97,7 +97,7 @@ describe( 'table properties', () => {
 				} );
 
 				describe( 'collapsed selection', () => {
-					it( 'should set selected table cell height to a passed value', () => {
+					it( 'should set selected table height to a passed value', () => {
 						setData( model, modelTable( [ [ 'foo[]' ] ] ) );
 
 						command.execute( { value: '25px' } );
@@ -105,7 +105,7 @@ describe( 'table properties', () => {
 						assertTableStyle( editor, 'height:25px;' );
 					} );
 
-					it( 'should change selected table cell height to a passed value', () => {
+					it( 'should change selected table height to a passed value', () => {
 						setData( model, modelTable( [ [ '[]foo' ] ], { height: '100px' } ) );
 
 						command.execute( { value: '25px' } );
@@ -113,7 +113,7 @@ describe( 'table properties', () => {
 						assertTableStyle( editor, 'height:25px;' );
 					} );
 
-					it( 'should remove height from a selected table cell if no value is passed', () => {
+					it( 'should remove height from a selected table if no value is passed', () => {
 						setData( model, modelTable( [ [ { height: '100px', contents: '[]foo' } ] ] ) );
 
 						command.execute();
@@ -123,7 +123,7 @@ describe( 'table properties', () => {
 				} );
 
 				describe( 'non-collapsed selection', () => {
-					it( 'should set selected table cell height to a passed value', () => {
+					it( 'should set selected table height to a passed value', () => {
 						setData( model, modelTable( [ [ '[foo]' ] ] ) );
 
 						command.execute( { value: '25px' } );
@@ -131,7 +131,7 @@ describe( 'table properties', () => {
 						assertTableStyle( editor, 'height:25px;' );
 					} );
 
-					it( 'should change selected table cell height to a passed value', () => {
+					it( 'should change selected table height to a passed value', () => {
 						setData( model, modelTable( [ [ '[foo]' ] ] ) );
 
 						command.execute( { value: '25px' } );
@@ -139,7 +139,7 @@ describe( 'table properties', () => {
 						assertTableStyle( editor, 'height:25px;' );
 					} );
 
-					it( 'should remove height from a selected table cell if no value is passed', () => {
+					it( 'should remove height from a selected table if no value is passed', () => {
 						setData( model, modelTable( [ [ '[foo]' ] ] ) );
 
 						command.execute();

+ 14 - 14
packages/ckeditor5-table/tests/tableproperties/commands/tablewidthcommand.js

@@ -32,24 +32,24 @@ describe( 'table properties', () => {
 
 			describe( 'isEnabled', () => {
 				describe( 'collapsed selection', () => {
-					it( 'should be false if selection does not have table cell', () => {
+					it( 'should be false if selection does not have table', () => {
 						setData( model, '<paragraph>foo[]</paragraph>' );
 						expect( command.isEnabled ).to.be.false;
 					} );
 
-					it( 'should be true is selection has table cell', () => {
+					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 cell', () => {
+					it( 'should be false if selection does not have table', () => {
 						setData( model, '<paragraph>f[oo]</paragraph>' );
 						expect( command.isEnabled ).to.be.false;
 					} );
 
-					it( 'should be true is selection has table cell', () => {
+					it( 'should be true is selection has table', () => {
 						setData( model, modelTable( [ [ 'f[o]o' ] ] ) );
 						expect( command.isEnabled ).to.be.true;
 					} );
@@ -58,13 +58,13 @@ describe( 'table properties', () => {
 
 			describe( 'value', () => {
 				describe( 'collapsed selection', () => {
-					it( 'should be undefined if selected table cell has no width property', () => {
+					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 cell has width property', () => {
+					it( 'should be set if selected table has width property', () => {
 						setData( model, modelTable( [ [ '[]foo' ] ], { width: '100px' } ) );
 
 						expect( command.value ).to.equal( '100px' );
@@ -72,13 +72,13 @@ describe( 'table properties', () => {
 				} );
 
 				describe( 'non-collapsed selection', () => {
-					it( 'should be false if selection does not have table cell', () => {
+					it( 'should be false if selection does not have table', () => {
 						setData( model, '<paragraph>f[oo]</paragraph>' );
 
 						expect( command.value ).to.be.undefined;
 					} );
 
-					it( 'should be true is selection has table cell', () => {
+					it( 'should be true is selection has table', () => {
 						setData( model, modelTable( [ [ 'f[o]o' ] ], { width: '100px' } ) );
 
 						expect( command.value ).to.equal( '100px' );
@@ -97,7 +97,7 @@ describe( 'table properties', () => {
 				} );
 
 				describe( 'collapsed selection', () => {
-					it( 'should set selected table cell width to a passed value', () => {
+					it( 'should set selected table width to a passed value', () => {
 						setData( model, modelTable( [ [ 'foo[]' ] ] ) );
 
 						command.execute( { value: '25px' } );
@@ -105,7 +105,7 @@ describe( 'table properties', () => {
 						assertTableStyle( editor, 'width:25px;' );
 					} );
 
-					it( 'should change selected table cell width to a passed value', () => {
+					it( 'should change selected table width to a passed value', () => {
 						setData( model, modelTable( [ [ '[]foo' ] ], { width: '100px' } ) );
 
 						command.execute( { value: '25px' } );
@@ -113,7 +113,7 @@ describe( 'table properties', () => {
 						assertTableStyle( editor, 'width:25px;' );
 					} );
 
-					it( 'should remove width from a selected table cell if no value is passed', () => {
+					it( 'should remove width from a selected table if no value is passed', () => {
 						setData( model, modelTable( [ [ '[]foo' ] ], { width: '100px' } ) );
 
 						command.execute();
@@ -123,7 +123,7 @@ describe( 'table properties', () => {
 				} );
 
 				describe( 'non-collapsed selection', () => {
-					it( 'should set selected table cell width to a passed value', () => {
+					it( 'should set selected table width to a passed value', () => {
 						setData( model, modelTable( [ [ '[foo]' ] ] ) );
 
 						command.execute( { value: '25px' } );
@@ -131,7 +131,7 @@ describe( 'table properties', () => {
 						assertTableStyle( editor, 'width:25px;' );
 					} );
 
-					it( 'should change selected table cell width to a passed value', () => {
+					it( 'should change selected table width to a passed value', () => {
 						setData( model, modelTable( [ [ '[foo]' ] ] ) );
 
 						command.execute( { value: '25px' } );
@@ -139,7 +139,7 @@ describe( 'table properties', () => {
 						assertTableStyle( editor, 'width:25px;' );
 					} );
 
-					it( 'should remove width from a selected table cell if no value is passed', () => {
+					it( 'should remove width from a selected table if no value is passed', () => {
 						setData( model, modelTable( [ [ '[foo]' ] ] ) );
 
 						command.execute();