ChannelCodeRoutes.test.tsx
5.01 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
import { cleanup, render, screen, waitFor, within } from '@testing-library/react';
import { MemoryRouter } from 'react-router-dom';
import { beforeEach, describe, expect, it, vi } from 'vitest';
import { App } from '../../App';
import { getChannelQRCodeList, getChannelQRCodeDetail, getEmployeeMounts, type ChannelQRCodeListItem, type ChannelQRCodeStatus, type ChannelQRCodeDetail } from '../../api/channelCode';
import { getPositionTagList } from '../../api/positionTag';
vi.mock('../../api/channelCode', () => ({
getChannelQRCodeList: vi.fn(),
getChannelQRCodeDetail: vi.fn(),
createChannelQRCode: vi.fn(),
updateChannelQRCode: vi.fn(),
enableChannelQRCode: vi.fn(),
disableChannelQRCode: vi.fn(),
getPositionEmployees: vi.fn(),
getEmployeeConfiguredQRCodes: vi.fn(),
getEmployeeMounts: vi.fn(),
}));
vi.mock('../../api/positionTag', () => ({
getPositionTagList: vi.fn(),
createPositionTag: vi.fn(),
updatePositionTag: vi.fn(),
getPositionTagReferences: vi.fn(),
deletePositionTag: vi.fn(),
}));
const mockChannelCodes: ChannelQRCodeListItem[] = [
{
qrcode_id: 1,
qrcode_name: '店销顾问渠道活码',
employee_name: '张小萌',
position_name: '店销顾问',
channel_code: 'DSC-QR-001',
qrcode_url: 'https://picsum.photos/seed/store_sales/200/200',
agent_count: 2,
agent_names_preview: ['店销助手Agent', '管家Agent'],
status: 'enable' as ChannelQRCodeStatus,
created_at: '2026-05-10 09:00:00',
operations: ['view', 'edit', 'disable'],
},
];
const mockPositionTags = [
{ tag_id: 1, name: '店销顾问', code: 'store_sales', agent_count: 2, created_at: '2026-05-15' },
];
const mockChannelCodeDetail: ChannelQRCodeDetail = {
qrcode_id: 1,
employee_id: 'emp_001',
position_code: 'store_sales',
qrcode_name: '店销顾问渠道活码',
employee_name: '张小萌',
position_name: '店销顾问',
channel_code: 'DSC-QR-001',
qrcode_url: 'https://cdn.weixin.qq.com/qrcode/test.png',
agent_ids: [1, 2],
agent_names: ['店销助手Agent', '管家Agent'],
status: 'enable' as ChannelQRCodeStatus,
remark: '备注',
welcome_message: '欢迎',
created_at: '2026-05-15T10:00:00Z',
created_by: 'admin',
};
const mockEmployeeMountsData = {
employee_count: 1,
employees: [
{
employee_id: 'emp_001',
wechat_user_id: 'zhangsan_wx',
employee_name: '张三',
employee_role_text: '店销主管',
position_code: 'store_sales',
position_name: '驻店销售岗',
qrcode_count: 0,
qrcodes: [],
agent_names: ['店销助手Agent'],
},
],
};
function renderChannelCodes(path = '/channel-codes') {
window.localStorage.setItem('agent-admin-local-login', 'true');
return render(
<MemoryRouter initialEntries={[path]}>
<App />
</MemoryRouter>,
);
}
function getMenuItem(label: string) {
const item = within(screen.getByLabelText('左侧导航')).getByText(label).closest('li');
if (!item) throw new Error(`Menu item ${label} was not found`);
return item;
}
describe('Channel code routes and navigation', () => {
beforeEach(() => {
window.localStorage.clear();
vi.clearAllMocks();
vi.mocked(getChannelQRCodeList).mockResolvedValue({
list: mockChannelCodes,
total: mockChannelCodes.length,
page: 1,
page_size: 10,
});
vi.mocked(getPositionTagList).mockResolvedValue({
list: mockPositionTags,
total: mockPositionTags.length,
page: 1,
page_size: 100,
});
vi.mocked(getChannelQRCodeDetail).mockResolvedValue(mockChannelCodeDetail);
vi.mocked(getEmployeeMounts).mockResolvedValue(mockEmployeeMountsData);
});
it('renders the list route at /channel-codes', async () => {
renderChannelCodes();
await waitFor(() => {
expect(screen.getByRole('heading', { name: '渠道活码管理' })).toBeInTheDocument();
});
});
it('renders the new route at /channel-codes/new', async () => {
vi.mocked(getPositionTagList).mockResolvedValue({
list: [{ tag_id: 1, name: '驻店销售岗', code: 'store_sales', agent_count: 2, created_at: '2026-05-15', agents: [{ id: 1, name: '店销助手Agent' }] }],
total: 1,
page: 1,
page_size: 100,
});
renderChannelCodes('/channel-codes/new');
await waitFor(() => {
expect(screen.getByRole('heading', { name: '新增员工渠道活码' })).toBeInTheDocument();
});
});
it('renders the edit route at /channel-codes/:codeId/edit', async () => {
renderChannelCodes('/channel-codes/1/edit');
await waitFor(() => {
expect(screen.getByRole('heading', { name: '编辑员工渠道活码' })).toBeInTheDocument();
});
});
it('shows navigation entry under Agent群管理 group', async () => {
renderChannelCodes();
await waitFor(() => {
expect(getMenuItem('渠道活码')).toBeInTheDocument();
});
});
it('marks navigation entry as active when on channel-codes routes', async () => {
renderChannelCodes();
await waitFor(() => {
expect(getMenuItem('渠道活码')).toHaveClass('t-is-active');
});
});
});