|
| 1 | +import { render, screen, act } from "@testing-library/react"; |
| 2 | +import { describe, it, expect, vi, beforeEach, afterEach } from "vitest"; |
| 3 | +import { ExpiryCountdown } from "./ExpiryCountdown"; |
| 4 | + |
| 5 | +describe("ExpiryCountdown", () => { |
| 6 | + beforeEach(() => { |
| 7 | + vi.useFakeTimers(); |
| 8 | + }); |
| 9 | + |
| 10 | + afterEach(() => { |
| 11 | + vi.restoreAllMocks(); |
| 12 | + }); |
| 13 | + |
| 14 | + it("returns null if no expiresAt is provided", () => { |
| 15 | + const { container } = render(<ExpiryCountdown expiresAt={null} />); |
| 16 | + expect(container.firstChild).toBeNull(); |
| 17 | + }); |
| 18 | + |
| 19 | + it("displays correct time remaining", () => { |
| 20 | + const now = new Date("2026-07-25T10:00:00Z").getTime(); |
| 21 | + vi.setSystemTime(now); |
| 22 | + |
| 23 | + // 1 hour, 30 minutes, 15 seconds from now |
| 24 | + const expiresAt = new Date(now + 1000 * (15 + 60 * 30 + 60 * 60 * 1)).toISOString(); |
| 25 | + |
| 26 | + render(<ExpiryCountdown expiresAt={expiresAt} />); |
| 27 | + |
| 28 | + const countdown = screen.getByTestId("countdown-timer"); |
| 29 | + expect(countdown.textContent).toContain("1h 30m 15s remaining"); |
| 30 | + }); |
| 31 | + |
| 32 | + it("updates countdown over time", () => { |
| 33 | + const now = new Date("2026-07-25T10:00:00Z").getTime(); |
| 34 | + vi.setSystemTime(now); |
| 35 | + |
| 36 | + const expiresAt = new Date(now + 10000).toISOString(); // 10 seconds |
| 37 | + |
| 38 | + render(<ExpiryCountdown expiresAt={expiresAt} />); |
| 39 | + |
| 40 | + expect(screen.getByTestId("countdown-timer").textContent).toContain("10s remaining"); |
| 41 | + |
| 42 | + act(() => { |
| 43 | + vi.advanceTimersByTime(2000); |
| 44 | + }); |
| 45 | + |
| 46 | + expect(screen.getByTestId("countdown-timer").textContent).toContain("8s remaining"); |
| 47 | + }); |
| 48 | + |
| 49 | + it("shows Expired badge when time is up", () => { |
| 50 | + const now = new Date("2026-07-25T10:00:00Z").getTime(); |
| 51 | + vi.setSystemTime(now); |
| 52 | + |
| 53 | + const expiresAt = new Date(now - 1000).toISOString(); // 1 second ago |
| 54 | + |
| 55 | + render(<ExpiryCountdown expiresAt={expiresAt} />); |
| 56 | + |
| 57 | + const badge = screen.getByTestId("expired-badge"); |
| 58 | + expect(badge).toBeDefined(); |
| 59 | + expect(badge.textContent).toBe("Expired"); |
| 60 | + }); |
| 61 | + |
| 62 | + it("changes color based on time remaining", () => { |
| 63 | + const now = new Date("2026-07-25T10:00:00Z").getTime(); |
| 64 | + vi.setSystemTime(now); |
| 65 | + |
| 66 | + // Red: < 5 minutes |
| 67 | + const expiresAtRed = new Date(now + 1000 * 60 * 4).toISOString(); |
| 68 | + const { rerender } = render(<ExpiryCountdown expiresAt={expiresAtRed} />); |
| 69 | + expect(screen.getByTestId("countdown-timer").className).toContain("text-red-500"); |
| 70 | + |
| 71 | + // Orange: < 1 hour |
| 72 | + const expiresAtOrange = new Date(now + 1000 * 60 * 30).toISOString(); |
| 73 | + rerender(<ExpiryCountdown expiresAt={expiresAtOrange} />); |
| 74 | + expect(screen.getByTestId("countdown-timer").className).toContain("text-orange-500"); |
| 75 | + |
| 76 | + // Yellow: < 24 hours |
| 77 | + const expiresAtYellow = new Date(now + 1000 * 60 * 60 * 12).toISOString(); |
| 78 | + rerender(<ExpiryCountdown expiresAt={expiresAtYellow} />); |
| 79 | + expect(screen.getByTestId("countdown-timer").className).toContain("text-yellow-500"); |
| 80 | + |
| 81 | + // Green: >= 24 hours |
| 82 | + const expiresAtGreen = new Date(now + 1000 * 60 * 60 * 48).toISOString(); |
| 83 | + rerender(<ExpiryCountdown expiresAt={expiresAtGreen} />); |
| 84 | + expect(screen.getByTestId("countdown-timer").className).toContain("text-green-500"); |
| 85 | + }); |
| 86 | +}); |
0 commit comments