-
Notifications
You must be signed in to change notification settings - Fork 619
test: Add wallet connection hook tests #5689
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
52 changes: 52 additions & 0 deletions
52
packages/thirdweb/src/react/core/hooks/wallets/useAddConnectedWallet.test.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| import { renderHook } from "@testing-library/react"; | ||
| import type { ReactNode } from "react"; | ||
| import { describe, expect, it } 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 { useAddConnectedWallet } from "./useAddConnectedWallet.js"; | ||
|
|
||
| describe("useAddConnectedWallet", () => { | ||
| // 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 add a wallet to the connection manager", async () => { | ||
| // Render the hook | ||
| const { result } = renderHook(() => useAddConnectedWallet(), { wrapper }); | ||
| result.current(wallet); | ||
|
|
||
| expect(manager.connectedWallets.getValue()).toHaveLength(1); | ||
| expect(manager.connectedWallets.getValue()[0]).toEqual(wallet); | ||
| // add connected wallet should not set the active wallet | ||
| expect(manager.activeWalletStore.getValue()).toBeUndefined(); | ||
| }); | ||
|
|
||
| it("should throw an error when used outside of ThirdwebProvider", () => { | ||
| // Render the hook without a provider | ||
| expect(() => { | ||
| renderHook(() => useAddConnectedWallet()); | ||
| }).toThrow("useAddConnectedWallet must be used within <ThirdwebProvider>"); | ||
| }); | ||
| }); |
105 changes: 105 additions & 0 deletions
105
packages/thirdweb/src/react/core/hooks/wallets/useConnect.test.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,105 @@ | ||
| import { renderHook } from "@testing-library/react"; | ||
| import type { ReactNode } from "react"; | ||
| import { beforeEach, describe, expect, it } 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 { | ||
| type ConnectionManager, | ||
| createConnectionManager, | ||
| } from "../../../../wallets/manager/index.js"; | ||
| import { ConnectionManagerCtx } from "../../providers/connection-manager.js"; | ||
| import { useActiveWalletConnectionStatus } from "./useActiveWalletConnectionStatus.js"; | ||
| import { useConnect } from "./useConnect.js"; | ||
|
|
||
| describe("useAddConnectedWallet", () => { | ||
| // Mock the connection manager | ||
| const mockStorage = new MockStorage(); | ||
| let manager: ConnectionManager; | ||
|
|
||
| // 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: () => {}, | ||
| }); | ||
|
|
||
| beforeEach(() => { | ||
| manager = createConnectionManager(mockStorage); | ||
| }); | ||
|
|
||
| it("should connect a wallet to the connection manager", async () => { | ||
| const { result: statusResult } = renderHook( | ||
| () => useActiveWalletConnectionStatus(), | ||
| { | ||
| wrapper, | ||
| }, | ||
| ); | ||
| const { result } = renderHook(() => useConnect(), { wrapper }); | ||
| expect(statusResult.current).toEqual("disconnected"); | ||
| await result.current.connect(async () => wallet); | ||
| expect(statusResult.current).toEqual("connected"); | ||
|
|
||
| // should add to connected wallets | ||
| expect(manager.connectedWallets.getValue()).toHaveLength(1); | ||
| expect(manager.connectedWallets.getValue()[0]).toEqual(wallet); | ||
| // should set the active wallet | ||
| expect(manager.activeWalletStore.getValue()).toEqual(wallet); | ||
| }); | ||
|
|
||
| it("should handle a function that returns a wallet", async () => { | ||
| const { result: statusResult } = renderHook( | ||
| () => useActiveWalletConnectionStatus(), | ||
| { | ||
| wrapper, | ||
| }, | ||
| ); | ||
| const { result } = renderHook(() => useConnect(), { wrapper }); | ||
| expect(statusResult.current).toEqual("disconnected"); | ||
| await result.current.connect(async () => wallet); | ||
| expect(statusResult.current).toEqual("connected"); | ||
|
|
||
| // should add to connected wallets | ||
| expect(manager.connectedWallets.getValue()).toHaveLength(1); | ||
| expect(manager.connectedWallets.getValue()[0]).toEqual(wallet); | ||
| // should set the active wallet | ||
| expect(manager.activeWalletStore.getValue()).toEqual(wallet); | ||
| }); | ||
|
|
||
| it("should handle an error when connecting a wallet", async () => { | ||
| const { result: statusResult } = renderHook( | ||
| () => useActiveWalletConnectionStatus(), | ||
| { | ||
| wrapper, | ||
| }, | ||
| ); | ||
| expect(statusResult.current).toEqual("disconnected"); | ||
| const { result } = renderHook(() => useConnect(), { wrapper }); | ||
| await result.current.connect(async () => { | ||
| throw new Error("test"); | ||
| }); | ||
|
|
||
| expect(statusResult.current).toEqual("disconnected"); | ||
| // should set the active wallet | ||
| expect(manager.activeWalletStore.getValue()).toEqual(undefined); | ||
| }); | ||
|
|
||
| it("should throw an error when used outside of ThirdwebProvider", () => { | ||
| // Render the hook without a provider | ||
| expect(() => { | ||
| renderHook(() => useConnect()); | ||
| }).toThrow("useConnect must be used within <ThirdwebProvider>"); | ||
| }); | ||
| }); | ||
53 changes: 53 additions & 0 deletions
53
packages/thirdweb/src/react/core/hooks/wallets/useSetActiveWallet.test.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| import { renderHook } from "@testing-library/react"; | ||
| import type { ReactNode } from "react"; | ||
| import { describe, expect, it } 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"; | ||
|
|
||
| describe("useAddConnectedWallet", () => { | ||
| // 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 add a wallet to the connection manager", async () => { | ||
| // Render the hook | ||
| const { result } = renderHook(() => useSetActiveWallet(), { wrapper }); | ||
| result.current(wallet); | ||
|
|
||
| // should add to connected wallets | ||
| expect(manager.connectedWallets.getValue()).toHaveLength(1); | ||
| expect(manager.connectedWallets.getValue()[0]).toEqual(wallet); | ||
| // should set the active wallet | ||
| expect(manager.activeWalletStore.getValue()).toEqual(wallet); | ||
| }); | ||
|
|
||
| it("should throw an error when used outside of ThirdwebProvider", () => { | ||
| // Render the hook without a provider | ||
| expect(() => { | ||
| renderHook(() => useSetActiveWallet()); | ||
| }).toThrow("useSetActiveWallet must be used within <ThirdwebProvider>"); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The describe block title should be
useConnectto match the hook under test. The current titleuseAddConnectedWalletappears to be a copy/paste error.Spotted by Graphite Reviewer
Is this helpful? React 👍 or 👎 to let us know.