UNPKG

@starknet-react/core

Version:

Starknet React is a collection of React hooks for Starknet.

56 lines (48 loc) 1.55 kB
import { describe, expect, it } from "vitest"; import { act, renderHook, waitFor } from "../../test/react"; import { constants } from "starknet"; import { defaultConnector } from "../../test/devnet"; import { useConnect } from "./use-connect"; import { useDisconnect } from "./use-disconnect"; import { useSwitchChain } from "./use-switch-chain"; function useSwitchChainWithConnect() { return { switchChain: useSwitchChain({ params: { chainId: constants.StarknetChainId.SN_MAIN }, }), connect: useConnect(), disconnect: useDisconnect(), }; } describe("useSwitchChain", () => { it("switch chain to mainnet", async () => { const { result } = renderHook(() => useSwitchChainWithConnect()); await act(async () => { result.current.connect.connect({ connector: defaultConnector, }); }); await act(async () => { result.current.switchChain.switchChain(); }); await waitFor(() => { expect(result.current.switchChain.isSuccess).toBeTruthy(); }); }); it("throws error if user cancels to switch to mainnet", async () => { const { result } = renderHook(() => useSwitchChainWithConnect(), { connectorOptions: { rejectRequest: true }, }); await act(async () => { result.current.connect.connect({ connector: defaultConnector, }); }); await act(async () => { result.current.switchChain.switchChain(); }); await waitFor(() => { expect(result.current.switchChain.isError).toBeTruthy(); }); }); });