postMock.test.js
5.51 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
/**
* POST Mock API 单元测试
*
* @description 测试 AI 测试环境下的 POST 请求 Mock 功能
* @module utils/__tests__/postMock.test
*/
import { describe, it, expect, beforeEach, afterEach } from 'vitest'
import { mockPostAPI } from '../mockData'
describe('POST Mock API', () => {
describe('URL 参数解析', () => {
it('should parse action parameter correctly', async () => {
const result = await mockPostAPI('/srv/?a=feedback', {})
expect(result.code).toBeDefined()
})
it('should parse action and type parameters correctly', async () => {
const result = await mockPostAPI('/srv/?a=favorite&t=add', { meta_id: 123 })
expect(result.code).toBe(1)
expect(result.msg).toBe('收藏成功')
})
})
describe('favorite 模块', () => {
it('should mock favorite/add', async () => {
const result = await mockPostAPI('/srv/?a=favorite&t=add', { meta_id: 123 })
expect(result.code).toBe(1)
expect(result.msg).toBe('收藏成功')
})
it('should mock favorite/del', async () => {
const result = await mockPostAPI('/srv/?a=favorite&t=del', { meta_id: 123 })
expect(result.code).toBe(1)
expect(result.msg).toBe('取消成功')
})
})
describe('event 模块', () => {
it('should mock event/add', async () => {
const result = await mockPostAPI('/srv/?a=event&t=add', {
type: 'READ_FILE',
object_id: 123
})
expect(result.code).toBe(1)
expect(result.msg).toBe('success')
})
})
describe('sms 模块', () => {
it('should mock sms', async () => {
const result = await mockPostAPI('/srv/?a=sms', { phone: '13800138000' })
expect(result.code).toBe(1)
expect(result.msg).toBe('发送成功')
})
})
describe('upload 模块', () => {
it('should mock upload (qiniu token)', async () => {
const result = await mockPostAPI('/srv/?a=upload', {
filename: 'test.jpg',
file: 'data:image/jpeg;base64,...'
})
expect(result.code).toBe(1)
expect(result.data.token).toContain('mock_qiniu_token_')
})
it('should mock upload/save_file', async () => {
const result = await mockPostAPI('/srv/?a=upload&t=save_file', {
format: 'jpg',
hash: 'abc123',
filekey: 'test.jpg'
})
expect(result.code).toBe(1)
expect(result.data.src).toContain('cdn.ipadbiz.cn')
})
})
describe('openid 模块', () => {
it('should mock mini program auth', async () => {
const result = await mockPostAPI('/srv/?a=openid', { code: 'test_code' })
expect(result.code).toBe(1)
expect(result.msg).toBe('授权成功')
expect(result.data.user).toBeDefined()
expect(result.data.user.name).toBe('AI测试用户')
})
})
describe('proposal 模块', () => {
it('should mock proposal/add', async () => {
const result = await mockPostAPI('/srv/?a=proposal&t=add', {
customer_name: '测试客户',
product_id: 1
})
expect(result.code).toBe(1)
expect(result.msg).toBe('创建成功')
expect(result.data.order_id).toContain('mock_order_')
})
it('should mock proposal/del', async () => {
const result = await mockPostAPI('/srv/?a=proposal&t=del', { order_id: '123' })
expect(result.code).toBe(1)
expect(result.msg).toBe('删除成功')
})
it('should mock proposal/view', async () => {
const result = await mockPostAPI('/srv/?a=proposal&t=view', { order_id: '123' })
expect(result.code).toBe(1)
expect(result.data.status).toBe(7)
expect(result.data.pdf_url).toContain('proposal.pdf')
})
})
describe('user 模块', () => {
it('should mock user/login', async () => {
const result = await mockPostAPI('/srv/?a=user&t=login', {
uuid: 'test_uuid',
password: 'test_pass'
})
expect(result.code).toBe(1)
expect(result.msg).toBe('登录成功')
expect(result.data.userid).toBe('mock_test_user')
})
it('should mock user/logout', async () => {
const result = await mockPostAPI('/srv/?a=user&t=logout', {})
expect(result.code).toBe(1)
expect(result.msg).toBe('退出成功')
})
it('should mock user/update_profile', async () => {
const avatar = { src: 'avatar.jpg' }
const result = await mockPostAPI('/srv/?a=user&t=update_profile', { avatar })
expect(result.code).toBe(1)
expect(result.msg).toBe('更新成功')
expect(result.data.avatar).toEqual(avatar)
})
})
describe('feedback 模块', () => {
it('should mock feedback/add', async () => {
const result = await mockPostAPI('/srv/?a=feedback&t=add', {
category: '1',
note: '测试反馈内容',
images: []
})
expect(result.code).toBe(1)
expect(result.msg).toBe('提交成功')
expect(result.data.id).toBeDefined()
})
})
describe('wx_pay 模块', () => {
it('should mock wechat pay', async () => {
const result = await mockPostAPI('/srv/?a=icbc_pay_wxamp', { pay_id: 'test_pay_id' })
expect(result.code).toBe(1)
expect(result.data.timeStamp).toBeDefined()
expect(result.data.nonceStr).toContain('mock_nonce_')
expect(result.data.package).toContain('prepay_id')
})
})
describe('未定义的 API', () => {
it('should return default response for unknown API', async () => {
const result = await mockPostAPI('/srv/?a=unknown_api', {})
expect(result.code).toBe(1)
expect(result.msg).toBe('success (mock)')
expect(result.data).toBeNull()
})
})
})