File size: 2,684 Bytes
af1bee9
 
fb43301
af1bee9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
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 });
  });
});