Skip to content

Commit

Permalink
chore: boost coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
tmm committed Oct 22, 2024
1 parent e2c26f1 commit 77aa9fb
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 28 deletions.
48 changes: 24 additions & 24 deletions packages/core/src/createConfig.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { accounts, chain, wait } from '@wagmi/test'
import {
announceProvider,
type EIP1193Provider,
type EIP6963ProviderDetail,
announceProvider,
} from 'mipd'
import { http } from 'viem'
import { expect, test, vi } from 'vitest'
Expand All @@ -17,6 +17,29 @@ import { createStorage } from './createStorage.js'

const { mainnet, optimism } = chain

vi.mock(import('mipd'), async (importOriginal) => {
const mod = await importOriginal()

let cache: typeof mod | undefined
if (!cache)
cache = {
...mod,
createStore() {
const store = mod.createStore()
return {
...store,
getProviders() {
return [
getProviderDetail({ name: 'Example', rdns: 'com.example' }),
getProviderDetail({ name: 'Mock', rdns: 'com.mock' }),
]
},
}
},
}
return cache
})

test('default', () => {
const config = createConfig({
chains: [mainnet],
Expand Down Expand Up @@ -412,26 +435,3 @@ function getProviderDetail(
provider: `<EIP1193Provider_${info.rdns}>` as unknown as EIP1193Provider,
}
}

vi.mock(import('mipd'), async (importOriginal) => {
const mod = await importOriginal()

let _cache: typeof mod | undefined
if (!_cache)
_cache = {
...mod,
createStore() {
const store = mod.createStore()
return {
...store,
getProviders() {
return [
getProviderDetail({ name: 'Example', rdns: 'com.example' }),
getProviderDetail({ name: 'Mock', rdns: 'com.mock' }),
]
},
}
},
}
return _cache
})
61 changes: 57 additions & 4 deletions packages/core/src/hydrate.test.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,52 @@
import { config } from '@wagmi/test'
import { accounts, config, wait } from '@wagmi/test'
import { http } from 'viem'
import { mainnet } from 'viem/chains'
import { expect, test } from 'vitest'
import { expect, test, vi } from 'vitest'
import type { EIP1193Provider } from 'mipd'

import { createConfig } from './createConfig.js'
import { createStorage } from './createStorage.js'
import { hydrate } from './hydrate.js'
import { cookieStorage } from './utils/cookie.js'
import { createConnector } from './connectors/createConnector.js'
import { mock } from './connectors/mock.js'

vi.mock(import('mipd'), async (importOriginal) => {
const mod = await importOriginal()

let cache: typeof mod | undefined
if (!cache)
cache = {
...mod,
createStore() {
const store = mod.createStore()
return {
...store,
getProviders() {
const info = {
icon: 'data:image/svg+xml,<svg width="32px" height="32px" viewBox="0 0 32 32"/>',
uuid: crypto.randomUUID(),
} as const
const provider = '<EIP1193Provider>' as unknown as EIP1193Provider
return [
{ info: { ...info, name: 'Foo', rdns: 'com.foo' }, provider },
{ info: { ...info, name: 'Bar', rdns: 'com.bar' }, provider },
{ info: { ...info, name: 'Mock', rdns: 'com.mock' }, provider },
]
},
}
},
}
return cache
})

test('default', () => {
const { onMount } = hydrate(config, {
initialState: undefined,
reconnectOnMount: false,
})
onMount()

expect(onMount).toBeDefined()
})

Expand All @@ -35,15 +68,24 @@ test('initialState', () => {
reconnectOnMount: true,
})
onMount()

expect(onMount).toBeDefined()
})

test('ssr', () => {
test('ssr', async () => {
const config = createConfig({
chains: [mainnet],
transports: { [mainnet.id]: http() },
connectors: [
createConnector((c) => {
return {
...mock({ accounts })(c),
rdns: 'com.mock',
}
}),
],
ssr: true,
storage: createStorage({ storage: cookieStorage }),
transports: { [mainnet.id]: http() },
})

const { onMount } = hydrate(config, {
Expand All @@ -58,4 +100,15 @@ test('ssr', () => {
onMount()
expect(onMount).toBeDefined()
expect(config.chains[0].id).toBe(1)

await wait(100)
expect(config.connectors.map((x) => x.rdns ?? x.id)).toMatchInlineSnapshot(
`
[
"com.mock",
"com.foo",
"com.bar",
]
`,
)
})

0 comments on commit 77aa9fb

Please sign in to comment.