shoc-frontend-new/src/test/test-utils.tsx
Alexandre Brandizzi c68d47af7b
Some checks failed
CI / ci (push) Has been cancelled
Deploy / deploy (push) Has been cancelled
chore: upgrade frontend dependencies (#24)
* chore: upgrade frontend application dependencies

* chore: upgrade frontend infrastructure dependencies

* fix: align lockfiles with CI npm

* fix: preserve ky error semantics

* fix: sanitize timeout error message
2026-07-14 21:35:59 -03:00

62 lines
1.7 KiB
TypeScript

import { CssBaseline, ThemeProvider } from "@mui/material";
import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
import { render, type RenderOptions } from "@testing-library/react";
import type { ReactElement, ReactNode } from "react";
import { MemoryRouter, type MemoryRouterProps } from "react-router";
import { muiTheme } from "@/lib/theme/mui-theme";
import { AuthProvider } from "@/providers/auth-provider";
export function createTestQueryClient(): QueryClient {
return new QueryClient({
defaultOptions: {
queries: {
retry: false,
gcTime: 0,
},
mutations: {
retry: false,
},
},
});
}
type RenderWithProvidersOptions = Omit<RenderOptions, "wrapper"> & {
route?: string;
routerProps?: Omit<MemoryRouterProps, "children">;
withAuth?: boolean;
queryClient?: QueryClient;
};
export function renderWithProviders(
ui: ReactElement,
{
route = "/",
routerProps,
withAuth = true,
queryClient = createTestQueryClient(),
...renderOptions
}: RenderWithProvidersOptions = {},
) {
const initialEntries = routerProps?.initialEntries ?? [route];
function Wrapper({ children }: { children: ReactNode }) {
const content = withAuth ? <AuthProvider>{children}</AuthProvider> : children;
return (
<QueryClientProvider client={queryClient}>
<ThemeProvider theme={muiTheme}>
<CssBaseline />
<MemoryRouter {...routerProps} initialEntries={initialEntries}>
{content}
</MemoryRouter>
</ThemeProvider>
</QueryClientProvider>
);
}
return {
...render(ui, { wrapper: Wrapper, ...renderOptions }),
queryClient,
};
}