Skip to content

Commit 25e313f

Browse files
committed
test: add test for multiple mode
1 parent b5ed12f commit 25e313f

1 file changed

Lines changed: 44 additions & 8 deletions

File tree

packages/react-use-calendar-component/src/test/useCalendarComponent.test.ts

Lines changed: 44 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,15 @@ import type { ExcludedDates } from '../lib/types';
88
const now = new Date(2022, 9, 1);
99
const nowYear = now.getFullYear();
1010
const nowMonth = now.getMonth();
11+
const sep25 = new Date(2022, 8, 25);
12+
const nov05 = new Date(2022, 10, 5);
13+
14+
beforeEach(() => {
15+
jest.useFakeTimers({ now });
16+
});
1117

1218
describe('useCalendarComponent', () => {
1319
it('should generate correct calendar cell', () => {
14-
jest.useFakeTimers({ now });
1520
const { result } = renderHook(() => useCalendarComponent());
1621
const cells = result.current.getDateCellInfos();
1722
const previousCells = cells.splice(0, 6);
@@ -39,12 +44,11 @@ describe('useCalendarComponent', () => {
3944
});
4045

4146
it('should select the date cell correctly with single mode', () => {
42-
jest.useFakeTimers({ now });
4347
const { result } = renderHook(() => {
4448
const [value, setValue] = useState(now);
4549
return { ...useCalendarComponent({ value, onChange: setValue }), value };
4650
});
47-
const cells = result.current.getDateCellInfos();
51+
let cells = result.current.getDateCellInfos();
4852
// 2022-10-01
4953
expect(cells[6]).toMatchObject({
5054
isToday: true,
@@ -56,19 +60,51 @@ describe('useCalendarComponent', () => {
5660
// select the date at previous month
5761
act(() => {
5862
// 2022-09-25
59-
cells[0]?.selectThisDate();
63+
cells[0]?.selectThisDate({ changeDisplayedValues: false });
6064
});
61-
expect(result.current.value).toEqual(new Date(2022, 8, 25));
65+
expect(result.current.value).toEqual(sep25);
66+
cells = result.current.getDateCellInfos();
6267
// select the date at next month
6368
act(() => {
6469
// 2022-11-05
65-
cells.at(-1)?.selectThisDate();
70+
cells.at(-1)?.selectThisDate({ changeDisplayedValues: false });
71+
});
72+
expect(result.current.value).toEqual(nov05);
73+
});
74+
75+
it('should select the date cells correctly with multiple mode', () => {
76+
const { result } = renderHook(() => {
77+
const [value, setValue] = useState<Date[]>([]);
78+
return {
79+
...useCalendarComponent({
80+
selectType: 'multiple',
81+
value,
82+
onChange: setValue,
83+
}),
84+
value,
85+
};
86+
});
87+
let cells = result.current.getDateCellInfos();
88+
act(() => {
89+
cells[6]?.selectThisDate({ changeDisplayedValues: false });
90+
});
91+
expect(result.current.value).toEqual([now]);
92+
93+
cells = result.current.getDateCellInfos();
94+
act(() => {
95+
cells[0]?.selectThisDate({ changeDisplayedValues: false });
96+
});
97+
expect(result.current.value).toEqual([now, sep25]);
98+
99+
cells = result.current.getDateCellInfos();
100+
// unselect the date
101+
act(() => {
102+
cells[0]?.selectThisDate({ changeDisplayedValues: false });
66103
});
67-
expect(result.current.value).toEqual(new Date(2022, 10, 5));
104+
expect(result.current.value).toEqual([now]);
68105
});
69106

70107
it('should distinguish whether a date is excluded', () => {
71-
jest.useFakeTimers({ now });
72108
const excludedDates: ExcludedDates = {
73109
min: now,
74110
max: new Date(2022, 10, 1),

0 commit comments

Comments
 (0)