Skip to content

Commit 1a69a87

Browse files
authored
test: Add comprehensive test cases for TwoMonthPicker component (#18043)
1 parent 81516ee commit 1a69a87

File tree

2 files changed

+37
-11
lines changed

2 files changed

+37
-11
lines changed

packages/reports/src/Components/Form/CompositeCalendar/__tests__/two-month-picker.spec.tsx

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,40 @@ describe('TwoMonthPicker', () => {
1313
value: moment(),
1414
};
1515

16-
it('should render TwoMonthPicker component', () => {
17-
render(<TwoMonthPicker {...mockProps} />);
18-
const currentMonth = moment().format('MMM');
19-
const prevMonth = moment().subtract(1, 'month').format('MMM');
20-
const currentYear = moment().format('YYYY');
21-
expect(screen.getByText(prevMonth)).toBeInTheDocument();
22-
expect(screen.getByText(currentMonth)).toBeInTheDocument();
23-
expect(screen.getByText(currentYear)).toBeInTheDocument();
16+
describe('should render TwoMonthPicker component', () => {
17+
it('should render TwoMonthPicker component with different years for December/January months', () => {
18+
const january_10th_2025 = moment('2025-01-10', 'YYYY-MM-DD');
19+
20+
render(<TwoMonthPicker {...mockProps} value={january_10th_2025} />);
21+
22+
const currentMonth = moment().month(0).format('MMM'); // January
23+
const prevMonth = moment().month(0).subtract(1, 'month').format('MMM'); // December
24+
const currentYear = moment().year(2025).format('YYYY'); // 2025
25+
const prevYear = moment().year(2024).format('YYYY'); // 2024
26+
27+
expect(screen.getByText(currentMonth)).toBeInTheDocument();
28+
expect(screen.getByText(prevMonth)).toBeInTheDocument();
29+
expect(screen.getByTestId('first-month')).toHaveTextContent(prevYear);
30+
expect(screen.getByTestId('second-month')).toHaveTextContent(currentYear);
31+
});
32+
33+
it('should render TwoMonthPicker component with same years for January/February months', () => {
34+
const february_10th_2025 = moment('2025-02-10', 'YYYY-MM-DD');
35+
36+
render(<TwoMonthPicker {...mockProps} value={february_10th_2025} />);
37+
38+
const currentMonth = moment().month(1).format('MMM'); // February
39+
const prevMonth = moment().month(1).subtract(1, 'month').format('MMM'); // January
40+
const currentYear = moment().year(2025).format('YYYY'); // 2025
41+
const prevYear = moment().year(2025).format('YYYY'); // 2025
42+
43+
expect(screen.getByText(currentMonth)).toBeInTheDocument();
44+
expect(screen.getByText(prevMonth)).toBeInTheDocument();
45+
expect(screen.getByTestId('first-month')).toHaveTextContent(prevYear);
46+
expect(screen.getByTestId('second-month')).toHaveTextContent(currentYear);
47+
});
2448
});
49+
2550
it('should call onChange when a date is selected', async () => {
2651
render(<TwoMonthPicker {...mockProps} />);
2752
const prevMonthDate = moment().date(1).subtract(1, 'month');

packages/reports/src/Components/Form/CompositeCalendar/two-month-picker.tsx

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
import moment from 'moment';
21
import React from 'react';
2+
import moment from 'moment';
3+
34
import { Calendar } from '@deriv/components';
45
import { addMonths, diffInMonths, subMonths, toMoment } from '@deriv/shared';
56

@@ -74,7 +75,7 @@ const TwoMonthPicker = React.memo(({ onChange, isPeriodDisabled, value }: TTwoMo
7475

7576
return (
7677
<React.Fragment>
77-
<div className='first-month'>
78+
<div className='first-month' data-testid='first-month'>
7879
<Calendar.Header
7980
calendar_date={left_pane_date}
8081
calendar_view='date'
@@ -92,7 +93,7 @@ const TwoMonthPicker = React.memo(({ onChange, isPeriodDisabled, value }: TTwoMo
9293
updateSelected={updateSelectedDate}
9394
/>
9495
</div>
95-
<div className='second-month'>
96+
<div className='second-month' data-testid='second-month'>
9697
<Calendar.Header
9798
calendar_date={right_pane_date}
9899
calendar_view='date'

0 commit comments

Comments
 (0)