Skip to content
Source
Sign inSign up
fuko/trytanstackdb.com
Open History to see the last commit

dates.test.ts

35 lines1.2 KB

1import { describe, expect, test } from "vite-plus/test";
2​
3import { dateGroupLabel, formatListDate, groupByDate } from "./dates";
4​
5const now = new Date(2026, 10, 3, 20, 40);
6const at = (month: number, day: number, hour = 12, year = 2026) => new Date(year, month, day, hour);
7​
8describe("dateGroupLabel", () => {
9 test.each([
10 [at(10, 3, 9), "Today"],
11 [at(10, 2), "Yesterday"],
12 [at(9, 29), "Previous 7 Days"],
13 [at(9, 10), "Previous 30 Days"],
14 [at(6, 1), "July"],
15 [at(4, 1, 12, 2025), "2025"],
16 ])("%s → %s", (date, label) => {
17 expect(dateGroupLabel(date, now)).toBe(label);
18 });
19});
20​
21test("formatListDate", () => {
22 expect(formatListDate(at(10, 3, 9), now)).toBe("09:00");
23 expect(formatListDate(at(10, 2), now)).toBe("Yesterday");
24 expect(formatListDate(at(9, 31), now)).toBe("Saturday");
25 expect(formatListDate(at(8, 12), now)).toBe("12/09/2026");
26});
27​
28test("groupByDate keeps consecutive items together", () => {
29 const groups = groupByDate([at(10, 3), at(10, 3, 8), at(10, 2), at(9, 1)], (d) => d, now);
30 expect(groups.map((group) => [group.label, group.items.length])).toEqual([
31 ["Today", 2],
32 ["Yesterday", 1],
33 ["October", 1],
34 ]);
35});