From bd3f096d3407e4cb471586cfac547b5506708a5b Mon Sep 17 00:00:00 2001 From: banruo Date: Tue, 6 Feb 2024 17:01:21 +0800 Subject: [PATCH] fix(WeekPicker):cross year select not selected at panel --- src/utils/dateUtil.ts | 5 +++-- tests/util.spec.tsx | 36 ++++++++++++++++++++++++++++++------ 2 files changed, 33 insertions(+), 8 deletions(-) diff --git a/src/utils/dateUtil.ts b/src/utils/dateUtil.ts index d84aaa3b1..0f04be73c 100644 --- a/src/utils/dateUtil.ts +++ b/src/utils/dateUtil.ts @@ -123,9 +123,10 @@ export function isSameWeek( if (typeof equal === 'boolean') { return equal; } - + const weekStartDate1 = generateConfig.locale.getWeekFirstDate(locale, date1!); + const weekStartDate2 = generateConfig.locale.getWeekFirstDate(locale, date2!); return ( - isSameYear(generateConfig, date1!, date2!) && + isSameYear(generateConfig, weekStartDate1, weekStartDate2) && generateConfig.locale.getWeek(locale, date1!) === generateConfig.locale.getWeek(locale, date2!) ); } diff --git a/tests/util.spec.tsx b/tests/util.spec.tsx index 0348574ee..c66067412 100644 --- a/tests/util.spec.tsx +++ b/tests/util.spec.tsx @@ -1,7 +1,7 @@ import momentGenerateConfig from '../src/generate/moment'; import { getLowerBoundTime, setTime, getLastDay } from '../src/utils/timeUtil'; import { toArray } from '../src/utils/miscUtil'; -import { isSameTime, isSameDecade } from '../src/utils/dateUtil'; +import { isSameTime, isSameDecade, isSameWeek } from '../src/utils/dateUtil'; import { getMoment } from './util/commonUtil'; describe('Picker.Util', () => { @@ -66,11 +66,35 @@ describe('Picker.Util', () => { }); describe('getLastDay', () => { + expect(getLastDay(momentGenerateConfig, getMoment('2020-10-01'))).toEqual('2020-10-31'); + }); + + it('isSameWeek', () => { + // 2024 CN first date === 2024-01-01 monday expect( - getLastDay( - momentGenerateConfig, - getMoment('2020-10-01'), - ), - ).toEqual('2020-10-31'); + isSameWeek(momentGenerateConfig, 'zh_CN', getMoment('2023-12-31'), getMoment('2024-01-01')), + ).toBeFalsy(); + expect( + isSameWeek(momentGenerateConfig, 'zh_CN', getMoment('2023-12-31'), getMoment('2024-12-31')), + ).toBeFalsy(); + expect( + isSameWeek(momentGenerateConfig, 'zh_CN', getMoment('2024-01-02'), getMoment('2024-01-01')), + ).toBeTruthy(); + expect( + isSameWeek(momentGenerateConfig, 'zh_CN', getMoment('2024-01-02'), getMoment('2024-03-01')), + ).toBeFalsy(); + // 2024 US first date === 2023-12-31 sunday + expect( + isSameWeek(momentGenerateConfig, 'en_US', getMoment('2023-12-31'), getMoment('2024-01-01')), + ).toBeTruthy(); + expect( + isSameWeek(momentGenerateConfig, 'en_US', getMoment('2023-12-31'), getMoment('2024-12-31')), + ).toBeFalsy(); + expect( + isSameWeek(momentGenerateConfig, 'en_US', getMoment('2024-01-02'), getMoment('2024-01-01')), + ).toBeTruthy(); + expect( + isSameWeek(momentGenerateConfig, 'en_US', getMoment('2024-01-02'), getMoment('2024-03-01')), + ).toBeFalsy(); }); });