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

timestampFormat.test.ts

224 lines7.3 KB

1import { afterEach, beforeEach, describe, expect, it, vi } from "vite-plus/test";
2​
3import {
4 formatDayAwareTimestamp,
5 formatElapsedDurationLabel,
6 formatExpiresInLabel,
7 formatRelativeTime,
8 formatRelativeTimeLabel,
9 formatRelativeTimeUntil,
10 formatRelativeTimeUntilLabel,
11 formatShortTimestamp,
12 formatTimestamp,
13 getRelativeTimeState,
14 getTimestampFormatOptions,
15} from "./timestampFormat";
16​
17describe("getTimestampFormatOptions", () => {
18 it("omits hour12 when locale formatting is requested", () => {
19 expect(getTimestampFormatOptions("locale", true)).toEqual({
20 hour: "numeric",
21 minute: "2-digit",
22 second: "2-digit",
23 });
24 });
25​
26 it("builds a 12-hour formatter with seconds when requested", () => {
27 expect(getTimestampFormatOptions("12-hour", true)).toEqual({
28 hour: "numeric",
29 minute: "2-digit",
30 second: "2-digit",
31 hour12: true,
32 });
33 });
34​
35 it("builds a 24-hour formatter without seconds when requested", () => {
36 expect(getTimestampFormatOptions("24-hour", false)).toEqual({
37 hour: "numeric",
38 minute: "2-digit",
39 hour12: false,
40 });
41 });
42});
43​
44describe("formatRelativeTimeUntilLabel", () => {
45 beforeEach(() => {
46 vi.useFakeTimers();
47 vi.setSystemTime(new Date("2026-04-07T12:00:00.000Z"));
48 });
49​
50 afterEach(() => {
51 vi.useRealTimers();
52 });
53​
54 it("returns Expired when the instant is in the past", () => {
55 expect(formatRelativeTimeUntilLabel("2026-04-07T11:59:00.000Z")).toBe("Expired");
56 });
57​
58 it("formats seconds remaining", () => {
59 expect(formatRelativeTimeUntilLabel("2026-04-07T12:00:45.000Z")).toBe("45s left");
60 });
61​
62 it("formats minutes remaining", () => {
63 expect(formatRelativeTimeUntilLabel("2026-04-07T12:15:00.000Z")).toBe("15m left");
64 });
65​
66 it("formats hours remaining", () => {
67 expect(formatRelativeTimeUntilLabel("2026-04-07T18:00:00.000Z")).toBe("6h left");
68 });
69});
70​
71describe("formatExpiresInLabel", () => {
72 beforeEach(() => {
73 vi.useFakeTimers();
74 vi.setSystemTime(new Date("2026-04-07T12:00:00.000Z"));
75 });
76​
77 afterEach(() => {
78 vi.useRealTimers();
79 });
80​
81 it("returns Expired when the instant is in the past", () => {
82 expect(formatExpiresInLabel("2026-04-07T11:59:00.000Z")).toBe("Expired");
83 });
84​
85 it("uses sub-minute second count", () => {
86 expect(formatExpiresInLabel("2026-04-07T12:00:45.000Z")).toBe("Expires in 45s");
87 });
88​
89 it("uses minutes and seconds under one hour", () => {
90 expect(formatExpiresInLabel("2026-04-07T12:04:12.000Z")).toBe("Expires in 4m 12s");
91 expect(formatExpiresInLabel("2026-04-07T12:15:00.000Z")).toBe("Expires in 15m");
92 });
93​
94 it("uses hours with minute and second remainder", () => {
95 expect(formatExpiresInLabel("2026-04-07T14:02:03.000Z")).toBe("Expires in 2h 2m 3s");
96 expect(formatExpiresInLabel("2026-04-07T18:00:00.000Z")).toBe("Expires in 6h");
97 });
98});
99​
100describe("formatDayAwareTimestamp", () => {
101 // Instants are built with the local-time Date constructor so the
102 // calendar-day boundaries hold in any test timezone or locale.
103 const iso = (y: number, monthIndex: number, d: number, h: number, mi: number) =>
104 new Date(y, monthIndex, d, h, mi).toISOString();
105 const now = new Date(2026, 7, 14, 12, 0).getTime();
106 const time = (isoDate: string) => formatShortTimestamp(isoDate, "12-hour");
107​
108 it("shows time only for today", () => {
109 const messageAt = iso(2026, 7, 14, 9, 30);
110 expect(formatDayAwareTimestamp(messageAt, "12-hour", now)).toBe(time(messageAt));
111 });
112​
113 it("labels the previous calendar day as yesterday even when under 24h old", () => {
114 const messageAt = iso(2026, 7, 13, 23, 30);
115 const justPastMidnight = new Date(2026, 7, 14, 0, 30).getTime();
116 expect(formatDayAwareTimestamp(messageAt, "12-hour", justPastMidnight)).toBe(
117 `yesterday at ${time(messageAt)}`,
118 );
119 });
120​
121 it("prefixes older same-year messages with the numeric date", () => {
122 const messageAt = iso(2026, 7, 12, 12, 34);
123 const datePart = new Intl.DateTimeFormat(undefined, {
124 month: "numeric",
125 day: "numeric",
126 }).format(new Date(messageAt));
127 expect(formatDayAwareTimestamp(messageAt, "12-hour", now)).toBe(
128 `${datePart} ${time(messageAt)}`,
129 );
130 });
131​
132 it("includes the year once the calendar year differs", () => {
133 const messageAt = iso(2025, 11, 31, 18, 0);
134 const datePart = new Intl.DateTimeFormat(undefined, {
135 month: "numeric",
136 day: "numeric",
137 year: "numeric",
138 }).format(new Date(messageAt));
139 expect(formatDayAwareTimestamp(messageAt, "12-hour", now)).toBe(
140 `${datePart} ${time(messageAt)}`,
141 );
142 });
143​
144 it("returns an empty string for invalid input", () => {
145 expect(formatDayAwareTimestamp("not-a-date", "12-hour", now)).toBe("");
146 });
147});
148​
149describe("invalid timestamp inputs", () => {
150 it("returns an empty timestamp instead of throwing", () => {
151 expect(() => formatTimestamp("not-a-date", "12-hour")).not.toThrow();
152 expect(formatTimestamp("not-a-date", "12-hour")).toBe("");
153 });
154​
155 it("returns an empty short timestamp instead of throwing", () => {
156 expect(() => formatShortTimestamp("not-a-date", "12-hour")).not.toThrow();
157 expect(formatShortTimestamp("not-a-date", "12-hour")).toBe("");
158 });
159​
160 it("returns an empty relative time label instead of a NaN label", () => {
161 expect(formatRelativeTime("not-a-date")).toBeNull();
162 expect(formatRelativeTimeLabel("not-a-date")).toBe("");
163 });
164​
165 it("distinguishes missing and invalid relative time state", () => {
166 expect(getRelativeTimeState(null)).toEqual({ status: "missing" });
167 expect(getRelativeTimeState("not-a-date")).toEqual({ status: "invalid" });
168 });
169​
170 it("returns an empty elapsed duration instead of a NaN label", () => {
171 expect(formatElapsedDurationLabel("not-a-date")).toBe("");
172 });
173​
174 it("returns an empty relative time until label instead of a NaN label", () => {
175 expect(formatRelativeTimeUntil("not-a-date")).toBeNull();
176 expect(formatRelativeTimeUntilLabel("not-a-date")).toBe("");
177 });
178​
179 it("returns an empty expires-in label instead of a NaN label", () => {
180 expect(formatExpiresInLabel("not-a-date")).toBe("");
181 });
182});
183​
184describe("getRelativeTimeState", () => {
185 beforeEach(() => {
186 vi.useFakeTimers();
187 vi.setSystemTime(new Date("2026-04-07T12:00:00.000Z"));
188 });
189​
190 afterEach(() => {
191 vi.useRealTimers();
192 });
193​
194 it("returns relative parts for valid timestamps", () => {
195 expect(getRelativeTimeState("2026-04-07T11:45:00.000Z")).toEqual({
196 status: "relative",
197 value: "15m",
198 suffix: "ago",
199 });
200 });
201});
202​
203describe("formatElapsedDurationLabel", () => {
204 beforeEach(() => {
205 vi.useFakeTimers();
206 vi.setSystemTime(new Date("2026-04-07T12:00:00.000Z"));
207 });
208​
209 afterEach(() => {
210 vi.useRealTimers();
211 });
212​
213 it("returns just now when the instant is current or in the future", () => {
214 expect(formatElapsedDurationLabel("2026-04-07T12:00:00.000Z")).toBe("just now");
215 expect(formatElapsedDurationLabel("2026-04-07T12:01:00.000Z")).toBe("just now");
216 });
217​
218 it("formats seconds, minutes, hours, and days", () => {
219 expect(formatElapsedDurationLabel("2026-04-07T11:59:45.000Z")).toBe("15s");
220 expect(formatElapsedDurationLabel("2026-04-07T11:45:00.000Z")).toBe("15m");
221 expect(formatElapsedDurationLabel("2026-04-07T06:00:00.000Z")).toBe("6h");
222 expect(formatElapsedDurationLabel("2026-04-03T12:00:00.000Z")).toBe("4d");
223 });
224});