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' ;
@@ -89,7 +89,58 @@ describe('MatHorizontalStepper', () => {
89
89
90
90
it ( 'should support keyboard events to move and select focus' , ( ) => {
91
91
let stepHeaders = fixture . debugElement . queryAll ( By . css ( '.mat-horizontal-stepper-header' ) ) ;
92
- assertCorrectKeyboardInteraction ( fixture , stepHeaders ) ;
92
+ let stepperComponent = fixture . debugElement . query ( By . directive ( MatStepper ) ) . componentInstance ;
93
+ let stepHeaderEl = stepHeaders [ 0 ] . nativeElement ;
94
+
95
+ expect ( stepperComponent . _focusIndex ) . toBe ( 0 ) ;
96
+ expect ( stepperComponent . selectedIndex ) . toBe ( 0 ) ;
97
+
98
+ dispatchKeyboardEvent ( stepHeaderEl , 'keydown' , RIGHT_ARROW ) ;
99
+ fixture . detectChanges ( ) ;
100
+
101
+ expect ( stepperComponent . _focusIndex )
102
+ . toBe ( 1 , 'Expected index of focused step to increase by 1 after RIGHT_ARROW event.' ) ;
103
+ expect ( stepperComponent . selectedIndex )
104
+ . toBe ( 0 , 'Expected index of selected step to remain unchanged after RIGHT_ARROW event.' ) ;
105
+
106
+ stepHeaderEl = stepHeaders [ 1 ] . nativeElement ;
107
+ dispatchKeyboardEvent ( stepHeaderEl , 'keydown' , ENTER ) ;
108
+ fixture . detectChanges ( ) ;
109
+
110
+ expect ( stepperComponent . _focusIndex )
111
+ . toBe ( 1 , 'Expected index of focused step to remain unchanged after ENTER event.' ) ;
112
+ expect ( stepperComponent . selectedIndex ) . toBe ( 1 ,
113
+ 'Expected index of selected step to change to index of focused step after ENTER event.' ) ;
114
+
115
+ stepHeaderEl = stepHeaders [ 1 ] . nativeElement ;
116
+ dispatchKeyboardEvent ( stepHeaderEl , 'keydown' , LEFT_ARROW ) ;
117
+ fixture . detectChanges ( ) ;
118
+
119
+ expect ( stepperComponent . _focusIndex )
120
+ . toBe ( 0 , 'Expected index of focused step to decrease by 1 after LEFT_ARROW event.' ) ;
121
+ expect ( stepperComponent . selectedIndex )
122
+ . toBe ( 1 , 'Expected index of selected step to remain unchanged after LEFT_ARROW event.' ) ;
123
+
124
+ // When the focus is on the last step and right arrow key is pressed, the focus should cycle
125
+ // through to the first step.
126
+ stepperComponent . _focusIndex = 2 ;
127
+ stepHeaderEl = stepHeaders [ 2 ] . nativeElement ;
128
+ dispatchKeyboardEvent ( stepHeaderEl , 'keydown' , RIGHT_ARROW ) ;
129
+ fixture . detectChanges ( ) ;
130
+
131
+ expect ( stepperComponent . _focusIndex ) . toBe ( 0 ,
132
+ 'Expected index of focused step to cycle through to index 0 after RIGHT_ARROW event.' ) ;
133
+ expect ( stepperComponent . selectedIndex )
134
+ . toBe ( 1 , 'Expected index of selected step to remain unchanged after RIGHT_ARROW event.' ) ;
135
+
136
+ stepHeaderEl = stepHeaders [ 0 ] . nativeElement ;
137
+ dispatchKeyboardEvent ( stepHeaderEl , 'keydown' , SPACE ) ;
138
+ fixture . detectChanges ( ) ;
139
+
140
+ expect ( stepperComponent . _focusIndex )
141
+ . toBe ( 0 , 'Expected index of focused to remain unchanged after SPACE event.' ) ;
142
+ expect ( stepperComponent . selectedIndex )
143
+ . toBe ( 0 , 'Expected index of selected step to change after SPACE event.' ) ;
93
144
} ) ;
94
145
95
146
it ( 'should not set focus on header of selected step if header is not clicked' , ( ) => {
@@ -135,7 +186,21 @@ describe('MatHorizontalStepper', () => {
135
186
136
187
it ( 'should reverse arrow key focus in RTL mode' , ( ) => {
137
188
let stepHeaders = fixture . debugElement . queryAll ( By . css ( '.mat-horizontal-stepper-header' ) ) ;
138
- assertArrowKeyInteractionInRtl ( fixture , stepHeaders ) ;
189
+ let stepperComponent = fixture . debugElement . query ( By . directive ( MatStepper ) ) . componentInstance ;
190
+ let stepHeaderEl = stepHeaders [ 0 ] . nativeElement ;
191
+
192
+ expect ( stepperComponent . _focusIndex ) . toBe ( 0 ) ;
193
+
194
+ dispatchKeyboardEvent ( stepHeaderEl , 'keydown' , LEFT_ARROW ) ;
195
+ fixture . detectChanges ( ) ;
196
+
197
+ expect ( stepperComponent . _focusIndex ) . toBe ( 1 ) ;
198
+
199
+ stepHeaderEl = stepHeaders [ 1 ] . nativeElement ;
200
+ dispatchKeyboardEvent ( stepHeaderEl , 'keydown' , RIGHT_ARROW ) ;
201
+ fixture . detectChanges ( ) ;
202
+
203
+ expect ( stepperComponent . _focusIndex ) . toBe ( 0 ) ;
139
204
} ) ;
140
205
141
206
it ( 'should reverse animation in RTL mode' , ( ) => {
@@ -273,7 +338,58 @@ describe('MatVerticalStepper', () => {
273
338
274
339
it ( 'should support keyboard events to move and select focus' , ( ) => {
275
340
let stepHeaders = fixture . debugElement . queryAll ( By . css ( '.mat-vertical-stepper-header' ) ) ;
276
- assertCorrectKeyboardInteraction ( fixture , stepHeaders ) ;
341
+ let stepperComponent = fixture . debugElement . query ( By . directive ( MatStepper ) ) . componentInstance ;
342
+ let stepHeaderEl = stepHeaders [ 0 ] . nativeElement ;
343
+
344
+ expect ( stepperComponent . _focusIndex ) . toBe ( 0 ) ;
345
+ expect ( stepperComponent . selectedIndex ) . toBe ( 0 ) ;
346
+
347
+ dispatchKeyboardEvent ( stepHeaderEl , 'keydown' , DOWN_ARROW ) ;
348
+ fixture . detectChanges ( ) ;
349
+
350
+ expect ( stepperComponent . _focusIndex )
351
+ . toBe ( 1 , 'Expected index of focused step to increase by 1 after DOWN_ARROW event.' ) ;
352
+ expect ( stepperComponent . selectedIndex )
353
+ . toBe ( 0 , 'Expected index of selected step to remain unchanged after DOWN_ARROW event.' ) ;
354
+
355
+ stepHeaderEl = stepHeaders [ 1 ] . nativeElement ;
356
+ dispatchKeyboardEvent ( stepHeaderEl , 'keydown' , ENTER ) ;
357
+ fixture . detectChanges ( ) ;
358
+
359
+ expect ( stepperComponent . _focusIndex )
360
+ . toBe ( 1 , 'Expected index of focused step to remain unchanged after ENTER event.' ) ;
361
+ expect ( stepperComponent . selectedIndex ) . toBe ( 1 ,
362
+ 'Expected index of selected step to change to index of focused step after ENTER event.' ) ;
363
+
364
+ stepHeaderEl = stepHeaders [ 1 ] . nativeElement ;
365
+ dispatchKeyboardEvent ( stepHeaderEl , 'keydown' , UP_ARROW ) ;
366
+ fixture . detectChanges ( ) ;
367
+
368
+ expect ( stepperComponent . _focusIndex )
369
+ . toBe ( 0 , 'Expected index of focused step to decrease by 1 after UP_ARROW event.' ) ;
370
+ expect ( stepperComponent . selectedIndex )
371
+ . toBe ( 1 , 'Expected index of selected step to remain unchanged after UP_ARROW event.' ) ;
372
+
373
+ // When the focus is on the last step and right arrow key is pressed, the focus should cycle
374
+ // through to the first step.
375
+ stepperComponent . _focusIndex = 2 ;
376
+ stepHeaderEl = stepHeaders [ 2 ] . nativeElement ;
377
+ dispatchKeyboardEvent ( stepHeaderEl , 'keydown' , DOWN_ARROW ) ;
378
+ fixture . detectChanges ( ) ;
379
+
380
+ expect ( stepperComponent . _focusIndex ) . toBe ( 0 ,
381
+ 'Expected index of focused step to cycle through to index 0 after DOWN_ARROW event.' ) ;
382
+ expect ( stepperComponent . selectedIndex )
383
+ . toBe ( 1 , 'Expected index of selected step to remain unchanged after DOWN_ARROW event.' ) ;
384
+
385
+ stepHeaderEl = stepHeaders [ 0 ] . nativeElement ;
386
+ dispatchKeyboardEvent ( stepHeaderEl , 'keydown' , SPACE ) ;
387
+ fixture . detectChanges ( ) ;
388
+
389
+ expect ( stepperComponent . _focusIndex )
390
+ . toBe ( 0 , 'Expected index of focused to remain unchanged after SPACE event.' ) ;
391
+ expect ( stepperComponent . selectedIndex )
392
+ . toBe ( 0 , 'Expected index of selected step to change after SPACE event.' ) ;
277
393
} ) ;
278
394
279
395
it ( 'should not set focus on header of selected step if header is not clicked' , ( ) => {
@@ -302,11 +418,6 @@ describe('MatVerticalStepper', () => {
302
418
fixture . detectChanges ( ) ;
303
419
} ) ;
304
420
305
- it ( 'should reverse arrow key focus in RTL mode' , ( ) => {
306
- let stepHeaders = fixture . debugElement . queryAll ( By . css ( '.mat-vertical-stepper-header' ) ) ;
307
- assertArrowKeyInteractionInRtl ( fixture , stepHeaders ) ;
308
- } ) ;
309
-
310
421
it ( 'should reverse animation in RTL mode' , ( ) => {
311
422
assertCorrectStepAnimationDirection ( fixture , 'rtl' ) ;
312
423
} ) ;
@@ -514,66 +625,6 @@ function assertCorrectStepAnimationDirection(fixture: ComponentFixture<any>, rtl
514
625
expect ( stepperComponent . _getAnimationDirection ( 2 ) ) . toBe ( 'current' ) ;
515
626
}
516
627
517
- /** Asserts that keyboard interaction works correctly. */
518
- function assertCorrectKeyboardInteraction ( fixture : ComponentFixture < any > ,
519
- stepHeaders : DebugElement [ ] ) {
520
- let stepperComponent = fixture . debugElement . query ( By . directive ( MatStepper ) ) . componentInstance ;
521
-
522
- expect ( stepperComponent . _focusIndex ) . toBe ( 0 ) ;
523
- expect ( stepperComponent . selectedIndex ) . toBe ( 0 ) ;
524
-
525
- let stepHeaderEl = stepHeaders [ 0 ] . nativeElement ;
526
- dispatchKeyboardEvent ( stepHeaderEl , 'keydown' , RIGHT_ARROW ) ;
527
- fixture . detectChanges ( ) ;
528
-
529
- expect ( stepperComponent . _focusIndex )
530
- . toBe ( 1 , 'Expected index of focused step to increase by 1 after RIGHT_ARROW event.' ) ;
531
- expect ( stepperComponent . selectedIndex )
532
- . toBe ( 0 , 'Expected index of selected step to remain unchanged after RIGHT_ARROW event.' ) ;
533
-
534
- stepHeaderEl = stepHeaders [ 1 ] . nativeElement ;
535
- dispatchKeyboardEvent ( stepHeaderEl , 'keydown' , ENTER ) ;
536
- fixture . detectChanges ( ) ;
537
-
538
- expect ( stepperComponent . _focusIndex )
539
- . toBe ( 1 , 'Expected index of focused step to remain unchanged after ENTER event.' ) ;
540
- expect ( stepperComponent . selectedIndex )
541
- . toBe ( 1 ,
542
- 'Expected index of selected step to change to index of focused step after ENTER event.' ) ;
543
-
544
- stepHeaderEl = stepHeaders [ 1 ] . nativeElement ;
545
- dispatchKeyboardEvent ( stepHeaderEl , 'keydown' , LEFT_ARROW ) ;
546
- fixture . detectChanges ( ) ;
547
-
548
- expect ( stepperComponent . _focusIndex )
549
- . toBe ( 0 , 'Expected index of focused step to decrease by 1 after LEFT_ARROW event.' ) ;
550
- expect ( stepperComponent . selectedIndex )
551
- . toBe ( 1 , 'Expected index of selected step to remain unchanged after LEFT_ARROW event.' ) ;
552
-
553
- // When the focus is on the last step and right arrow key is pressed, the focus should cycle
554
- // through to the first step.
555
- stepperComponent . _focusIndex = 2 ;
556
- stepHeaderEl = stepHeaders [ 2 ] . nativeElement ;
557
- dispatchKeyboardEvent ( stepHeaderEl , 'keydown' , RIGHT_ARROW ) ;
558
- fixture . detectChanges ( ) ;
559
-
560
- expect ( stepperComponent . _focusIndex )
561
- . toBe ( 0 ,
562
- 'Expected index of focused step to cycle through to index 0 after RIGHT_ARROW event.' ) ;
563
- expect ( stepperComponent . selectedIndex )
564
- . toBe ( 1 , 'Expected index of selected step to remain unchanged after RIGHT_ARROW event.' ) ;
565
-
566
- stepHeaderEl = stepHeaders [ 0 ] . nativeElement ;
567
- dispatchKeyboardEvent ( stepHeaderEl , 'keydown' , SPACE ) ;
568
- fixture . detectChanges ( ) ;
569
-
570
- expect ( stepperComponent . _focusIndex )
571
- . toBe ( 0 , 'Expected index of focused to remain unchanged after SPACE event.' ) ;
572
- expect ( stepperComponent . selectedIndex )
573
- . toBe ( 0 ,
574
- 'Expected index of selected step to change to index of focused step after SPACE event.' ) ;
575
- }
576
-
577
628
/** Asserts that step selection change using stepper buttons does not focus step header. */
578
629
function assertStepHeaderFocusNotCalled ( fixture : ComponentFixture < any > ) {
579
630
let stepperComponent = fixture . debugElement . query ( By . directive ( MatStepper ) ) . componentInstance ;
@@ -588,26 +639,6 @@ function assertStepHeaderFocusNotCalled(fixture: ComponentFixture<any>) {
588
639
expect ( stepHeaderEl . focus ) . not . toHaveBeenCalled ( ) ;
589
640
}
590
641
591
- /** Asserts that arrow key direction works correctly in RTL mode. */
592
- function assertArrowKeyInteractionInRtl ( fixture : ComponentFixture < any > ,
593
- stepHeaders : DebugElement [ ] ) {
594
- let stepperComponent = fixture . debugElement . query ( By . directive ( MatStepper ) ) . componentInstance ;
595
-
596
- expect ( stepperComponent . _focusIndex ) . toBe ( 0 ) ;
597
-
598
- let stepHeaderEl = stepHeaders [ 0 ] . nativeElement ;
599
- dispatchKeyboardEvent ( stepHeaderEl , 'keydown' , LEFT_ARROW ) ;
600
- fixture . detectChanges ( ) ;
601
-
602
- expect ( stepperComponent . _focusIndex ) . toBe ( 1 ) ;
603
-
604
- stepHeaderEl = stepHeaders [ 1 ] . nativeElement ;
605
- dispatchKeyboardEvent ( stepHeaderEl , 'keydown' , RIGHT_ARROW ) ;
606
- fixture . detectChanges ( ) ;
607
-
608
- expect ( stepperComponent . _focusIndex ) . toBe ( 0 ) ;
609
- }
610
-
611
642
/**
612
643
* Asserts that linear stepper does not allow step selection change if current step is not valid.
613
644
*/
0 commit comments