Преглед изворни кода

Tests: Added tests for new keyboard helpers.

Aleksander Nowodzinski пре 5 година
родитељ
комит
7e98beef84
2 измењених фајлова са 116 додато и 9 уклоњено
  1. 8 8
      packages/ckeditor5-utils/src/keyboard.js
  2. 108 1
      packages/ckeditor5-utils/tests/keyboard.js

+ 8 - 8
packages/ckeditor5-utils/src/keyboard.js

@@ -143,16 +143,16 @@ export function isArrowKeyCode( keyCode ) {
 }
 
 /**
- * Returns the direction from the provided key code considering the language direction of the
- * editor content.
+ * Returns the direction in which the {@link module:engine/model/documentselection~DocumentSelection selection}
+ * will move when a provided arrow key code is pressed considering the language direction of the editor content.
  *
- * For instance, in right–to–left (RTL) languages, pressing the left arrow means moving selection right (forward)
+ * For instance, in right–to–left (RTL) content languages, pressing the left arrow means moving selection right (forward)
  * in the model structure. Similarly, pressing the right arrow moves the selection left (backward).
  *
  * @param {Number} keyCode
- * @param {String} contentLanguageDirection The content language direction, corresponding to
+ * @param {'ltr'|'rtl'} contentLanguageDirection The content language direction, corresponding to
  * {@link module:utils/locale~Locale#contentLanguageDirection}.
- * @returns {'left'|'up'|'right'|'down'} Arrow direction.
+ * @returns {'left'|'up'|'right'|'down'} Localized arrow direction.
  */
 export function getLocalizedArrowKeyCodeDirection( keyCode, contentLanguageDirection ) {
 	const isLtrContent = contentLanguageDirection === 'ltr';
@@ -173,14 +173,14 @@ export function getLocalizedArrowKeyCodeDirection( keyCode, contentLanguageDirec
 }
 
 /**
- * Determines if the provided key code moves the selection forward or backward considering
- * the language direction of the editor content.
+ * Determines if the provided key code moves the {@link module:engine/model/documentselection~DocumentSelection selection}
+ * forward or backward considering the language direction of the editor content.
  *
  * For instance, in right–to–left (RTL) languages, pressing the left arrow means moving forward
  * in the model structure. Similarly, pressing the right arrow moves the selection backward.
  *
  * @param {Number} keyCode
- * @param {String} contentLanguageDirection The content language direction, corresponding to
+ * @param {'ltr'|'rtl'} contentLanguageDirection The content language direction, corresponding to
  * {@link module:utils/locale~Locale#contentLanguageDirection}.
  * @returns {Boolean}
  */

+ 108 - 1
packages/ckeditor5-utils/tests/keyboard.js

@@ -4,7 +4,15 @@
  */
 
 import env from '../src/env';
-import { keyCodes, getCode, parseKeystroke, getEnvKeystrokeText } from '../src/keyboard';
+import {
+	keyCodes,
+	getCode,
+	parseKeystroke,
+	getEnvKeystrokeText,
+	isArrowKeyCode,
+	isForwardArrowKeyCode,
+	getLocalizedArrowKeyCodeDirection
+} from '../src/keyboard';
 import { expectToThrowCKEditorError } from './_utils/utils';
 
 describe( 'Keyboard', () => {
@@ -160,4 +168,103 @@ describe( 'Keyboard', () => {
 			} );
 		} );
 	} );
