1- import { Type , Component , ViewChild , ElementRef , Directive } from '@angular/core' ;
1+ import { Type , Component , ViewChild , ElementRef , Directive , Provider } from '@angular/core' ;
22import { ComponentFixture , TestBed , inject , fakeAsync , tick , flush } from '@angular/core/testing' ;
33import {
44 FormsModule ,
@@ -10,14 +10,15 @@ import {
1010 NgModel ,
1111} from '@angular/forms' ;
1212import { NoopAnimationsModule } from '@angular/platform-browser/animations' ;
13+ import { Directionality } from '@angular/cdk/bidi' ;
1314import { OverlayContainer } from '@angular/cdk/overlay' ;
1415import { ErrorStateMatcher , MatNativeDateModule } from '@angular/material/core' ;
1516import { MatDatepickerModule } from './datepicker-module' ;
1617import { MatLegacyFormFieldModule } from '@angular/material/legacy-form-field' ;
1718import { MatLegacyInputModule } from '@angular/material/legacy-input' ;
1819import { dispatchFakeEvent , dispatchKeyboardEvent } from '../../cdk/testing/private' ;
1920import { FocusMonitor } from '@angular/cdk/a11y' ;
20- import { BACKSPACE } from '@angular/cdk/keycodes' ;
21+ import { BACKSPACE , LEFT_ARROW , RIGHT_ARROW } from '@angular/cdk/keycodes' ;
2122import { MatDateRangeInput } from './date-range-input' ;
2223import { MatDateRangePicker } from './date-range-picker' ;
2324import { MatStartDate , MatEndDate } from './date-range-input-parts' ;
@@ -27,6 +28,7 @@ describe('MatDateRangeInput', () => {
2728 function createComponent < T > (
2829 component : Type < T > ,
2930 declarations : Type < any > [ ] = [ ] ,
31+ providers : Provider [ ] = [ ] ,
3032 ) : ComponentFixture < T > {
3133 TestBed . configureTestingModule ( {
3234 imports : [
@@ -38,6 +40,7 @@ describe('MatDateRangeInput', () => {
3840 ReactiveFormsModule ,
3941 MatNativeDateModule ,
4042 ] ,
43+ providers,
4144 declarations : [ component , ...declarations ] ,
4245 } ) ;
4346
@@ -721,6 +724,98 @@ describe('MatDateRangeInput', () => {
721724 expect ( start . nativeElement . focus ) . not . toHaveBeenCalled ( ) ;
722725 } ) ;
723726
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+
724819 it ( 'should be able to get the input placeholder' , ( ) => {
725820 const fixture = createComponent ( StandardRangePicker ) ;
726821 fixture . detectChanges ( ) ;
0 commit comments