1
1
import { Directionality } from '@angular/cdk/bidi' ;
2
- import { ENTER , LEFT_ARROW , RIGHT_ARROW , SPACE } from '@angular/cdk/keycodes' ;
2
+ import { ENTER , LEFT_ARROW , RIGHT_ARROW , UP_ARROW , DOWN_ARROW , SPACE } from '@angular/cdk/keycodes' ;
3
3
import { dispatchKeyboardEvent } from '@angular/cdk/testing' ;
4
4
import { Component , DebugElement } from '@angular/core' ;
5
5
import { async , ComponentFixture , TestBed , inject } from '@angular/core/testing' ;
6
6
import { AbstractControl , AsyncValidatorFn , FormControl , FormGroup , ReactiveFormsModule ,
7
7
ValidationErrors , Validators } from '@angular/forms' ;
8
8
import { By } from '@angular/platform-browser' ;
9
9
import { NoopAnimationsModule } from '@angular/platform-browser/animations' ;
10
+ import { StepperOrientation } from '@angular/cdk/stepper' ;
10
11
import { map } from 'rxjs/operators/map' ;
11
12
import { take } from 'rxjs/operators/take' ;
12
13
import { Observable } from 'rxjs/Observable' ;
@@ -89,9 +90,9 @@ describe('MatHorizontalStepper', () => {
89
90
assertCorrectStepAnimationDirection ( fixture ) ;
90
91
} ) ;
91
92
92
- it ( 'should support keyboard events to move and select focus' , ( ) => {
93
+ it ( 'should support using the left/right arrows to move focus' , ( ) => {
93
94
let stepHeaders = fixture . debugElement . queryAll ( By . css ( '.mat-horizontal-stepper-header' ) ) ;
94
- assertCorrectKeyboardInteraction ( fixture , stepHeaders ) ;
95
+ assertCorrectKeyboardInteraction ( fixture , stepHeaders , 'horizontal' ) ;
95
96
} ) ;
96
97
97
98
it ( 'should not set focus on header of selected step if header is not clicked' , ( ) => {
@@ -321,9 +322,14 @@ describe('MatVerticalStepper', () => {
321
322
assertCorrectStepAnimationDirection ( fixture ) ;
322
323
} ) ;
323
324
324
- it ( 'should support keyboard events to move and select focus' , ( ) => {
325
+ it ( 'should support using the left/right arrows to move focus' , ( ) => {
325
326
let stepHeaders = fixture . debugElement . queryAll ( By . css ( '.mat-vertical-stepper-header' ) ) ;
326
- assertCorrectKeyboardInteraction ( fixture , stepHeaders ) ;
327
+ assertCorrectKeyboardInteraction ( fixture , stepHeaders , 'horizontal' ) ;
328
+ } ) ;
329
+
330
+ it ( 'should support using the up/down arrows to move focus' , ( ) => {
331
+ let stepHeaders = fixture . debugElement . queryAll ( By . css ( '.mat-vertical-stepper-header' ) ) ;
332
+ assertCorrectKeyboardInteraction ( fixture , stepHeaders , 'vertical' ) ;
327
333
} ) ;
328
334
329
335
it ( 'should not set focus on header of selected step if header is not clicked' , ( ) => {
@@ -566,20 +572,23 @@ function assertCorrectStepAnimationDirection(fixture: ComponentFixture<any>, rtl
566
572
567
573
/** Asserts that keyboard interaction works correctly. */
568
574
function assertCorrectKeyboardInteraction ( fixture : ComponentFixture < any > ,
569
- stepHeaders : DebugElement [ ] ) {
575
+ stepHeaders : DebugElement [ ] ,
576
+ orientation : StepperOrientation ) {
570
577
let stepperComponent = fixture . debugElement . query ( By . directive ( MatStepper ) ) . componentInstance ;
578
+ let nextKey = orientation === 'vertical' ? DOWN_ARROW : RIGHT_ARROW ;
579
+ let prevKey = orientation === 'vertical' ? UP_ARROW : LEFT_ARROW ;
571
580
572
581
expect ( stepperComponent . _focusIndex ) . toBe ( 0 ) ;
573
582
expect ( stepperComponent . selectedIndex ) . toBe ( 0 ) ;
574
583
575
584
let stepHeaderEl = stepHeaders [ 0 ] . nativeElement ;
576
- dispatchKeyboardEvent ( stepHeaderEl , 'keydown' , RIGHT_ARROW ) ;
585
+ dispatchKeyboardEvent ( stepHeaderEl , 'keydown' , nextKey ) ;
577
586
fixture . detectChanges ( ) ;
578
587
579
588
expect ( stepperComponent . _focusIndex )
580
- . toBe ( 1 , 'Expected index of focused step to increase by 1 after RIGHT_ARROW event .' ) ;
589
+ . toBe ( 1 , 'Expected index of focused step to increase by 1 after pressing the next key .' ) ;
581
590
expect ( stepperComponent . selectedIndex )
582
- . toBe ( 0 , 'Expected index of selected step to remain unchanged after RIGHT_ARROW event .' ) ;
591
+ . toBe ( 0 , 'Expected index of selected step to remain unchanged after pressing the next key .' ) ;
583
592
584
593
stepHeaderEl = stepHeaders [ 1 ] . nativeElement ;
585
594
dispatchKeyboardEvent ( stepHeaderEl , 'keydown' , ENTER ) ;
@@ -592,26 +601,25 @@ function assertCorrectKeyboardInteraction(fixture: ComponentFixture<any>,
592
601
'Expected index of selected step to change to index of focused step after ENTER event.' ) ;
593
602
594
603
stepHeaderEl = stepHeaders [ 1 ] . nativeElement ;
595
- dispatchKeyboardEvent ( stepHeaderEl , 'keydown' , LEFT_ARROW ) ;
604
+ dispatchKeyboardEvent ( stepHeaderEl , 'keydown' , prevKey ) ;
596
605
fixture . detectChanges ( ) ;
597
606
598
607
expect ( stepperComponent . _focusIndex )
599
- . toBe ( 0 , 'Expected index of focused step to decrease by 1 after LEFT_ARROW event .' ) ;
600
- expect ( stepperComponent . selectedIndex )
601
- . toBe ( 1 , 'Expected index of selected step to remain unchanged after LEFT_ARROW event .' ) ;
608
+ . toBe ( 0 , 'Expected index of focused step to decrease by 1 after pressing the previous key .' ) ;
609
+ expect ( stepperComponent . selectedIndex ) . toBe ( 1 ,
610
+ 'Expected index of selected step to remain unchanged after pressing the previous key .' ) ;
602
611
603
612
// When the focus is on the last step and right arrow key is pressed, the focus should cycle
604
613
// through to the first step.
605
614
stepperComponent . _focusIndex = 2 ;
606
615
stepHeaderEl = stepHeaders [ 2 ] . nativeElement ;
607
- dispatchKeyboardEvent ( stepHeaderEl , 'keydown' , RIGHT_ARROW ) ;
616
+ dispatchKeyboardEvent ( stepHeaderEl , 'keydown' , nextKey ) ;
608
617
fixture . detectChanges ( ) ;
609
618
610
- expect ( stepperComponent . _focusIndex )
611
- . toBe ( 0 ,
612
- 'Expected index of focused step to cycle through to index 0 after RIGHT_ARROW event.' ) ;
619
+ expect ( stepperComponent . _focusIndex ) . toBe ( 0 ,
620
+ 'Expected index of focused step to cycle through to index 0 after pressing the next key.' ) ;
613
621
expect ( stepperComponent . selectedIndex )
614
- . toBe ( 1 , 'Expected index of selected step to remain unchanged after RIGHT_ARROW event .' ) ;
622
+ . toBe ( 1 , 'Expected index of selected step to remain unchanged after pressing the next key .' ) ;
615
623
616
624
stepHeaderEl = stepHeaders [ 0 ] . nativeElement ;
617
625
dispatchKeyboardEvent ( stepHeaderEl , 'keydown' , SPACE ) ;
0 commit comments