|
| 1 | +import { renderHook, waitFor } from "@testing-library/react"; |
| 2 | +import { useWalletStore } from "@/store/walletStore"; |
| 3 | +import { useAuth } from "../useAuth"; |
| 4 | + |
| 5 | +jest.mock("@/store/walletStore", () => ({ |
| 6 | + useWalletStore: jest.fn(), |
| 7 | +})); |
| 8 | + |
| 9 | +const mockUseWalletStore = useWalletStore as jest.MockedFunction< |
| 10 | + typeof useWalletStore |
| 11 | +>; |
| 12 | + |
| 13 | +const connectedAddress = "0x1234567890123456789012345678901234567890"; |
| 14 | + |
| 15 | +const setWalletState = ( |
| 16 | + overrides: Partial<ReturnType<typeof useWalletStore>> = {}, |
| 17 | +) => { |
| 18 | + mockUseWalletStore.mockReturnValue({ |
| 19 | + address: null, |
| 20 | + isConnected: false, |
| 21 | + ...overrides, |
| 22 | + } as ReturnType<typeof useWalletStore>); |
| 23 | +}; |
| 24 | + |
| 25 | +describe("useAuth", () => { |
| 26 | + beforeEach(() => { |
| 27 | + jest.clearAllMocks(); |
| 28 | + document.cookie = ""; |
| 29 | + setWalletState(); |
| 30 | + }); |
| 31 | + |
| 32 | + it("reports an unauthenticated state with no connected wallet and no cookie", async () => { |
| 33 | + const { result } = renderHook(() => useAuth()); |
| 34 | + |
| 35 | + await waitFor(() => expect(result.current.isLoading).toBe(false)); |
| 36 | + |
| 37 | + expect(result.current.isAuthenticated).toBe(false); |
| 38 | + expect(result.current.userAddress).toBeNull(); |
| 39 | + }); |
| 40 | + |
| 41 | + it("does not authenticate from cookie presence alone", async () => { |
| 42 | + document.cookie = "auth-token=garbage-token"; |
| 43 | + const { result } = renderHook(() => useAuth()); |
| 44 | + |
| 45 | + await waitFor(() => expect(result.current.isLoading).toBe(false)); |
| 46 | + |
| 47 | + expect(result.current.isAuthenticated).toBe(false); |
| 48 | + expect(result.current.userAddress).toBeNull(); |
| 49 | + }); |
| 50 | + |
| 51 | + it("does not authenticate from an expired or invalid cookie payload", async () => { |
| 52 | + document.cookie = "auth-token=eyJhbGciOiJIUzI1NiJ9.invalid-payload"; |
| 53 | + const { result } = renderHook(() => useAuth()); |
| 54 | + |
| 55 | + await waitFor(() => expect(result.current.isLoading).toBe(false)); |
| 56 | + |
| 57 | + expect(result.current.isAuthenticated).toBe(false); |
| 58 | + expect(result.current.userAddress).toBeNull(); |
| 59 | + }); |
| 60 | + |
| 61 | + it("authenticates with the connected wallet address", async () => { |
| 62 | + setWalletState({ address: connectedAddress, isConnected: true }); |
| 63 | + const { result } = renderHook(() => useAuth()); |
| 64 | + |
| 65 | + await waitFor(() => expect(result.current.isLoading).toBe(false)); |
| 66 | + |
| 67 | + expect(result.current.isAuthenticated).toBe(true); |
| 68 | + expect(result.current.userAddress).toBe(connectedAddress); |
| 69 | + expect(result.current.userAddress).not.toBe("0x..."); |
| 70 | + }); |
| 71 | + |
| 72 | + it("does not authenticate when the wallet has an address but is disconnected", async () => { |
| 73 | + setWalletState({ address: connectedAddress, isConnected: false }); |
| 74 | + const { result } = renderHook(() => useAuth()); |
| 75 | + |
| 76 | + await waitFor(() => expect(result.current.isLoading).toBe(false)); |
| 77 | + |
| 78 | + expect(result.current.isAuthenticated).toBe(false); |
| 79 | + expect(result.current.userAddress).toBeNull(); |
| 80 | + }); |
| 81 | +}); |
0 commit comments