App.test.tsx
5.08 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
import { fireEvent, render, screen } from '@testing-library/react';
import { MemoryRouter } from 'react-router-dom';
import { beforeEach, describe, expect, it } from 'vitest';
import { App } from './App';
const loginStorageKey = 'agent-admin-local-login';
function renderApp(path: string) {
return render(
<MemoryRouter initialEntries={[path]}>
<App />
</MemoryRouter>,
);
}
describe('App foundation', () => {
beforeEach(() => {
window.localStorage.clear();
});
it('renders the root layout for the React application', () => {
renderApp('/components');
expect(screen.getByRole('main')).toBeInTheDocument();
expect(screen.getByText('Agent Admin Web')).toBeInTheDocument();
});
it('renders the independent component demo route', () => {
renderApp('/components');
expect(screen.getByRole('heading', { name: '组件库 Demo' })).toBeInTheDocument();
expect(screen.getByText('全局 UI 组件库展示入口')).toBeInTheDocument();
expect(screen.queryByRole('heading', { name: 'Agent管理' })).not.toBeInTheDocument();
expect(screen.queryByRole('button', { name: '退出登录' })).not.toBeInTheDocument();
});
});
describe('Admin login and protected routes', () => {
beforeEach(() => {
window.localStorage.clear();
});
it('redirects anonymous users to login and returns to the requested protected route after login', () => {
renderApp('/agents?scenario=error');
expect(screen.getByRole('heading', { name: '岗位Agent管理后台' })).toBeInTheDocument();
expect(screen.getByLabelText('账号')).toBeInTheDocument();
expect(screen.getByLabelText('密码')).toBeInTheDocument();
fireEvent.change(screen.getByLabelText('账号'), { target: { value: 'admin' } });
fireEvent.change(screen.getByLabelText('密码'), { target: { value: '123456' } });
fireEvent.click(screen.getByRole('button', { name: '登录' }));
expect(window.localStorage.getItem(loginStorageKey)).toBe('true');
expect(screen.getByRole('heading', { name: 'Agent管理' })).toBeInTheDocument();
expect(screen.getByRole('alert', { name: '加载失败' })).toBeInTheDocument();
expect(screen.getByText('岗位Agent管理后台')).toBeInTheDocument();
expect(screen.getByLabelText('管理员')).toBeInTheDocument();
expect(screen.getByRole('button', { name: '退出登录' })).toBeInTheDocument();
});
it('shows validation and credential errors without marking the user as logged in', () => {
renderApp('/login');
fireEvent.click(screen.getByRole('button', { name: '登录' }));
expect(screen.getByText('请输入账号')).toBeInTheDocument();
expect(screen.getByText('请输入密码')).toBeInTheDocument();
expect(window.localStorage.getItem(loginStorageKey)).toBeNull();
fireEvent.change(screen.getByLabelText('账号'), { target: { value: 'admin' } });
fireEvent.change(screen.getByLabelText('密码'), { target: { value: 'wrong' } });
fireEvent.click(screen.getByRole('button', { name: '登录' }));
expect(screen.getByText('账号或密码错误')).toBeInTheDocument();
expect(window.localStorage.getItem(loginStorageKey)).toBeNull();
});
it('keeps logged-in users on refresh and clears local state on logout', () => {
window.localStorage.setItem(loginStorageKey, 'true');
renderApp('/agents');
expect(screen.getByRole('heading', { name: 'Agent管理' })).toBeInTheDocument();
fireEvent.click(screen.getByRole('button', { name: '退出登录' }));
expect(screen.getByRole('dialog', { name: '确认退出登录?' })).toBeInTheDocument();
expect(window.localStorage.getItem(loginStorageKey)).toBe('true');
fireEvent.click(screen.getByRole('button', { name: '取消' }));
expect(screen.queryByRole('dialog', { name: '确认退出登录?' })).not.toBeInTheDocument();
expect(window.localStorage.getItem(loginStorageKey)).toBe('true');
fireEvent.click(screen.getByRole('button', { name: '退出登录' }));
fireEvent.click(screen.getByRole('button', { name: '确定' }));
expect(window.localStorage.getItem(loginStorageKey)).toBeNull();
expect(screen.getByRole('heading', { name: '岗位Agent管理后台' })).toBeInTheDocument();
});
it('renders hierarchical admin navigation with the child route selected', () => {
window.localStorage.setItem(loginStorageKey, 'true');
renderApp('/agents');
expect(screen.getByRole('button', { name: 'Agent管理' })).toHaveAttribute('data-active-parent', 'true');
expect(screen.getByRole('button', { name: 'Agent列表' })).toHaveAttribute('aria-current', 'page');
});
it('renders the skill route inside the admin shell navigation', () => {
window.localStorage.setItem(loginStorageKey, 'true');
renderApp('/skills');
expect(screen.getByRole('heading', { name: 'Skill目录' })).toBeInTheDocument();
expect(screen.getByRole('button', { name: 'Agent管理' })).toHaveAttribute('data-active-parent', 'true');
expect(screen.getByRole('button', { name: 'Skill目录' })).toHaveAttribute('aria-current', 'page');
expect(screen.getByText('Agent管理 / Skill目录')).toBeInTheDocument();
});
});