- TextPart: default import → named import - MaterialCard: 使用 formatDateLabel 显示今天/昨天/日期 - 清理旧测试文件,新增 ResourceTable 测试
110 lines
3.5 KiB
TypeScript
110 lines
3.5 KiB
TypeScript
import { describe, expect, test } from "bun:test";
|
|
|
|
import {
|
|
formatCountdown,
|
|
formatDateLabel,
|
|
formatDurationUnit,
|
|
formatRelativeTime,
|
|
isOlderThan,
|
|
subtractHours,
|
|
} from "../../../src/web/shared/utils/time";
|
|
|
|
describe("subtractHours", () => {
|
|
test("正常扣减小时", () => {
|
|
const result = subtractHours(new Date("2025-01-15T12:00:00.000Z"), 3);
|
|
|
|
expect(result.toISOString()).toBe("2025-01-15T09:00:00.000Z");
|
|
});
|
|
|
|
test("跨天扣减", () => {
|
|
const result = subtractHours(new Date("2025-01-15T02:00:00.000Z"), 6);
|
|
|
|
expect(result.toISOString()).toBe("2025-01-14T20:00:00.000Z");
|
|
});
|
|
|
|
test("跨月扣减", () => {
|
|
const result = subtractHours(new Date("2025-03-01T01:00:00.000Z"), 2);
|
|
|
|
expect(result.toISOString()).toBe("2025-02-28T23:00:00.000Z");
|
|
});
|
|
|
|
test("扣减 0 小时返回相同时间", () => {
|
|
const result = subtractHours(new Date("2025-01-15T12:00:00.000Z"), 0);
|
|
|
|
expect(result.toISOString()).toBe("2025-01-15T12:00:00.000Z");
|
|
});
|
|
});
|
|
|
|
describe("formatRelativeTime", () => {
|
|
const now = new Date("2025-01-01T00:02:00.000Z");
|
|
|
|
test("格式化秒和分钟", () => {
|
|
expect(formatRelativeTime("2025-01-01T00:01:45.000Z", now)).toBe("15秒前");
|
|
expect(formatRelativeTime("2025-01-01T00:00:00.000Z", now)).toBe("2分钟前");
|
|
});
|
|
|
|
test("无时间返回占位", () => {
|
|
expect(formatRelativeTime(null, now)).toBe("尚无检查数据");
|
|
expect(formatRelativeTime("invalid", now)).toBe("尚无检查数据");
|
|
});
|
|
});
|
|
|
|
describe("formatDurationUnit", () => {
|
|
test("按秒、分钟、小时动态格式化", () => {
|
|
expect(formatDurationUnit(1500)).toEqual({ suffix: "秒", value: 1.5 });
|
|
expect(formatDurationUnit(120000)).toEqual({ suffix: "分钟", value: 2 });
|
|
expect(formatDurationUnit(5400000)).toEqual({ suffix: "小时", value: 1.5 });
|
|
});
|
|
|
|
test("空时长返回占位", () => {
|
|
expect(formatDurationUnit(null)).toEqual({ suffix: "", value: 0 });
|
|
});
|
|
});
|
|
|
|
describe("formatCountdown", () => {
|
|
test("格式化秒级和分钟级倒计时", () => {
|
|
expect(formatCountdown(0)).toBe("0秒");
|
|
expect(formatCountdown(59)).toBe("59秒");
|
|
expect(formatCountdown(60)).toBe("1分0秒");
|
|
expect(formatCountdown(299)).toBe("4分59秒");
|
|
});
|
|
});
|
|
|
|
describe("isOlderThan", () => {
|
|
test("判断时间是否超过阈值", () => {
|
|
const now = new Date("2025-01-01T00:02:00.000Z");
|
|
|
|
expect(isOlderThan("2025-01-01T00:00:59.000Z", 60000, now)).toBe(true);
|
|
expect(isOlderThan("2025-01-01T00:01:30.000Z", 60000, now)).toBe(false);
|
|
});
|
|
});
|
|
|
|
describe("formatDateLabel", () => {
|
|
const now = new Date("2026-06-03T12:00:00.000Z");
|
|
|
|
test("今天", () => {
|
|
expect(formatDateLabel("2026-06-03", now)).toBe("今天");
|
|
expect(formatDateLabel("2026-06-03T08:00:00.000Z", now)).toBe("今天");
|
|
});
|
|
|
|
test("昨天", () => {
|
|
expect(formatDateLabel("2026-06-02", now)).toBe("昨天");
|
|
expect(formatDateLabel("2026-06-02T23:59:59.000Z", now)).toBe("昨天");
|
|
});
|
|
|
|
test("其他日期返回 YYYY-MM-DD", () => {
|
|
expect(formatDateLabel("2026-05-30", now)).toBe("2026-05-30");
|
|
expect(formatDateLabel("2025-01-15", now)).toBe("2025-01-15");
|
|
});
|
|
|
|
test("无效输入返回占位符", () => {
|
|
expect(formatDateLabel("", now)).toBe("—");
|
|
expect(formatDateLabel("not-a-date", now)).toBe("—");
|
|
});
|
|
|
|
test("不传 now 参数使用当前日期", () => {
|
|
const result = formatDateLabel(new Date().toISOString().slice(0, 10));
|
|
expect(result).toBe("今天");
|
|
});
|
|
});
|