BusinessAdminShell.test.tsx
6.84 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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
import { fireEvent, render, screen, within } from '@testing-library/react';
import { RobotIcon, SettingIcon } from 'tdesign-icons-react';
import { describe, expect, it, vi } from 'vitest';
import { BusinessAdminShell, type BusinessAdminShellMenuItem } from '@gl/business-components';
const menuItems: BusinessAdminShellMenuItem[] = [
{
key: 'agent-management',
label: 'Agent管理',
icon: <RobotIcon data-testid="agent-menu-icon" />,
children: [
{ key: 'agent-list', label: 'Agent列表' },
{ key: 'agent-tag-list', label: '岗位标签管理' },
],
},
{
key: 'official-site-management',
label: '官网配置',
icon: <SettingIcon data-testid="official-site-menu-icon" />,
children: [{ key: 'official-site-job-tabs', label: '岗位展示配置' }],
},
];
describe('BusinessAdminShell', () => {
it('renders the fixed post-login shell structure with system name, menu, header and 24px content padding', () => {
render(
<BusinessAdminShell
systemName="岗位Agent管理后台"
menuItems={menuItems}
activeMenuKey="agent-list"
expandedMenuKeys={['agent-management']}
breadcrumbItems={[
{ key: 'agent-management', label: 'Agent管理' },
{ key: 'agent-list', label: 'Agent列表' },
]}
user={{ name: '管理员' }}
onMenuChange={vi.fn()}
onLogout={vi.fn()}
>
<h2>页面内容</h2>
</BusinessAdminShell>,
);
expect(screen.getByText('岗位Agent管理后台')).toBeInTheDocument();
expect(screen.getAllByText('Agent管理').length).toBeGreaterThan(0);
expect(screen.getByTestId('agent-menu-icon')).toBeInTheDocument();
expect(screen.getByTestId('official-site-menu-icon')).toBeInTheDocument();
const leftNavigation = screen.getByLabelText('左侧导航');
expect(within(leftNavigation).getByText('Agent列表').closest('li')).toHaveClass('t-is-active');
expect(within(leftNavigation).getByText('Agent管理')).toBeInTheDocument();
expect(within(leftNavigation).getByText('Agent列表')).toBeInTheDocument();
expect(screen.getByText('页面内容').closest('main')).toHaveStyle({ padding: '24px' });
expect(screen.getByText('管理员')).toBeInTheDocument();
expect(screen.getByLabelText('管理员头像')).toHaveTextContent('管');
expect(screen.getByRole('button', { name: '退出登录' })).toBeInTheDocument();
});
it('collapses and expands the left menu while preserving configured menu icons', () => {
render(
<BusinessAdminShell
systemName="岗位Agent管理后台"
menuItems={menuItems}
activeMenuKey="agent-list"
expandedMenuKeys={['agent-management']}
breadcrumbItems={[
{ key: 'agent-management', label: 'Agent管理' },
{ key: 'agent-list', label: 'Agent列表' },
]}
user={{ name: '管理员' }}
onMenuChange={vi.fn()}
onLogout={vi.fn()}
>
<h2>页面内容</h2>
</BusinessAdminShell>,
);
fireEvent.click(screen.getByRole('button', { name: '收起菜单' }));
expect(screen.getByRole('button', { name: '展开菜单' })).toBeInTheDocument();
expect(screen.getByTestId('agent-menu-icon')).toBeInTheDocument();
expect(screen.getByLabelText('左侧导航')).toHaveStyle({ width: '64px' });
fireEvent.click(screen.getByRole('button', { name: '展开菜单' }));
expect(screen.getByRole('button', { name: '收起菜单' })).toBeInTheDocument();
expect(screen.getByLabelText('左侧导航')).toHaveStyle({ width: '220px' });
expect(screen.getByText('岗位Agent管理后台')).toBeInTheDocument();
});
it('toggles a first-level menu section without being locked by route expanded keys', () => {
render(
<BusinessAdminShell
systemName="岗位Agent管理后台"
menuItems={menuItems}
activeMenuKey="agent-list"
expandedMenuKeys={['agent-management']}
breadcrumbItems={[
{ key: 'agent-management', label: 'Agent管理' },
{ key: 'agent-list', label: 'Agent列表' },
]}
user={{ name: '管理员' }}
onMenuChange={vi.fn()}
onLogout={vi.fn()}
>
<h2>页面内容</h2>
</BusinessAdminShell>,
);
const leftNavigation = screen.getByLabelText('左侧导航');
const agentMenuSection = within(leftNavigation).getByText('Agent管理').closest('li');
expect(agentMenuSection).toHaveClass('t-is-opened');
fireEvent.click(within(leftNavigation).getByText('Agent管理'));
expect(agentMenuSection).not.toHaveClass('t-is-opened');
fireEvent.click(within(leftNavigation).getByText('Agent管理'));
expect(agentMenuSection).toHaveClass('t-is-opened');
});
it('supports independent page breadcrumbs returning to the secondary menu page', () => {
const handleBreadcrumbClick = vi.fn();
render(
<BusinessAdminShell
systemName="岗位Agent管理后台"
menuItems={menuItems}
activeMenuKey="agent-list"
expandedMenuKeys={['agent-management']}
breadcrumbItems={[
{ key: 'agent-management', label: 'Agent管理' },
{ key: 'agent-list', label: 'Agent列表', onClick: handleBreadcrumbClick },
{ key: 'agent-create', label: '新增Agent' },
]}
user={{ name: '管理员' }}
onMenuChange={vi.fn()}
onLogout={vi.fn()}
>
<h2>新增Agent</h2>
</BusinessAdminShell>,
);
expect(screen.getByRole('heading', { name: '新增Agent' })).toBeInTheDocument();
fireEvent.click(screen.getByRole('button', { name: 'Agent列表' }));
expect(handleBreadcrumbClick).toHaveBeenCalledTimes(1);
});
it('shows profile and logout actions in avatar dropdown when profile is enabled', () => {
const handleProfile = vi.fn();
const handleLogout = vi.fn();
render(
<BusinessAdminShell
systemName="岗位Agent管理后台"
menuItems={menuItems}
activeMenuKey="agent-list"
expandedMenuKeys={['agent-management']}
breadcrumbItems={[
{ key: 'agent-management', label: 'Agent管理' },
{ key: 'agent-list', label: 'Agent列表' },
]}
user={{ name: '管理员' }}
hasProfile
onMenuChange={vi.fn()}
onProfile={handleProfile}
onLogout={handleLogout}
>
<h2>页面内容</h2>
</BusinessAdminShell>,
);
expect(screen.queryByRole('button', { name: '退出登录' })).not.toBeInTheDocument();
fireEvent.click(screen.getByRole('button', { name: '管理员 用户菜单' }));
fireEvent.click(screen.getByText('个人信息'));
fireEvent.click(screen.getByRole('button', { name: '管理员 用户菜单' }));
fireEvent.click(screen.getByText('退出登录'));
expect(handleProfile).toHaveBeenCalledTimes(1);
expect(handleLogout).toHaveBeenCalledTimes(1);
});
});