Skip to content

Commit 82667ca

Browse files
authored
[SDK] Test: Adds useSwitchActiveWalletChain test (#5835)
1 parent a83fee6 commit 82667ca

File tree

3 files changed

+54
-3
lines changed

3 files changed

+54
-3
lines changed
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import { renderHook } from "@testing-library/react";
2+
import type { ReactNode } from "react";
3+
import { optimism } from "thirdweb/chains";
4+
import { describe, expect, it, vi } from "vitest";
5+
import { MockStorage } from "../../../../../test/src/mocks/storage.js";
6+
import { TEST_CLIENT } from "../../../../../test/src/test-clients.js";
7+
import { TEST_ACCOUNT_A } from "../../../../../test/src/test-wallets.js";
8+
import { createWalletAdapter } from "../../../../adapters/wallet-adapter.js";
9+
import { ethereum } from "../../../../chains/chain-definitions/ethereum.js";
10+
import { createConnectionManager } from "../../../../wallets/manager/index.js";
11+
import { ConnectionManagerCtx } from "../../providers/connection-manager.js";
12+
import { useSetActiveWallet } from "./useSetActiveWallet.js";
13+
import { useSwitchActiveWalletChain } from "./useSwitchActiveWalletChain.js";
14+
15+
const switchChain = vi.fn();
16+
describe("useSwitchActiveWalletChain", () => {
17+
// Mock the connection manager
18+
const mockStorage = new MockStorage();
19+
const manager = createConnectionManager(mockStorage);
20+
21+
// Create a wrapper component with the mocked context
22+
const wrapper = ({ children }: { children: ReactNode }) => {
23+
return (
24+
<ConnectionManagerCtx.Provider value={manager}>
25+
{children}
26+
</ConnectionManagerCtx.Provider>
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 switch the active wallet chain", async () => {
39+
const { result } = renderHook(() => useSetActiveWallet(), { wrapper });
40+
await result.current(wallet);
41+
const { result: switchChainResult } = renderHook(
42+
() => useSwitchActiveWalletChain(),
43+
{
44+
wrapper,
45+
},
46+
);
47+
await switchChainResult.current(optimism);
48+
49+
expect(switchChain).toHaveBeenCalledWith(optimism);
50+
});
51+
});

packages/thirdweb/src/wallets/manager/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -247,11 +247,11 @@ export function createConnectionManager(storage: AsyncStorage) {
247247
const switchActiveWalletChain = async (chain: Chain) => {
248248
const wallet = activeWalletStore.getValue();
249249
if (!wallet) {
250-
throw new Error("no wallet found");
250+
throw new Error("No active wallet found");
251251
}
252252

253253
if (!wallet.switchChain) {
254-
throw new Error("wallet does not support switching chains");
254+
throw new Error("Wallet does not support switching chains");
255255
}
256256

257257
if (wallet.id === "smart") {

packages/thirdweb/src/wallets/smart/smart.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { generateAccount } from "../utils/generateAccount.js";
55
import { connectSmartWallet, disconnectSmartWallet } from "./index.js";
66
import { smartWallet } from "./smart-wallet.js";
77

8-
describe("Smart Wallet Index", () => {
8+
describe.runIf(process.env.TW_SECRET_KEY)("Smart Wallet Index", () => {
99
const chain = defineChain(1); // Ethereum mainnet
1010
const client = TEST_CLIENT;
1111

0 commit comments

Comments
 (0)