|
| 1 | +import { userEvent } from "@testing-library/user-event"; |
| 2 | +import { beforeEach, describe, expect, it, vi } from "vitest"; |
| 3 | +import { TEST_ACCOUNT_A } from "~test/test-wallets.js"; |
| 4 | +import { render, waitFor } from "../../../../../../test/src/react-render.js"; |
| 5 | +import { TEST_CLIENT } from "../../../../../../test/src/test-clients.js"; |
| 6 | +import { createWallet } from "../../../../../wallets/create-wallet.js"; |
| 7 | +import { useActiveWallet } from "../../../../core/hooks/wallets/useActiveWallet.js"; |
| 8 | +import type { ConnectLocale } from "../locale/types.js"; |
| 9 | +import { SignatureScreen } from "./SignatureScreen.js"; |
| 10 | + |
| 11 | +const mockAuth = vi.hoisted(() => ({ |
| 12 | + doLogin: vi.fn().mockResolvedValue(undefined), |
| 13 | + doLogout: vi.fn().mockResolvedValue(undefined), |
| 14 | + getLoginPayload: vi.fn().mockResolvedValue(undefined), |
| 15 | + isLoggedIn: vi.fn().mockResolvedValue(true), |
| 16 | +})); |
| 17 | + |
| 18 | +vi.mock("../../../../core/hooks/auth/useSiweAuth", () => ({ |
| 19 | + useSiweAuth: () => mockAuth, |
| 20 | +})); |
| 21 | + |
| 22 | +vi.mock("../../../../core/hooks/wallets/useActiveWallet", () => ({ |
| 23 | + useActiveWallet: vi.fn().mockReturnValue(createWallet("io.metamask")), |
| 24 | +})); |
| 25 | + |
| 26 | +vi.mock("../../../../core/hooks/wallets/useActiveAccount", () => ({ |
| 27 | + useActiveAccount: () => vi.fn().mockReturnValue(TEST_ACCOUNT_A), |
| 28 | +})); |
| 29 | + |
| 30 | +vi.mock("../../../../core/hooks/wallets/useAdminWallet", () => ({ |
| 31 | + useAdminWallet: () => vi.fn().mockReturnValue(null), |
| 32 | +})); |
| 33 | + |
| 34 | +const mockConnectLocale = { |
| 35 | + signatureScreen: { |
| 36 | + title: "Sign In", |
| 37 | + instructionScreen: { |
| 38 | + title: "Sign Message", |
| 39 | + instruction: "Please sign the message", |
| 40 | + signInButton: "Sign In", |
| 41 | + disconnectWallet: "Disconnect", |
| 42 | + }, |
| 43 | + signingScreen: { |
| 44 | + title: "Signing", |
| 45 | + inProgress: "Signing in progress...", |
| 46 | + failedToSignIn: "Failed to sign in", |
| 47 | + prompt: "Please check your wallet", |
| 48 | + tryAgain: "Try Again", |
| 49 | + }, |
| 50 | + }, |
| 51 | + agreement: { |
| 52 | + prefix: "By connecting, you agree to our", |
| 53 | + termsOfService: "Terms of Service", |
| 54 | + and: "and", |
| 55 | + privacyPolicy: "Privacy Policy", |
| 56 | + }, |
| 57 | +} as unknown as ConnectLocale; |
| 58 | + |
| 59 | +describe("SignatureScreen", () => { |
| 60 | + beforeEach(() => { |
| 61 | + vi.clearAllMocks(); |
| 62 | + mockAuth.doLogin.mockResolvedValue(undefined); |
| 63 | + }); |
| 64 | + |
| 65 | + it("renders initial state correctly", () => { |
| 66 | + const { getByTestId } = render( |
| 67 | + <SignatureScreen |
| 68 | + onDone={() => {}} |
| 69 | + modalSize="wide" |
| 70 | + connectLocale={mockConnectLocale} |
| 71 | + client={TEST_CLIENT} |
| 72 | + auth={mockAuth} |
| 73 | + />, |
| 74 | + { setConnectedWallet: true }, |
| 75 | + ); |
| 76 | + |
| 77 | + expect(getByTestId("sign-in-button")).toBeInTheDocument(); |
| 78 | + expect(getByTestId("disconnect-button")).toBeInTheDocument(); |
| 79 | + }); |
| 80 | + |
| 81 | + it("handles signing flow", async () => { |
| 82 | + const onDoneMock = vi.fn(); |
| 83 | + const { getByRole, getByText } = render( |
| 84 | + <SignatureScreen |
| 85 | + onDone={onDoneMock} |
| 86 | + modalSize="wide" |
| 87 | + connectLocale={mockConnectLocale} |
| 88 | + client={TEST_CLIENT} |
| 89 | + auth={mockAuth} |
| 90 | + />, |
| 91 | + { setConnectedWallet: true }, |
| 92 | + ); |
| 93 | + |
| 94 | + const signInButton = getByRole("button", { name: "Sign In" }); |
| 95 | + await userEvent.click(signInButton); |
| 96 | + |
| 97 | + // Should show signing in progress |
| 98 | + await waitFor(() => { |
| 99 | + expect(getByText("Signing in progress...")).toBeInTheDocument(); |
| 100 | + }); |
| 101 | + }); |
| 102 | + |
| 103 | + it("shows loading state when wallet is undefined", async () => { |
| 104 | + vi.mocked(useActiveWallet).mockReturnValueOnce(undefined); |
| 105 | + |
| 106 | + const { queryByTestId } = render( |
| 107 | + <SignatureScreen |
| 108 | + onDone={() => {}} |
| 109 | + modalSize="wide" |
| 110 | + connectLocale={mockConnectLocale} |
| 111 | + client={TEST_CLIENT} |
| 112 | + auth={mockAuth} |
| 113 | + />, |
| 114 | + { setConnectedWallet: true }, |
| 115 | + ); |
| 116 | + |
| 117 | + expect(queryByTestId("sign-in-button")).not.toBeInTheDocument(); |
| 118 | + }); |
| 119 | + |
| 120 | + it("handles error state", async () => { |
| 121 | + mockAuth.doLogin.mockRejectedValueOnce(new Error("Signing failed")); |
| 122 | + const { getByTestId, getByRole, getByText } = render( |
| 123 | + <SignatureScreen |
| 124 | + onDone={() => {}} |
| 125 | + modalSize="wide" |
| 126 | + connectLocale={mockConnectLocale} |
| 127 | + client={TEST_CLIENT} |
| 128 | + auth={mockAuth} |
| 129 | + />, |
| 130 | + { setConnectedWallet: true }, |
| 131 | + ); |
| 132 | + |
| 133 | + const signInButton = await waitFor(() => { |
| 134 | + return getByTestId("sign-in-button"); |
| 135 | + }); |
| 136 | + await userEvent.click(signInButton); |
| 137 | + |
| 138 | + // Should show error state |
| 139 | + await waitFor( |
| 140 | + () => { |
| 141 | + expect(getByText("Signing failed")).toBeInTheDocument(); |
| 142 | + expect(getByRole("button", { name: "Try Again" })).toBeInTheDocument(); |
| 143 | + }, |
| 144 | + { |
| 145 | + timeout: 2000, |
| 146 | + }, |
| 147 | + ); |
| 148 | + }); |
| 149 | + |
| 150 | + describe("HeadlessSignIn", () => { |
| 151 | + const mockWallet = createWallet("inApp"); |
| 152 | + beforeEach(() => { |
| 153 | + vi.mocked(useActiveWallet).mockReturnValue(mockWallet); |
| 154 | + }); |
| 155 | + |
| 156 | + it("automatically triggers sign in on mount", async () => { |
| 157 | + render( |
| 158 | + <SignatureScreen |
| 159 | + onDone={() => {}} |
| 160 | + modalSize="wide" |
| 161 | + connectLocale={mockConnectLocale} |
| 162 | + client={TEST_CLIENT} |
| 163 | + auth={mockAuth} |
| 164 | + />, |
| 165 | + { setConnectedWallet: true }, |
| 166 | + ); |
| 167 | + |
| 168 | + await waitFor(() => { |
| 169 | + expect(mockAuth.doLogin).toHaveBeenCalledTimes(1); |
| 170 | + }); |
| 171 | + }); |
| 172 | + |
| 173 | + it("shows signing message during signing state", async () => { |
| 174 | + const { getByText } = render( |
| 175 | + <SignatureScreen |
| 176 | + onDone={() => {}} |
| 177 | + modalSize="wide" |
| 178 | + connectLocale={mockConnectLocale} |
| 179 | + client={TEST_CLIENT} |
| 180 | + auth={mockAuth} |
| 181 | + />, |
| 182 | + { setConnectedWallet: true }, |
| 183 | + ); |
| 184 | + |
| 185 | + await waitFor(() => { |
| 186 | + expect(getByText("Signing")).toBeInTheDocument(); |
| 187 | + }); |
| 188 | + }); |
| 189 | + |
| 190 | + it("shows error and retry button when signing fails", async () => { |
| 191 | + mockAuth.doLogin.mockRejectedValueOnce( |
| 192 | + new Error("Headless signing failed"), |
| 193 | + ); |
| 194 | + |
| 195 | + const { getByText, getByRole } = render( |
| 196 | + <SignatureScreen |
| 197 | + onDone={() => {}} |
| 198 | + modalSize="wide" |
| 199 | + connectLocale={mockConnectLocale} |
| 200 | + client={TEST_CLIENT} |
| 201 | + auth={mockAuth} |
| 202 | + />, |
| 203 | + { setConnectedWallet: true }, |
| 204 | + ); |
| 205 | + |
| 206 | + await waitFor( |
| 207 | + () => { |
| 208 | + expect(getByText("Headless signing failed")).toBeInTheDocument(); |
| 209 | + expect( |
| 210 | + getByRole("button", { name: "Try Again" }), |
| 211 | + ).toBeInTheDocument(); |
| 212 | + }, |
| 213 | + { timeout: 2000 }, |
| 214 | + ); |
| 215 | + }); |
| 216 | + |
| 217 | + it("allows retry after failure", async () => { |
| 218 | + mockAuth.doLogin |
| 219 | + .mockRejectedValueOnce(new Error("Failed first time")) |
| 220 | + .mockResolvedValueOnce(undefined); |
| 221 | + |
| 222 | + const { getByRole, getByText } = render( |
| 223 | + <SignatureScreen |
| 224 | + onDone={() => {}} |
| 225 | + modalSize="wide" |
| 226 | + connectLocale={mockConnectLocale} |
| 227 | + client={TEST_CLIENT} |
| 228 | + auth={mockAuth} |
| 229 | + />, |
| 230 | + { setConnectedWallet: true }, |
| 231 | + ); |
| 232 | + |
| 233 | + // Wait for initial failure |
| 234 | + await waitFor( |
| 235 | + () => { |
| 236 | + expect(getByText("Failed first time")).toBeInTheDocument(); |
| 237 | + }, |
| 238 | + { timeout: 2000 }, |
| 239 | + ); |
| 240 | + |
| 241 | + // Click retry |
| 242 | + const retryButton = getByRole("button", { name: "Try Again" }); |
| 243 | + await userEvent.click(retryButton); |
| 244 | + |
| 245 | + // Should show loading again |
| 246 | + await waitFor(() => { |
| 247 | + expect(getByText("Signing")).toBeInTheDocument(); |
| 248 | + }); |
| 249 | + |
| 250 | + // Should have called login twice |
| 251 | + expect(mockAuth.doLogin).toHaveBeenCalledTimes(2); |
| 252 | + }); |
| 253 | + |
| 254 | + it("allows disconnecting wallet after failure", async () => { |
| 255 | + const mockDisconnect = vi.fn().mockResolvedValue(undefined); |
| 256 | + mockAuth.doLogin.mockRejectedValueOnce(new Error("Failed")); |
| 257 | + vi.mocked(useActiveWallet).mockReturnValueOnce({ |
| 258 | + ...createWallet("io.metamask"), |
| 259 | + disconnect: mockDisconnect, |
| 260 | + }); |
| 261 | + |
| 262 | + const { getByTestId } = render( |
| 263 | + <SignatureScreen |
| 264 | + onDone={() => {}} |
| 265 | + modalSize="wide" |
| 266 | + connectLocale={mockConnectLocale} |
| 267 | + client={TEST_CLIENT} |
| 268 | + auth={mockAuth} |
| 269 | + />, |
| 270 | + { setConnectedWallet: true }, |
| 271 | + ); |
| 272 | + |
| 273 | + // Wait for failure and click disconnect |
| 274 | + await waitFor( |
| 275 | + () => { |
| 276 | + return getByTestId("disconnect-button"); |
| 277 | + }, |
| 278 | + { timeout: 2000 }, |
| 279 | + ).then((button) => userEvent.click(button)); |
| 280 | + |
| 281 | + // Should have attempted to disconnect |
| 282 | + await waitFor(() => { |
| 283 | + expect(mockDisconnect).toHaveBeenCalled(); |
| 284 | + }); |
| 285 | + }); |
| 286 | + }); |
| 287 | +}); |
0 commit comments