|
| 1 | +import { renderHook, act } from "@testing-library/react-native"; |
| 2 | +import { useCountdown } from "@/hooks/use-countdown"; |
| 3 | + |
| 4 | +describe("useCountdown", () => { |
| 5 | + beforeEach(() => { |
| 6 | + jest.useFakeTimers(); |
| 7 | + }); |
| 8 | + |
| 9 | + afterEach(() => { |
| 10 | + jest.useRealTimers(); |
| 11 | + }); |
| 12 | + |
| 13 | + it("returns inactive state when expiresAt is null", () => { |
| 14 | + const { result } = renderHook(() => useCountdown({ expiresAt: null })); |
| 15 | + |
| 16 | + expect(result.current.remainingSeconds).toBe(0); |
| 17 | + expect(result.current.isExpired).toBe(false); |
| 18 | + expect(result.current.isActive).toBe(false); |
| 19 | + }); |
| 20 | + |
| 21 | + it("calculates remaining seconds from expiresAt", () => { |
| 22 | + const now = Date.now() / 1000; |
| 23 | + const expiresAt = now + 120; // 2 minutes from now |
| 24 | + |
| 25 | + const { result } = renderHook(() => useCountdown({ expiresAt })); |
| 26 | + |
| 27 | + expect(result.current.remainingSeconds).toBe(120); |
| 28 | + expect(result.current.isActive).toBe(true); |
| 29 | + expect(result.current.isExpired).toBe(false); |
| 30 | + }); |
| 31 | + |
| 32 | + it("counts down every second", () => { |
| 33 | + const now = Date.now() / 1000; |
| 34 | + const expiresAt = now + 10; |
| 35 | + |
| 36 | + const { result } = renderHook(() => useCountdown({ expiresAt })); |
| 37 | + |
| 38 | + expect(result.current.remainingSeconds).toBe(10); |
| 39 | + |
| 40 | + act(() => { |
| 41 | + jest.advanceTimersByTime(1000); |
| 42 | + }); |
| 43 | + expect(result.current.remainingSeconds).toBe(9); |
| 44 | + |
| 45 | + act(() => { |
| 46 | + jest.advanceTimersByTime(3000); |
| 47 | + }); |
| 48 | + expect(result.current.remainingSeconds).toBe(6); |
| 49 | + }); |
| 50 | + |
| 51 | + it("calls onExpired exactly once when timer reaches zero", () => { |
| 52 | + const onExpired = jest.fn(); |
| 53 | + const now = Date.now() / 1000; |
| 54 | + const expiresAt = now + 3; |
| 55 | + |
| 56 | + renderHook(() => useCountdown({ expiresAt, onExpired })); |
| 57 | + |
| 58 | + act(() => { |
| 59 | + jest.advanceTimersByTime(3000); |
| 60 | + }); |
| 61 | + |
| 62 | + expect(onExpired).toHaveBeenCalledTimes(1); |
| 63 | + |
| 64 | + // Advancing further should not call again |
| 65 | + act(() => { |
| 66 | + jest.advanceTimersByTime(5000); |
| 67 | + }); |
| 68 | + |
| 69 | + expect(onExpired).toHaveBeenCalledTimes(1); |
| 70 | + }); |
| 71 | + |
| 72 | + it("does not call onExpired when expiresAt is null", () => { |
| 73 | + const onExpired = jest.fn(); |
| 74 | + |
| 75 | + renderHook(() => useCountdown({ expiresAt: null, onExpired })); |
| 76 | + |
| 77 | + act(() => { |
| 78 | + jest.advanceTimersByTime(5000); |
| 79 | + }); |
| 80 | + |
| 81 | + expect(onExpired).not.toHaveBeenCalled(); |
| 82 | + }); |
| 83 | + |
| 84 | + it("returns isExpired true when expiresAt is in the past", () => { |
| 85 | + const now = Date.now() / 1000; |
| 86 | + const expiresAt = now - 10; // 10 seconds ago |
| 87 | + |
| 88 | + const { result } = renderHook(() => useCountdown({ expiresAt })); |
| 89 | + |
| 90 | + expect(result.current.remainingSeconds).toBe(0); |
| 91 | + expect(result.current.isExpired).toBe(true); |
| 92 | + expect(result.current.isActive).toBe(false); |
| 93 | + }); |
| 94 | + |
| 95 | + it("fires onExpired when expiresAt is already in the past", () => { |
| 96 | + const onExpired = jest.fn(); |
| 97 | + const now = Date.now() / 1000; |
| 98 | + const expiresAt = now - 5; |
| 99 | + |
| 100 | + renderHook(() => useCountdown({ expiresAt, onExpired })); |
| 101 | + |
| 102 | + act(() => { |
| 103 | + jest.advanceTimersByTime(1000); |
| 104 | + }); |
| 105 | + |
| 106 | + expect(onExpired).toHaveBeenCalledTimes(1); |
| 107 | + }); |
| 108 | + |
| 109 | + it("resets when expiresAt changes", () => { |
| 110 | + const now = Date.now() / 1000; |
| 111 | + let expiresAt: number | null = now + 10; |
| 112 | + |
| 113 | + const { result, rerender } = renderHook(() => useCountdown({ expiresAt })); |
| 114 | + |
| 115 | + expect(result.current.remainingSeconds).toBe(10); |
| 116 | + |
| 117 | + act(() => { |
| 118 | + jest.advanceTimersByTime(5000); |
| 119 | + }); |
| 120 | + expect(result.current.remainingSeconds).toBe(5); |
| 121 | + |
| 122 | + // Change expiresAt to a new value |
| 123 | + expiresAt = now + 60; |
| 124 | + rerender({}); |
| 125 | + |
| 126 | + expect(result.current.remainingSeconds).toBe(55); // 60 - 5 seconds elapsed |
| 127 | + expect(result.current.isActive).toBe(true); |
| 128 | + }); |
| 129 | +}); |
0 commit comments