11import type * as ReactNavigationNative from '@react-navigation/native' ;
22import { fireEvent , render , screen , userEvent , within } from '@testing-library/react-native' ;
33import { addMonths , addYears , subMonths , subYears } from 'date-fns' ;
4+ import type { ComponentType , ReactNode } from 'react' ;
45import CalendarPicker from '@components/DatePicker/CalendarPicker' ;
56import DateUtils from '@libs/DateUtils' ;
67import CONST from '@src/CONST' ;
78
9+ type MockPressableProps = { testID ?: string ; accessibilityLabel ?: string ; role ?: string ; onPress ?: ( ) => void ; children ?: ReactNode } ;
10+ type MockTextProps = { children ?: ReactNode } ;
11+ type MockViewProps = { testID ?: string ; children ?: ReactNode } ;
12+ type MockReactNativePrimitives = {
13+ Pressable : ComponentType < MockPressableProps > ;
14+ Text : ComponentType < MockTextProps > ;
15+ View : ComponentType < MockViewProps > ;
16+ } ;
17+
818const monthNames = DateUtils . getMonthNames ( ) ;
919
1020jest . mock ( '@react-navigation/native' , ( ) => ( {
@@ -21,48 +31,53 @@ jest.mock('../../src/hooks/useLocalize', () =>
2131
2232jest . mock ( '@src/components/ConfirmedRoute.tsx' ) ;
2333
34+ type MockMonthPickerModalProps = { isVisible : boolean ; onMonthChange ?: ( month : number ) => void ; onClose ?: ( ) => void } ;
35+ type MockYearPickerModalProps = {
36+ isVisible : boolean ;
37+ years : Array < { value : number ; text : string } > ;
38+ onYearChange ?: ( year : number ) => void ;
39+ onClose ?: ( ) => void ;
40+ } ;
41+
2442jest . mock ( '@components/DatePicker/CalendarPicker/MonthPickerModal' , ( ) => {
25- const { Pressable, Text, View} = jest . requireActual ( 'react-native' ) ;
26- return function MockMonthPickerModal ( { isVisible, onMonthChange, onClose} : { isVisible : boolean ; currentMonth ?: number ; onMonthChange ?: ( month : number ) => void ; onClose ?: ( ) => void } ) {
43+ const ReactNativeActual = jest . requireActual < MockReactNativePrimitives > ( 'react-native' ) ;
44+ const { Pressable, Text, View} = ReactNativeActual ;
45+ const MONTH_KEYS = [ 'jan' , 'feb' , 'mar' , 'apr' , 'may' , 'jun' , 'jul' , 'aug' , 'sep' , 'oct' , 'nov' , 'dec' ] ;
46+ function MockMonthPickerModal ( { isVisible, onMonthChange, onClose} : MockMonthPickerModalProps ) {
2747 if ( ! isVisible ) {
2848 return null ;
2949 }
3050 return (
3151 < View testID = "MonthPickerModal" >
32- { Array . from ( { length : 12 } ) . map ( ( _ , i ) => (
52+ { MONTH_KEYS . map ( ( key , i ) => (
3353 < Pressable
34- key = { i }
54+ key = { key }
3555 testID = { `month-option-${ i } ` }
56+ accessibilityLabel = { `month-${ i } ` }
57+ role = "button"
3658 onPress = { ( ) => onMonthChange ?.( i ) }
3759 >
3860 < Text > { `month-${ i } ` } </ Text >
3961 </ Pressable >
4062 ) ) }
4163 < Pressable
4264 testID = "month-modal-close"
65+ accessibilityLabel = "close"
66+ role = "button"
4367 onPress = { onClose }
4468 >
4569 < Text > close</ Text >
4670 </ Pressable >
4771 </ View >
4872 ) ;
49- } ;
73+ }
74+ return MockMonthPickerModal ;
5075} ) ;
5176
5277jest . mock ( '@components/DatePicker/CalendarPicker/YearPickerModal' , ( ) => {
53- const { Pressable, Text, View} = jest . requireActual ( 'react-native' ) ;
54- return function MockYearPickerModal ( {
55- isVisible,
56- years,
57- onYearChange,
58- onClose,
59- } : {
60- isVisible : boolean ;
61- years : Array < { value : number ; text : string } > ;
62- currentYear ?: number ;
63- onYearChange ?: ( year : number ) => void ;
64- onClose ?: ( ) => void ;
65- } ) {
78+ const ReactNativeActual = jest . requireActual < MockReactNativePrimitives > ( 'react-native' ) ;
79+ const { Pressable, Text, View} = ReactNativeActual ;
80+ function MockYearPickerModal ( { isVisible, years, onYearChange, onClose} : MockYearPickerModalProps ) {
6681 if ( ! isVisible ) {
6782 return null ;
6883 }
@@ -72,20 +87,25 @@ jest.mock('@components/DatePicker/CalendarPicker/YearPickerModal', () => {
7287 < Pressable
7388 key = { year . value }
7489 testID = { `year-option-${ year . value } ` }
90+ accessibilityLabel = { year . text }
91+ role = "button"
7592 onPress = { ( ) => onYearChange ?.( year . value ) }
7693 >
7794 < Text > { year . text } </ Text >
7895 </ Pressable >
7996 ) ) }
8097 < Pressable
8198 testID = "year-modal-close"
99+ accessibilityLabel = "close"
100+ role = "button"
82101 onPress = { onClose }
83102 >
84103 < Text > close</ Text >
85104 </ Pressable >
86105 </ View >
87106 ) ;
88- } ;
107+ }
108+ return MockYearPickerModal ;
89109} ) ;
90110
91111describe ( 'CalendarPicker' , ( ) => {
0 commit comments