Files
Alfred/tests/web/utils/date-group.test.ts

105 lines
3.4 KiB
TypeScript

import { describe, expect, test } from "bun:test";
import { getDateGroup, groupByDate } from "../../../src/web/shared/utils/date-group";
function makeDate(daysAgo: number): string {
const d = new Date();
d.setDate(d.getDate() - daysAgo);
return d.toISOString();
}
describe("getDateGroup", () => {
test("今天的日期返回 today", () => {
const now = new Date();
const result = getDateGroup(now.toISOString(), now);
expect(result).toBe("today");
});
test("昨天的日期返回 yesterday", () => {
const now = new Date();
const yesterday = new Date(now);
yesterday.setDate(yesterday.getDate() - 1);
const result = getDateGroup(yesterday.toISOString(), now);
expect(result).toBe("yesterday");
});
test("本周内的日期返回 thisWeek", () => {
const now = new Date();
const dayOfWeek = now.getDay();
const mondayOffset = dayOfWeek === 0 ? 6 : dayOfWeek - 1;
const wednesday = new Date(now);
wednesday.setDate(now.getDate() - (dayOfWeek === 0 ? 6 : dayOfWeek - 1) + 2);
if (wednesday > now) {
const tuesday = new Date(now);
tuesday.setDate(now.getDate() - mondayOffset + 1);
if (tuesday < now && tuesday.getDate() !== now.getDate() - 1) {
const result = getDateGroup(tuesday.toISOString(), now);
expect(result).toBe("thisWeek");
return;
}
}
const tuesday = new Date(now);
tuesday.setDate(now.getDate() - mondayOffset + 1);
if (tuesday.toDateString() !== now.toDateString()) {
const yesterday = new Date(now);
yesterday.setDate(yesterday.getDate() - 1);
if (tuesday.toDateString() !== yesterday.toDateString()) {
const result = getDateGroup(tuesday.toISOString(), now);
expect(result).toBe("thisWeek");
return;
}
}
expect(true).toBe(true);
});
test("本月内的日期返回 thisMonth", () => {
const now = new Date(2026, 5, 15);
const earlier = new Date(2026, 5, 3);
const result = getDateGroup(earlier.toISOString(), now);
expect(result).toBe("thisMonth");
});
test("更早的日期返回 earlier", () => {
const now = new Date(2026, 5, 15);
const earlier = new Date(2026, 3, 1);
const result = getDateGroup(earlier.toISOString(), now);
expect(result).toBe("earlier");
});
});
describe("groupByDate", () => {
test("按 dateField 分组并返回有序结果", () => {
const now = new Date();
const items = [
{ id: "1", title: "今天", updatedAt: now.toISOString() },
{ id: "2", title: "昨天", updatedAt: makeDate(1) },
{ id: "3", title: "更早", updatedAt: makeDate(60) },
];
const groups = groupByDate(items, "updatedAt");
const todayGroup = groups.find((g) => g.key === "today")!;
expect(todayGroup.items.length).toBe(1);
expect(todayGroup.items[0]!.id).toBe("1");
const yesterdayGroup = groups.find((g) => g.key === "yesterday")!;
expect(yesterdayGroup.items.length).toBe(1);
const earlierGroup = groups.find((g) => g.key === "earlier")!;
expect(earlierGroup.items.length).toBe(1);
const emptyGroups = groups.filter((g) => g.key === "thisWeek" || g.key === "thisMonth");
emptyGroups.forEach((g) => {
expect(g.items.length).toBe(0);
});
});
test("空数组返回所有空分组", () => {
const groups = groupByDate([] as Array<{ updatedAt: string }>, "updatedAt");
expect(groups.length).toBe(5);
groups.forEach((g) => {
expect(g.items.length).toBe(0);
});
});
});