diff --git a/src/components/date-picker/date-picker-week-utils.ts b/src/components/date-picker/date-picker-week-utils.ts index e6022018c7..8100b266aa 100644 --- a/src/components/date-picker/date-picker-week-utils.ts +++ b/src/components/date-picker/date-picker-week-utils.ts @@ -1,8 +1,8 @@ -import type { ReactNode } from 'react' import dayjs from 'dayjs' +import isLeapYear from 'dayjs/plugin/isLeapYear' import isoWeek from 'dayjs/plugin/isoWeek' import isoWeeksInYear from 'dayjs/plugin/isoWeeksInYear' -import isLeapYear from 'dayjs/plugin/isLeapYear' +import type { ReactNode } from 'react' import { PickerColumn } from '../picker' import type { DatePickerFilter } from './date-picker-utils' @@ -123,13 +123,12 @@ export function convertDateToStringArray( } export function convertStringArrayToDate< - T extends string | number | null | undefined + T extends string | number | null | undefined, >(value: T[]): Date { const yearString = value[0] ?? '1900' const weekString = value[1] ?? '1' const weekdayString = value[2] ?? '1' - const day = dayjs() - .year(parseInt(yearString as string)) + const day = dayjs(`${parseInt(yearString as string)}-01-01`) .isoWeek(parseInt(weekString as string)) .isoWeekday(parseInt(weekdayString as string)) .hour(0) diff --git a/src/components/date-picker/tests/date-picker.test.tsx b/src/components/date-picker/tests/date-picker.test.tsx index 8fc0527c56..398098845b 100644 --- a/src/components/date-picker/tests/date-picker.test.tsx +++ b/src/components/date-picker/tests/date-picker.test.tsx @@ -1,17 +1,18 @@ +import dayjs from 'dayjs' +import * as React from 'react' import { - render, - testA11y, + act, fireEvent, - waitFor, + render, screen, sleep, - act, + testA11y, + waitFor, waitForElementToBeRemoved, } from 'testing' -import * as React from 'react' import DatePicker from '../' -import dayjs from 'dayjs' import Button from '../../button' +import { convertStringArrayToDate } from '../date-picker-week-utils' const classPrefix = `adm-picker` @@ -205,4 +206,16 @@ describe('DatePicker', () => { expect(res.toDateString()).toEqual(now.toDateString()) expect(res.tillNow).toBeTruthy() }) + + test('convertStringArrayToDate of week should correct', () => { + // jest mock today is `2024-12-30` + jest.useFakeTimers({ + now: new Date('2024-12-30'), + }) + + const date = convertStringArrayToDate(['2024', '1', '1']) + expect(date.getFullYear()).toBe(2024) + expect(date.getMonth()).toBe(0) + expect(date.getDate()).toBe(1) + }) })