diff --git a/src/packages/calendar/__tests__/calendar.spec.tsx b/src/packages/calendar/__tests__/calendar.spec.tsx index 38f38491d3..1eac2bd6ae 100644 --- a/src/packages/calendar/__tests__/calendar.spec.tsx +++ b/src/packages/calendar/__tests__/calendar.spec.tsx @@ -1,7 +1,17 @@ import * as React from 'react' import { render, fireEvent } from '@testing-library/react' import '@testing-library/jest-dom' -import { CalendarDay } from '@/types' +import { CalendarDay, CalendarRef } from '@/types' +import { getPreMonthDates } from '@/utils/date' +import { + splitDate, + isMultiple, + isCurrDay, + getCurrDate, + isStart, + isEnd, + isStartAndEnd, +} from '../utils' import { Calendar } from '../calendar' @@ -154,3 +164,128 @@ test('choose event when click item', async () => { fireEvent.click(calendarConfirmBtn) expect(onConfirm).toBeCalled() }) + +test('December padding days of a January month should pass year - 1 (#3580)', () => { + // 1. 直接验证 getPreMonthDates 跨年与非跨年边界计算 + const jan2025Prev = getPreMonthDates('prev', 2025, 1, 0) + expect(jan2025Prev).toEqual([ + { day: 29, type: 'prev', year: 2024, month: 12 }, + { day: 30, type: 'prev', year: 2024, month: 12 }, + { day: 31, type: 'prev', year: 2024, month: 12 }, + ]) + + const feb2025Prev = getPreMonthDates('prev', 2025, 2, 0) + expect(feb2025Prev).toEqual([ + { day: 26, type: 'prev', year: 2025, month: 1 }, + { day: 27, type: 'prev', year: 2025, month: 1 }, + { day: 28, type: 'prev', year: 2025, month: 1 }, + { day: 29, type: 'prev', year: 2025, month: 1 }, + { day: 30, type: 'prev', year: 2025, month: 1 }, + { day: 31, type: 'prev', year: 2025, month: 1 }, + ]) + + // 2. 验证通过 Calendar 组件渲染时传给 renderDay / renderDayTop / renderDayBottom 的 CalendarDay 对象 + const prevDaysFromDay: string[] = [] + const prevDaysFromTop: string[] = [] + const prevDaysFromBottom: string[] = [] + + render( + { + if (d.type === 'prev') { + prevDaysFromDay.push(`${d.year}-${d.month}-${d.day}`) + } + return d.day + }} + renderDayTop={(d: CalendarDay) => { + if (d.type === 'prev') { + prevDaysFromTop.push(`${d.year}-${d.month}-${d.day}`) + } + return '' + }} + renderDayBottom={(d: CalendarDay) => { + if (d.type === 'prev') { + prevDaysFromBottom.push(`${d.year}-${d.month}-${d.day}`) + } + return ( + + {d.year}-{d.month} + + ) + }} + /> + ) + + expect(prevDaysFromDay.slice(0, 3)).toEqual([ + '2024-12-29', + '2024-12-30', + '2024-12-31', + ]) + expect(prevDaysFromDay.slice(3)).toEqual([ + '2025-1-26', + '2025-1-27', + '2025-1-28', + '2025-1-29', + '2025-1-30', + '2025-1-31', + ]) + expect(prevDaysFromTop.slice(0, 3)).toEqual([ + '2024-12-29', + '2024-12-30', + '2024-12-31', + ]) + expect(prevDaysFromBottom.slice(0, 3)).toEqual([ + '2024-12-29', + '2024-12-30', + '2024-12-31', + ]) +}) + +test('calendar utils and ref methods coverage', () => { + expect(splitDate('2025-01-15')).toEqual(['2025', '01', '15']) + expect(splitDate('2025/01/15')).toEqual(['2025', '01', '15']) + expect(isMultiple('2025-01-15', ['2025-01-15', '2025-01-16'])).toBe(true) + expect(isMultiple('2025-01-18', ['2025-01-15'])).toBe(false) + expect(isMultiple('2025-01-18', [])).toBe(false) + expect(isStart('2025-01-15', ['2025-01-15', '2025-01-20'])).toBe(true) + expect(isEnd('2025-01-20', ['2025-01-15', '2025-01-20'])).toBe(true) + expect(isStartAndEnd(['2025-01-15', '2025-01-15'])).toBe(true) + expect(isStartAndEnd(['2025-01-15', '2025-01-20'])).toBe(false) + + const monthInfo = { + curData: ['2025', '01', '15'], + title: '2025-01', + monthData: [], + cssHeight: 350, + scrollTop: 0, + } + expect(getCurrDate({ day: 5, type: 'active' }, monthInfo)).toBe('2025/01/05') + expect(isCurrDay(monthInfo, '01')).toBe(false) + + const onClose = vi.fn() + const ref = React.createRef() + const { container } = render( + + ) + + ref.current?.scrollToDate('2025-02-01') + + const closeBtn = container.querySelector('.nut-popup-title-right') + if (closeBtn) { + fireEvent.click(closeBtn) + expect(onClose).toHaveBeenCalled() + } +}) diff --git a/src/utils/date.ts b/src/utils/date.ts index 091bf8a2b1..0e81ce3ee5 100644 --- a/src/utils/date.ts +++ b/src/utils/date.ts @@ -244,7 +244,7 @@ export const getPreMonthDates = ( let preYear = year if (preMonth <= 0) { preMonth = 12 - preYear += 1 + preYear -= 1 } let days = Utils.getMonthPreDay(+year, +month) days -= firstDayOfWeek