officialSiteTab.test.ts
6.94 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
import { describe, it, expect, vi, beforeEach } from 'vitest';
import {
getOfficialSiteTabList,
createOfficialSiteTab,
updateOfficialSiteTab,
deleteOfficialSiteTab,
updateOfficialSiteTabStatus,
getAvailableQRCodes,
} 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', 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', 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', 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_name: '更新名称',
qrcode_id: '5',
main_title: '新标题',
sub_title: '新说明',
display_status: 'enable',
sort_order: 2,
});
expect(apiClient.put).toHaveBeenCalledWith('/official-site/tabs/1', {
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' } });
});
});
});