Explorar o código

Added tests for treeView.Position#getShiftedBy and createFromPosition methods.

Szymon Kupś %!s(int64=9) %!d(string=hai) anos
pai
achega
414872d407
Modificáronse 1 ficheiros con 36 adicións e 2 borrados
  1. 36 2
      packages/ckeditor5-engine/tests/treeview/position.js

+ 36 - 2
packages/ckeditor5-engine/tests/treeview/position.js

@@ -10,13 +10,47 @@
 import Position from '/ckeditor5/core/treeview/position.js';
 
 describe( 'Position', () => {
+	const parentMock = {};
+
 	describe( 'constructor', () => {
 		it( 'should create element without attributes', () => {
-			const parentMock = {};
 			const elem = new Position( parentMock, 5 );
 
 			expect( elem ).to.have.property( 'parent' ).that.equals( parentMock );
 			expect( elem ).to.have.property( 'offset' ).that.equals( 5 );
 		} );
 	} );
-} );
+
+	describe( 'getShiftedBy', () => {
+		it( 'returns new instance with shifted offset', () => {
+			const position = new Position( parentMock, 10 );
+			const shifted = position.getShiftedBy( 12 );
+			expect( shifted.offset ).to.equal( 22 );
+		} );
+
+		it( 'accepts negative values', () => {
+			const position = new Position( parentMock, 10 );
+			const shifted = position.getShiftedBy( -5 );
+			expect( shifted.offset ).to.equal( 5 );
+		} );
+
+		it( 'prevents offset to be a negative value', () => {
+			const position = new Position( parentMock, 10 );
+			const shifted = position.getShiftedBy( -20 );
+
+			expect( shifted.offset ).to.equal( 0 );
+		} );
+	} );
+
+	describe( 'createFromPosition', () => {
+		it( 'creates new Position with same parent and offset', () => {
+			const offset = 50;
+			const position = new Position( parentMock, offset );
+			const newPosition = Position.createFromPosition( position );
+
+			expect( position ).to.not.equal( newPosition );
+			expect( position.offset ).to.equal( offset );
+			expect( position.parent ).to.equal( parentMock );
+		} );
+	} );
+} );