officialSiteTab.test.ts
9.93 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
import { describe, it, expect, vi, beforeEach } from 'vitest';
import {
getOfficialSiteTabList,
createOfficialSiteTab,
updateOfficialSiteTab,
deleteOfficialSiteTab,
updateOfficialSiteTabStatus,
getAvailableQRCodes,
getOfficialSiteTabDetail,
} from './officialSiteTab';
import { apiClient, ApiError } from './client';
vi.mock('./client', () => ({
apiClient: {
get: vi.fn(),
post: vi.fn(),
put: vi.fn(),
delete: vi.fn(),
},
ApiError: class ApiError extends Error {
constructor(message: string, public code: number) {
super(message);
this.name = 'ApiError';
}
},
}));
describe('officialSiteTab API', () => {
beforeEach(() => {
vi.clearAllMocks();
});
describe('getOfficialSiteTabList', () => {
it('调用正确的接口路径', async () => {
vi.mocked(apiClient.get).mockResolvedValueOnce({ list: [], total: 0, page: 1, page_size: 10 });
await getOfficialSiteTabList({ page: 1, page_size: 10 });
expect(apiClient.get).toHaveBeenCalledWith('/official-site/tabs', { params: { page: 1, page_size: 10 } });
});
it('传递筛选参数', async () => {
vi.mocked(apiClient.get).mockResolvedValueOnce({ list: [], total: 0 });
await getOfficialSiteTabList({ page: 1, page_size: 10, keyword: '销售', display_status: 'enable' });
expect(apiClient.get).toHaveBeenCalledWith('/official-site/tabs', { params: { page: 1, page_size: 10, keyword: '销售', display_status: 'enable' } });
});
it('为 enable 状态的 Tab 推断 operations 包含 disable', async () => {
vi.mocked(apiClient.get).mockResolvedValueOnce({
list: [{ tab_id: 1, display_status: 'enable' as const, position_code: '', position_name: '', main_title: '', sub_title: '', qrcode_id: 0, qrcode_name: '', qrcode_url: '', qrcode_status: 'enable', channel_code: '', qrcode_employee_name: '', sort_order: 0, agent_count: 0, created_at: '' }],
total: 1,
});
const result = await getOfficialSiteTabList({ page: 1, page_size: 10 });
expect(result.list[0].operations).toContain('disable');
});
it('为 disable 状态的 Tab 推断 operations 包含 enable', async () => {
vi.mocked(apiClient.get).mockResolvedValueOnce({
list: [{ tab_id: 1, display_status: 'disable' as const, position_code: '', position_name: '', main_title: '', sub_title: '', qrcode_id: 0, qrcode_name: '', qrcode_url: '', qrcode_status: 'enable', channel_code: '', qrcode_employee_name: '', sort_order: 0, agent_count: 0, created_at: '' }],
total: 1,
});
const result = await getOfficialSiteTabList({ page: 1, page_size: 10 });
expect(result.list[0].operations).toContain('enable');
});
it('当后端返回空数组 operations 时也根据状态推断', async () => {
vi.mocked(apiClient.get).mockResolvedValueOnce({
list: [{ tab_id: 1, display_status: 'enable' as const, operations: [], position_code: '', position_name: '', main_title: '', sub_title: '', qrcode_id: 0, qrcode_name: '', qrcode_url: '', qrcode_status: 'enable', channel_code: '', qrcode_employee_name: '', sort_order: 0, agent_count: 0, created_at: '' }],
total: 1,
});
const result = await getOfficialSiteTabList({ page: 1, page_size: 10 });
expect(result.list[0]?.operations).toContain('disable');
expect(result.list[0]?.operations?.length ?? 0).toBeGreaterThan(0);
});
});
describe('createOfficialSiteTab', () => {
it('调用正确的接口路径和方法', async () => {
vi.mocked(apiClient.post).mockResolvedValueOnce({ tab_id: 1 });
await createOfficialSiteTab({
position_code: 'store_sales',
position_name: '驻店销售岗',
qrcode_id: '5',
main_title: '标题',
sub_title: '说明',
display_status: 'enable',
sort_order: 1,
});
expect(apiClient.post).toHaveBeenCalledWith('/official-site/tabs', {
position_code: 'store_sales',
position_name: '驻店销售岗',
qrcode_id: '5',
main_title: '标题',
sub_title: '说明',
display_status: 'enable',
sort_order: 1,
});
});
it('岗位重复时抛出错误', async () => {
vi.mocked(apiClient.post).mockRejectedValueOnce(new ApiError('岗位已存在官网 Tab 配置', 400));
await expect(createOfficialSiteTab({
position_code: 'existing',
position_name: '岗位',
qrcode_id: '1',
main_title: '标题',
sub_title: '说明',
display_status: 'enable',
sort_order: 1,
})).rejects.toThrow('岗位已存在官网 Tab 配置');
});
});
describe('updateOfficialSiteTab', () => {
it('调用正确的接口路径和方法', async () => {
vi.mocked(apiClient.put).mockResolvedValueOnce({ success: true, updated_at: '2026-05-16T14:00:00Z' });
await updateOfficialSiteTab(1, {
position_code: 'store_sales',
position_name: '更新名称',
qrcode_id: '5',
main_title: '新标题',
sub_title: '新说明',
display_status: 'enable',
sort_order: 2,
});
expect(apiClient.put).toHaveBeenCalledWith('/official-site/tabs/1', {
position_code: 'store_sales',
position_name: '更新名称',
qrcode_id: '5',
main_title: '新标题',
sub_title: '新说明',
display_status: 'enable',
sort_order: 2,
});
});
});
describe('deleteOfficialSiteTab', () => {
it('调用正确的接口路径和方法', async () => {
vi.mocked(apiClient.delete).mockResolvedValueOnce({ success: true, deleted_at: '2026-05-16T15:00:00Z', cleaned_agent_configs: 3 });
await deleteOfficialSiteTab(1);
expect(apiClient.delete).toHaveBeenCalledWith('/official-site/tabs/1', undefined);
});
});
describe('updateOfficialSiteTabStatus', () => {
it('上架操作', async () => {
vi.mocked(apiClient.put).mockResolvedValueOnce({ success: true, previous_status: 'disable', updated_at: '2026-05-16T16:00:00Z' });
await updateOfficialSiteTabStatus(1, { display_status: 'enable' });
expect(apiClient.put).toHaveBeenCalledWith('/official-site/tabs/1/status', { display_status: 'enable' });
});
it('下架操作', async () => {
vi.mocked(apiClient.put).mockResolvedValueOnce({ success: true, previous_status: 'enable' });
await updateOfficialSiteTabStatus(1, { display_status: 'disable', reason: '活动结束' });
expect(apiClient.put).toHaveBeenCalledWith('/official-site/tabs/1/status', { display_status: 'disable', reason: '活动结束' });
});
});
describe('getAvailableQRCodes', () => {
it('调用正确的接口路径', async () => {
vi.mocked(apiClient.get).mockResolvedValueOnce({ list: [] });
await getAvailableQRCodes();
expect(apiClient.get).toHaveBeenCalledWith('/official-site/tabs/qrcodes/available', undefined);
});
it('传递岗位筛选参数', async () => {
vi.mocked(apiClient.get).mockResolvedValueOnce({ list: [] });
await getAvailableQRCodes({ position_code: 'store_sales' });
expect(apiClient.get).toHaveBeenCalledWith('/official-site/tabs/qrcodes/available', { params: { position_code: 'store_sales' } });
});
});
describe('getOfficialSiteTabDetail', () => {
it('调用正确的接口路径', async () => {
vi.mocked(apiClient.get).mockResolvedValueOnce({
tab_id: 1,
position_name: '店销顾问',
qrcode_name: '店销顾问渠道活码',
channel_code: 'DSC-QR-001',
employee_name: '张小萌',
employee_role_text: '店销主管',
});
await getOfficialSiteTabDetail(1);
expect(apiClient.get).toHaveBeenCalledWith('/official-site/tabs/1');
});
it('返回岗位 Tab 详情数据', async () => {
const mockDetail = {
tab_id: 1,
position_code: 'store_sales',
position_name: '店销顾问',
qrcode_id: 1,
qrcode_name: '店销顾问渠道活码',
channel_code: 'DSC-QR-001',
employee_name: '张小萌',
employee_role_text: '店销主管',
main_title: 'AI 智能助手',
sub_title: '提升店销效率',
display_status: 'enable',
};
vi.mocked(apiClient.get).mockResolvedValueOnce(mockDetail);
const result = await getOfficialSiteTabDetail(1);
expect(result.position_name).toBe('店销顾问');
expect(result.qrcode_name).toBe('店销顾问渠道活码');
expect(result.channel_code).toBe('DSC-QR-001');
expect(result.employee_name).toBe('张小萌');
});
it('兼容后端返回的同义字段名(qrcode_name 别名)', async () => {
// 后端可能返回 channel_qrcode_name 而非 qrcode_name
vi.mocked(apiClient.get).mockResolvedValueOnce({
tab_id: 1,
position_name: '店销顾问',
channel_qrcode_name: '店销顾问渠道活码',
channel_code: 'DSC-QR-001',
employee_name: '张小萌',
employee_role_text: '店销主管',
});
const result = await getOfficialSiteTabDetail(1);
expect(result.qrcode_name).toBe('店销顾问渠道活码');
});
it('兼容后端返回的同义字段名(employee_name 别名)', async () => {
// 后端可能返回承接员工字段别名
vi.mocked(apiClient.get).mockResolvedValueOnce({
tab_id: 1,
position_name: '店销顾问',
qrcode_name: '店销顾问渠道活码',
channel_code: 'DSC-QR-001',
steward_name: '张小萌',
steward_role: '店销主管',
});
const result = await getOfficialSiteTabDetail(1);
expect(result.employee_name).toBe('张小萌');
expect(result.employee_role_text).toBe('店销主管');
});
it('请求失败时抛出错误', async () => {
vi.mocked(apiClient.get).mockRejectedValueOnce(new ApiError('岗位展示配置不存在', 404));
await expect(getOfficialSiteTabDetail(999)).rejects.toThrow('岗位展示配置不存在');
});
});
});