|
| 1 | +import { describe, expect, it } from "vitest"; |
| 2 | +import { |
| 3 | + type FormatTimestampOptions, |
| 4 | + formatTimestamp, |
| 5 | +} from "./format-timestamp"; |
| 6 | + |
| 7 | +const today = new Date("2025-06-02T12:00:00.000Z"); |
| 8 | + |
| 9 | +const defaultOptions: FormatTimestampOptions = { |
| 10 | + today, |
| 11 | + locale: "en-US", |
| 12 | +}; |
| 13 | + |
| 14 | +describe("format timestamps", () => { |
| 15 | + describe("past timestamps", () => { |
| 16 | + it("format just now", () => { |
| 17 | + const timestamp = new Date("2025-06-02T11:59:59.500Z"); |
| 18 | + expect(formatTimestamp(timestamp, defaultOptions)).toBe("Just now"); |
| 19 | + }); |
| 20 | + |
| 21 | + it("format seconds ago", () => { |
| 22 | + const timestamp = new Date("2025-06-02T11:59:30.000Z"); |
| 23 | + expect(formatTimestamp(timestamp, defaultOptions)).toBe("30s ago"); |
| 24 | + }); |
| 25 | + |
| 26 | + it("format minutes ago", () => { |
| 27 | + const timestamp = new Date("2025-06-02T11:30:00.000Z"); |
| 28 | + expect(formatTimestamp(timestamp, defaultOptions)).toBe("30m ago"); |
| 29 | + }); |
| 30 | + |
| 31 | + it("format hours ago", () => { |
| 32 | + const timestamp = new Date("2025-06-02T09:00:00.000Z"); |
| 33 | + expect(formatTimestamp(timestamp, defaultOptions)).toBe("3h ago"); |
| 34 | + }); |
| 35 | + |
| 36 | + it("format this year", () => { |
| 37 | + const timestamp = new Date("2025-06-01T12:00:00.000Z"); |
| 38 | + expect(formatTimestamp(timestamp, defaultOptions)).toBe("Jun 1"); |
| 39 | + }); |
| 40 | + |
| 41 | + it("format last year", () => { |
| 42 | + const timestamp = new Date("2024-06-01T12:00:00.000Z"); |
| 43 | + expect(formatTimestamp(timestamp, defaultOptions)).toBe("Jun 1, 2024"); |
| 44 | + }); |
| 45 | + }); |
| 46 | + |
| 47 | + describe("future timestamps", () => { |
| 48 | + it("format just now", () => { |
| 49 | + const timestamp = new Date("2025-06-02T12:00:00.000Z"); |
| 50 | + expect(formatTimestamp(timestamp, defaultOptions)).toBe("Just now"); |
| 51 | + }); |
| 52 | + |
| 53 | + it("format seconds from now", () => { |
| 54 | + const timestamp = new Date("2025-06-02T12:00:30.000Z"); |
| 55 | + expect(formatTimestamp(timestamp, defaultOptions)).toBe("30s from now"); |
| 56 | + }); |
| 57 | + |
| 58 | + it("format minutes from now", () => { |
| 59 | + const timestamp = new Date("2025-06-02T12:30:00.000Z"); |
| 60 | + expect(formatTimestamp(timestamp, defaultOptions)).toBe("30m from now"); |
| 61 | + }); |
| 62 | + |
| 63 | + it("format hours from now", () => { |
| 64 | + const timestamp = new Date("2025-06-02T15:00:00.000Z"); |
| 65 | + expect(formatTimestamp(timestamp, defaultOptions)).toBe("3h from now"); |
| 66 | + }); |
| 67 | + |
| 68 | + it("format this year", () => { |
| 69 | + const timestamp = new Date("2025-06-03T12:00:00.000Z"); |
| 70 | + expect(formatTimestamp(timestamp, defaultOptions)).toBe("Jun 3"); |
| 71 | + }); |
| 72 | + |
| 73 | + it("format last year", () => { |
| 74 | + const timestamp = new Date("2024-06-03T12:00:00.000Z"); |
| 75 | + expect(formatTimestamp(timestamp, defaultOptions)).toBe("Jun 3, 2024"); |
| 76 | + }); |
| 77 | + }); |
| 78 | + |
| 79 | + describe("custom options", () => { |
| 80 | + it("format with custom locale", () => { |
| 81 | + const timestamp = new Date("2025-06-01T12:00:00.000Z"); |
| 82 | + const options: FormatTimestampOptions = { |
| 83 | + ...defaultOptions, |
| 84 | + locale: "en-GB", |
| 85 | + }; |
| 86 | + expect(formatTimestamp(timestamp, options)).toBe("1 Jun"); |
| 87 | + }); |
| 88 | + |
| 89 | + it("automatic locale detection", () => { |
| 90 | + const timestamp = new Date("2025-06-01T12:00:00.000Z"); |
| 91 | + expect( |
| 92 | + formatTimestamp(timestamp, { |
| 93 | + today, |
| 94 | + }), |
| 95 | + ).toBe("Jun 1"); |
| 96 | + }); |
| 97 | + }); |
| 98 | +}); |
0 commit comments