|
| 1 | +import { beforeEach } from "node:test"; |
1 | 2 | import { isAddress } from "ethers6"; |
2 | 3 | import { describe, expect, it, vi } from "vitest"; |
3 | 4 | import { MockStorage } from "~test/mocks/storage.js"; |
4 | 5 | import { TEST_CLIENT } from "~test/test-clients.js"; |
5 | 6 | import { TEST_ACCOUNT_A } from "~test/test-wallets.js"; |
6 | 7 | import { createWalletAdapter } from "../../adapters/wallet-adapter.js"; |
7 | 8 | import { ethereum } from "../../chains/chain-definitions/ethereum.js"; |
| 9 | +import { AUTH_TOKEN_LOCAL_STORAGE_NAME } from "../in-app/core/constants/settings.js"; |
| 10 | +import { getUrlToken } from "../in-app/web/lib/get-url-token.js"; |
8 | 11 | import { createConnectionManager } from "../manager/index.js"; |
9 | 12 | import type { WalletId } from "../wallet-types.js"; |
10 | 13 | import { autoConnectCore, handleWalletConnection } from "./autoConnectCore.js"; |
11 | 14 |
|
| 15 | +vi.mock("../in-app/web/lib/get-url-token.ts"); |
| 16 | + |
12 | 17 | describe("useAutoConnectCore", () => { |
13 | 18 | const mockStorage = new MockStorage(); |
14 | 19 | const manager = createConnectionManager(mockStorage); |
15 | 20 |
|
| 21 | + beforeEach(() => { |
| 22 | + vi.resetAllMocks(); |
| 23 | + }); |
| 24 | + |
16 | 25 | it("should return a useQuery result", async () => { |
| 26 | + vi.mocked(getUrlToken).mockReturnValue({}); |
17 | 27 | const wallet = createWalletAdapter({ |
18 | 28 | adaptedAccount: TEST_ACCOUNT_A, |
19 | 29 | client: TEST_CLIENT, |
@@ -45,6 +55,8 @@ describe("useAutoConnectCore", () => { |
45 | 55 | }); |
46 | 56 |
|
47 | 57 | it("should return `false` if there's no lastConnectedWalletIds", async () => { |
| 58 | + vi.mocked(getUrlToken).mockReturnValue({}); |
| 59 | + |
48 | 60 | const wallet = createWalletAdapter({ |
49 | 61 | adaptedAccount: TEST_ACCOUNT_A, |
50 | 62 | client: TEST_CLIENT, |
@@ -76,6 +88,8 @@ describe("useAutoConnectCore", () => { |
76 | 88 | }); |
77 | 89 |
|
78 | 90 | it("should call onTimeout on ... timeout", async () => { |
| 91 | + vi.mocked(getUrlToken).mockReturnValue({}); |
| 92 | + |
79 | 93 | const wallet = createWalletAdapter({ |
80 | 94 | adaptedAccount: TEST_ACCOUNT_A, |
81 | 95 | client: TEST_CLIENT, |
@@ -124,6 +138,113 @@ describe("useAutoConnectCore", () => { |
124 | 138 | expect(infoSpy).toHaveBeenCalledWith("TIMEOUTTED"); |
125 | 139 | warnSpy.mockRestore(); |
126 | 140 | }); |
| 141 | + |
| 142 | + it("should handle auth cookie storage correctly", async () => { |
| 143 | + const mockAuthCookie = "mock-auth-cookie"; |
| 144 | + const wallet = createWalletAdapter({ |
| 145 | + adaptedAccount: TEST_ACCOUNT_A, |
| 146 | + client: TEST_CLIENT, |
| 147 | + chain: ethereum, |
| 148 | + onDisconnect: () => {}, |
| 149 | + switchChain: () => {}, |
| 150 | + }); |
| 151 | + vi.mocked(getUrlToken).mockReturnValue({ |
| 152 | + authCookie: mockAuthCookie, |
| 153 | + walletId: wallet.id, |
| 154 | + }); |
| 155 | + |
| 156 | + await autoConnectCore({ |
| 157 | + storage: mockStorage, |
| 158 | + props: { |
| 159 | + wallets: [wallet], |
| 160 | + client: TEST_CLIENT, |
| 161 | + }, |
| 162 | + createWalletFn: () => wallet, |
| 163 | + manager, |
| 164 | + }); |
| 165 | + |
| 166 | + const storedCookie = await mockStorage.getItem( |
| 167 | + AUTH_TOKEN_LOCAL_STORAGE_NAME(TEST_CLIENT.clientId), |
| 168 | + ); |
| 169 | + expect(storedCookie).toBe(mockAuthCookie); |
| 170 | + }); |
| 171 | + |
| 172 | + it("should handle error when manager connection fails", async () => { |
| 173 | + const wallet1 = createWalletAdapter({ |
| 174 | + adaptedAccount: TEST_ACCOUNT_A, |
| 175 | + client: TEST_CLIENT, |
| 176 | + chain: ethereum, |
| 177 | + onDisconnect: () => {}, |
| 178 | + switchChain: () => {}, |
| 179 | + }); |
| 180 | + |
| 181 | + mockStorage.setItem("thirdweb:active-wallet-id", wallet1.id); |
| 182 | + mockStorage.setItem( |
| 183 | + "thirdweb:connected-wallet-ids", |
| 184 | + JSON.stringify([wallet1.id]), |
| 185 | + ); |
| 186 | + |
| 187 | + const addConnectedWalletSpy = vi |
| 188 | + .spyOn(manager, "connect") |
| 189 | + .mockRejectedValue(new Error("Connection failed")); |
| 190 | + |
| 191 | + await autoConnectCore({ |
| 192 | + storage: mockStorage, |
| 193 | + props: { |
| 194 | + wallets: [wallet1], |
| 195 | + client: TEST_CLIENT, |
| 196 | + }, |
| 197 | + createWalletFn: () => wallet1, |
| 198 | + manager, |
| 199 | + }); |
| 200 | + expect(addConnectedWalletSpy).toHaveBeenCalled(); |
| 201 | + }); |
| 202 | + |
| 203 | + it("should connect multiple wallets correctly", async () => { |
| 204 | + const wallet1 = createWalletAdapter({ |
| 205 | + adaptedAccount: TEST_ACCOUNT_A, |
| 206 | + client: TEST_CLIENT, |
| 207 | + chain: ethereum, |
| 208 | + onDisconnect: () => {}, |
| 209 | + switchChain: () => {}, |
| 210 | + }); |
| 211 | + |
| 212 | + const wallet2 = createWalletAdapter({ |
| 213 | + adaptedAccount: { ...TEST_ACCOUNT_A, address: "0x123" }, |
| 214 | + client: TEST_CLIENT, |
| 215 | + chain: ethereum, |
| 216 | + onDisconnect: () => {}, |
| 217 | + switchChain: () => {}, |
| 218 | + }); |
| 219 | + wallet2.id = "io.metamask" as unknown as "adapter"; |
| 220 | + |
| 221 | + mockStorage.setItem("thirdweb:active-wallet-id", wallet1.id); |
| 222 | + mockStorage.setItem( |
| 223 | + "thirdweb:connected-wallet-ids", |
| 224 | + JSON.stringify([wallet1.id, wallet2.id]), |
| 225 | + ); |
| 226 | + |
| 227 | + const warnSpy = vi.spyOn(console, "warn").mockImplementation(() => {}); |
| 228 | + const addConnectedWalletSpy = vi.spyOn(manager, "addConnectedWallet"); |
| 229 | + |
| 230 | + await autoConnectCore({ |
| 231 | + storage: mockStorage, |
| 232 | + props: { |
| 233 | + wallets: [wallet1, wallet2], |
| 234 | + client: TEST_CLIENT, |
| 235 | + }, |
| 236 | + createWalletFn: () => wallet1, |
| 237 | + manager, |
| 238 | + }); |
| 239 | + |
| 240 | + expect(addConnectedWalletSpy).toHaveBeenCalledWith(wallet2); |
| 241 | + |
| 242 | + expect(warnSpy).toHaveBeenCalled(); |
| 243 | + expect(warnSpy).toHaveBeenCalledWith( |
| 244 | + "Error auto connecting wallet:", |
| 245 | + "Connection failed", |
| 246 | + ); |
| 247 | + }); |
127 | 248 | }); |
128 | 249 |
|
129 | 250 | describe("handleWalletConnection", () => { |
|
0 commit comments