ChannelCodeStatusOperations.test.tsx
10.6 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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
import { fireEvent, render, screen, within, waitFor } from '@testing-library/react';
import { MemoryRouter } from 'react-router-dom';
import { beforeEach, describe, expect, it, vi } from 'vitest';
import { App } from '../../App';
import { ApiError } from '../../api/client';
import { getChannelQRCodeList, enableChannelQRCode, disableChannelQRCode, type ChannelQRCodeListItem, type ChannelQRCodeStatus } 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'],
},
{
qrcode_id: 2,
qrcode_name: '售后客服渠道活码',
employee_name: '李小燕',
position_name: '售后客服',
channel_code: 'ASC-QR-002',
qrcode_url: 'https://picsum.photos/seed/after_sales/200/200',
agent_count: 3,
agent_names_preview: ['售后问答Agent', '回访问答Agent', '管家Agent'],
status: 'enable' as ChannelQRCodeStatus,
created_at: '2026-05-11 10:15:00',
operations: ['view', 'edit', 'disable'],
},
{
qrcode_id: 3,
qrcode_name: '直播运营渠道活码',
employee_name: '王大力',
position_name: '直播运营',
channel_code: 'LVO-QR-003',
qrcode_url: 'https://picsum.photos/seed/live_ops/200/200',
agent_count: 2,
agent_names_preview: ['直播助手Agent', '管家Agent'],
status: 'disable' as ChannelQRCodeStatus,
created_at: '2026-05-12 11:30:00',
operations: ['view', 'edit', 'enable'],
},
{
qrcode_id: 5,
qrcode_name: '私域增长渠道活码',
employee_name: '陈欣欣',
position_name: '私域增长',
channel_code: 'CRM-QR-005',
qrcode_url: 'https://picsum.photos/seed/crm_growth/200/200',
agent_count: 3,
agent_names_preview: ['私域引流Agent', '客诉安抚Agent', '管家Agent'],
status: 'enable' as ChannelQRCodeStatus,
created_at: '2026-05-14 16:40:00',
operations: ['view', 'edit', 'disable'],
},
];
const mockPositionTags = [
{ tag_id: 1, name: '店销顾问', code: 'store_sales', agent_count: 2, created_at: '2026-05-15' },
];
function renderChannelCodes(path = '/channel-codes') {
window.localStorage.setItem('agent-admin-local-login', 'true');
return render(
<MemoryRouter initialEntries={[path]}>
<App />
</MemoryRouter>,
);
}
describe('Channel code enable/disable operations', () => {
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(enableChannelQRCode).mockResolvedValue({
success: true,
previous_status: 'disable',
});
vi.mocked(disableChannelQRCode).mockResolvedValue({
success: true,
disabled_at: '2026-05-15T15:00:00Z',
previous_status: 'enable',
});
});
// TDD: 操作列不展示启用和停用按钮
it('action column does NOT show enable/disable buttons', async () => {
renderChannelCodes();
await screen.findByText('直播运营渠道活码');
const rows = screen.getAllByRole('row');
// 检查所有行的操作列
for (const row of rows.slice(1)) { // skip header row
expect(within(row).queryByRole('button', { name: /启用/ })).not.toBeInTheDocument();
expect(within(row).queryByRole('button', { name: /停用/ })).not.toBeInTheDocument();
}
});
// TDD: 操作列仍展示查看和编辑
it('action column still shows view and edit buttons', async () => {
renderChannelCodes();
await screen.findByText('店销顾问渠道活码');
const enabledRow = screen.getByRole('row', { name: /店销顾问渠道活码/ });
expect(within(enabledRow).getByRole('button', { name: '查看店销顾问渠道活码' })).toBeInTheDocument();
expect(within(enabledRow).getByRole('button', { name: '编辑店销顾问渠道活码' })).toBeInTheDocument();
});
// TDD: 列表不触发启停确认弹窗(因为按钮不存在)
it('list does NOT trigger enable/disable confirmation dialogs', async () => {
renderChannelCodes();
await screen.findByText('店销顾问渠道活码');
// 确认弹窗不应存在
expect(screen.queryByRole('dialog', { name: /确认启用/ })).not.toBeInTheDocument();
expect(screen.queryByRole('dialog', { name: /确认停用/ })).not.toBeInTheDocument();
});
// TDD: 状态列和状态筛选仍存在
it('status column and status filter still exist', async () => {
renderChannelCodes();
await screen.findByText('店销顾问渠道活码');
// 状态列存在
expect(screen.getByRole('columnheader', { name: '状态' })).toBeInTheDocument();
// 状态筛选存在
expect(screen.getByLabelText('状态')).toBeInTheDocument();
});
// Legacy tests below (will be removed/skipped after implementation)
it.skip('shows enable confirmation dialog', async () => {
renderChannelCodes();
await screen.findByText('直播运营渠道活码');
// Find a disabled row (live_ops has status 'disable')
const disabledRow = screen.getByRole('row', { name: /直播运营渠道活码/ });
fireEvent.click(within(disabledRow).getByRole('button', { name: '启用直播运营渠道活码' }));
expect(screen.getByRole('dialog', { name: '确认启用"直播运营渠道活码"?' })).toBeInTheDocument();
expect(screen.getByText('启用后渠道活码将恢复可用,用户可以继续扫码添加。')).toBeInTheDocument();
});
it.skip('shows disable confirmation dialog', async () => {
renderChannelCodes();
await screen.findByText('店销顾问渠道活码');
// Find an enabled row (store_sales has status 'enable')
const enabledRow = screen.getByRole('row', { name: /店销顾问渠道活码/ });
fireEvent.click(within(enabledRow).getByRole('button', { name: '停用店销顾问渠道活码' }));
expect(screen.getByRole('dialog', { name: '确认停用"店销顾问渠道活码"?' })).toBeInTheDocument();
expect(screen.getByText('停用后渠道活码不可继续扫码使用,请确认是否继续。')).toBeInTheDocument();
});
it.skip('cancels enable operation without changing status', async () => {
renderChannelCodes();
await screen.findByText('直播运营渠道活码');
const disabledRow = screen.getByRole('row', { name: /直播运营渠道活码/ });
fireEvent.click(within(disabledRow).getByRole('button', { name: '启用直播运营渠道活码' }));
fireEvent.click(screen.getByRole('button', { name: '取消' }));
expect(screen.queryByRole('dialog', { name: '确认启用' })).not.toBeInTheDocument();
// Status should remain unchanged
expect(within(disabledRow).getByText('停用')).toBeInTheDocument();
});
it.skip('enables successfully and shows success message', async () => {
renderChannelCodes();
await screen.findByText('直播运营渠道活码');
const disabledRow = screen.getByRole('row', { name: /直播运营渠道活码/ });
fireEvent.click(within(disabledRow).getByRole('button', { name: '启用直播运营渠道活码' }));
vi.mocked(getChannelQRCodeList).mockResolvedValueOnce({
list: mockChannelCodes.map((c) => c.qrcode_id === 3 ? { ...c, status: 'enable', operations: ['view', 'edit', 'disable'] } : c),
total: mockChannelCodes.length,
page: 1,
page_size: 10,
});
fireEvent.click(screen.getByRole('button', { name: '确定' }));
await waitFor(() => {
expect(screen.queryByRole('dialog', { name: '确认启用' })).not.toBeInTheDocument();
});
await waitFor(() => {
expect(screen.getByRole('status', { name: '启用成功' })).toBeInTheDocument();
});
});
it.skip('disables successfully and shows success message', async () => {
renderChannelCodes();
await screen.findByText('私域增长渠道活码');
const enabledRow = screen.getByRole('row', { name: /私域增长渠道活码/ });
fireEvent.click(within(enabledRow).getByRole('button', { name: '停用私域增长渠道活码' }));
vi.mocked(getChannelQRCodeList).mockResolvedValueOnce({
list: mockChannelCodes.map((c) => c.qrcode_id === 5 ? { ...c, status: 'disable', operations: ['view', 'edit', 'enable'] } : c),
total: mockChannelCodes.length,
page: 1,
page_size: 10,
});
fireEvent.click(screen.getByRole('button', { name: '确定' }));
await waitFor(() => {
expect(screen.queryByRole('dialog', { name: '确认停用' })).not.toBeInTheDocument();
});
await waitFor(() => {
expect(screen.getByRole('status', { name: '停用成功' })).toBeInTheDocument();
});
});
it.skip('blocks disable with business error message', async () => {
vi.mocked(disableChannelQRCode).mockRejectedValueOnce(new ApiError('该渠道活码正在官网展示中,请先从官网配置中移除后再停用', 400));
renderChannelCodes();
await screen.findByText('店销顾问渠道活码');
const enabledRow = screen.getByRole('row', { name: /店销顾问渠道活码/ });
fireEvent.click(within(enabledRow).getByRole('button', { name: '停用店销顾问渠道活码' }));
fireEvent.click(screen.getByRole('button', { name: '确定' }));
// The error message should be shown
await waitFor(() => {
const alert = screen.getByRole('alert');
expect(alert).toHaveTextContent('该渠道活码正在官网展示中,请先从官网配置中移除后再停用');
});
// Status should remain enabled
expect(within(enabledRow).getByText('启用')).toBeInTheDocument();
});
});