Skip to content
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

fix: returning walletClient and reconnecting mock client #4334

5 changes: 5 additions & 0 deletions .changeset/strange-hounds-rest.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@wagmi/core": patch
---

Fixed getting wallet client and reconnecting logic for the mock client.
10 changes: 9 additions & 1 deletion packages/core/src/connectors/mock.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { connect, getWalletClient } from '@wagmi/core'
import { accounts, config } from '@wagmi/test'
import { expect, test } from 'vitest'

import { mock } from './mock.js'

test('setup', () => {
Expand All @@ -9,6 +9,14 @@ test('setup', () => {
expect(connector.name).toEqual('Mock Connector')
})

test('connector client is defined', async () => {
const connectorFn = mock({ accounts })
const connector = config._internal.connectors.setup(connectorFn)

await connect(config, { connector })
await expect(getWalletClient(config)).resolves.toBeDefined()
})

test('behavior: features.connectError', () => {
const connectorFn = mock({ accounts, features: { connectError: true } })
const connector = config._internal.connectors.setup(connectorFn)
Expand Down
25 changes: 23 additions & 2 deletions packages/core/src/connectors/mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,26 @@ export function mock(parameters: MockParameters) {
type Provider = ReturnType<
Transport<'custom', unknown, EIP1193RequestFn<WalletRpcSchema>>
>
type Properties = Record<string, unknown>
type StorageItem = {
[_ in 'mock.connected']: true
}
let connected = false
let connectedChainId: number

return createConnector<Provider>((config) => ({
return createConnector<Provider, Properties, StorageItem>((config) => ({
id: 'mock',
name: 'Mock Connector',
type: mock.type,
async setup() {
connectedChainId = config.chains[0].id

const isConnected = Boolean(features.reconnect
? await config.storage?.getItem('mock.connected')
: false)
if (isConnected) {
this.connect()
}
},
async connect({ chainId } = {}) {
if (features.connectError) {
Expand All @@ -74,6 +85,9 @@ export function mock(parameters: MockParameters) {
}

connected = true
if (features.reconnect) {
await config.storage?.setItem('mock.connected', true)
}

return {
accounts: accounts.map((x) => getAddress(x)),
Expand All @@ -82,6 +96,9 @@ export function mock(parameters: MockParameters) {
},
async disconnect() {
connected = false
if (features.reconnect) {
await config.storage?.removeItem('mock.connected')
}
},
async getAccounts() {
if (!connected) throw new ConnectorNotConnectedError()
Expand Down Expand Up @@ -125,6 +142,9 @@ export function mock(parameters: MockParameters) {
async onDisconnect(_error) {
config.emitter.emit('disconnect')
connected = false
if (features.reconnect) {
await config.storage?.removeItem('mock.connected')
}
},
async getProvider({ chainId } = {}) {
const chain =
Expand All @@ -134,7 +154,8 @@ export function mock(parameters: MockParameters) {
const request: EIP1193RequestFn = async ({ method, params }) => {
// eth methods
if (method === 'eth_chainId') return numberToHex(connectedChainId)
if (method === 'eth_requestAccounts') return parameters.accounts
if (method === 'eth_accounts' || method === 'eth_requestAccounts')
return parameters.accounts
if (method === 'eth_signTypedData_v4')
if (features.signTypedDataError) {
if (typeof features.signTypedDataError === 'boolean')
Expand Down
Loading