Skip to content

test(date-picker): add test case for locale of the formatDate method #2116

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 29 additions & 7 deletions js/date-picker/format.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,13 @@ export function extractTimeFormat(dateFormat: string = '') {
.trim();
}

// 由于 dayjs.locale(dayjsLocale) 方法的 dayjsLocale 不能为 undefined,
// 当 dayjsLocale 为空时取 dayjs 的 locale 作为默认值,
// 以保证格式化时能正确使用本地化语言
export function getDefaultLocale() {
return dayjs.locale();
}

// 统一解析日期格式字符串成 Dayjs 对象
export function parseToDayjs(
value: string | Date | number,
Expand All @@ -27,25 +34,30 @@ export function parseToDayjs(
) {
if (value === '' || value === null) return dayjs();

let _dayjsLocale = dayjsLocale;
if (!_dayjsLocale) {
_dayjsLocale = getDefaultLocale();
}

let dateText = value;
// format week
if (/[w|W]/g.test(format)) {
if (!isString(dateText)) {
dateText = dayjs(dateText).locale(dayjsLocale || 'zh-cn').format(format) as string;
dateText = dayjs(dateText).locale(_dayjsLocale).format(format) as string;
}

const yearStr = dateText.split(/[-/.\s]/)[0];
const weekStr = dateText.split(/[-/.\s]/)[1];
const weekFormatStr = format.split(/[-/.\s]/)[1];

let firstWeek = dayjs(yearStr, 'YYYY').locale(dayjsLocale || 'zh-cn').startOf('year');
let firstWeek = dayjs(yearStr, 'YYYY').locale(_dayjsLocale).startOf('year');
// 第一周ISO定义: 本年度第一个星期四所在的星期
// 如果第一年第一天在星期四后, 直接跳到下一周, 下一周必定是第一周
// 否则本周即为第一周
if (firstWeek.day() > 4 || firstWeek.day() === 0) firstWeek = firstWeek.add(1, 'week');

// 一年有52或者53周, 引入IsoWeeksInYear辅助查询
const weekCounts = dayjs(yearStr, 'YYYY').locale(dayjsLocale || 'zh-cn').isoWeeksInYear();
const weekCounts = dayjs(yearStr, 'YYYY').locale(_dayjsLocale).isoWeeksInYear();
for (let i = 0; i <= weekCounts; i += 1) {
let nextWeek = firstWeek.add(i, 'week');
// 重置为周的第一天
Expand All @@ -59,7 +71,7 @@ export function parseToDayjs(
// format quarter
if (/Q/g.test(format)) {
if (!isString(dateText)) {
dateText = dayjs(dateText).locale(dayjsLocale || 'zh-cn').format(format) as string;
dateText = dayjs(dateText).locale(_dayjsLocale).format(format) as string;
}

const yearStr = dateText.split(/[-/.\s]/)[0];
Expand Down Expand Up @@ -104,7 +116,12 @@ function formatRange({
}) {
if (!newDate || !Array.isArray(newDate)) return [];

let dayjsDateList = newDate.map((d) => d && parseToDayjs(d, format).locale(dayjsLocale));
let _dayjsLocale = dayjsLocale;
if (!_dayjsLocale) {
_dayjsLocale = getDefaultLocale();
}

let dayjsDateList = newDate.map((d) => d && parseToDayjs(d, format).locale(_dayjsLocale));

// 保证后面的时间大于前面的时间
if (
Expand Down Expand Up @@ -148,7 +165,12 @@ function formatSingle({
}) {
if (!newDate) return '';

const dayJsDate = parseToDayjs(newDate, format).locale(dayjsLocale);
let _dayjsLocale = dayjsLocale;
if (!_dayjsLocale) {
_dayjsLocale = getDefaultLocale();
}

const dayJsDate = parseToDayjs(newDate, format).locale(_dayjsLocale);

// 格式化失败提示
if (!dayJsDate.isValid()) {
Expand Down Expand Up @@ -186,7 +208,7 @@ export function formatDate(
{
format,
targetFormat,
dayjsLocale = 'zh-cn',
dayjsLocale,
autoSwap,
}: { format: string; dayjsLocale?: string, targetFormat?: string; autoSwap?: boolean }
) {
Expand Down
33 changes: 32 additions & 1 deletion test/unit/date-picker/utils.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import dayjs from 'dayjs';
import 'dayjs/locale/en';
import 'dayjs/locale/zh-cn';
import { describe, it, expect } from 'vitest';
import { extractTimeFormat } from '../../../js/date-picker/format';
import { extractTimeFormat, formatDate } from '../../../js/date-picker/format';

describe('utils', () => {
describe(' extractTimeFormat', () => {
Expand All @@ -18,4 +21,32 @@ describe('utils', () => {
expect(res).toBe('HH时mm分ss秒SSS毫秒');
});
});

describe('when dayjsLocale is undefined, locale should be consistent with dayjs locale', () => {
const dateDisplayFormat = 'MMM,YYYY';
const testDate = '2025-04-22';

it('zh-cn', () => {
dayjs.locale('zh-cn');

const res1 = formatDate('2025-04-22', {
format: dateDisplayFormat,
dayjsLocale: undefined,
});
const res2 = dayjs(testDate).format(dateDisplayFormat);

expect(res1).toBe(res2);
});

it('en', () => {
dayjs.locale('en');

const res3 = formatDate('2025-04-22', {
format: dateDisplayFormat,
dayjsLocale: undefined,
});
const res4 = dayjs(testDate).format(dateDisplayFormat);
expect(res3).toBe(res4);
});
});
});