Adds front-end set-up

This commit is contained in:
2026-06-20 17:04:17 +02:00
parent efd1569c26
commit 7dfc3a9692
62 changed files with 6875 additions and 3 deletions
+28
View File
@@ -0,0 +1,28 @@
import { describe, expect, it } from 'vitest';
import { screen } from '@testing-library/react';
import { renderApp, mockAuthenticated, mockGuest } from '@/test/utils';
describe('Route guards (BR-U1-05, BR-U1-06)', () => {
it('redirects an unauthenticated user from a protected route to /login', async () => {
mockGuest();
renderApp('/dashboard');
expect(await screen.findByTestId('login-form-submit-button')).toBeInTheDocument();
expect(screen.queryByTestId('dashboard-title')).not.toBeInTheDocument();
});
it('allows an authenticated user to reach a protected route', async () => {
mockAuthenticated();
renderApp('/dashboard');
expect(await screen.findByTestId('dashboard-title')).toBeInTheDocument();
});
it('redirects an authenticated user away from /login to the dashboard', async () => {
mockAuthenticated();
renderApp('/login');
expect(await screen.findByTestId('dashboard-title')).toBeInTheDocument();
expect(screen.queryByTestId('login-form-submit-button')).not.toBeInTheDocument();
});
});
+45
View File
@@ -0,0 +1,45 @@
import '@testing-library/jest-dom/vitest';
import { afterAll, afterEach, beforeAll, vi } from 'vitest';
import { cleanup } from '@testing-library/react';
import { server } from '@/mocks/server';
import { api } from '@/lib/api-client';
import '@/i18n/config';
// jsdom is missing a few browser APIs that Radix/sonner touch.
const globalAny = globalThis as unknown as {
matchMedia?: unknown;
ResizeObserver?: unknown;
};
if (typeof globalAny.matchMedia === 'undefined') {
globalAny.matchMedia = vi.fn().mockImplementation((query: string) => ({
matches: false,
media: query,
onchange: null,
addListener: vi.fn(),
removeListener: vi.fn(),
addEventListener: vi.fn(),
removeEventListener: vi.fn(),
dispatchEvent: vi.fn(),
}));
}
if (typeof globalAny.ResizeObserver === 'undefined') {
globalAny.ResizeObserver = class {
observe() {}
unobserve() {}
disconnect() {}
};
}
beforeAll(() => server.listen({ onUnhandledRequest: 'error' }));
afterEach(() => {
cleanup();
server.resetHandlers();
// Reset shared client state between tests.
api.setAccessToken(null);
localStorage.clear();
});
afterAll(() => server.close());
+64
View File
@@ -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>,
);
}