8
0
Просмотр исходного кода

Add tests for offset calculation.

Maciej Gołaszewski 6 лет назад
Родитель
Сommit
8fe8067997

+ 6 - 0
packages/ckeditor5-indent/src/indentblockcommand.js

@@ -75,6 +75,12 @@ export default class IndentBlockCommand extends Command {
 
 
 	_getIndentOffset( currentIndent ) {
 	_getIndentOffset( currentIndent ) {
 		const currentOffset = parseFloat( currentIndent || 0 );
 		const currentOffset = parseFloat( currentIndent || 0 );
+		const isSameUnit = !currentIndent || currentIndent.endsWith( this.unit );
+
+		if ( !isSameUnit ) {
+			return this.direction > 0 ? this.offset + this.unit : undefined;
+		}
+
 		const offsetToSet = currentOffset + this.direction * this.offset;
 		const offsetToSet = currentOffset + this.direction * this.offset;
 
 
 		return offsetToSet && offsetToSet > 0 ? offsetToSet + this.unit : undefined;
 		return offsetToSet && offsetToSet > 0 ? offsetToSet + this.unit : undefined;

+ 50 - 2
packages/ckeditor5-indent/tests/indentblockcommand.js

@@ -114,7 +114,31 @@ describe( 'IndentBlockCommand', () => {
 				} );
 				} );
 			} );
 			} );
 
 
-			describe( 'execute()', () => {} );
+			describe( 'execute()', () => {
+				it( 'should set first offset for non-indented block', () => {
+					setData( model, '<paragraph>f[]oo</paragraph>' );
+					command.execute();
+					expect( getData( model ) ).to.equal( '<paragraph indent="50px">f[]oo</paragraph>' );
+				} );
+
+				it( 'should calculate next offset for indented block', () => {
+					setData( model, '<paragraph indent="100px">f[]oo</paragraph>' );
+					command.execute();
+					expect( getData( model ) ).to.equal( '<paragraph indent="150px">f[]oo</paragraph>' );
+				} );
+
+				it( 'should calculate next offset for indented block even if current indent is not tied to offset', () => {
+					setData( model, '<paragraph indent="27px">f[]oo</paragraph>' );
+					command.execute();
+					expect( getData( model ) ).to.equal( '<paragraph indent="77px">f[]oo</paragraph>' );
+				} );
+
+				it( 'should set first offset if current indent has different unit', () => {
+					setData( model, '<paragraph indent="3mm">f[]oo</paragraph>' );
+					command.execute();
+					expect( getData( model ) ).to.equal( '<paragraph indent="50px">f[]oo</paragraph>' );
+				} );
+			} );
 		} );
 		} );
 	} );
 	} );
 
 
@@ -194,7 +218,31 @@ describe( 'IndentBlockCommand', () => {
 				} );
 				} );
 			} );
 			} );
 
 
-			describe( 'execute()', () => {} );
+			describe( 'execute()', () => {
+				it( 'should set remove offset if indent is on first offset', () => {
+					setData( model, '<paragraph indent="50px">f[]oo</paragraph>' );
+					command.execute();
+					expect( getData( model ) ).to.equal( '<paragraph>f[]oo</paragraph>' );
+				} );
+
+				it( 'should calculate next offset for indented block', () => {
+					setData( model, '<paragraph indent="100px">f[]oo</paragraph>' );
+					command.execute();
+					expect( getData( model ) ).to.equal( '<paragraph indent="50px">f[]oo</paragraph>' );
+				} );
+
+				it( 'should calculate next offset for indented block even if current indent is not tied to offset', () => {
+					setData( model, '<paragraph indent="92px">f[]oo</paragraph>' );
+					command.execute();
+					expect( getData( model ) ).to.equal( '<paragraph indent="42px">f[]oo</paragraph>' );
+				} );
+
+				it( 'should remove offset if current indent has different unit', () => {
+					setData( model, '<paragraph indent="3mm">f[]oo</paragraph>' );
+					command.execute();
+					expect( getData( model ) ).to.equal( '<paragraph>f[]oo</paragraph>' );
+				} );
+			} );
 		} );
 		} );
 	} );
 	} );
 } );
 } );