-
-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
146 additions
and
8 deletions.
There are no files selected for viewing
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
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 @@ | ||
# Actions |
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 @@ | ||
# Connectors |
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
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
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
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
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
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,69 @@ | ||
import { connect, createConfig, disconnect, http } from '@wagmi/core' | ||
import { accounts, chain, testConnector } from '@wagmi/test' | ||
import { createWrapper, renderHook, waitFor } from '@wagmi/test/react' | ||
import { expect, test, vi } from 'vitest' | ||
|
||
import { WagmiProvider } from '../context.js' | ||
import { useAccountState } from './useAccountState.js' | ||
import { useConnect } from './useConnect.js' | ||
import { useDisconnect } from './useDisconnect.js' | ||
|
||
test('behavior: connect and disconnect called once', async () => { | ||
const onConnect = vi.fn() | ||
const onDisconnect = vi.fn() | ||
|
||
const { result } = renderHook(() => ({ | ||
useAccountState: useAccountState({ onConnect, onDisconnect }), | ||
useConnect: useConnect(), | ||
useDisconnect: useDisconnect(), | ||
})) | ||
|
||
result.current.useConnect.connect({ | ||
connector: result.current.useConnect.connectors[0]!, | ||
}) | ||
await waitFor(() => expect(result.current.useConnect.isSuccess).toBeTruthy()) | ||
|
||
result.current.useConnect.connect({ | ||
connector: result.current.useConnect.connectors[0]!, | ||
}) | ||
await waitFor(() => expect(result.current.useConnect.isSuccess).toBeTruthy()) | ||
|
||
result.current.useDisconnect.disconnect() | ||
await waitFor(() => | ||
expect(result.current.useDisconnect.isSuccess).toBeTruthy(), | ||
) | ||
result.current.useDisconnect.disconnect() | ||
await waitFor(() => | ||
expect(result.current.useDisconnect.isSuccess).toBeTruthy(), | ||
) | ||
|
||
expect(onConnect).toBeCalledTimes(1) | ||
expect(onDisconnect).toBeCalledTimes(1) | ||
}) | ||
|
||
test('behavior: connect called on reconnect', async () => { | ||
const config = createConfig({ | ||
chains: [chain.mainnet], | ||
connectors: [ | ||
testConnector({ | ||
accounts, | ||
features: { reconnect: true }, | ||
}), | ||
], | ||
reconnectOnMount: true, | ||
transports: { [chain.mainnet.id]: http() }, | ||
}) | ||
|
||
await connect(config, { connector: config.connectors[0]! }) | ||
const onConnect = vi.fn((data) => { | ||
expect(data.isReconnected).toBeTruthy() | ||
}) | ||
|
||
renderHook(() => useAccountState({ onConnect }), { | ||
wrapper: createWrapper(WagmiProvider, { value: config }), | ||
}) | ||
|
||
await waitFor(() => expect(onConnect).toBeCalledTimes(1)) | ||
|
||
await disconnect(config) | ||
}) |
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,49 @@ | ||
import { type GetAccountReturnType, watchAccount } from '@wagmi/core' | ||
import type { Evaluate } from '@wagmi/core/internal' | ||
|
||
import { useConfig } from './useConfig.js' | ||
import { useEffect } from 'react' | ||
|
||
export type UseAccountStateParameters = { | ||
onConnect?( | ||
data: Evaluate< | ||
Pick< | ||
Extract<GetAccountReturnType, { status: 'connected' }>, | ||
'address' | 'addresses' | 'chainId' | 'connector' | ||
> & { | ||
isReconnected: boolean | ||
} | ||
>, | ||
): void | ||
onDisconnect?(): void | ||
} | ||
|
||
/** https://wagmi.sh/react/hooks/useAccountState */ | ||
export function useAccountState(parameters: UseAccountStateParameters = {}) { | ||
const { onConnect, onDisconnect } = parameters | ||
const config = useConfig() | ||
|
||
useEffect(() => { | ||
return watchAccount(config, { | ||
onChange(data, prevData) { | ||
if ( | ||
(prevData.status === 'reconnecting' || | ||
(prevData.status === 'connecting' && | ||
prevData.address === undefined)) && | ||
data.status === 'connected' | ||
) { | ||
const { address, addresses, chainId, connector } = data | ||
const isReconnected = | ||
prevData.status === 'reconnecting' || | ||
// if `previousAccount.status` is `undefined`, the connector connected immediately. | ||
prevData.status === undefined | ||
onConnect?.({ address, addresses, chainId, connector, isReconnected }) | ||
} else if ( | ||
prevData.status === 'connected' && | ||
data.status === 'disconnected' | ||
) | ||
onDisconnect?.() | ||
}, | ||
}) | ||
}, [config, onConnect, onDisconnect]) | ||
} |
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
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
bcc6c61
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.
Successfully deployed to the following URLs:
wagmi-v2 – ./docs
wagmi-v2.vercel.app
wagmi-v2-git-alpha-wagmi-dev.vercel.app
wagmi-v2-wagmi-dev.vercel.app
alpha.wagmi.sh