Parcourir la source

Tests: Refactor Font commands tests.

Maciej Gołaszewski il y a 8 ans
Parent
commit
d786fe2af0

+ 143 - 0
packages/ckeditor5-font/tests/fontcommand.js

@@ -0,0 +1,143 @@
+/**
+ * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
+ * For licensing, see LICENSE.md.
+ */
+
+import FontCommand from '../src/fontcommand';
+
+import Command from '@ckeditor/ckeditor5-core/src/command';
+import ModelTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/modeltesteditor';
+import { getData, setData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
+
+describe( 'FontCommand', () => {
+	let editor, model, command;
+
+	beforeEach( () => {
+		return ModelTestEditor.create()
+			.then( newEditor => {
+				editor = newEditor;
+				model = editor.model;
+
+				command = new FontCommand( editor, 'font' );
+				editor.commands.add( 'font', command );
+
+				model.schema.registerItem( 'paragraph', '$block' );
+				model.schema.allow( { name: '$inline', attributes: 'font', inside: '$block' } );
+			} );
+	} );
+
+	afterEach( () => {
+		editor.destroy();
+	} );
+
+	it( 'is a command', () => {
+		expect( FontCommand.prototype ).to.be.instanceOf( Command );
+		expect( command ).to.be.instanceOf( Command );
+	} );
+
+	describe( 'value', () => {
+		it( 'is set to font value when selection is in text with font attribute', () => {
+			setData( model, '<paragraph><$text font="foo">fo[]o</$text></paragraph>' );
+
+			expect( command ).to.have.property( 'value', 'foo' );
+		} );
+
+		it( 'is undefined when selection is not in text with font attribute', () => {
+			setData( model, '<paragraph>fo[]o</paragraph>' );
+
+			expect( command ).to.have.property( 'value', undefined );
+		} );
+	} );
+
+	describe( 'isEnabled', () => {
+		it( 'is true when selection is on text which can have font added', () => {
+			setData( model, '<paragraph>fo[]o</paragraph>' );
+
+			expect( command ).to.have.property( 'isEnabled', true );
+		} );
+	} );
+
+	describe( 'execute()', () => {
+		it( 'should add font attribute on selected text', () => {
+			setData( model, '<paragraph>a[bc<$text font="foo">fo]obar</$text>xyz</paragraph>' );
+
+			expect( command.value ).to.be.undefined;
+
+			command.execute( { value: 'foo' } );
+
+			expect( command.value ).to.equal( 'foo' );
+
+			expect( getData( model ) ).to.equal( '<paragraph>a[<$text font="foo">bcfo]obar</$text>xyz</paragraph>' );
+		} );
+
+		it( 'should add font attribute on selected nodes (multiple nodes)', () => {
+			setData(
+				model,
+				'<paragraph>abcabc[abc</paragraph>' +
+				'<paragraph>foofoofoo</paragraph>' +
+				'<paragraph>barbar]bar</paragraph>'
+			);
+
+			command.execute( { value: 'foo' } );
+
+			expect( command.value ).to.equal( 'foo' );
+
+			expect( getData( model ) ).to.equal(
+				'<paragraph>abcabc[<$text font="foo">abc</$text></paragraph>' +
+				'<paragraph><$text font="foo">foofoofoo</$text></paragraph>' +
+				'<paragraph><$text font="foo">barbar</$text>]bar</paragraph>'
+			);
+		} );
+
+		it( 'should change font attribute on selected nodes', () => {
+			setData(
+				model,
+				'<paragraph>abc[abc<$text font="text-small">abc</$text></paragraph>' +
+				'<paragraph><$text font="text-small">foofoofoo</$text></paragraph>' +
+				'<paragraph><$text font="text-small">bar]bar</$text>bar</paragraph>'
+			);
+
+			command.execute( { value: 'foo' } );
+
+			expect( command.value ).to.equal( 'foo' );
+
+			expect( getData( model ) ).to.equal(
+				'<paragraph>abc[<$text font="foo">abcabc</$text></paragraph>' +
+				'<paragraph><$text font="foo">foofoofoo</$text></paragraph>' +
+				'<paragraph><$text font="foo">bar</$text>]<$text font="text-small">bar</$text>bar</paragraph>'
+			);
+		} );
+
+		it( 'should do nothing on collapsed range', () => {
+			setData( model, '<paragraph>abc<$text font="foo">foo[]bar</$text>xyz</paragraph>' );
+
+			expect( command.value ).to.equal( 'foo' );
+
+			command.execute( { value: 'foo' } );
+
+			expect( getData( model ) ).to.equal( '<paragraph>abc<$text font="foo">foo[]bar</$text>xyz</paragraph>' );
+
+			expect( command.value ).to.equal( 'foo' );
+		} );
+
+		it( 'should remove font attribute on selected nodes when passing undefined font param', () => {
+			setData(
+				model,
+				'<paragraph>abcabc[<$text font="foo">abc</$text></paragraph>' +
+				'<paragraph><$text font="foo">foofoofoo</$text></paragraph>' +
+				'<paragraph><$text font="foo">barbar</$text>]bar</paragraph>'
+			);
+			expect( command.value ).to.equal( 'foo' );
+
+			command.execute();
+
+			expect( command.value ).to.be.undefined;
+
+			expect( getData( model ) ).to.equal(
+				'<paragraph>abcabc[abc</paragraph>' +
+				'<paragraph>foofoofoo</paragraph>' +
+				'<paragraph>barbar]bar</paragraph>'
+			);
+		} );
+	} );
+} );

+ 7 - 115
packages/ckeditor5-font/tests/fontfamily/fontfamilycommand.js

@@ -4,25 +4,19 @@
  */
 
 import FontFamilyCommand from '../../src/fontfamily/fontfamilycommand';
+import FontCommand from '../../src/fontcommand';
 
-import Command from '@ckeditor/ckeditor5-core/src/command';
 import ModelTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/modeltesteditor';
-import { getData, setData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
 
 describe( 'FontFamilyCommand', () => {
-	let editor, model, command;
+	let editor, command;
 
 	beforeEach( () => {
 		return ModelTestEditor.create()
 			.then( newEditor => {
 				editor = newEditor;
-				model = editor.model;
 
 				command = new FontFamilyCommand( editor );
-				editor.commands.add( 'fontFamily', command );
-
-				model.schema.registerItem( 'paragraph', '$block' );
-				model.schema.allow( { name: '$inline', attributes: 'fontFamily', inside: '$block' } );
 			} );
 	} );
 
@@ -30,114 +24,12 @@ describe( 'FontFamilyCommand', () => {
 		editor.destroy();
 	} );
 
-	it( 'is a command', () => {
-		expect( FontFamilyCommand.prototype ).to.be.instanceOf( Command );
-		expect( command ).to.be.instanceOf( Command );
-	} );
-
-	describe( 'value', () => {
-		it( 'is set to fontFamily value when selection is in text with fontFamily attribute', () => {
-			setData( model, '<paragraph><$text fontFamily="arial">fo[]o</$text></paragraph>' );
-
-			expect( command ).to.have.property( 'value', 'arial' );
-		} );
-
-		it( 'is undefined when selection is not in text with fontFamily attribute', () => {
-			setData( model, '<paragraph>fo[]o</paragraph>' );
-
-			expect( command ).to.have.property( 'value', undefined );
-		} );
+	it( 'is a FontCommand', () => {
+		expect( FontFamilyCommand.prototype ).to.be.instanceOf( FontCommand );
+		expect( command ).to.be.instanceOf( FontCommand );
 	} );
 
-	describe( 'isEnabled', () => {
-		it( 'is true when selection is on text which can have fontFamily added', () => {
-			setData( model, '<paragraph>fo[]o</paragraph>' );
-
-			expect( command ).to.have.property( 'isEnabled', true );
-		} );
-	} );
-
-	describe( 'execute()', () => {
-		it( 'should add fontFamily attribute on selected text', () => {
-			setData( model, '<paragraph>a[bc<$text fontFamily="arial">fo]obar</$text>xyz</paragraph>' );
-
-			expect( command.value ).to.be.undefined;
-
-			command.execute( { value: 'arial' } );
-
-			expect( command.value ).to.equal( 'arial' );
-
-			expect( getData( model ) ).to.equal( '<paragraph>a[<$text fontFamily="arial">bcfo]obar</$text>xyz</paragraph>' );
-		} );
-
-		it( 'should add fontFamily attribute on selected nodes (multiple nodes)', () => {
-			setData(
-				model,
-				'<paragraph>abcabc[abc</paragraph>' +
-				'<paragraph>foofoofoo</paragraph>' +
-				'<paragraph>barbar]bar</paragraph>'
-			);
-
-			command.execute( { value: 'arial' } );
-
-			expect( command.value ).to.equal( 'arial' );
-
-			expect( getData( model ) ).to.equal(
-				'<paragraph>abcabc[<$text fontFamily="arial">abc</$text></paragraph>' +
-				'<paragraph><$text fontFamily="arial">foofoofoo</$text></paragraph>' +
-				'<paragraph><$text fontFamily="arial">barbar</$text>]bar</paragraph>'
-			);
-		} );
-
-		it( 'should change fontFamily attribute on selected nodes', () => {
-			setData(
-				model,
-				'<paragraph>abc[abc<$text fontFamily="text-small">abc</$text></paragraph>' +
-				'<paragraph><$text fontFamily="text-small">foofoofoo</$text></paragraph>' +
-				'<paragraph><$text fontFamily="text-small">bar]bar</$text>bar</paragraph>'
-			);
-
-			command.execute( { value: 'arial' } );
-
-			expect( command.value ).to.equal( 'arial' );
-
-			expect( getData( model ) ).to.equal(
-				'<paragraph>abc[<$text fontFamily="arial">abcabc</$text></paragraph>' +
-				'<paragraph><$text fontFamily="arial">foofoofoo</$text></paragraph>' +
-				'<paragraph><$text fontFamily="arial">bar</$text>]<$text fontFamily="text-small">bar</$text>bar</paragraph>'
-			);
-		} );
-
-		it( 'should do nothing on collapsed range', () => {
-			setData( model, '<paragraph>abc<$text fontFamily="arial">foo[]bar</$text>xyz</paragraph>' );
-
-			expect( command.value ).to.equal( 'arial' );
-
-			command.execute( { value: 'arial' } );
-
-			expect( getData( model ) ).to.equal( '<paragraph>abc<$text fontFamily="arial">foo[]bar</$text>xyz</paragraph>' );
-
-			expect( command.value ).to.equal( 'arial' );
-		} );
-
-		it( 'should remove fontFamily attribute on selected nodes when passing undefined fontFamily param', () => {
-			setData(
-				model,
-				'<paragraph>abcabc[<$text fontFamily="arial">abc</$text></paragraph>' +
-				'<paragraph><$text fontFamily="arial">foofoofoo</$text></paragraph>' +
-				'<paragraph><$text fontFamily="arial">barbar</$text>]bar</paragraph>'
-			);
-			expect( command.value ).to.equal( 'arial' );
-
-			command.execute();
-
-			expect( command.value ).to.be.undefined;
-
-			expect( getData( model ) ).to.equal(
-				'<paragraph>abcabc[abc</paragraph>' +
-				'<paragraph>foofoofoo</paragraph>' +
-				'<paragraph>barbar]bar</paragraph>'
-			);
-		} );
+	it( 'opeartes on fontFamily attribute', () => {
+		expect( command ).to.have.property( 'attribute', 'fontFamily' );
 	} );
 } );

+ 7 - 115
packages/ckeditor5-font/tests/fontsize/fontsizecommand.js

@@ -4,25 +4,19 @@
  */
 
 import FontSizeCommand from '../../src/fontsize/fontsizecommand';
+import FontCommand from '../../src/fontcommand';
 
-import Command from '@ckeditor/ckeditor5-core/src/command';
 import ModelTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/modeltesteditor';
-import { getData, setData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
 
 describe( 'FontSizeCommand', () => {
-	let editor, model, command;
+	let editor, command;
 
 	beforeEach( () => {
 		return ModelTestEditor.create()
 			.then( newEditor => {
 				editor = newEditor;
-				model = editor.model;
 
 				command = new FontSizeCommand( editor );
-				editor.commands.add( 'fontSize', command );
-
-				model.schema.registerItem( 'paragraph', '$block' );
-				model.schema.allow( { name: '$inline', attributes: 'fontSize', inside: '$block' } );
 			} );
 	} );
 
@@ -30,114 +24,12 @@ describe( 'FontSizeCommand', () => {
 		editor.destroy();
 	} );
 
-	it( 'is a command', () => {
-		expect( FontSizeCommand.prototype ).to.be.instanceOf( Command );
-		expect( command ).to.be.instanceOf( Command );
-	} );
-
-	describe( 'value', () => {
-		it( 'is set to fontSize value when selection is in text with fontSize attribute', () => {
-			setData( model, '<paragraph><$text fontSize="huge">fo[]o</$text></paragraph>' );
-
-			expect( command ).to.have.property( 'value', 'huge' );
-		} );
-
-		it( 'is undefined when selection is not in text with fontSize attribute', () => {
-			setData( model, '<paragraph>fo[]o</paragraph>' );
-
-			expect( command ).to.have.property( 'value', undefined );
-		} );
+	it( 'is a FontCommand', () => {
+		expect( FontSizeCommand.prototype ).to.be.instanceOf( FontCommand );
+		expect( command ).to.be.instanceOf( FontCommand );
 	} );
 
-	describe( 'isEnabled', () => {
-		it( 'is true when selection is on text which can have fontSize added', () => {
-			setData( model, '<paragraph>fo[]o</paragraph>' );
-
-			expect( command ).to.have.property( 'isEnabled', true );
-		} );
-	} );
-
-	describe( 'execute()', () => {
-		it( 'should add fontSize attribute on selected text', () => {
-			setData( model, '<paragraph>a[bc<$text fontSize="huge">fo]obar</$text>xyz</paragraph>' );
-
-			expect( command.value ).to.be.undefined;
-
-			command.execute( { value: 'huge' } );
-
-			expect( command.value ).to.equal( 'huge' );
-
-			expect( getData( model ) ).to.equal( '<paragraph>a[<$text fontSize="huge">bcfo]obar</$text>xyz</paragraph>' );
-		} );
-
-		it( 'should add fontSize attribute on selected nodes (multiple nodes)', () => {
-			setData(
-				model,
-				'<paragraph>abcabc[abc</paragraph>' +
-				'<paragraph>foofoofoo</paragraph>' +
-				'<paragraph>barbar]bar</paragraph>'
-			);
-
-			command.execute( { value: 'huge' } );
-
-			expect( command.value ).to.equal( 'huge' );
-
-			expect( getData( model ) ).to.equal(
-				'<paragraph>abcabc[<$text fontSize="huge">abc</$text></paragraph>' +
-				'<paragraph><$text fontSize="huge">foofoofoo</$text></paragraph>' +
-				'<paragraph><$text fontSize="huge">barbar</$text>]bar</paragraph>'
-			);
-		} );
-
-		it( 'should change fontSize attribute on selected nodes', () => {
-			setData(
-				model,
-				'<paragraph>abc[abc<$text fontSize="text-small">abc</$text></paragraph>' +
-				'<paragraph><$text fontSize="text-small">foofoofoo</$text></paragraph>' +
-				'<paragraph><$text fontSize="text-small">bar]bar</$text>bar</paragraph>'
-			);
-
-			command.execute( { value: 'huge' } );
-
-			expect( command.value ).to.equal( 'huge' );
-
-			expect( getData( model ) ).to.equal(
-				'<paragraph>abc[<$text fontSize="huge">abcabc</$text></paragraph>' +
-				'<paragraph><$text fontSize="huge">foofoofoo</$text></paragraph>' +
-				'<paragraph><$text fontSize="huge">bar</$text>]<$text fontSize="text-small">bar</$text>bar</paragraph>'
-			);
-		} );
-
-		it( 'should do nothing on collapsed range', () => {
-			setData( model, '<paragraph>abc<$text fontSize="huge">foo[]bar</$text>xyz</paragraph>' );
-
-			expect( command.value ).to.equal( 'huge' );
-
-			command.execute();
-
-			expect( getData( model ) ).to.equal( '<paragraph>abc<$text fontSize="huge">foo[]bar</$text>xyz</paragraph>' );
-
-			expect( command.value ).to.equal( 'huge' );
-		} );
-
-		it( 'should remove fontSize attribute on selected nodes when passing undefined fontSize param', () => {
-			setData(
-				model,
-				'<paragraph>abcabc[<$text fontSize="huge">abc</$text></paragraph>' +
-				'<paragraph><$text fontSize="huge">foofoofoo</$text></paragraph>' +
-				'<paragraph><$text fontSize="huge">barbar</$text>]bar</paragraph>'
-			);
-			expect( command.value ).to.equal( 'huge' );
-
-			command.execute();
-
-			expect( command.value ).to.be.undefined;
-
-			expect( getData( model ) ).to.equal(
-				'<paragraph>abcabc[abc</paragraph>' +
-				'<paragraph>foofoofoo</paragraph>' +
-				'<paragraph>barbar]bar</paragraph>'
-			);
-		} );
+	it( 'opeartes on fontSize attribute', () => {
+		expect( command ).to.have.property( 'attribute', 'fontSize' );
 	} );
 } );