Skip to content

Commit 2972aad

Browse files
committed
update
1 parent a0835f7 commit 2972aad

File tree

6 files changed

+174
-1
lines changed

6 files changed

+174
-1
lines changed

.changeset/little-beds-dress.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"thirdweb": patch
3+
---
4+
5+
Add onTimeout callback to useAutoConnect

packages/thirdweb/src/react/core/hooks/connection/types.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,4 +113,9 @@ export type AutoConnectProps = {
113113
* Optional chain to autoconnect to
114114
*/
115115
chain?: Chain;
116+
117+
/**
118+
* Callback to be called when the connection is timeout-ed
119+
*/
120+
onTimeout?: () => void;
116121
};
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
import { renderHook, waitFor } from "@testing-library/react";
2+
import type { ReactNode } from "react";
3+
import { describe, expect, it } from "vitest";
4+
import { MockStorage } from "~test/mocks/storage.js";
5+
import { TEST_CLIENT } from "~test/test-clients.js";
6+
import { TEST_ACCOUNT_A } from "~test/test-wallets.js";
7+
import { createWalletAdapter } from "../../../../adapters/wallet-adapter.js";
8+
import { ethereum } from "../../../../chains/chain-definitions/ethereum.js";
9+
import { ThirdwebProvider } from "../../../../react/web/providers/thirdweb-provider.js";
10+
import { createConnectionManager } from "../../../../wallets/manager/index.js";
11+
import type { WalletId } from "../../../../wallets/wallet-types.js";
12+
import { ConnectionManagerCtx } from "../../providers/connection-manager.js";
13+
import { useAutoConnectCore } from "./useAutoConnect.js";
14+
15+
describe("useAutoConnectCore", () => {
16+
const mockStorage = new MockStorage();
17+
const manager = createConnectionManager(mockStorage);
18+
19+
// Create a wrapper component with the mocked context
20+
const wrapper = ({ children }: { children: ReactNode }) => {
21+
return (
22+
<ThirdwebProvider>
23+
<ConnectionManagerCtx.Provider value={manager}>
24+
{children}
25+
</ConnectionManagerCtx.Provider>
26+
</ThirdwebProvider>
27+
);
28+
};
29+
30+
const wallet = createWalletAdapter({
31+
adaptedAccount: TEST_ACCOUNT_A,
32+
client: TEST_CLIENT,
33+
chain: ethereum,
34+
onDisconnect: () => {},
35+
switchChain: () => {},
36+
});
37+
38+
it("should return a useQuery result", async () => {
39+
const { result } = renderHook(
40+
() =>
41+
useAutoConnectCore(
42+
mockStorage,
43+
{
44+
wallets: [wallet],
45+
client: TEST_CLIENT,
46+
},
47+
(id: WalletId) =>
48+
createWalletAdapter({
49+
adaptedAccount: TEST_ACCOUNT_A,
50+
client: TEST_CLIENT,
51+
chain: ethereum,
52+
onDisconnect: () => {
53+
console.log(id);
54+
},
55+
switchChain: () => {},
56+
}),
57+
),
58+
{ wrapper },
59+
);
60+
expect("data" in result.current).toBeTruthy();
61+
await waitFor(() => {
62+
expect(typeof result.current.data).toBe("boolean");
63+
});
64+
});
65+
});

packages/thirdweb/src/react/core/hooks/wallets/useAutoConnect.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ export function useAutoConnectCore(
2424
props: AutoConnectProps & { wallets: Wallet[] },
2525
createWalletFn: (id: WalletId) => Wallet,
2626
getInstalledWallets?: () => Wallet[],
27+
onTimeout?: () => void,
2728
) {
2829
const manager = useConnectionManagerCtx("useAutoConnect");
2930
const setConnectionStatus = useSetActiveWalletConnectionStatus();
@@ -103,6 +104,11 @@ export function useAutoConnectCore(
103104
await timeoutPromise(handleWalletConnection(activeWallet), {
104105
ms: timeout,
105106
message: `AutoConnect timeout: ${timeout}ms limit exceeded.`,
107+
}).catch((err) => {
108+
console.warn(err.message);
109+
if (onTimeout) {
110+
onTimeout();
111+
}
106112
});
107113

108114
// connected wallet could be activeWallet or smart wallet
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
import { afterEach, beforeEach, describe, expect, it } from "vitest";
2+
import { getUrlToken } from "./get-url-token.js";
3+
4+
describe("getUrlToken", () => {
5+
let originalLocation: Location;
6+
7+
beforeEach(() => {
8+
originalLocation = window.location;
9+
10+
Object.defineProperty(window, "location", {
11+
value: {
12+
...originalLocation,
13+
search: "",
14+
},
15+
writable: true,
16+
});
17+
});
18+
19+
afterEach(() => {
20+
// Restore the original location object after each test
21+
Object.defineProperty(window, "location", {
22+
value: originalLocation,
23+
writable: true,
24+
});
25+
});
26+
27+
it("should return an empty object if not in web context", () => {
28+
const originalWindow = window;
29+
// biome-ignore lint/suspicious/noExplicitAny: Test
30+
(global as any).window = undefined;
31+
32+
const result = getUrlToken();
33+
// biome-ignore lint/suspicious/noExplicitAny: Test
34+
(global as any).window = originalWindow;
35+
36+
expect(result).toEqual({});
37+
});
38+
39+
it("should return an empty object if no parameters are present", () => {
40+
const result = getUrlToken();
41+
expect(result).toEqual({});
42+
});
43+
44+
it("should parse walletId and authResult correctly", () => {
45+
window.location.search =
46+
"?walletId=123&authResult=%7B%22token%22%3A%22abc%22%7D";
47+
48+
const result = getUrlToken();
49+
50+
expect(result).toEqual({
51+
walletId: "123",
52+
authResult: { token: "abc" },
53+
authProvider: null,
54+
authCookie: null,
55+
});
56+
});
57+
58+
it("should handle authCookie and update URL correctly", () => {
59+
window.location.search = "?walletId=123&authCookie=myCookie";
60+
61+
const result = getUrlToken();
62+
63+
expect(result).toEqual({
64+
walletId: "123",
65+
authResult: undefined,
66+
authProvider: null,
67+
authCookie: "myCookie",
68+
});
69+
70+
// Check if URL has been updated correctly
71+
expect(window.location.search).toBe("?walletId=123&authCookie=myCookie");
72+
});
73+
74+
it("should handle all parameters correctly", () => {
75+
window.location.search =
76+
"?walletId=123&authResult=%7B%22token%22%3A%22xyz%22%7D&authProvider=provider1&authCookie=myCookie";
77+
78+
const result = getUrlToken();
79+
80+
expect(result).toEqual({
81+
walletId: "123",
82+
authResult: { token: "xyz" },
83+
authProvider: "provider1",
84+
authCookie: "myCookie",
85+
});
86+
87+
// Check if URL has been updated correctly
88+
expect(window.location.search).toBe(
89+
"?walletId=123&authResult=%7B%22token%22%3A%22xyz%22%7D&authProvider=provider1&authCookie=myCookie",
90+
);
91+
});
92+
});

packages/thirdweb/test/vitest.config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ export default defineConfig({
3131
],
3232
include: ["src/**"],
3333
},
34-
environmentMatchGlobs: [["src/react/**/*.test.tsx", "happy-dom"]],
34+
environmentMatchGlobs: [["src/**/*.test.tsx", "happy-dom"]],
3535
environment: "node",
3636
include: ["src/**/*.test.{ts,tsx}"],
3737
setupFiles: [join(__dirname, "./reactSetup.ts")],

0 commit comments

Comments
 (0)