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

timestampFormat.test.ts

114 lines3.5 KB

1import { afterEach, beforeEach, describe, expect, it, vi } from "vite-plus/test";
2​
3import {
4 formatElapsedDurationLabel,
5 formatExpiresInLabel,
6 formatRelativeTimeUntilLabel,
7 getTimestampFormatOptions,
8} from "./timestampFormat";
9​
10describe("getTimestampFormatOptions", () => {
11 it("omits hour12 when locale formatting is requested", () => {
12 expect(getTimestampFormatOptions("locale", true)).toEqual({
13 hour: "numeric",
14 minute: "2-digit",
15 second: "2-digit",
16 });
17 });
18​
19 it("builds a 12-hour formatter with seconds when requested", () => {
20 expect(getTimestampFormatOptions("12-hour", true)).toEqual({
21 hour: "numeric",
22 minute: "2-digit",
23 second: "2-digit",
24 hour12: true,
25 });
26 });
27​
28 it("builds a 24-hour formatter without seconds when requested", () => {
29 expect(getTimestampFormatOptions("24-hour", false)).toEqual({
30 hour: "numeric",
31 minute: "2-digit",
32 hour12: false,
33 });
34 });
35});
36​
37describe("formatRelativeTimeUntilLabel", () => {
38 beforeEach(() => {
39 vi.useFakeTimers();
40 vi.setSystemTime(new Date("2026-04-07T12:00:00.000Z"));
41 });
42​
43 afterEach(() => {
44 vi.useRealTimers();
45 });
46​
47 it("returns Expired when the instant is in the past", () => {
48 expect(formatRelativeTimeUntilLabel("2026-04-07T11:59:00.000Z")).toBe("Expired");
49 });
50​
51 it("formats seconds remaining", () => {
52 expect(formatRelativeTimeUntilLabel("2026-04-07T12:00:45.000Z")).toBe("45s left");
53 });
54​
55 it("formats minutes remaining", () => {
56 expect(formatRelativeTimeUntilLabel("2026-04-07T12:15:00.000Z")).toBe("15m left");
57 });
58​
59 it("formats hours remaining", () => {
60 expect(formatRelativeTimeUntilLabel("2026-04-07T18:00:00.000Z")).toBe("6h left");
61 });
62});
63​
64describe("formatExpiresInLabel", () => {
65 beforeEach(() => {
66 vi.useFakeTimers();
67 vi.setSystemTime(new Date("2026-04-07T12:00:00.000Z"));
68 });
69​
70 afterEach(() => {
71 vi.useRealTimers();
72 });
73​
74 it("returns Expired when the instant is in the past", () => {
75 expect(formatExpiresInLabel("2026-04-07T11:59:00.000Z")).toBe("Expired");
76 });
77​
78 it("uses sub-minute second count", () => {
79 expect(formatExpiresInLabel("2026-04-07T12:00:45.000Z")).toBe("Expires in 45s");
80 });
81​
82 it("uses minutes and seconds under one hour", () => {
83 expect(formatExpiresInLabel("2026-04-07T12:04:12.000Z")).toBe("Expires in 4m 12s");
84 expect(formatExpiresInLabel("2026-04-07T12:15:00.000Z")).toBe("Expires in 15m");
85 });
86​
87 it("uses hours with minute and second remainder", () => {
88 expect(formatExpiresInLabel("2026-04-07T14:02:03.000Z")).toBe("Expires in 2h 2m 3s");
89 expect(formatExpiresInLabel("2026-04-07T18:00:00.000Z")).toBe("Expires in 6h");
90 });
91});
92​
93describe("formatElapsedDurationLabel", () => {
94 beforeEach(() => {
95 vi.useFakeTimers();
96 vi.setSystemTime(new Date("2026-04-07T12:00:00.000Z"));
97 });
98​
99 afterEach(() => {
100 vi.useRealTimers();
101 });
102​
103 it("returns just now when the instant is current or in the future", () => {
104 expect(formatElapsedDurationLabel("2026-04-07T12:00:00.000Z")).toBe("just now");
105 expect(formatElapsedDurationLabel("2026-04-07T12:01:00.000Z")).toBe("just now");
106 });
107​
108 it("formats seconds, minutes, hours, and days", () => {
109 expect(formatElapsedDurationLabel("2026-04-07T11:59:45.000Z")).toBe("15s");
110 expect(formatElapsedDurationLabel("2026-04-07T11:45:00.000Z")).toBe("15m");
111 expect(formatElapsedDurationLabel("2026-04-07T06:00:00.000Z")).toBe("6h");
112 expect(formatElapsedDurationLabel("2026-04-03T12:00:00.000Z")).toBe("4d");
113 });
114});