|
| 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 | +}); |
0 commit comments