Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

messageTimestamp.test.ts

34 lines1.6 KB

1import { describe, expect, it } from "vite-plus/test";
2​
3import { createMessageTimeFormatter, formatMessageTimestamp } from "./messageTimestamp";
4​
5describe("formatMessageTimestamp", () => {
6 const now = new Date(2026, 10, 8, 20, 0).getTime();
7 const time = (date: Date) =>
8 date.toLocaleTimeString(undefined, { hour: "numeric", minute: "2-digit" });
9​
10 it("shows only the time for today, and the day and month for any earlier day", () => {
11 const today = new Date(2026, 10, 8, 0, 5);
12 const yesterday = new Date(2026, 10, 7, 23, 55);
13 const lastYear = new Date(2025, 10, 5, 19, 17);
14 expect(formatMessageTimestamp(today.toISOString(), now)).toBe(time(today));
15 expect(formatMessageTimestamp(yesterday.toISOString(), now)).toBe(
16 `${yesterday.toLocaleDateString(undefined, { month: "numeric", day: "numeric" })} ${time(yesterday)}`,
17 );
18 expect(formatMessageTimestamp(lastYear.toISOString(), now)).toBe(
19 `${lastYear.toLocaleDateString(undefined, { month: "numeric", day: "numeric", year: "numeric" })} ${time(lastYear)}`,
20 );
21 });
22});
23​
24describe("createMessageTimeFormatter", () => {
25 const now = new Date(2026, 0, 1, 20, 0).getTime();
26​
27 it("follows the system clock preference over the locale's hour cycle", () => {
28 const format24 = createMessageTimeFormatter(true, "en-US");
29 expect(format24(new Date(2026, 0, 1, 15, 7).toISOString(), now)).toBe("15:07");
30 expect(format24(new Date(2026, 0, 1, 0, 7).toISOString(), now)).toBe("00:07");
31 const format12 = createMessageTimeFormatter(false, "en-GB");
32 expect(format12(new Date(2026, 0, 1, 15, 7).toISOString(), now)).toMatch(/^3:07\spm$/i);
33 });
34});