mirror of
https://github.com/complexcaresolutions/dak.c2s.git
synced 2026-03-17 20:43:41 +00:00
feat: add test utilities (renderWithProviders, renderHookWithProviders)
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
parent
0416035ce9
commit
f14fa0d5f2
1 changed files with 66 additions and 0 deletions
66
frontend/src/test/utils.tsx
Normal file
66
frontend/src/test/utils.tsx
Normal file
|
|
@ -0,0 +1,66 @@
|
||||||
|
import { render, type RenderOptions } from '@testing-library/react'
|
||||||
|
import { renderHook } from '@testing-library/react'
|
||||||
|
import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
|
||||||
|
import { MemoryRouter } from 'react-router-dom'
|
||||||
|
import { AuthProvider } from '@/context/AuthContext'
|
||||||
|
import type { ReactElement, ReactNode } from 'react'
|
||||||
|
|
||||||
|
export function createTestQueryClient() {
|
||||||
|
return new QueryClient({
|
||||||
|
defaultOptions: {
|
||||||
|
queries: {
|
||||||
|
retry: false,
|
||||||
|
gcTime: 0,
|
||||||
|
},
|
||||||
|
mutations: {
|
||||||
|
retry: false,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
interface ProvidersProps {
|
||||||
|
children: ReactNode
|
||||||
|
initialRoute?: string
|
||||||
|
}
|
||||||
|
|
||||||
|
function Providers({ children, initialRoute = '/' }: ProvidersProps) {
|
||||||
|
const queryClient = createTestQueryClient()
|
||||||
|
return (
|
||||||
|
<QueryClientProvider client={queryClient}>
|
||||||
|
<MemoryRouter initialEntries={[initialRoute]}>
|
||||||
|
<AuthProvider>
|
||||||
|
{children}
|
||||||
|
</AuthProvider>
|
||||||
|
</MemoryRouter>
|
||||||
|
</QueryClientProvider>
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
export function renderWithProviders(
|
||||||
|
ui: ReactElement,
|
||||||
|
options?: Omit<RenderOptions, 'wrapper'> & { initialRoute?: string },
|
||||||
|
) {
|
||||||
|
const { initialRoute, ...renderOptions } = options ?? {}
|
||||||
|
return render(ui, {
|
||||||
|
wrapper: ({ children }) => (
|
||||||
|
<Providers initialRoute={initialRoute}>{children}</Providers>
|
||||||
|
),
|
||||||
|
...renderOptions,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
export function renderHookWithProviders<TResult>(
|
||||||
|
hook: () => TResult,
|
||||||
|
options?: { initialRoute?: string },
|
||||||
|
) {
|
||||||
|
const queryClient = createTestQueryClient()
|
||||||
|
const wrapper = ({ children }: { children: ReactNode }) => (
|
||||||
|
<QueryClientProvider client={queryClient}>
|
||||||
|
<MemoryRouter initialEntries={[options?.initialRoute ?? '/']}>
|
||||||
|
{children}
|
||||||
|
</MemoryRouter>
|
||||||
|
</QueryClientProvider>
|
||||||
|
)
|
||||||
|
return { ...renderHook(hook, { wrapper }), queryClient }
|
||||||
|
}
|
||||||
Loading…
Reference in a new issue