|
| 1 | +import { renderHook } from "@testing-library/react"; |
| 2 | +import { describe, expect, it, vi } from "vitest"; |
| 3 | + |
| 4 | +import { useRelativeTime } from "./useRelativeTime"; |
| 5 | + |
| 6 | +describe("useRelativeTime", () => { |
| 7 | + // Mock current date to ensure consistent test results |
| 8 | + const NOW = new Date("2025-07-18T12:00:00.000Z"); |
| 9 | + |
| 10 | + beforeEach(() => { |
| 11 | + vi.useFakeTimers(); |
| 12 | + vi.setSystemTime(NOW); |
| 13 | + }); |
| 14 | + |
| 15 | + afterEach(() => { |
| 16 | + vi.useRealTimers(); |
| 17 | + }); |
| 18 | + |
| 19 | + it("should format seconds ago", () => { |
| 20 | + const date = new Date("2025-07-18T11:59:30.000Z"); // 30 seconds ago |
| 21 | + |
| 22 | + const { result } = renderHook(() => useRelativeTime()); |
| 23 | + const format = result.current; |
| 24 | + |
| 25 | + expect(format(date)).toBe("30 seconds ago"); |
| 26 | + }); |
| 27 | + |
| 28 | + it("should format minutes ago", () => { |
| 29 | + const date = new Date("2025-07-18T11:55:00.000Z"); // 5 minutes ago |
| 30 | + |
| 31 | + const { result } = renderHook(() => useRelativeTime()); |
| 32 | + const format = result.current; |
| 33 | + |
| 34 | + expect(format(date)).toBe("5 minutes ago"); |
| 35 | + }); |
| 36 | + |
| 37 | + it("should format hours ago", () => { |
| 38 | + const date = new Date("2025-07-18T09:00:00.000Z"); // 3 hours ago |
| 39 | + |
| 40 | + const { result } = renderHook(() => useRelativeTime()); |
| 41 | + const format = result.current; |
| 42 | + |
| 43 | + expect(format(date)).toBe("3 hours ago"); |
| 44 | + }); |
| 45 | + |
| 46 | + it("should format days ago", () => { |
| 47 | + const date = new Date("2025-07-16T12:00:00.000Z"); // 2 days ago |
| 48 | + |
| 49 | + const { result } = renderHook(() => useRelativeTime()); |
| 50 | + const format = result.current; |
| 51 | + |
| 52 | + expect(format(date)).toBe("2 days ago"); |
| 53 | + }); |
| 54 | + |
| 55 | + it("should format weeks ago", () => { |
| 56 | + const date = new Date("2025-07-04T12:00:00.000Z"); // 2 weeks ago |
| 57 | + |
| 58 | + const { result } = renderHook(() => useRelativeTime()); |
| 59 | + const format = result.current; |
| 60 | + |
| 61 | + expect(format(date)).toBe("2 weeks ago"); |
| 62 | + }); |
| 63 | + |
| 64 | + it("should format months ago", () => { |
| 65 | + const date = new Date("2025-05-20T12:00:00.000Z"); // 2 months ago |
| 66 | + |
| 67 | + const { result } = renderHook(() => useRelativeTime()); |
| 68 | + const format = result.current; |
| 69 | + |
| 70 | + expect(format(date)).toBe("2 months ago"); |
| 71 | + }); |
| 72 | + |
| 73 | + it("should format years ago", () => { |
| 74 | + const date = new Date("2024-07-18T12:00:00.000Z"); // 1 year ago |
| 75 | + |
| 76 | + const { result } = renderHook(() => useRelativeTime()); |
| 77 | + const format = result.current; |
| 78 | + |
| 79 | + expect(format(date)).toBe("last year"); |
| 80 | + }); |
| 81 | + |
| 82 | + it("should format future dates", () => { |
| 83 | + const date = new Date("2025-07-19T12:00:00.000Z"); // 1 day in future |
| 84 | + |
| 85 | + const { result } = renderHook(() => useRelativeTime()); |
| 86 | + const format = result.current; |
| 87 | + |
| 88 | + expect(format(date)).toBe("tomorrow"); |
| 89 | + }); |
| 90 | + |
| 91 | + it("should handle string date input", () => { |
| 92 | + const date = "2025-07-17T12:00:00.000Z"; // 1 day ago |
| 93 | + |
| 94 | + const { result } = renderHook(() => useRelativeTime()); |
| 95 | + const format = result.current; |
| 96 | + |
| 97 | + expect(format(date)).toBe("yesterday"); |
| 98 | + }); |
| 99 | +}); |
0 commit comments