@@ -10,7 +10,7 @@ import {
10
10
UP_ARROW ,
11
11
} from '@angular/cdk/keycodes' ;
12
12
import { StepperOrientation , MAT_STEPPER_GLOBAL_OPTIONS , STEP_STATE } from '@angular/cdk/stepper' ;
13
- import { dispatchKeyboardEvent } from '@angular/cdk/testing' ;
13
+ import { dispatchKeyboardEvent , createKeyboardEvent , dispatchEvent } from '@angular/cdk/testing' ;
14
14
import { Component , DebugElement , EventEmitter , OnInit , Type , Provider } from '@angular/core' ;
15
15
import { ComponentFixture , fakeAsync , flush , inject , TestBed } from '@angular/core/testing' ;
16
16
import {
@@ -345,6 +345,16 @@ describe('MatStepper', () => {
345
345
346
346
expect ( stepperComponent . selectedIndex ) . toBe ( 1 ) ;
347
347
} ) ;
348
+
349
+ it ( 'should not do anything when pressing the ENTER key with a modifier' , ( ) => {
350
+ const stepHeaders = fixture . debugElement . queryAll ( By . css ( '.mat-vertical-stepper-header' ) ) ;
351
+ assertSelectKeyWithModifierInteraction ( fixture , stepHeaders , 'vertical' , ENTER ) ;
352
+ } ) ;
353
+
354
+ it ( 'should not do anything when pressing the SPACE key with a modifier' , ( ) => {
355
+ const stepHeaders = fixture . debugElement . queryAll ( By . css ( '.mat-vertical-stepper-header' ) ) ;
356
+ assertSelectKeyWithModifierInteraction ( fixture , stepHeaders , 'vertical' , SPACE ) ;
357
+ } ) ;
348
358
} ) ;
349
359
350
360
describe ( 'basic stepper when attempting to set the selected step too early' , ( ) => {
@@ -1063,6 +1073,38 @@ function assertArrowKeyInteractionInRtl(fixture: ComponentFixture<any>,
1063
1073
expect ( stepperComponent . _getFocusIndex ( ) ) . toBe ( 0 ) ;
1064
1074
}
1065
1075
1076
+ /** Asserts that keyboard interaction works correctly when the user is pressing a modifier key. */
1077
+ function assertSelectKeyWithModifierInteraction ( fixture : ComponentFixture < any > ,
1078
+ stepHeaders : DebugElement [ ] ,
1079
+ orientation : StepperOrientation ,
1080
+ selectionKey : number ) {
1081
+ const stepperComponent = fixture . debugElement . query ( By . directive ( MatStepper ) ) . componentInstance ;
1082
+ const modifiers = [ 'altKey' , 'shiftKey' , 'ctrlKey' , 'metaKey' ] ;
1083
+
1084
+ expect ( stepperComponent . _getFocusIndex ( ) ) . toBe ( 0 ) ;
1085
+ expect ( stepperComponent . selectedIndex ) . toBe ( 0 ) ;
1086
+
1087
+ dispatchKeyboardEvent ( stepHeaders [ 0 ] . nativeElement , 'keydown' ,
1088
+ orientation === 'vertical' ? DOWN_ARROW : RIGHT_ARROW ) ;
1089
+ fixture . detectChanges ( ) ;
1090
+
1091
+ expect ( stepperComponent . _getFocusIndex ( ) )
1092
+ . toBe ( 1 , 'Expected index of focused step to increase by 1 after pressing the next key.' ) ;
1093
+ expect ( stepperComponent . selectedIndex )
1094
+ . toBe ( 0 , 'Expected index of selected step to remain unchanged after pressing the next key.' ) ;
1095
+
1096
+ modifiers . forEach ( modifier => {
1097
+ const event : KeyboardEvent = createKeyboardEvent ( 'keydown' , selectionKey ) ;
1098
+ Object . defineProperty ( event , modifier , { get : ( ) => true } ) ;
1099
+ dispatchEvent ( stepHeaders [ 1 ] . nativeElement , event ) ;
1100
+ fixture . detectChanges ( ) ;
1101
+
1102
+ expect ( stepperComponent . selectedIndex ) . toBe ( 0 , `Expected selected index to remain unchanged ` +
1103
+ `when pressing the selection key with ${ modifier } modifier.` ) ;
1104
+ expect ( event . defaultPrevented ) . toBe ( false ) ;
1105
+ } ) ;
1106
+ }
1107
+
1066
1108
function asyncValidator ( minLength : number , validationTrigger : Subject < void > ) : AsyncValidatorFn {
1067
1109
return ( control : AbstractControl ) : Observable < ValidationErrors | null > => {
1068
1110
return validationTrigger . pipe (
0 commit comments