-
Notifications
You must be signed in to change notification settings - Fork 0
/
test-utils.tsx
61 lines (54 loc) · 1.77 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
import { ThemeContext, IThemeContext, Theme } from "./ThemeContext"
import { render, RenderOptions } from "@testing-library/react"
import { MemoryRouter } from "react-router-dom"
import { ReactElement } from "react"
import {
QueryClient,
QueryClientProvider,
QueryClientConfig,
} from "@tanstack/react-query"
export function setupQueryClient(config?: QueryClientConfig | undefined) {
if (!config) {
return new QueryClient({
defaultOptions: {
queries: {
retry: false,
},
},
})
}
return new QueryClient(config)
}
interface IExtendedRenderOptions extends RenderOptions {
theme?: IThemeContext
initialEntries?: string[]
queryClient?: QueryClient
}
export const customRender = (
ui: ReactElement,
options?: Omit<IExtendedRenderOptions, "wrapper">,
) => {
const defaultTheme: IThemeContext = {
theme: Theme.LIGHT,
setTheme: vi.fn(),
}
const Wrapper = ({ children }: { children: React.ReactNode }) => {
return (
<ThemeContext.Provider value={options?.theme ?? defaultTheme}>
<MemoryRouter initialEntries={options?.initialEntries ?? ["/"]}>
{options?.queryClient ? (
<QueryClientProvider client={options?.queryClient}>
{children}
</QueryClientProvider>
) : (
children
)}
</MemoryRouter>
</ThemeContext.Provider>
)
}
return render(ui, { wrapper: Wrapper, ...options })
}
// eslint-disable-next-line react-refresh/only-export-components
export * from "@testing-library/react"
export { customRender as render }