From f14fa0d5f2cf856603382ad3893accd5c17b8ce7 Mon Sep 17 00:00:00 2001 From: CCS Admin Date: Thu, 26 Feb 2026 21:10:29 +0000 Subject: [PATCH] feat: add test utilities (renderWithProviders, renderHookWithProviders) Co-Authored-By: Claude Opus 4.6 --- frontend/src/test/utils.tsx | 66 +++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 frontend/src/test/utils.tsx diff --git a/frontend/src/test/utils.tsx b/frontend/src/test/utils.tsx new file mode 100644 index 0000000..327e8c5 --- /dev/null +++ b/frontend/src/test/utils.tsx @@ -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 ( + + + + {children} + + + + ) +} + +export function renderWithProviders( + ui: ReactElement, + options?: Omit & { initialRoute?: string }, +) { + const { initialRoute, ...renderOptions } = options ?? {} + return render(ui, { + wrapper: ({ children }) => ( + {children} + ), + ...renderOptions, + }) +} + +export function renderHookWithProviders( + hook: () => TResult, + options?: { initialRoute?: string }, +) { + const queryClient = createTestQueryClient() + const wrapper = ({ children }: { children: ReactNode }) => ( + + + {children} + + + ) + return { ...renderHook(hook, { wrapper }), queryClient } +}