|
| 1 | +import { act, renderHook } from '@testing-library/react'; |
| 2 | +import { useState } from 'react'; |
| 3 | + |
| 4 | +import useCalendarComponent from '../lib'; |
| 5 | +import type { ExcludedDates } from '../lib/types'; |
| 6 | + |
| 7 | +// use 2022-10-01 to test |
| 8 | +const now = new Date(2022, 9, 1); |
| 9 | +const nowYear = now.getFullYear(); |
| 10 | +const nowMonth = now.getMonth(); |
| 11 | + |
| 12 | +describe('useCalendarComponent', () => { |
| 13 | + it('should generate correct calendar cell', () => { |
| 14 | + jest.useFakeTimers({ now }); |
| 15 | + const { result } = renderHook(() => useCalendarComponent()); |
| 16 | + const cells = result.current.getDateCellInfos(); |
| 17 | + const previousCells = cells.splice(0, 6); |
| 18 | + const currentCells = cells.splice(0, 31); |
| 19 | + const nextCells = cells; |
| 20 | + |
| 21 | + previousCells.forEach(({ year, month, monthDay, monthStatus }, index) => { |
| 22 | + expect(year).toBe(nowYear); |
| 23 | + expect(month).toBe(nowMonth - 1); |
| 24 | + expect(monthDay).toBe(25 + index); |
| 25 | + expect(monthStatus).toBe('previous'); |
| 26 | + }); |
| 27 | + currentCells.forEach(({ year, month, monthDay, monthStatus }, index) => { |
| 28 | + expect(year).toBe(nowYear); |
| 29 | + expect(month).toBe(nowMonth); |
| 30 | + expect(monthDay).toBe(1 + index); |
| 31 | + expect(monthStatus).toBe('current'); |
| 32 | + }); |
| 33 | + nextCells.forEach(({ year, month, monthDay, monthStatus }, index) => { |
| 34 | + expect(year).toBe(nowYear); |
| 35 | + expect(month).toBe(nowMonth + 1); |
| 36 | + expect(monthDay).toBe(1 + index); |
| 37 | + expect(monthStatus).toBe('next'); |
| 38 | + }); |
| 39 | + }); |
| 40 | + |
| 41 | + it('should select the date cell correctly with single mode', () => { |
| 42 | + jest.useFakeTimers({ now }); |
| 43 | + const { result } = renderHook(() => { |
| 44 | + const [value, setValue] = useState(now); |
| 45 | + return { ...useCalendarComponent({ value, onChange: setValue }), value }; |
| 46 | + }); |
| 47 | + const cells = result.current.getDateCellInfos(); |
| 48 | + // 2022-10-01 |
| 49 | + expect(cells[6]).toMatchObject({ |
| 50 | + isToday: true, |
| 51 | + isSelected: true, |
| 52 | + isExcluded: false, |
| 53 | + }); |
| 54 | + expect(result.current.value).toEqual(now); |
| 55 | + |
| 56 | + // select the date at previous month |
| 57 | + act(() => { |
| 58 | + // 2022-09-25 |
| 59 | + cells[0]?.selectThisDate(); |
| 60 | + }); |
| 61 | + expect(result.current.value).toEqual(new Date(2022, 8, 25)); |
| 62 | + // select the date at next month |
| 63 | + act(() => { |
| 64 | + // 2022-11-05 |
| 65 | + cells.at(-1)?.selectThisDate(); |
| 66 | + }); |
| 67 | + expect(result.current.value).toEqual(new Date(2022, 10, 5)); |
| 68 | + }); |
| 69 | + |
| 70 | + it('should distinguish whether a date is excluded', () => { |
| 71 | + jest.useFakeTimers({ now }); |
| 72 | + const excludedDates: ExcludedDates = { |
| 73 | + min: now, |
| 74 | + max: new Date(2022, 10, 1), |
| 75 | + arbitrary: [new Date(2022, 9, 15)], |
| 76 | + }; |
| 77 | + const { result } = renderHook( |
| 78 | + excludedDates => useCalendarComponent({ excludedDates }), |
| 79 | + { |
| 80 | + initialProps: excludedDates, |
| 81 | + } |
| 82 | + ); |
| 83 | + const cells = result.current.getDateCellInfos(); |
| 84 | + const previousCells = cells.splice(0, 6); |
| 85 | + const currentCells = cells.splice(0, 31); |
| 86 | + const nextCells = cells; |
| 87 | + previousCells.forEach(({ isExcluded }) => { |
| 88 | + expect(isExcluded).toBe(true); |
| 89 | + }); |
| 90 | + currentCells.forEach(({ monthDay, isExcluded }) => { |
| 91 | + if (monthDay === 15) expect(isExcluded).toBe(true); |
| 92 | + else expect(isExcluded).toBe(false); |
| 93 | + }); |
| 94 | + nextCells.forEach(({ isExcluded }) => { |
| 95 | + expect(isExcluded).toBe(true); |
| 96 | + }); |
| 97 | + }); |
| 98 | +}); |
0 commit comments