-
Notifications
You must be signed in to change notification settings - Fork 428
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: Add wallet connection hook tests (#5689)
- Loading branch information
1 parent
29d32b8
commit 77c8d0e
Showing
4 changed files
with
231 additions
and
4 deletions.
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 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 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 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 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