Skip to content
Source
Sign inSign up

messageTimestamp.test.ts

22 lines1.0 KB

1import { describe, expect, it } from "vite-plus/test";
2​
3import { 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});