+
+	describe( 'isArrowKeyCode()', () => {
+		it( 'should return "true" for right arrow', () => {
+			expect( isArrowKeyCode( keyCodes.arrowright ) ).to.be.true;
+		} );
+
+		it( 'should return "true" for left arrow', () => {
+			expect( isArrowKeyCode( keyCodes.arrowleft ) ).to.be.true;
+		} );
+
+		it( 'should return "true" for up arrow', () => {
+			expect( isArrowKeyCode( keyCodes.arrowup ) ).to.be.true;
+		} );
+
+		it( 'should return "true" for down arrow', () => {
+			expect( isArrowKeyCode( keyCodes.arrowdown ) ).to.be.true;
+		} );
+
+		it( 'should return "false" for non-arrow keystrokes', () => {
+			expect( isArrowKeyCode( keyCodes.a ) ).to.be.false;
+			expect( isArrowKeyCode( keyCodes.ctrl ) ).to.be.false;
+		} );
+	} );
+
+	describe( 'getLocalizedArrowKeyCodeDirection()', () => {
+		describe( 'for a left–to–right content language direction', () => {
+			it( 'should return "left" for left arrow', () => {
+				expect( getLocalizedArrowKeyCodeDirection( keyCodes.arrowleft, 'ltr' ) ).to.equal( 'left' );
+			} );
+
+			it( 'should return "right" for right arrow', () => {
+				expect( getLocalizedArrowKeyCodeDirection( keyCodes.arrowright, 'ltr' ) ).to.equal( 'right' );
+			} );
+
+			it( 'should return "up" for up arrow', () => {
+				expect( getLocalizedArrowKeyCodeDirection( keyCodes.arrowup, 'ltr' ) ).to.equal( 'up' );
+			} );
+
+			it( 'should return "down" for down arrow', () => {
+				expect( getLocalizedArrowKeyCodeDirection( keyCodes.arrowdown, 'ltr' ) ).to.equal( 'down' );
+			} );
+		} );
+
+		describe( 'for a right-to-left content language direction', () => {
+			it( 'should return "right" for left arrow', () => {
+				expect( getLocalizedArrowKeyCodeDirection( keyCodes.arrowleft, 'rtl' ) ).to.equal( 'right' );
+			} );
+
+			it( 'should return "left" for right arrow', () => {
+				expect( getLocalizedArrowKeyCodeDirection( keyCodes.arrowright, 'rtl' ) ).to.equal( 'left' );
+			} );
+
+			it( 'should return "up" for up arrow', () => {
+				expect( getLocalizedArrowKeyCodeDirection( keyCodes.arrowup, 'rtl' ) ).to.equal( 'up' );
+			} );
+
+			it( 'should return "down" for down arrow', () => {
+				expect( getLocalizedArrowKeyCodeDirection( keyCodes.arrowdown, 'rtl' ) ).to.equal( 'down' );
+			} );
+		} );
+	} );
+
+	describe( 'isForwardArrowKeyCode()', () => {
+		describe( 'for a left–to–right content language direction', () => {
+			it( 'should return "true" for down arrow', () => {
+				expect( isForwardArrowKeyCode( keyCodes.arrowdown, 'ltr' ) ).to.be.true;
+			} );
+
+			it( 'should return "true" for right arrow', () => {
+				expect( isForwardArrowKeyCode( keyCodes.arrowright, 'ltr' ) ).to.be.true;
+			} );
+
+			it( 'should return "false" for up arrow', () => {
+				expect( isForwardArrowKeyCode( keyCodes.arrowup, 'ltr' ) ).to.be.false;
+			} );
+
+			it( 'should return "false" for left arrow', () => {
+				expect( isForwardArrowKeyCode( keyCodes.arrowleft, 'ltr' ) ).to.be.false;
+			} );
+		} );
+
+		describe( 'for a right-to-left content language direction', () => {
+			it( 'should return "true" for down arrow', () => {
+				expect( isForwardArrowKeyCode( keyCodes.arrowdown, 'rtl' ) ).to.be.true;
+			} );
+
+			it( 'should return "true" for left arrow', () => {
+				expect( isForwardArrowKeyCode( keyCodes.arrowleft, 'rtl' ) ).to.be.true;
+			} );
+
+			it( 'should return "false" for up arrow', () => {
+				expect( isForwardArrowKeyCode( keyCodes.arrowup, 'rtl' ) ).to.be.false;
+			} );
+
+			it( 'should return "false" for right arrow', () => {
+				expect( isForwardArrowKeyCode( keyCodes.arrowright, 'rtl' ) ).to.be.false;
+			} );
+		} );
+	} );
 } );