RequireAuth.test.tsx
2.1 KB
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
49
50
51
52
53
// REQ-USR-004: RequireAuth 守卫三态(BR1) — authResolving / unauthenticated / ready
import { describe, it, expect } from 'vitest';
import { screen } from '@testing-library/react';
import { Routes, Route, Outlet, useLocation } from 'react-router-dom';
import { renderShell } from './renderShell';
import RequireAuth from '../../src/router/RequireAuth';
// 哨兵:登录页读出 state.from,便于断言重定向携带来源
function LoginSentinel() {
const loc = useLocation();
const from = (loc.state as { from?: string } | null)?.from;
return <div data-testid="login-sentinel">login from={from ?? 'none'}</div>;
}
function ProtectedSentinel() {
return <div data-testid="protected-sentinel">protected-content</div>;
}
function renderGuard(initialEntries: string[], preloadedAuth?: Parameters<typeof renderShell>[1]['preloadedAuth']) {
return renderShell(
<Routes>
<Route path="/login" element={<LoginSentinel />} />
<Route element={<RequireAuth />}>
<Route path="/" element={<ProtectedSentinel />} />
</Route>
</Routes>,
{ initialEntries, preloadedAuth },
);
}
describe('RequireAuth', () => {
it('redirects to /login when no token', () => {
renderGuard(['/'], { token: null, user: null });
expect(screen.getByTestId('login-sentinel')).toBeInTheDocument();
expect(screen.getByText(/from=\//)).toBeInTheDocument();
expect(screen.queryByTestId('protected-sentinel')).not.toBeInTheDocument();
});
it('renders Spin placeholder when token present but user not resolved', () => {
renderGuard(['/'], { token: 't', user: null });
expect(screen.getByTestId('auth-resolving')).toBeInTheDocument();
expect(screen.queryByTestId('protected-sentinel')).not.toBeInTheDocument();
expect(screen.queryByTestId('login-sentinel')).not.toBeInTheDocument();
});
it('renders protected content when token and user ready', () => {
renderGuard(['/'], {
token: 't',
user: { id: 1, sUserName: '朱子纯', sUserType: '超级管理员', sLanguage: '中文' },
});
expect(screen.getByTestId('protected-sentinel')).toBeInTheDocument();
});
});