Skip to content

Commit

Permalink
[SDK] Test: Adds useSwitchActiveWalletChain test (#5835)
Browse files Browse the repository at this point in the history
  • Loading branch information
gregfromstl authored Dec 23, 2024
1 parent a83fee6 commit 82667ca
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { renderHook } from "@testing-library/react";
import type { ReactNode } from "react";
import { optimism } from "thirdweb/chains";
import { describe, expect, it, vi } from "vitest";
import { MockStorage } from "../../../../../test/src/mocks/storage.js";
import { TEST_CLIENT } from "../../../../../test/src/test-clients.js";
import { TEST_ACCOUNT_A } from "../../../../../test/src/test-wallets.js";
import { createWalletAdapter } from "../../../../adapters/wallet-adapter.js";
import { ethereum } from "../../../../chains/chain-definitions/ethereum.js";
import { createConnectionManager } from "../../../../wallets/manager/index.js";
import { ConnectionManagerCtx } from "../../providers/connection-manager.js";
import { useSetActiveWallet } from "./useSetActiveWallet.js";
import { useSwitchActiveWalletChain } from "./useSwitchActiveWalletChain.js";

const switchChain = vi.fn();
describe("useSwitchActiveWalletChain", () => {
// Mock the connection manager
const mockStorage = new MockStorage();
const manager = createConnectionManager(mockStorage);

// Create a wrapper component with the mocked context
const wrapper = ({ children }: { children: ReactNode }) => {
return (
<ConnectionManagerCtx.Provider value={manager}>
{children}
</ConnectionManagerCtx.Provider>
);
};

const wallet = createWalletAdapter({
adaptedAccount: TEST_ACCOUNT_A,
client: TEST_CLIENT,
chain: ethereum,
onDisconnect: () => {},
switchChain,
});

it("should switch the active wallet chain", async () => {
const { result } = renderHook(() => useSetActiveWallet(), { wrapper });
await result.current(wallet);
const { result: switchChainResult } = renderHook(
() => useSwitchActiveWalletChain(),
{
wrapper,
},
);
await switchChainResult.current(optimism);

expect(switchChain).toHaveBeenCalledWith(optimism);
});
});
4 changes: 2 additions & 2 deletions packages/thirdweb/src/wallets/manager/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -247,11 +247,11 @@ export function createConnectionManager(storage: AsyncStorage) {
const switchActiveWalletChain = async (chain: Chain) => {
const wallet = activeWalletStore.getValue();
if (!wallet) {
throw new Error("no wallet found");
throw new Error("No active wallet found");
}

if (!wallet.switchChain) {
throw new Error("wallet does not support switching chains");
throw new Error("Wallet does not support switching chains");
}

if (wallet.id === "smart") {
Expand Down
2 changes: 1 addition & 1 deletion packages/thirdweb/src/wallets/smart/smart.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { generateAccount } from "../utils/generateAccount.js";
import { connectSmartWallet, disconnectSmartWallet } from "./index.js";
import { smartWallet } from "./smart-wallet.js";

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

Expand Down

0 comments on commit 82667ca

Please sign in to comment.