acf60c2a07
Wave 6. Swaps react-scripts' build/dev tooling for Next.js while keeping the app's actual routing/rendering model unchanged -- the app is almost entirely behind auth with no proven SSR/SEO need, so a real App Router rewrite would touch ~90 files for zero user-visible benefit. - next.config.js: output:'export' (static HTML+JS, same "single index.html served by nginx with try_files fallback" deploy as CRA). - app/layout.tsx + app/page.tsx: root shell ports public/index.html's <head>, mounts the whole existing App tree client-only (ssr:false) since it reads window/localStorage during initial render and Next's static prerender would otherwise execute that on the server. - Renamed src/pages/ -> src/views/ (Next's Pages Router auto-detects any `pages/` dir under the app root and tried to build our React Router page components as its own routes). - REACT_APP_* -> NEXT_PUBLIC_* across code, .env.development, Dockerfile, docker-compose.yml build args. - Replaced the CRA SVGR import (`ReactComponent` from .svg, unsupported under Turbopack) with a small inline JobbjaktMark component. - TypeScript 4.9 -> 5.9 (MUI v8's type-checked build needs syntax 4.9's parser rejects; CRA never hit this because babel doesn't type-check). - Dropped CRA-only files (index.tsx, reportWebVitals, react-app-env.d.ts, public/index.html); kept react-scripts as the Jest test runner only (next/jest migration not needed -- the existing config already works). Verified: `next build` static export succeeds, `next dev` serves the landing page and client-side routes (login etc.) correctly, all 57 frontend tests + 172 backend tests still green. Known caveat: deep-linking straight to a sub-route (e.g. /login) 404s in `next dev` since there's no server route for it -- the app only ever mounts at "/". Production is unaffected: nginx's existing try_files fallback still serves index.html for any path.
85 lines
3.1 KiB
TypeScript
85 lines
3.1 KiB
TypeScript
import React from 'react';
|
|
import { render, screen, waitFor } from '@testing-library/react';
|
|
import userEvent from '@testing-library/user-event';
|
|
import { MemoryRouter } from 'react-router-dom';
|
|
|
|
import LoginPage from './views/LoginPage';
|
|
import { ToastProvider } from './toast';
|
|
import { I18nProvider } from './i18n/I18nProvider';
|
|
import { api } from './api';
|
|
|
|
const mockNavigate = jest.fn();
|
|
|
|
jest.mock('react-router-dom', () => ({
|
|
...jest.requireActual('react-router-dom'),
|
|
useNavigate: () => mockNavigate,
|
|
}));
|
|
|
|
const mockedApi = api as jest.Mocked<typeof api>;
|
|
|
|
let consoleErrorSpy: jest.SpyInstance;
|
|
|
|
function renderLoginPage() {
|
|
return render(
|
|
<MemoryRouter future={{ v7_startTransition: true, v7_relativeSplatPath: true }}>
|
|
<I18nProvider>
|
|
<ToastProvider>
|
|
<LoginPage />
|
|
</ToastProvider>
|
|
</I18nProvider>
|
|
</MemoryRouter>,
|
|
);
|
|
}
|
|
|
|
describe('LoginPage', () => {
|
|
beforeEach(() => {
|
|
const originalConsoleError = console.error.bind(console);
|
|
consoleErrorSpy = jest.spyOn(console, 'error').mockImplementation((...args: unknown[]) => {
|
|
const [first] = args;
|
|
const message = typeof first === 'string' ? first : '';
|
|
if (message.includes('ForwardRef(TouchRipple) inside a test was not wrapped in act')) {
|
|
return;
|
|
}
|
|
originalConsoleError(...args);
|
|
});
|
|
window.localStorage.clear();
|
|
window.sessionStorage.clear();
|
|
mockedApi.post.mockReset();
|
|
mockNavigate.mockReset();
|
|
});
|
|
|
|
afterEach(() => {
|
|
consoleErrorSpy.mockRestore();
|
|
});
|
|
|
|
it('posts remember-me preference without storing an auth token in browser storage', async () => {
|
|
mockedApi.post.mockResolvedValueOnce({ data: { authenticated: true, provider: 'local' } } as any);
|
|
mockedApi.get.mockResolvedValueOnce({ data: { roles: [], email: 'person@example.com', userName: 'person' } } as any);
|
|
|
|
renderLoginPage();
|
|
await screen.findByLabelText('Email');
|
|
|
|
await userEvent.type(screen.getByLabelText('Email'), 'person@example.com');
|
|
await userEvent.type(screen.getByLabelText('Current password'), 'hunter2');
|
|
await userEvent.click(screen.getByLabelText('Remember me'));
|
|
await userEvent.click(screen.getByRole('button', { name: 'Sign in' }));
|
|
|
|
await waitFor(() => expect(mockedApi.post).toHaveBeenCalledWith('/auth/login', { email: 'person@example.com', password: 'hunter2', rememberMe: false }));
|
|
await waitFor(() => expect(mockedApi.get).toHaveBeenCalledWith('/auth/me'));
|
|
|
|
expect(window.sessionStorage.getItem('authToken')).toBeNull();
|
|
expect(window.localStorage.getItem('authToken')).toBeNull();
|
|
expect(window.localStorage.getItem('authTokenPersistence')).toBe('session');
|
|
});
|
|
|
|
it('opens the separate forgot-password page with the typed email prefilled', async () => {
|
|
renderLoginPage();
|
|
await screen.findByLabelText('Email');
|
|
|
|
await userEvent.type(screen.getByLabelText('Email'), 'person@example.com');
|
|
await userEvent.click(screen.getByRole('button', { name: 'Forgot password?' }));
|
|
|
|
expect(mockNavigate).toHaveBeenCalledWith('/forgot-password?email=person%40example.com');
|
|
});
|
|
});
|