Skip to content

Commit

Permalink
test: up
Browse files Browse the repository at this point in the history
  • Loading branch information
tmm committed Sep 27, 2024
1 parent 87c378b commit d11e71f
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 16 deletions.
7 changes: 6 additions & 1 deletion packages/core/src/actions/sendTransaction.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { config, privateKey, transactionHashRegex } from '@wagmi/test'
import { parseEther } from 'viem'
import { expect, test } from 'vitest'
import { beforeEach, expect, test } from 'vitest'

import { privateKeyToAccount } from 'viem/accounts'
import { connect } from './connect.js'
Expand All @@ -9,6 +9,11 @@ import { sendTransaction } from './sendTransaction.js'

const connector = config.connectors[0]!

beforeEach(async () => {
if (config.state.current === connector.uid)
await disconnect(config, { connector })
})

test('default', async () => {
await connect(config, { connector })
await expect(
Expand Down
75 changes: 75 additions & 0 deletions packages/core/src/transports/connector.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
import { config } from '@wagmi/test'
import { expect, test } from 'vitest'

import { mock } from '../connectors/mock.js'
import { injected } from '../connectors/injected.js'
import { unstable_connector } from './connector.js'
import { createStore } from 'zustand'
import { optimism } from 'viem/chains'

const connector = config.connectors[0]!

test('setup', () => {
expect(unstable_connector(injected)({})).toMatchInlineSnapshot(`
Expand All @@ -19,3 +26,71 @@ test('setup', () => {
}
`)
})

test('behavior: connector type not found', () => {
const transport = unstable_connector({ type: 'foo' })({})
expect(() =>
transport.request({ method: 'eth_chainId' }),
).rejects.toThrowErrorMatchingInlineSnapshot(`
[ProviderDisconnectedError: The Provider is disconnected from all chains.
Details: Could not find connector of type "foo" in \`connectors\` passed to \`createConfig\`.
Version: viem@2.17.0]
`)
})

test('behavior: provider is disconnected', () => {
const transport = unstable_connector(mock)({
connectors: createStore(() => [
{
...connector,
async getProvider() {
return undefined
},
},
]),
})

expect(() =>
transport.request({ method: 'eth_chainId' }),
).rejects.toThrowErrorMatchingInlineSnapshot(`
[ProviderDisconnectedError: The Provider is disconnected from all chains.
Details: Provider is disconnected.
Version: viem@2.17.0]
`)
})

test('behavior: chainId mismatch', () => {
const transport = unstable_connector(mock)({
chain: optimism,
connectors: createStore(() => [
{
...connector,
async getProvider(options = {}) {
if (options.chainId === optimism.id) return connector.getProvider()
return undefined
},
},
]),
})

expect(() =>
transport.request({ method: 'eth_chainId' }),
).rejects.toThrowErrorMatchingInlineSnapshot(`
[ChainDisconnectedError: The Provider is not connected to the requested chain.
Details: The current chain of the connector (id: 1) does not match the target chain for the request (id: 10 – OP Mainnet).
Version: viem@2.17.0]
`)
})

test('behavior: request', () => {
const transport = unstable_connector(mock)({
connectors: createStore(() => [connector]),
})

expect(
transport.request({ method: 'eth_chainId' }),
).resolves.toThrowErrorMatchingInlineSnapshot(`"0x1"`)
})
17 changes: 10 additions & 7 deletions packages/react/src/hooks/useBalance.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,17 @@ test('default', async () => {

await waitFor(() => expect(result.current.isSuccess).toBeTruthy())

expect(result.current).toMatchInlineSnapshot(`
const { data, ...rest } = result.current
expect(data).toMatchObject(
expect.objectContaining({
decimals: expect.any(Number),
formatted: expect.any(String),
symbol: expect.any(String),
value: expect.any(BigInt),
}),
)
expect(rest).toMatchInlineSnapshot(`
{
"data": {
"decimals": 18,
"formatted": "10000",
"symbol": "ETH",
"value": 10000000000000000000000n,
},
"dataUpdatedAt": 1675209600000,
"error": null,
"errorUpdateCount": 0,
Expand Down
16 changes: 8 additions & 8 deletions packages/vue/src/composables/useBalance.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@ test('default', async () => {

await waitFor(query.isSuccess)

expect(query.data.value).toMatchInlineSnapshot(`
{
"decimals": 18,
"formatted": "10000",
"symbol": "ETH",
"value": 10000000000000000000000n,
}
`)
expect(query.data.value).toMatchObject(
expect.objectContaining({
decimals: expect.any(Number),
formatted: expect.any(String),
symbol: expect.any(String),
value: expect.any(BigInt),
}),
)
})

test('parameters: chainId', async () => {
Expand Down

0 comments on commit d11e71f

Please sign in to comment.