-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest-utils.tsx
133 lines (115 loc) · 3.9 KB
/
test-utils.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/* eslint-disable import/no-extraneous-dependencies */
import { StaticJsonRpcProvider } from '@ethersproject/providers/lib/url-json-rpc-provider'
import { Wallet } from '@ethersproject/wallet'
import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
import { RenderOptions, render } from '@testing-library/react'
import { RenderHookOptions, renderHook } from '@testing-library/react-hooks'
import userEvent from '@testing-library/user-event'
import { MockConnector } from '@wagmi/core/connectors/mock'
import React, { FC, ReactElement } from 'react'
import { ThemeProvider } from 'styled-components'
import { WagmiConfig, createClient } from 'wagmi'
import { ThorinGlobalStyles, lightTheme } from '@ensdomains/thorin'
import { DeepPartial } from './types'
jest.mock('@app/hooks/useRegistrationReducer', () => jest.fn(() => ({ item: { stepIndex: 0 } })))
jest.mock('wagmi', () => {
const {
useQuery,
useQueryClient,
useInfiniteQuery,
useMutation,
createClient: _createClient,
WagmiConfig: _WagmiConfig,
} = jest.requireActual('wagmi')
return {
useQuery,
useQueryClient,
useInfiniteQuery,
useMutation,
createClient: _createClient,
WagmiConfig: _WagmiConfig,
useAccount: jest.fn(() => ({ address: '0x123' })),
useBalance: jest.fn(() => ({ data: { value: { lt: () => false } } })),
useNetwork: jest.fn(() => ({ chainId: 314 })),
useFeeData: jest.fn(),
useProvider: jest.fn(),
useSigner: jest.fn(),
useSignTypedData: jest.fn(),
useBlockNumber: jest.fn(),
useSendTransaction: jest.fn(),
configureChains: jest.fn(() => ({})),
}
})
jest.mock('react-i18next', () => ({
useTranslation: () => ({
t: (value: string, opts: any) => {
const optsTxt = opts?.value || opts?.count || ''
return [value, ...(optsTxt ? [optsTxt] : [])].join('.')
},
i18n: {
isInitialized: true,
},
}),
Trans: ({ i18nKey, values }: { i18nKey: string; values: string[] }) =>
`${i18nKey} ${values ? Object.values(values).join(', ') : ''}`,
}))
const queryClient = new QueryClient({
defaultOptions: {
queries: {
cacheTime: Infinity,
retry: false,
},
},
logger: {
log: console.log,
warn: console.warn,
error: () => {},
},
})
beforeEach(() => queryClient.clear())
const privateKey = '0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80'
class EthersProviderWrapper extends StaticJsonRpcProvider {
toJSON() {
return `<Provider network={${this.network.chainId}} />`
}
}
const wagmiClient = createClient({
connectors: [
new MockConnector({
options: {
signer: new Wallet(privateKey, new EthersProviderWrapper()),
},
}) as any,
],
provider: () => new EthersProviderWrapper(),
})
jest.mock('@app/utils/query', () => ({
wagmiClientWithRefetch: wagmiClient,
}))
const AllTheProviders: FC<{ children: React.ReactNode }> = ({ children }) => {
return (
<QueryClientProvider client={queryClient}>
<WagmiConfig client={wagmiClient}>
<ThemeProvider theme={lightTheme}>
<ThorinGlobalStyles />
{children}
</ThemeProvider>
</WagmiConfig>
</QueryClientProvider>
)
}
const customRender = (ui: ReactElement, options?: Omit<RenderOptions, 'wrapper'>) =>
render(ui, { wrapper: AllTheProviders, ...options })
const customRenderHook = <TProps, TResult>(
callback: (props: TProps) => TResult,
options?: Omit<RenderHookOptions<TProps>, 'wrapper'>,
) => renderHook(callback, { wrapper: AllTheProviders as any, ...options })
export type PartialMockedFunction<T extends (...args: any) => any> = (
...args: Parameters<T>
) => DeepPartial<ReturnType<T>>
export const mockFunction = <T extends (...args: any) => any>(func: T) =>
func as unknown as jest.MockedFunction<PartialMockedFunction<T>>
export * from '@testing-library/react'
export { customRender as render }
export { customRenderHook as renderHook }
export { userEvent }