1
- import { Type , Component , ViewChild , ElementRef , Directive } from '@angular/core' ;
1
+ import { Type , Component , ViewChild , ElementRef , Directive , Provider } from '@angular/core' ;
2
2
import { ComponentFixture , TestBed , inject , fakeAsync , tick , flush } from '@angular/core/testing' ;
3
3
import {
4
4
FormsModule ,
@@ -10,14 +10,15 @@ import {
10
10
NgModel ,
11
11
} from '@angular/forms' ;
12
12
import { NoopAnimationsModule } from '@angular/platform-browser/animations' ;
13
+ import { Directionality } from '@angular/cdk/bidi' ;
13
14
import { OverlayContainer } from '@angular/cdk/overlay' ;
14
15
import { ErrorStateMatcher , MatNativeDateModule } from '@angular/material/core' ;
15
16
import { MatDatepickerModule } from './datepicker-module' ;
16
17
import { MatFormFieldModule } from '@angular/material/form-field' ;
17
18
import { MatInputModule } from '@angular/material/input' ;
18
19
import { dispatchFakeEvent , dispatchKeyboardEvent } from '../../cdk/testing/private' ;
19
20
import { FocusMonitor } from '@angular/cdk/a11y' ;
20
- import { BACKSPACE } from '@angular/cdk/keycodes' ;
21
+ import { BACKSPACE , LEFT_ARROW , RIGHT_ARROW } from '@angular/cdk/keycodes' ;
21
22
import { MatDateRangeInput } from './date-range-input' ;
22
23
import { MatDateRangePicker } from './date-range-picker' ;
23
24
import { MatStartDate , MatEndDate } from './date-range-input-parts' ;
@@ -27,6 +28,7 @@ describe('MatDateRangeInput', () => {
27
28
function createComponent < T > (
28
29
component : Type < T > ,
29
30
declarations : Type < any > [ ] = [ ] ,
31
+ providers : Provider [ ] = [ ] ,
30
32
) : ComponentFixture < T > {
31
33
TestBed . configureTestingModule ( {
32
34
imports : [
@@ -38,6 +40,7 @@ describe('MatDateRangeInput', () => {
38
40
ReactiveFormsModule ,
39
41
MatNativeDateModule ,
40
42
] ,
43
+ providers,
41
44
declarations : [ component , ...declarations ] ,
42
45
} ) ;
43
46
@@ -721,6 +724,98 @@ describe('MatDateRangeInput', () => {
721
724
expect ( start . nativeElement . focus ) . not . toHaveBeenCalled ( ) ;
722
725
} ) ;
723
726
727
+ it ( 'moves focus between fields with arrow keys when cursor is at edge (LTR)' , ( ) => {
728
+ const fixture = createComponent ( StandardRangePicker ) ;
729
+ fixture . detectChanges ( ) ;
730
+ const { start, end} = fixture . componentInstance ;
731
+
732
+ start . nativeElement . value = '09/10/2020' ;
733
+ end . nativeElement . value = '10/10/2020' ;
734
+
735
+ start . nativeElement . focus ( ) ;
736
+ start . nativeElement . setSelectionRange ( 9 , 9 ) ;
737
+ dispatchKeyboardEvent ( start . nativeElement , 'keydown' , RIGHT_ARROW ) ;
738
+ fixture . detectChanges ( ) ;
739
+ expect ( document . activeElement ) . toBe ( start . nativeElement ) ;
740
+
741
+ start . nativeElement . setSelectionRange ( 10 , 10 ) ;
742
+ dispatchKeyboardEvent ( start . nativeElement , 'keydown' , LEFT_ARROW ) ;
743
+ fixture . detectChanges ( ) ;
744
+ expect ( document . activeElement ) . toBe ( start . nativeElement ) ;
745
+
746
+ start . nativeElement . setSelectionRange ( 10 , 10 ) ;
747
+ dispatchKeyboardEvent ( start . nativeElement , 'keydown' , RIGHT_ARROW ) ;
748
+ fixture . detectChanges ( ) ;
749
+ expect ( document . activeElement ) . toBe ( end . nativeElement ) ;
750
+
751
+ end . nativeElement . setSelectionRange ( 1 , 1 ) ;
752
+ dispatchKeyboardEvent ( end . nativeElement , 'keydown' , LEFT_ARROW ) ;
753
+ fixture . detectChanges ( ) ;
754
+ expect ( document . activeElement ) . toBe ( end . nativeElement ) ;
755
+
756
+ end . nativeElement . setSelectionRange ( 0 , 0 ) ;
757
+ dispatchKeyboardEvent ( end . nativeElement , 'keydown' , RIGHT_ARROW ) ;
758
+ fixture . detectChanges ( ) ;
759
+ expect ( document . activeElement ) . toBe ( end . nativeElement ) ;
760
+
761
+ end . nativeElement . setSelectionRange ( 0 , 0 ) ;
762
+ dispatchKeyboardEvent ( end . nativeElement , 'keydown' , LEFT_ARROW ) ;
763
+ fixture . detectChanges ( ) ;
764
+ expect ( document . activeElement ) . toBe ( start . nativeElement ) ;
765
+ } ) ;
766
+
767
+ it ( 'moves focus between fields with arrow keys when cursor is at edge (RTL)' , ( ) => {
768
+ class RTL extends Directionality {
769
+ override readonly value = 'rtl' ;
770
+ }
771
+ const fixture = createComponent (
772
+ StandardRangePicker ,
773
+ [ ] ,
774
+ [
775
+ {
776
+ provide : Directionality ,
777
+ useFactory : ( ) => new RTL ( null ) ,
778
+ } ,
779
+ ] ,
780
+ ) ;
781
+ fixture . detectChanges ( ) ;
782
+ const { start, end} = fixture . componentInstance ;
783
+
784
+ start . nativeElement . value = '09/10/2020' ;
785
+ end . nativeElement . value = '10/10/2020' ;
786
+
787
+ start . nativeElement . focus ( ) ;
788
+ start . nativeElement . setSelectionRange ( 9 , 9 ) ;
789
+ dispatchKeyboardEvent ( start . nativeElement , 'keydown' , LEFT_ARROW ) ;
790
+ fixture . detectChanges ( ) ;
791
+ expect ( document . activeElement ) . toBe ( start . nativeElement ) ;
792
+
793
+ start . nativeElement . setSelectionRange ( 10 , 10 ) ;
794
+ dispatchKeyboardEvent ( start . nativeElement , 'keydown' , RIGHT_ARROW ) ;
795
+ fixture . detectChanges ( ) ;
796
+ expect ( document . activeElement ) . toBe ( start . nativeElement ) ;
797
+
798
+ start . nativeElement . setSelectionRange ( 10 , 10 ) ;
799
+ dispatchKeyboardEvent ( start . nativeElement , 'keydown' , LEFT_ARROW ) ;
800
+ fixture . detectChanges ( ) ;
801
+ expect ( document . activeElement ) . toBe ( end . nativeElement ) ;
802
+
803
+ end . nativeElement . setSelectionRange ( 1 , 1 ) ;
804
+ dispatchKeyboardEvent ( end . nativeElement , 'keydown' , RIGHT_ARROW ) ;
805
+ fixture . detectChanges ( ) ;
806
+ expect ( document . activeElement ) . toBe ( end . nativeElement ) ;
807
+
808
+ end . nativeElement . setSelectionRange ( 0 , 0 ) ;
809
+ dispatchKeyboardEvent ( end . nativeElement , 'keydown' , LEFT_ARROW ) ;
810
+ fixture . detectChanges ( ) ;
811
+ expect ( document . activeElement ) . toBe ( end . nativeElement ) ;
812
+
813
+ end . nativeElement . setSelectionRange ( 0 , 0 ) ;
814
+ dispatchKeyboardEvent ( end . nativeElement , 'keydown' , RIGHT_ARROW ) ;
815
+ fixture . detectChanges ( ) ;
816
+ expect ( document . activeElement ) . toBe ( start . nativeElement ) ;
817
+ } ) ;
818
+
724
819
it ( 'should be able to get the input placeholder' , ( ) => {
725
820
const fixture = createComponent ( StandardRangePicker ) ;
726
821
fixture . detectChanges ( ) ;
0 commit comments