Files
slp-modular-cms/frontend/src/test/utils.tsx
T
SluijsensandClaude Sonnet 5 11ec08aac3
Continuous Integration / config (pull_request) Successful in 11s
Continuous Integration / changes (pull_request) Successful in 21s
Continuous Integration / backend-build (pull_request) Skipped
Continuous Integration / backend-test (pull_request) Skipped
Continuous Integration / vulnerability-scan (pull_request) Skipped
Continuous Integration / frontend-prepare (pull_request) Successful in 1m37s
Continuous Integration / frontend-build (pull_request) Successful in 2m12s
Continuous Integration / frontend-test (pull_request) Successful in 4m42s
Continuous Integration / frontend-lint (pull_request) Successful in 2m2s
Continuous Integration / publish-production (pull_request) Skipped
Continuous Integration / deploy-production (pull_request) Skipped
Continuous Integration / publish-test (pull_request) Successful in 6m51s
Continuous Integration / deploy-test (pull_request) Skipped
Restructures frontend to a feature-based folder layout
Groups auth, setup, invitation, profile, users, cms, availability and
system code (services/hooks, components, schemas, mocks, pages) under
src/features/<name> instead of splitting by technical layer (api/,
components/, lib/schemas/, mocks/, pages/). Renames the old
connection-oriented `api` layer to `services` per feature, and splits
the monolithic api/types.ts into per-feature types.ts files (with
ProblemDetails/ApiResult merged into lib/api-client.ts as shared
infra). Layout-agnostic code (ui primitives, app shell, i18n, test
utils, lib) stays at the top level.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-07-31 22:30:37 +02:00

78 lines
2.7 KiB
TypeScript

/* eslint-disable react-refresh/only-export-components */
import { useState, type ReactElement } from 'react';
import { render } from '@testing-library/react';
import { http, HttpResponse } from 'msw';
import { createMemoryHistory, createRouter, RouterProvider } from '@tanstack/react-router';
import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
import { I18nextProvider } from 'react-i18next';
import { AuthProvider } from '@/features/auth/context/AuthProvider';
import { useAuth } from '@/features/auth/context/auth-context';
import { Toaster } from '@/components/ui/sonner';
import { routeTree } from '@/router';
import { server } from '@/mocks/server';
import { API_BASE, makeAuthResponse } from '@/features/auth/mocks/fixtures';
import i18n from '@/i18n/config';
function createTestQueryClient() {
return new QueryClient({
defaultOptions: { queries: { retry: false } },
});
}
/** Force the silent-refresh-on-mount to fail, leaving the app in guest state. */
export function mockGuest(): void {
server.use(
http.post(`${API_BASE}/api/v1/auth/refresh`, () =>
HttpResponse.json({ status: 401, title: 'Unauthorized' }, { status: 401 }),
),
);
}
/** Force the silent-refresh-on-mount to succeed, leaving the app authenticated. */
export function mockAuthenticated(): void {
server.use(
http.post(`${API_BASE}/api/v1/auth/refresh`, () => HttpResponse.json(makeAuthResponse())),
);
}
/** Render an arbitrary element wrapped in i18n + auth + query providers. */
export function renderWithProviders(ui: ReactElement) {
return render(
<QueryClientProvider client={createTestQueryClient()}>
<I18nextProvider i18n={i18n}>
<AuthProvider>{ui}</AuthProvider>
</I18nextProvider>
</QueryClientProvider>,
);
}
function AppHarness({ initialPath }: { initialPath: string }) {
const auth = useAuth();
const [router] = useState(() =>
createRouter({
routeTree,
history: createMemoryHistory({ initialEntries: [initialPath] }),
context: { auth },
}),
);
if (auth.status === 'loading') {
return null;
}
return <RouterProvider router={router} context={{ auth }} />;
}
/** Render the full application router at a given path, inside all providers. */
export function renderApp(initialPath = '/') {
return render(
<QueryClientProvider client={createTestQueryClient()}>
<I18nextProvider i18n={i18n}>
<AuthProvider>
<AppHarness initialPath={initialPath} />
<Toaster />
</AuthProvider>
</I18nextProvider>
</QueryClientProvider>,
);
}