patchwise-agent-studio / web /src /components /ErrorPages.test.tsx
EnergyVenom's picture
Tidy the web app: pages by area, CSS in load order, web/README.md (b2a039d)
fb43301 verified
Raw History Blame Contribute Delete
2.68 kB
import { fireEvent, screen } from '@testing-library/react';
import { afterEach, describe, expect, it, vi } from 'vitest';
import { LoginPage } from '../pages/auth/LoginPage';
import { jsonResponse, renderUi } from '../test/utils';
import { NotFoundPage, ScreenErrorBoundary } from './ErrorPages';
function Boom(): never {
throw new Error('render failed');
}
describe('error pages and sign-in hints', () => {
afterEach(() => vi.restoreAllMocks());
it('an unknown address shows a page-not-found page with a way home', () => {
renderUi(<NotFoundPage />, { route: '/no/such/page' });
expect(screen.getByRole('heading', { name: 'This page does not exist' })).toBeInTheDocument();
expect(screen.getByText('/no/such/page')).toBeInTheDocument();
expect(screen.getByRole('link', { name: 'Go to Home' })).toHaveAttribute('href', '/');
});
it('a screen that crashes shows a recovery page instead of a blank screen', () => {
vi.spyOn(console, 'error').mockImplementation(() => {});
renderUi(<ScreenErrorBoundary resetKey="/x"><Boom /></ScreenErrorBoundary>);
expect(screen.getByRole('heading', { name: 'This screen stopped working' })).toBeInTheDocument();
expect(screen.getByRole('button', { name: 'Reload the page' })).toBeInTheDocument();
});
it('sign-in warns about Caps Lock and sends "keep me signed in"', async () => {
const bodies: unknown[] = [];
vi.spyOn(globalThis, 'fetch').mockImplementation(async (input, init) => {
const req = input instanceof Request ? input : new Request(new URL(String(input), 'http://localhost'), init);
if (req.url.endsWith('/api/auth/me')) return jsonResponse({ detail: 'not signed in', code: 'UNAUTHENTICATED' }, 401);
if (req.url.endsWith('/api/auth/login')) { bodies.push(await req.json()); return jsonResponse({ detail: 'too many', code: 'RATE_LIMITED' }, 429); }
return jsonResponse({});
});
renderUi(<LoginPage />, { auth: true, route: '/login' });
const pw = screen.getByLabelText(/Password/);
fireEvent.keyDown(pw, { key: 'A', modifierCapsLock: true });
expect(await screen.findByText('Caps Lock is on.')).toBeInTheDocument();
fireEvent.change(screen.getByLabelText(/Username/), { target: { value: 'alice' } });
fireEvent.change(pw, { target: { value: 'secret-pw' } });
fireEvent.click(screen.getByLabelText(/Keep me signed in/));
fireEvent.click(screen.getByRole('button', { name: 'Sign in' }));
expect(await screen.findByTestId('signin-error')).toHaveTextContent(/paused for a few minutes/);
expect(bodies[0]).toMatchObject({ username: 'alice', remember: true });
});
});