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 { MatLegacyFormFieldModule } from '@angular/material/legacy-form-field' ;
17
18
import { MatLegacyInputModule } from '@angular/material/legacy-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,110 @@ 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
+ dispatchKeyboardEvent ( start . nativeElement , 'keyup' , RIGHT_ARROW ) ;
739
+ fixture . detectChanges ( ) ;
740
+ expect ( document . activeElement ) . toBe ( start . nativeElement ) ;
741
+
742
+ start . nativeElement . setSelectionRange ( 10 , 10 ) ;
743
+ dispatchKeyboardEvent ( start . nativeElement , 'keydown' , LEFT_ARROW ) ;
744
+ dispatchKeyboardEvent ( start . nativeElement , 'keyup' , LEFT_ARROW ) ;
745
+ fixture . detectChanges ( ) ;
746
+ expect ( document . activeElement ) . toBe ( start . nativeElement ) ;
747
+
748
+ start . nativeElement . setSelectionRange ( 10 , 10 ) ;
749
+ dispatchKeyboardEvent ( start . nativeElement , 'keydown' , RIGHT_ARROW ) ;
750
+ dispatchKeyboardEvent ( start . nativeElement , 'keyup' , RIGHT_ARROW ) ;
751
+ fixture . detectChanges ( ) ;
752
+ expect ( document . activeElement ) . toBe ( end . nativeElement ) ;
753
+
754
+ end . nativeElement . setSelectionRange ( 1 , 1 ) ;
755
+ dispatchKeyboardEvent ( end . nativeElement , 'keydown' , LEFT_ARROW ) ;
756
+ dispatchKeyboardEvent ( end . nativeElement , 'keyup' , LEFT_ARROW ) ;
757
+ fixture . detectChanges ( ) ;
758
+ expect ( document . activeElement ) . toBe ( end . nativeElement ) ;
759
+
760
+ end . nativeElement . setSelectionRange ( 0 , 0 ) ;
761
+ dispatchKeyboardEvent ( end . nativeElement , 'keydown' , RIGHT_ARROW ) ;
762
+ dispatchKeyboardEvent ( end . nativeElement , 'keyup' , RIGHT_ARROW ) ;
763
+ fixture . detectChanges ( ) ;
764
+ expect ( document . activeElement ) . toBe ( end . nativeElement ) ;
765
+
766
+ end . nativeElement . setSelectionRange ( 0 , 0 ) ;
767
+ dispatchKeyboardEvent ( end . nativeElement , 'keydown' , LEFT_ARROW ) ;
768
+ dispatchKeyboardEvent ( end . nativeElement , 'keyup' , LEFT_ARROW ) ;
769
+ fixture . detectChanges ( ) ;
770
+ expect ( document . activeElement ) . toBe ( start . nativeElement ) ;
771
+ } ) ;
772
+
773
+ it ( 'moves focus between fields with arrow keys when cursor is at edge (RTL)' , ( ) => {
774
+ class RTL extends Directionality {
775
+ override readonly value = 'rtl' ;
776
+ }
777
+ const fixture = createComponent (
778
+ StandardRangePicker ,
779
+ [ ] ,
780
+ [
781
+ {
782
+ provide : Directionality ,
783
+ useFactory : ( ) => new RTL ( null ) ,
784
+ } ,
785
+ ] ,
786
+ ) ;
787
+ fixture . detectChanges ( ) ;
788
+ const { start, end} = fixture . componentInstance ;
789
+
790
+ start . nativeElement . value = '09/10/2020' ;
791
+ end . nativeElement . value = '10/10/2020' ;
792
+
793
+ start . nativeElement . focus ( ) ;
794
+ start . nativeElement . setSelectionRange ( 9 , 9 ) ;
795
+ dispatchKeyboardEvent ( start . nativeElement , 'keydown' , LEFT_ARROW ) ;
796
+ dispatchKeyboardEvent ( start . nativeElement , 'keyup' , LEFT_ARROW ) ;
797
+ fixture . detectChanges ( ) ;
798
+ expect ( document . activeElement ) . toBe ( start . nativeElement ) ;
799
+
800
+ start . nativeElement . setSelectionRange ( 10 , 10 ) ;
801
+ dispatchKeyboardEvent ( start . nativeElement , 'keydown' , RIGHT_ARROW ) ;
802
+ dispatchKeyboardEvent ( start . nativeElement , 'keyup' , RIGHT_ARROW ) ;
803
+ fixture . detectChanges ( ) ;
804
+ expect ( document . activeElement ) . toBe ( start . nativeElement ) ;
805
+
806
+ start . nativeElement . setSelectionRange ( 10 , 10 ) ;
807
+ dispatchKeyboardEvent ( start . nativeElement , 'keydown' , LEFT_ARROW ) ;
808
+ dispatchKeyboardEvent ( start . nativeElement , 'keyup' , LEFT_ARROW ) ;
809
+ fixture . detectChanges ( ) ;
810
+ expect ( document . activeElement ) . toBe ( end . nativeElement ) ;
811
+
812
+ end . nativeElement . setSelectionRange ( 1 , 1 ) ;
813
+ dispatchKeyboardEvent ( end . nativeElement , 'keydown' , RIGHT_ARROW ) ;
814
+ dispatchKeyboardEvent ( end . nativeElement , 'keyup' , RIGHT_ARROW ) ;
815
+ fixture . detectChanges ( ) ;
816
+ expect ( document . activeElement ) . toBe ( end . nativeElement ) ;
817
+
818
+ end . nativeElement . setSelectionRange ( 0 , 0 ) ;
819
+ dispatchKeyboardEvent ( end . nativeElement , 'keydown' , LEFT_ARROW ) ;
820
+ dispatchKeyboardEvent ( end . nativeElement , 'keyup' , LEFT_ARROW ) ;
821
+ fixture . detectChanges ( ) ;
822
+ expect ( document . activeElement ) . toBe ( end . nativeElement ) ;
823
+
824
+ end . nativeElement . setSelectionRange ( 0 , 0 ) ;
825
+ dispatchKeyboardEvent ( end . nativeElement , 'keydown' , RIGHT_ARROW ) ;
826
+ dispatchKeyboardEvent ( end . nativeElement , 'keyup' , RIGHT_ARROW ) ;
827
+ fixture . detectChanges ( ) ;
828
+ expect ( document . activeElement ) . toBe ( start . nativeElement ) ;
829
+ } ) ;
830
+
724
831
it ( 'should be able to get the input placeholder' , ( ) => {
725
832
const fixture = createComponent ( StandardRangePicker ) ;
726
833
fixture . detectChanges ( ) ;
0 commit comments