Open History to see the last commit
1import { afterEach, beforeEach, describe, expect, it, vi } from "vite-plus/test";
2
3import {
4 formatElapsedDurationLabel,
5 formatExpiresInLabel,
6 formatRelativeTime,
7 formatRelativeTimeLabel,
8 formatRelativeTimeUntil,
9 formatRelativeTimeUntilLabel,
10 formatShortTimestamp,
11 formatTimestamp,
12 getRelativeTimeState,
13 getTimestampFormatOptions,
14} from "./timestampFormat";
15
16describe("getTimestampFormatOptions", () => {
17 it("omits hour12 when locale formatting is requested", () => {
18 expect(getTimestampFormatOptions("locale", true)).toEqual({
19 hour: "numeric",
20 minute: "2-digit",
21 second: "2-digit",
22 });
23 });
24
25 it("builds a 12-hour formatter with seconds when requested", () => {
26 expect(getTimestampFormatOptions("12-hour", true)).toEqual({
27 hour: "numeric",
28 minute: "2-digit",
29 second: "2-digit",
30 hour12: true,
31 });
32 });
33
34 it("builds a 24-hour formatter without seconds when requested", () => {
35 expect(getTimestampFormatOptions("24-hour", false)).toEqual({
36 hour: "numeric",
37 minute: "2-digit",
38 hour12: false,
39 });
40 });
41});
42
43describe("formatRelativeTimeUntilLabel", () => {
44 beforeEach(() => {
45 vi.useFakeTimers();
46 vi.setSystemTime(new Date("2026-04-07T12:00:00.000Z"));
47 });
48
49 afterEach(() => {
50 vi.useRealTimers();
51 });
52
53 it("returns Expired when the instant is in the past", () => {
54 expect(formatRelativeTimeUntilLabel("2026-04-07T11:59:00.000Z")).toBe("Expired");
55 });
56
57 it("formats seconds remaining", () => {
58 expect(formatRelativeTimeUntilLabel("2026-04-07T12:00:45.000Z")).toBe("45s left");
59 });
60
61 it("formats minutes remaining", () => {
62 expect(formatRelativeTimeUntilLabel("2026-04-07T12:15:00.000Z")).toBe("15m left");
63 });
64
65 it("formats hours remaining", () => {
66 expect(formatRelativeTimeUntilLabel("2026-04-07T18:00:00.000Z")).toBe("6h left");
67 });
68});
69
70describe("formatExpiresInLabel", () => {
71 beforeEach(() => {
72 vi.useFakeTimers();
73 vi.setSystemTime(new Date("2026-04-07T12:00:00.000Z"));
74 });
75
76 afterEach(() => {
77 vi.useRealTimers();
78 });
79
80 it("returns Expired when the instant is in the past", () => {
81 expect(formatExpiresInLabel("2026-04-07T11:59:00.000Z")).toBe("Expired");
82 });
83
84 it("uses sub-minute second count", () => {
85 expect(formatExpiresInLabel("2026-04-07T12:00:45.000Z")).toBe("Expires in 45s");
86 });
87
88 it("uses minutes and seconds under one hour", () => {
89 expect(formatExpiresInLabel("2026-04-07T12:04:12.000Z")).toBe("Expires in 4m 12s");
90 expect(formatExpiresInLabel("2026-04-07T12:15:00.000Z")).toBe("Expires in 15m");
91 });
92
93 it("uses hours with minute and second remainder", () => {
94 expect(formatExpiresInLabel("2026-04-07T14:02:03.000Z")).toBe("Expires in 2h 2m 3s");
95 expect(formatExpiresInLabel("2026-04-07T18:00:00.000Z")).toBe("Expires in 6h");
96 });
97});
98
99describe("invalid timestamp inputs", () => {
100 it("returns an empty timestamp instead of throwing", () => {
101 expect(() => formatTimestamp("not-a-date", "12-hour")).not.toThrow();
102 expect(formatTimestamp("not-a-date", "12-hour")).toBe("");
103 });
104
105 it("returns an empty short timestamp instead of throwing", () => {
106 expect(() => formatShortTimestamp("not-a-date", "12-hour")).not.toThrow();
107 expect(formatShortTimestamp("not-a-date", "12-hour")).toBe("");
108 });
109
110 it("returns an empty relative time label instead of a NaN label", () => {
111 expect(formatRelativeTime("not-a-date")).toBeNull();
112 expect(formatRelativeTimeLabel("not-a-date")).toBe("");
113 });
114
115 it("distinguishes missing and invalid relative time state", () => {
116 expect(getRelativeTimeState(null)).toEqual({ status: "missing" });
117 expect(getRelativeTimeState("not-a-date")).toEqual({ status: "invalid" });
118 });
119
120 it("returns an empty elapsed duration instead of a NaN label", () => {
121 expect(formatElapsedDurationLabel("not-a-date")).toBe("");
122 });
123
124 it("returns an empty relative time until label instead of a NaN label", () => {
125 expect(formatRelativeTimeUntil("not-a-date")).toBeNull();
126 expect(formatRelativeTimeUntilLabel("not-a-date")).toBe("");
127 });
128
129 it("returns an empty expires-in label instead of a NaN label", () => {
130 expect(formatExpiresInLabel("not-a-date")).toBe("");
131 });
132});
133
134describe("getRelativeTimeState", () => {
135 beforeEach(() => {
136 vi.useFakeTimers();
137 vi.setSystemTime(new Date("2026-04-07T12:00:00.000Z"));
138 });
139
140 afterEach(() => {
141 vi.useRealTimers();
142 });
143
144 it("returns relative parts for valid timestamps", () => {
145 expect(getRelativeTimeState("2026-04-07T11:45:00.000Z")).toEqual({
146 status: "relative",
147 value: "15m",
148 suffix: "ago",
149 });
150 });
151});
152
153describe("formatElapsedDurationLabel", () => {
154 beforeEach(() => {
155 vi.useFakeTimers();
156 vi.setSystemTime(new Date("2026-04-07T12:00:00.000Z"));
157 });
158
159 afterEach(() => {
160 vi.useRealTimers();
161 });
162
163 it("returns just now when the instant is current or in the future", () => {
164 expect(formatElapsedDurationLabel("2026-04-07T12:00:00.000Z")).toBe("just now");
165 expect(formatElapsedDurationLabel("2026-04-07T12:01:00.000Z")).toBe("just now");
166 });
167
168 it("formats seconds, minutes, hours, and days", () => {
169 expect(formatElapsedDurationLabel("2026-04-07T11:59:45.000Z")).toBe("15s");
170 expect(formatElapsedDurationLabel("2026-04-07T11:45:00.000Z")).toBe("15m");
171 expect(formatElapsedDurationLabel("2026-04-07T06:00:00.000Z")).toBe("6h");
172 expect(formatElapsedDurationLabel("2026-04-03T12:00:00.000Z")).toBe("4d");
173 });
174});