Adds front-end set-up
This commit is contained in:
@@ -0,0 +1,64 @@
|
||||
/* 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 { I18nextProvider } from 'react-i18next';
|
||||
import { AuthProvider } from '@/contexts/AuthProvider';
|
||||
import { useAuth } from '@/contexts/auth-context';
|
||||
import { routeTree } from '@/router';
|
||||
import { server } from '@/mocks/server';
|
||||
import { API_BASE, makeAuthResponse } from '@/mocks/auth/fixtures';
|
||||
import i18n from '@/i18n/config';
|
||||
|
||||
/** 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 the i18n + auth providers. */
|
||||
export function renderWithProviders(ui: ReactElement) {
|
||||
return render(
|
||||
<I18nextProvider i18n={i18n}>
|
||||
<AuthProvider>{ui}</AuthProvider>
|
||||
</I18nextProvider>,
|
||||
);
|
||||
}
|
||||
|
||||
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(
|
||||
<I18nextProvider i18n={i18n}>
|
||||
<AuthProvider>
|
||||
<AppHarness initialPath={initialPath} />
|
||||
</AuthProvider>
|
||||
</I18nextProvider>,
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user