channel-code-mock.test.ts
7.09 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
import { beforeEach, describe, expect, it } from 'vitest';
import {
channelCodeMockFixtures,
getChannelCodeListPage,
getChannelCodeRecord,
getEmployeeQRCodes,
getPositionDerivedInfo,
getPositionsWithChannelCode,
saveChannelCodeRecord,
updateChannelCodeStatus,
resetChannelCodeMockState,
type ChannelCodeMockItem,
} from './channel-code-mock';
describe('channel code mock contract', () => {
beforeEach(() => {
window.localStorage.clear();
resetChannelCodeMockState();
});
it('provides the minimal page-facing fields and required fixtures', () => {
const [code] = channelCodeMockFixtures.normal.items;
expect(code).toMatchObject({
code_id: expect.any(Number),
code_name: expect.any(String),
position_code: expect.any(String),
position_name: expect.any(String),
channel_code: expect.any(String),
employee_name: expect.any(String),
employee_role_text: expect.any(String),
agent_names: expect.any(Array),
status: expect.any(String),
qrcode_url: expect.any(String),
description: expect.any(String),
welcome_message: expect.any(String),
created_at: expect.any(String),
updated_at: expect.any(String),
operations: expect.any(Array),
});
expect(Object.keys(channelCodeMockFixtures).sort()).toEqual([
'empty',
'filteredEmpty',
'loadError',
'normal',
'paginationBoundary',
'qrcodeLoadError',
'saveError',
'statusBlockError',
]);
});
it('filters by code name, position and status', () => {
const result1 = getChannelCodeListPage({ keyword: '售后', page: 1, pageSize: 10 });
expect(result1.items).toHaveLength(1);
expect(result1.items[0].code_name).toContain('售后');
const result2 = getChannelCodeListPage({ positionCode: 'store_sales', page: 1, pageSize: 10 });
expect(result2.items).toHaveLength(1);
expect(result2.items[0].position_code).toBe('store_sales');
const result3 = getChannelCodeListPage({ status: 'enable', page: 1, pageSize: 10 });
expect(result3.items.every((item) => item.status === 'enable')).toBe(true);
const result4 = getChannelCodeListPage({ status: 'disable', page: 1, pageSize: 10 });
expect(result4.items.every((item) => item.status === 'disable')).toBe(true);
});
it('paginates from page-facing query fields', () => {
const paged = getChannelCodeListPage({ page: 2, pageSize: 2 });
expect(paged.total).toBe(5);
expect(paged.items).toHaveLength(2);
expect(paged.page).toBe(2);
expect(paged.pageSize).toBe(2);
});
it('provides position derived info including channel code, employee and agents', () => {
const info = getPositionDerivedInfo('store_sales');
expect(info).toBeDefined();
expect(info!).toMatchObject({
position_code: 'store_sales',
position_name: expect.any(String),
channel_code: expect.any(String),
employee_name: expect.any(String),
employee_role_text: expect.any(String),
agent_names: expect.any(Array),
});
expect(info!.agent_names.length).toBeGreaterThan(0);
});
it('returns undefined for position derived info when position does not exist', () => {
const info = getPositionDerivedInfo('nonexistent_position');
expect(info).toBeUndefined();
});
it('tracks positions that already have channel codes (one position one code)', () => {
const positionsWithCodes = getPositionsWithChannelCode();
expect(positionsWithCodes).toContain('store_sales');
expect(positionsWithCodes).toContain('after_sales_service');
expect(positionsWithCodes.length).toBeGreaterThan(0);
});
it('creates a new channel code with status disable by default', () => {
const created = saveChannelCodeRecord({
code_name: '测试渠道活码',
position_code: 'live_ops',
description: '测试描述',
welcome_message: '欢迎添加',
status: 'disable',
});
expect(created.code_id).toBeGreaterThan(0);
expect(created.status).toBe('disable');
expect(created.code_name).toBe('测试渠道活码');
expect(getChannelCodeRecord(String(created.code_id))).toBeDefined();
expect(getPositionsWithChannelCode()).toContain('live_ops');
});
it('updates existing channel code without changing position', () => {
const existing = channelCodeMockFixtures.normal.items[0];
const updated = saveChannelCodeRecord({
codeId: existing.code_id,
code_name: '更新后的活码名称',
description: '更新后的描述',
welcome_message: '更新后的欢迎语',
status: 'enable',
});
expect(updated.code_id).toBe(existing.code_id);
expect(updated.code_name).toBe('更新后的活码名称');
expect(updated.position_code).toBe(existing.position_code);
expect(updated.status).toBe('enable');
});
it('enables and disables channel code status', () => {
const existing = channelCodeMockFixtures.normal.items.find((item) => item.status === 'disable');
if (!existing) throw new Error('No disabled item found');
const enabled = updateChannelCodeStatus(existing.code_id, 'enable', 'normal');
expect(enabled.status).toBe('enable');
expect(enabled.updated_at).not.toBe(existing.updated_at);
});
it('blocks disable when status update returns business blocking error', () => {
const existing = channelCodeMockFixtures.normal.items.find((item) => item.status === 'enable');
if (!existing) throw new Error('No enabled item found');
const result = updateChannelCodeStatus(existing.code_id, 'disable', 'statusBlockError');
expect(result.blocked).toBe(true);
expect(result.message).toContain('官网');
expect(result.status).toBe('enable');
});
it('handles qrcode image load error fixture', () => {
const fixture = channelCodeMockFixtures.qrcodeLoadError;
expect(fixture.items[0].qrcode_load_error).toBe(true);
});
it('returns undefined for non-existent channel code record', () => {
const record = getChannelCodeRecord('99999');
expect(record).toBeUndefined();
});
describe('getEmployeeQRCodes API', () => {
it('returns employee QR codes info for valid employee_id', () => {
// 岗位 store_sales 的员工 ID 是 1001
const response = getEmployeeQRCodes(1001);
expect(response).toBeDefined();
expect(response!.employee_id).toBe(1001);
expect(response!.employee_name).toBe('张小萌');
expect(response!.employee_role_text).toBe('店销主管');
expect(response!.qrcode_count).toBeGreaterThan(0);
expect(response!.qrcodes.length).toBeGreaterThan(0);
expect(response!.qrcodes[0].position_name).toBe('店销顾问');
});
it('returns undefined for non-existent employee_id', () => {
const response = getEmployeeQRCodes(99999);
expect(response).toBeUndefined();
});
it('includes employee derived info from API response', () => {
const response = getEmployeeQRCodes(1002); // 售后客服员工
expect(response).toBeDefined();
expect(response!.employee_name).toBe('李小燕');
expect(response!.employee_role_text).toBe('客服专员');
expect(response!.qrcodes[0].channel_code).toBe('ASC-QR-002');
});
});
});