Skip to content

Commit b3245fa

Browse files
committed
added missing tests
1 parent 0918858 commit b3245fa

1 file changed

Lines changed: 131 additions & 0 deletions

File tree

tests/unit/CalendarPickerTest.tsx

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,73 @@ jest.mock('../../src/hooks/useLocalize', () =>
2121

2222
jest.mock('@src/components/ConfirmedRoute.tsx');
2323

24+
jest.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}) {
27+
if (!isVisible) {
28+
return null;
29+
}
30+
return (
31+
<View testID="MonthPickerModal">
32+
{Array.from({length: 12}).map((_, i) => (
33+
<Pressable
34+
key={i}
35+
testID={`month-option-${i}`}
36+
onPress={() => onMonthChange?.(i)}
37+
>
38+
<Text>{`month-${i}`}</Text>
39+
</Pressable>
40+
))}
41+
<Pressable
42+
testID="month-modal-close"
43+
onPress={onClose}
44+
>
45+
<Text>close</Text>
46+
</Pressable>
47+
</View>
48+
);
49+
};
50+
});
51+
52+
jest.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+
}) {
66+
if (!isVisible) {
67+
return null;
68+
}
69+
return (
70+
<View testID="YearPickerModal">
71+
{years.map((year) => (
72+
<Pressable
73+
key={year.value}
74+
testID={`year-option-${year.value}`}
75+
onPress={() => onYearChange?.(year.value)}
76+
>
77+
<Text>{year.text}</Text>
78+
</Pressable>
79+
))}
80+
<Pressable
81+
testID="year-modal-close"
82+
onPress={onClose}
83+
>
84+
<Text>close</Text>
85+
</Pressable>
86+
</View>
87+
);
88+
};
89+
});
90+
2491
describe('CalendarPicker', () => {
2592
test('renders calendar component', () => {
2693
render(<CalendarPicker />);
@@ -488,6 +555,70 @@ describe('CalendarPicker', () => {
488555
expect(within(screen.getByTestId('currentMonthText')).getByText(monthNames.at(0) ?? '')).toBeTruthy();
489556
});
490557

558+
test('clicking the month button opens the month picker and selecting a month updates the calendar', () => {
559+
const minDate = new Date('2020-01-01');
560+
const maxDate = new Date('2030-12-31');
561+
const value = '2025-06-15';
562+
render(
563+
<CalendarPicker
564+
value={value}
565+
minDate={minDate}
566+
maxDate={maxDate}
567+
/>,
568+
);
569+
570+
fireEvent.press(screen.getByTestId('currentMonthButton'));
571+
572+
const monthPickerModal = screen.getByTestId('MonthPickerModal');
573+
expect(monthPickerModal).toBeTruthy();
574+
575+
fireEvent.press(within(monthPickerModal).getByTestId('month-option-8'));
576+
577+
expect(within(screen.getByTestId('currentMonthText')).getByText(monthNames.at(8) ?? '')).toBeTruthy();
578+
});
579+
580+
test('clicking the year button opens the year picker and selecting a year updates the calendar', () => {
581+
const minDate = new Date('2020-01-01');
582+
const maxDate = new Date('2030-12-31');
583+
const value = '2025-06-15';
584+
render(
585+
<CalendarPicker
586+
value={value}
587+
minDate={minDate}
588+
maxDate={maxDate}
589+
/>,
590+
);
591+
592+
fireEvent.press(screen.getByTestId('currentYearButton'));
593+
594+
const yearPickerModal = screen.getByTestId('YearPickerModal');
595+
expect(yearPickerModal).toBeTruthy();
596+
597+
fireEvent.press(within(yearPickerModal).getByTestId('year-option-2027'));
598+
599+
expect(within(screen.getByTestId('currentYearText')).getByText('2027')).toBeTruthy();
600+
});
601+
602+
test('closing the year picker via onClose hides the modal', () => {
603+
render(<CalendarPicker />);
604+
605+
fireEvent.press(screen.getByTestId('currentYearButton'));
606+
expect(screen.getByTestId('YearPickerModal')).toBeTruthy();
607+
608+
fireEvent.press(screen.getByTestId('year-modal-close'));
609+
expect(screen.queryByTestId('YearPickerModal')).toBeNull();
610+
});
611+
612+
test('closing the month picker via onClose hides the modal', () => {
613+
render(<CalendarPicker />);
614+
615+
fireEvent.press(screen.getByTestId('currentMonthButton'));
616+
expect(screen.getByTestId('MonthPickerModal')).toBeTruthy();
617+
618+
fireEvent.press(screen.getByTestId('month-modal-close'));
619+
expect(screen.queryByTestId('MonthPickerModal')).toBeNull();
620+
});
621+
491622
test('month picker should always return all 12 months', () => {
492623
const allMonths = DateUtils.getFilteredMonthItems(monthNames, 6);
493624

0 commit comments

Comments
 (0)