Kaynağa Gözat

Tests: Added RemoveFormatCommand#execute tests. Also first steps to reuse code for both suites.

I'd like isEnable and execute() tests to be run by a helper function, as the markup will be very similar in both cases, but with different assertions.
Marek Lewandowski 6 yıl önce
ebeveyn
işleme
47c3e2307c

+ 103 - 29
packages/ckeditor5-remove-format/tests/removeformatcommand.js

@@ -7,7 +7,10 @@ import RemoveFormatCommand from '../src/removeformatcommand';
 
 import Command from '@ckeditor/ckeditor5-core/src/command';
 import ModelTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/modeltesteditor';
-import { setData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
+import {
+	getData,
+	setData
+} from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
 
 describe( 'RemoveFormatCommand', () => {
 	let editor, model, command;
@@ -37,34 +40,105 @@ describe( 'RemoveFormatCommand', () => {
 	} );
 
 	describe( 'isEnabled', () => {
-		it( 'state when in non-formatting markup', () => {
-			setData( model, '<p>fo[]o</p>' );
-
-			expect( command ).to.have.property( 'isEnabled', false );
-		} );
-
-		it( 'state with collapsed selection in formatting markup', () => {
-			setData( model, '<p>f<$text bold="true">o[]o</$text></p>' );
-
-			expect( command ).to.have.property( 'isEnabled', true );
-		} );
-
-		it( 'state with selection containing formatting in the middle', () => {
-			setData( model, '<p>f[oo <$text bold="true">bar</$text> ba]z</p>' );
-
-			expect( command ).to.have.property( 'isEnabled', true );
-		} );
-
-		it( 'state with partially selected formatting at the start', () => {
-			setData( model, '<p><$text bold="true">b[ar</$text> ba]z</p>' );
-
-			expect( command ).to.have.property( 'isEnabled', true );
-		} );
-
-		it( 'state with partially selected formatting at the end', () => {
-			setData( model, '<p>f[oo <$text bold="true">ba]z</$text></p>' );
+		const expectEnabledPropertyToBe = expectedValue => expect( command ).to.have.property( 'isEnabled', expectedValue );
+		const cases = {
+			'state when in non-formatting markup': {
+				input: '<p>fo[]o</p>',
+				assert: () => expectEnabledPropertyToBe( false )
+			},
+
+			'state with collapsed selection in formatting markup': {
+				input: '<p>f<$text bold="true">o[]o</$text></p>',
+				assert: () => expectEnabledPropertyToBe( true )
+			},
+
+			'state with selection containing formatting in the middle': {
+				input: '<p>f[oo <$text bold="true">bar</$text> ba]z</p>',
+				assert: () => expectEnabledPropertyToBe( true )
+			},
+
+			'state with partially selected formatting at the start': {
+				input: '<p><$text bold="true">b[ar</$text> ba]z</p>',
+				assert: () => expectEnabledPropertyToBe( true )
+			},
+
+			'state with partially selected formatting at the end': {
+				input: '<p>f[oo <$text bold="true">ba]z</$text></p>',
+				assert: () => expectEnabledPropertyToBe( true )
+			},
+
+			'state with formatted selection alone': {
+				input: '<p>fo[]o</p>',
+				setDataOptions: {
+					selectionAttributes: {
+						bold: true
+					}
+				},
+				assert: () => expectEnabledPropertyToBe( true )
+			}
+		};
+
+		for ( const [ key, testConfig ] of Object.entries( cases ) ) {
+			it( key, () => {
+				setData( model, testConfig.input, testConfig.setDataOptions );
+
+				testConfig.assert();
+			} );
+		}
+	} );
 
-			expect( command ).to.have.property( 'isEnabled', true );
-		} );
+	describe( 'execute()', () => {
+		const cases = {
+			'state when in non-formatting markup': {
+				input: '<p>fo[]o</p>',
+				assert: '<p>fo[]o</p>'
+			},
+
+			'state with collapsed selection in formatting markup': {
+				input: '<p>f<$text bold="true">o[]o</$text></p>',
+				assert: '<p>f<$text bold="true">o[]o</$text></p>'
+			},
+
+			'state with selection containing formatting in the middle': {
+				input: '<p>f[oo <$text bold="true">bar</$text> ba]z</p>',
+				assert: '<p>f[oo bar ba]z</p>'
+			},
+
+			'state with partially selected formatting at the start': {
+				input: '<p><$text bold="true">b[ar</$text> ba]z</p>',
+				assert: '<p><$text bold="true">b</$text>[ar ba]z</p>',
+			},
+
+			'state with partially selected formatting at the end': {
+				input: '<p>f[oo <$text bold="true">ba]z</$text></p>',
+				assert: '<p>f[oo ba]<$text bold="true">z</$text></p>'
+			},
+
+			'state with formatted selection alone': {
+				input: '<p>fo[]o</p>',
+				setDataOptions: {
+					selectionAttributes: {
+						bold: true
+					}
+				},
+				assert: () => {
+					expect( model.document.selection.hasAttribute( 'bold' ) ).to.equal( false );
+				}
+			}
+		};
+
+		for ( const [ key, testConfig ] of Object.entries( cases ) ) {
+			it( key, () => {
+				setData( model, testConfig.input, testConfig.setDataOptions );
+
+				command.execute();
+
+				if ( typeof testConfig.assert == 'string' ) {
+					expect( getData( model ) ).to.equal( testConfig.assert );
+				} else {
+					testConfig.assert();
+				}
+			} );
+		}
 	} );
 } );