Skip to content

Commit

Permalink
feat(core): mock defaultConnected
Browse files Browse the repository at this point in the history
  • Loading branch information
tmm committed Oct 21, 2024
1 parent b250fc2 commit 052e72e
Show file tree
Hide file tree
Showing 9 changed files with 80 additions and 63 deletions.
5 changes: 5 additions & 0 deletions .changeset/angry-geckos-thank.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@wagmi/core": patch
---

Added `defaultConnected` feature to `mock` connector.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
},
"devDependencies": {
"@arethetypeswrong/cli": "^0.16.4",
"@biomejs/biome": "^1.9.2",
"@biomejs/biome": "^1.9.4",
"@changesets/changelog-github": "0.4.6",
"@changesets/cli": "^2.27.8",
"@types/bun": "^1.1.10",
Expand Down
10 changes: 7 additions & 3 deletions packages/core/src/connectors/mock.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { accounts, config } from '@wagmi/test'
import { expect, test } from 'vitest'

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

test('setup', () => {
Expand Down Expand Up @@ -86,9 +85,14 @@ test('behavior: connector.getProvider request errors', async () => {
})

test('behavior: reconnect', async () => {
const connectorFn = mock({ accounts, features: { reconnect: true } })
const connectorFn = mock({
accounts,
features: {
defaultConnected: true,
reconnect: true,
},
})
const connector = config._internal.connectors.setup(connectorFn)
await connect(config, { connector })

await expect(connector.isAuthorized()).resolves.toBeTruthy()
})
7 changes: 5 additions & 2 deletions packages/core/src/connectors/mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export type MockParameters = {
accounts: readonly [Address, ...Address[]]
features?:
| {
defaultConnected?: boolean | undefined
connectError?: boolean | Error | undefined
switchChainError?: boolean | Error | undefined
signMessageError?: boolean | Error | undefined
Expand All @@ -40,12 +41,14 @@ export type MockParameters = {
mock.type = 'mock' as const
export function mock(parameters: MockParameters) {
const transactionCache = new Map<Hex, Hex[]>()
const features = parameters.features ?? {}
const features =
parameters.features ??
({ defaultConnected: false } satisfies MockParameters['features'])

type Provider = ReturnType<
Transport<'custom', unknown, EIP1193RequestFn<WalletRpcSchema>>
>
let connected = false
let connected = features.defaultConnected
let connectedChainId: number

return createConnector<Provider>((config) => ({
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/exports/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -528,9 +528,9 @@ export { fallback } from '../transports/fallback.js'
// Types
////////////////////////////////////////////////////////////////////////////////

export { type SelectChains } from '../types/chain.js'
export type { SelectChains } from '../types/chain.js'

export { type Register, type ResolvedRegister } from '../types/register.js'
export type { Register, ResolvedRegister } from '../types/register.js'

////////////////////////////////////////////////////////////////////////////////
// Utilities
Expand Down
30 changes: 15 additions & 15 deletions packages/core/src/exports/internal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,24 +23,24 @@ export {
// Types
////////////////////////////////////////////////////////////////////////////////

export { type SelectChains } from '../types/chain.js'
export type { SelectChains } from '../types/chain.js'

export {
type ChainIdParameter,
type ConnectorParameter,
type ScopeKeyParameter,
export type {
ChainIdParameter,
ConnectorParameter,
ScopeKeyParameter,
} from '../types/properties.js'

export {
type Compute,
type ExactPartial,
type Mutable,
type StrictOmit as Omit,
type OneOf,
type RemoveUndefined,
type UnionCompute,
type UnionStrictOmit,
type UnionExactPartial,
export type {
Compute,
ExactPartial,
Mutable,
StrictOmit as Omit,
OneOf,
RemoveUndefined,
UnionCompute,
UnionStrictOmit,
UnionExactPartial,
} from '../types/utils.js'

////////////////////////////////////////////////////////////////////////////////
Expand Down
2 changes: 1 addition & 1 deletion packages/vue/src/exports/nuxt.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { wagmiModule } from '../nuxt/module.js'

export { type WagmiModuleOptions } from '../nuxt/module.js'
export type { WagmiModuleOptions } from '../nuxt/module.js'
export default wagmiModule
76 changes: 38 additions & 38 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 6 additions & 1 deletion site/shared/connectors/mock.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,11 @@ const connector = mock({
}, // [!code focus]
})
```
#### defaultConnected

`boolean | undefined`

Whether the connector is connected by default.

#### connectError

Expand Down Expand Up @@ -120,4 +125,4 @@ Whether to throw an error when `'eth_signTypedData_v4'` is called.

`boolean | Error | undefined`

Whether to throw an error when `connector.switchChain` is called.
Whether to throw an error when `connector.switchChain` is called.

0 comments on commit 052e72e

Please sign in to comment.