|
| 1 | +import { renderHook } from "@testing-library/react"; |
| 2 | +import type { ReactNode } from "react"; |
| 3 | +import { beforeEach, describe, expect, it } from "vitest"; |
| 4 | +import { MockStorage } from "../../../../../test/src/mocks/storage.js"; |
| 5 | +import { TEST_CLIENT } from "../../../../../test/src/test-clients.js"; |
| 6 | +import { TEST_ACCOUNT_A } from "../../../../../test/src/test-wallets.js"; |
| 7 | +import { createWalletAdapter } from "../../../../adapters/wallet-adapter.js"; |
| 8 | +import { ethereum } from "../../../../chains/chain-definitions/ethereum.js"; |
| 9 | +import { |
| 10 | + type ConnectionManager, |
| 11 | + createConnectionManager, |
| 12 | +} from "../../../../wallets/manager/index.js"; |
| 13 | +import { ConnectionManagerCtx } from "../../providers/connection-manager.js"; |
| 14 | +import { useActiveWalletConnectionStatus } from "./useActiveWalletConnectionStatus.js"; |
| 15 | +import { useConnect } from "./useConnect.js"; |
| 16 | + |
| 17 | +describe("useAddConnectedWallet", () => { |
| 18 | + // Mock the connection manager |
| 19 | + const mockStorage = new MockStorage(); |
| 20 | + let manager: ConnectionManager; |
| 21 | + |
| 22 | + // Create a wrapper component with the mocked context |
| 23 | + const wrapper = ({ children }: { children: ReactNode }) => { |
| 24 | + return ( |
| 25 | + <ConnectionManagerCtx.Provider value={manager}> |
| 26 | + {children} |
| 27 | + </ConnectionManagerCtx.Provider> |
| 28 | + ); |
| 29 | + }; |
| 30 | + |
| 31 | + const wallet = createWalletAdapter({ |
| 32 | + adaptedAccount: TEST_ACCOUNT_A, |
| 33 | + client: TEST_CLIENT, |
| 34 | + chain: ethereum, |
| 35 | + onDisconnect: () => {}, |
| 36 | + switchChain: () => {}, |
| 37 | + }); |
| 38 | + |
| 39 | + beforeEach(() => { |
| 40 | + manager = createConnectionManager(mockStorage); |
| 41 | + }); |
| 42 | + |
| 43 | + it("should connect a wallet to the connection manager", async () => { |
| 44 | + const { result: statusResult } = renderHook( |
| 45 | + () => useActiveWalletConnectionStatus(), |
| 46 | + { |
| 47 | + wrapper, |
| 48 | + }, |
| 49 | + ); |
| 50 | + const { result } = renderHook(() => useConnect(), { wrapper }); |
| 51 | + expect(statusResult.current).toEqual("disconnected"); |
| 52 | + await result.current.connect(async () => wallet); |
| 53 | + expect(statusResult.current).toEqual("connected"); |
| 54 | + |
| 55 | + // should add to connected wallets |
| 56 | + expect(manager.connectedWallets.getValue()).toHaveLength(1); |
| 57 | + expect(manager.connectedWallets.getValue()[0]).toEqual(wallet); |
| 58 | + // should set the active wallet |
| 59 | + expect(manager.activeWalletStore.getValue()).toEqual(wallet); |
| 60 | + }); |
| 61 | + |
| 62 | + it("should handle a function that returns a wallet", async () => { |
| 63 | + const { result: statusResult } = renderHook( |
| 64 | + () => useActiveWalletConnectionStatus(), |
| 65 | + { |
| 66 | + wrapper, |
| 67 | + }, |
| 68 | + ); |
| 69 | + const { result } = renderHook(() => useConnect(), { wrapper }); |
| 70 | + expect(statusResult.current).toEqual("disconnected"); |
| 71 | + await result.current.connect(async () => wallet); |
| 72 | + expect(statusResult.current).toEqual("connected"); |
| 73 | + |
| 74 | + // should add to connected wallets |
| 75 | + expect(manager.connectedWallets.getValue()).toHaveLength(1); |
| 76 | + expect(manager.connectedWallets.getValue()[0]).toEqual(wallet); |
| 77 | + // should set the active wallet |
| 78 | + expect(manager.activeWalletStore.getValue()).toEqual(wallet); |
| 79 | + }); |
| 80 | + |
| 81 | + it("should handle an error when connecting a wallet", async () => { |
| 82 | + const { result: statusResult } = renderHook( |
| 83 | + () => useActiveWalletConnectionStatus(), |
| 84 | + { |
| 85 | + wrapper, |
| 86 | + }, |
| 87 | + ); |
| 88 | + expect(statusResult.current).toEqual("disconnected"); |
| 89 | + const { result } = renderHook(() => useConnect(), { wrapper }); |
| 90 | + await result.current.connect(async () => { |
| 91 | + throw new Error("test"); |
| 92 | + }); |
| 93 | + |
| 94 | + expect(statusResult.current).toEqual("disconnected"); |
| 95 | + // should set the active wallet |
| 96 | + expect(manager.activeWalletStore.getValue()).toEqual(undefined); |
| 97 | + }); |
| 98 | + |
| 99 | + it("should throw an error when used outside of ThirdwebProvider", () => { |
| 100 | + // Render the hook without a provider |
| 101 | + expect(() => { |
| 102 | + renderHook(() => useConnect()); |
| 103 | + }).toThrow("useConnect must be used within <ThirdwebProvider>"); |
| 104 | + }); |
| 105 | +}); |
0 commit comments