@lx-frontend/multi-ajax 0.2.0 → 0.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/__mocks__/axios.ts +2 -2
- package/coverage/coverage-summary.json +12 -0
- package/coverage/lcov-report/base.css +224 -0
- package/coverage/lcov-report/block-navigation.js +87 -0
- package/coverage/lcov-report/favicon.png +0 -0
- package/coverage/lcov-report/index.html +131 -0
- package/coverage/lcov-report/prettify.css +1 -0
- package/coverage/lcov-report/prettify.js +2 -0
- package/coverage/lcov-report/sort-arrow-sprite.png +0 -0
- package/coverage/lcov-report/sorter.js +196 -0
- package/coverage/lcov-report/src/adapters/index.html +131 -0
- package/coverage/lcov-report/src/adapters/index.ts.html +100 -0
- package/coverage/lcov-report/src/adapters/wx-adapter.ts.html +436 -0
- package/coverage/lcov-report/src/cancelLastSameRequest.ts.html +211 -0
- package/coverage/lcov-report/src/canceller.ts.html +265 -0
- package/coverage/lcov-report/src/compatibleMethods.ts.html +301 -0
- package/coverage/lcov-report/src/duplicateRequestPrevent.ts.html +151 -0
- package/coverage/lcov-report/src/helpers.ts.html +319 -0
- package/coverage/lcov-report/src/index.html +221 -0
- package/coverage/lcov-report/src/index.ts.html +211 -0
- package/coverage/lcov-report/src/methods.ts.html +475 -0
- package/coverage/lcov-report/src/utils.ts.html +334 -0
- package/coverage/lcov.info +610 -0
- package/lib/adapters/wx-adapter.js +10 -9
- package/lib/adapters/wx-adapter.js.map +1 -1
- package/lib/cancelLastSameRequest.d.ts +0 -1
- package/lib/cancelLastSameRequest.js +6 -9
- package/lib/cancelLastSameRequest.js.map +1 -1
- package/lib/canceller.d.ts +11 -2
- package/lib/canceller.js +57 -2
- package/lib/canceller.js.map +1 -1
- package/lib/compatibleMethods.d.ts +3 -3
- package/lib/compatibleMethods.js +1 -1
- package/lib/compatibleMethods.js.map +1 -1
- package/lib/duplicateRequestPrevent.d.ts +1 -1
- package/lib/duplicateRequestPrevent.js +1 -3
- package/lib/duplicateRequestPrevent.js.map +1 -1
- package/lib/helpers.d.ts +3 -2
- package/lib/helpers.js +28 -7
- package/lib/helpers.js.map +1 -1
- package/lib/index.d.ts +1 -1
- package/lib/index.js +6 -6
- package/lib/index.js.map +1 -1
- package/lib/methods.d.ts +3 -3
- package/lib/methods.js +10 -10
- package/lib/methods.js.map +1 -1
- package/lib/types.d.ts +7 -3
- package/lib/utils.js +0 -1
- package/lib/utils.js.map +1 -1
- package/package.json +10 -10
- package/src/adapters/wx-adapter.ts +19 -13
- package/src/cancelLastSameRequest.ts +5 -5
- package/src/canceller.ts +58 -2
- package/src/compatibleMethods.ts +4 -4
- package/src/duplicateRequestPrevent.ts +3 -4
- package/src/helpers.ts +40 -6
- package/src/index.ts +11 -10
- package/src/methods.ts +26 -19
- package/src/types.ts +8 -4
- package/src/utils.ts +1 -2
- package/tests/duplicateRequestPrevent.test.ts +2 -2
- package/tests/helpers.test.ts +2 -2
- package/tests/index.test.ts +449 -229
- package/tsconfig.json +2 -1
- package/types/global.d.ts +4 -0
package/tests/index.test.ts
CHANGED
|
@@ -4,7 +4,7 @@ import type { RequestMethods } from '../lib/types.d'
|
|
|
4
4
|
import type { OldAjaxMethods } from '../lib/compatibleMethods.d'
|
|
5
5
|
|
|
6
6
|
// 生成随机URL,避免测试用例之间的干扰
|
|
7
|
-
function getRandomUrl(len
|
|
7
|
+
function getRandomUrl(len = 4) {
|
|
8
8
|
const aCode = 'a'.charCodeAt(0)
|
|
9
9
|
const getIdx = () => Math.floor(Math.random() * 26) + aCode
|
|
10
10
|
return new Array(len).fill(null)
|
|
@@ -12,7 +12,7 @@ function getRandomUrl(len: number = 4) {
|
|
|
12
12
|
.join('/')
|
|
13
13
|
}
|
|
14
14
|
|
|
15
|
-
function getRandomKey(len
|
|
15
|
+
function getRandomKey(len = 4) {
|
|
16
16
|
const aCode = 'a'.charCodeAt(0)
|
|
17
17
|
const getIdx = () => Math.floor(Math.random() * 26) + aCode
|
|
18
18
|
return new Array(len).fill(null)
|
|
@@ -22,16 +22,28 @@ function getRandomKey(len: number = 4) {
|
|
|
22
22
|
|
|
23
23
|
jest.mock('axios')
|
|
24
24
|
|
|
25
|
-
|
|
26
|
-
|
|
25
|
+
const originAbortController = global.AbortController
|
|
26
|
+
function undoAbortController() {
|
|
27
|
+
// 微信环境没有AbortController
|
|
28
|
+
Object.defineProperty(global, 'AbortController', {
|
|
29
|
+
value: undefined
|
|
30
|
+
})
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
function restoreAbortController() {
|
|
34
|
+
Object.defineProperty(global, 'AbortController', {
|
|
35
|
+
value: originAbortController
|
|
36
|
+
})
|
|
27
37
|
}
|
|
28
38
|
|
|
29
39
|
const methods: RequestMethods[] = ['get', 'post', 'head', 'patch', 'put', 'delete']
|
|
30
40
|
const hisMethods: OldAjaxMethods[] = ['create', 'deletes', 'query', 'patchWay', 'putWay']
|
|
31
41
|
|
|
42
|
+
const methodsStr = methods.join('|')
|
|
43
|
+
const hisMethodsStr = hisMethods.join('|')
|
|
44
|
+
|
|
32
45
|
describe('initAjax test', () => {
|
|
33
|
-
|
|
34
|
-
const hisMethods: OldAjaxMethods[] = ['create', 'deletes', 'query', 'patchWay', 'putWay']
|
|
46
|
+
|
|
35
47
|
test('初始化测试', () => {
|
|
36
48
|
const ajax = initAjax({})
|
|
37
49
|
expect(ajax.$options).toEqual({});
|
|
@@ -62,63 +74,104 @@ describe('initAjax test', () => {
|
|
|
62
74
|
expect(getType(ajax[method](getRandomUrl(), {})())).toBe('promise')
|
|
63
75
|
})
|
|
64
76
|
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
77
|
+
test('ajax.canceller的任意属性均为函数', () => {
|
|
78
|
+
|
|
79
|
+
const ajax = initAjax({})
|
|
80
|
+
|
|
81
|
+
const randomKey = getRandomKey()
|
|
82
|
+
|
|
83
|
+
expect(typeof ajax.canceller[randomKey]).toBe('function')
|
|
84
|
+
const fn = ajax.canceller[randomKey]
|
|
85
|
+
// 没有生成请求,获取任意属性,会给出提示
|
|
86
|
+
expect(fn()).toMatch(`ajax.canceller对象上不存在名为${randomKey}的函数!`)
|
|
87
|
+
})
|
|
88
|
+
})
|
|
89
|
+
|
|
90
|
+
describe('手动取消请求测试', () => {
|
|
91
|
+
|
|
92
|
+
describe('web或node环境', () => {
|
|
93
|
+
|
|
94
|
+
test.each(methods)(methodsStr, async (method) => {
|
|
95
|
+
|
|
68
96
|
const ajax = initAjax({})
|
|
97
|
+
|
|
69
98
|
// 发出请求
|
|
70
99
|
const cancelKey = getRandomKey()
|
|
71
|
-
const
|
|
72
|
-
|
|
73
|
-
|
|
100
|
+
const fetch = ajax[method](getRandomUrl())({}, {cancelKey})
|
|
101
|
+
// 取消请求
|
|
102
|
+
ajax.canceller[cancelKey]()
|
|
103
|
+
|
|
104
|
+
// 模拟的axios直接返回了请求被取消,实际使用可能不是这样
|
|
105
|
+
await expect(fetch).rejects.toMatch('请求被取消')
|
|
74
106
|
})
|
|
75
107
|
|
|
76
|
-
|
|
108
|
+
})
|
|
109
|
+
|
|
110
|
+
describe('wx环境', () => {
|
|
111
|
+
beforeAll(() => {
|
|
77
112
|
global.wx = {
|
|
78
113
|
request: jest.fn((config: any) => {
|
|
79
114
|
setTimeout(() => {
|
|
80
115
|
config.success({
|
|
81
|
-
data: {
|
|
116
|
+
data: {}
|
|
82
117
|
}, 50)
|
|
83
118
|
})
|
|
119
|
+
|
|
120
|
+
return { abort: jest.fn() }
|
|
84
121
|
})
|
|
85
122
|
}
|
|
123
|
+
undoAbortController()
|
|
124
|
+
})
|
|
86
125
|
|
|
126
|
+
afterAll(() => {
|
|
127
|
+
delete global.wx
|
|
128
|
+
restoreAbortController()
|
|
129
|
+
})
|
|
130
|
+
|
|
131
|
+
test.each(methods)(methodsStr, async (method) => {
|
|
132
|
+
|
|
87
133
|
const ajax = initAjax({})
|
|
88
|
-
|
|
89
|
-
expect(ajax.$options.adapter).not.toBeUndefined()
|
|
134
|
+
|
|
90
135
|
// 发出请求
|
|
91
136
|
const cancelKey = getRandomKey()
|
|
92
|
-
const
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
137
|
+
const fetch = ajax[method](getRandomUrl())({}, {cancelKey})
|
|
138
|
+
// 取消请求
|
|
139
|
+
ajax.canceller[cancelKey]()
|
|
140
|
+
|
|
141
|
+
await expect(fetch).rejects.toHaveProperty('message', '请求被取消!')
|
|
142
|
+
await expect(fetch).rejects.toHaveProperty('code', 'ERR_CANCELED')
|
|
97
143
|
})
|
|
98
144
|
})
|
|
99
145
|
|
|
100
|
-
|
|
146
|
+
})
|
|
147
|
+
|
|
148
|
+
describe('多个具有相同cancelKey,但不同参数的请求发出并取消测试', () => {
|
|
101
149
|
|
|
102
|
-
|
|
103
|
-
|
|
150
|
+
describe('web或node环境', () => {
|
|
151
|
+
|
|
152
|
+
test.each(methods)(methodsStr, async (method) => {
|
|
153
|
+
|
|
104
154
|
const ajax = initAjax({})
|
|
105
155
|
|
|
106
156
|
// 发出请求
|
|
107
157
|
const cancelKey = getRandomKey()
|
|
108
|
-
|
|
158
|
+
// 请求参数不同
|
|
159
|
+
const fetchA = ajax[method](getRandomUrl())({a:1}, {cancelKey})
|
|
160
|
+
const fetchB = ajax[method](getRandomUrl())({a:2}, {cancelKey})
|
|
161
|
+
const fetchC = ajax[method](getRandomUrl())({a:3}, {cancelKey})
|
|
109
162
|
// 取消请求
|
|
110
163
|
ajax.canceller[cancelKey]()
|
|
111
164
|
|
|
112
|
-
//
|
|
113
|
-
await expect(
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
expect(ajax.canceller[cancelKey]).toBeUndefined()
|
|
117
|
-
|
|
165
|
+
// 请求都应该被取消
|
|
166
|
+
await expect(fetchA).rejects.toMatch('请求被取消')
|
|
167
|
+
await expect(fetchB).rejects.toMatch('请求被取消')
|
|
168
|
+
await expect(fetchC).rejects.toMatch('请求被取消')
|
|
118
169
|
})
|
|
119
170
|
|
|
120
|
-
|
|
121
|
-
|
|
171
|
+
})
|
|
172
|
+
|
|
173
|
+
describe('wx环境', () => {
|
|
174
|
+
beforeAll(() => {
|
|
122
175
|
global.wx = {
|
|
123
176
|
request: jest.fn((config: any) => {
|
|
124
177
|
setTimeout(() => {
|
|
@@ -130,41 +183,109 @@ describe('initAjax test', () => {
|
|
|
130
183
|
return { abort: jest.fn() }
|
|
131
184
|
})
|
|
132
185
|
}
|
|
186
|
+
|
|
187
|
+
undoAbortController()
|
|
188
|
+
})
|
|
189
|
+
|
|
190
|
+
afterAll(() => {
|
|
191
|
+
delete global.wx
|
|
192
|
+
restoreAbortController()
|
|
193
|
+
})
|
|
194
|
+
|
|
195
|
+
test.each(methods)(methodsStr, async (method) => {
|
|
133
196
|
|
|
134
|
-
const
|
|
197
|
+
const ajax = initAjax({})
|
|
135
198
|
|
|
136
|
-
//
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
199
|
+
// 发出请求
|
|
200
|
+
const cancelKey = getRandomKey()
|
|
201
|
+
const fetchA = ajax[method](getRandomUrl())({a:1}, {cancelKey})
|
|
202
|
+
const fetchB = ajax[method](getRandomUrl())({a:2}, {cancelKey})
|
|
203
|
+
const fetchC = ajax[method](getRandomUrl())({a:3}, {cancelKey})
|
|
204
|
+
// 取消请求
|
|
205
|
+
ajax.canceller[cancelKey]()
|
|
206
|
+
|
|
207
|
+
await expect(fetchA).rejects.toHaveProperty('message', '请求被取消!')
|
|
208
|
+
await expect(fetchB).rejects.toHaveProperty('message', '请求被取消!')
|
|
209
|
+
await expect(fetchC).rejects.toHaveProperty('message', '请求被取消!')
|
|
210
|
+
})
|
|
211
|
+
})
|
|
212
|
+
})
|
|
213
|
+
|
|
214
|
+
describe('多个具有相同cancelKey,不同参数的请求,交替发出并取消测试', () => {
|
|
215
|
+
|
|
216
|
+
describe('web或node环境', () => {
|
|
217
|
+
|
|
218
|
+
test.each(methods)(methodsStr, async (method) => {
|
|
142
219
|
|
|
143
220
|
const ajax = initAjax({})
|
|
144
221
|
|
|
145
222
|
// 发出请求
|
|
146
223
|
const cancelKey = getRandomKey()
|
|
147
|
-
|
|
148
|
-
|
|
224
|
+
// 请求参数不同,且交替发出取消请求,最后一个没有被取消的请求应该正常返回
|
|
225
|
+
const fetchA = ajax[method](getRandomUrl())({a:1}, {cancelKey})
|
|
226
|
+
ajax.canceller[cancelKey]()
|
|
227
|
+
const fetchB = ajax[method](getRandomUrl())({a:2}, {cancelKey})
|
|
149
228
|
ajax.canceller[cancelKey]()
|
|
229
|
+
const fetchC = ajax[method](getRandomUrl())({a:3}, {cancelKey})
|
|
230
|
+
// 取消请求
|
|
150
231
|
|
|
151
|
-
|
|
152
|
-
await expect(
|
|
153
|
-
|
|
154
|
-
expect(
|
|
232
|
+
// 请求都应该被取消
|
|
233
|
+
await expect(fetchA).rejects.toMatch('请求被取消')
|
|
234
|
+
await expect(fetchB).rejects.toMatch('请求被取消')
|
|
235
|
+
await expect(fetchC).resolves.not.toBeNaN()
|
|
236
|
+
})
|
|
237
|
+
|
|
238
|
+
})
|
|
239
|
+
|
|
240
|
+
describe('wx环境', () => {
|
|
241
|
+
beforeAll(() => {
|
|
242
|
+
global.wx = {
|
|
243
|
+
request: jest.fn((config: any) => {
|
|
244
|
+
setTimeout(() => {
|
|
245
|
+
config.success({
|
|
246
|
+
data: {}
|
|
247
|
+
}, 50)
|
|
248
|
+
})
|
|
155
249
|
|
|
250
|
+
return { abort: jest.fn() }
|
|
251
|
+
})
|
|
252
|
+
}
|
|
253
|
+
undoAbortController()
|
|
254
|
+
})
|
|
255
|
+
|
|
256
|
+
afterAll(() => {
|
|
156
257
|
delete global.wx
|
|
157
|
-
|
|
158
|
-
get() {
|
|
159
|
-
return originAbortController
|
|
160
|
-
}
|
|
161
|
-
})
|
|
258
|
+
restoreAbortController()
|
|
162
259
|
})
|
|
163
|
-
})
|
|
164
260
|
|
|
165
|
-
|
|
166
|
-
|
|
261
|
+
test.each(methods)(methodsStr, async (method) => {
|
|
262
|
+
|
|
167
263
|
const ajax = initAjax({})
|
|
264
|
+
|
|
265
|
+
// 发出请求
|
|
266
|
+
const cancelKey = getRandomKey()
|
|
267
|
+
const fetchA = ajax[method](getRandomUrl())({a:1}, {cancelKey})
|
|
268
|
+
ajax.canceller[cancelKey]()
|
|
269
|
+
const fetchB = ajax[method](getRandomUrl())({a:2}, {cancelKey})
|
|
270
|
+
ajax.canceller[cancelKey]()
|
|
271
|
+
const fetchC = ajax[method](getRandomUrl())({a:3}, {cancelKey})
|
|
272
|
+
// 取消请求
|
|
273
|
+
|
|
274
|
+
await expect(fetchA).rejects.toHaveProperty('message', '请求被取消!')
|
|
275
|
+
await expect(fetchB).rejects.toHaveProperty('message', '请求被取消!')
|
|
276
|
+
await expect(fetchC).resolves.toHaveProperty('data', {})
|
|
277
|
+
})
|
|
278
|
+
|
|
279
|
+
})
|
|
280
|
+
|
|
281
|
+
})
|
|
282
|
+
|
|
283
|
+
describe('300ms内的并行触发的重复请求不会触发二次网络请求,后一个请求复用前一个结果', () => {
|
|
284
|
+
|
|
285
|
+
describe('web或node环境', () => {
|
|
286
|
+
|
|
287
|
+
test.each(methods)(methodsStr, async (method) => {
|
|
288
|
+
const ajax = initAjax({reUseDuration: 300})
|
|
168
289
|
const url = getRandomUrl()
|
|
169
290
|
// 并行发出请求
|
|
170
291
|
const req1 = ajax[method](url)()
|
|
@@ -175,9 +296,9 @@ describe('initAjax test', () => {
|
|
|
175
296
|
// mock的axios.request每次会返回一个随机数,如果复用上一个请求,两者应该相等
|
|
176
297
|
expect(res1).toBe(res2)
|
|
177
298
|
})
|
|
178
|
-
|
|
179
|
-
test.each(hisMethods)(
|
|
180
|
-
const ajax = initAjax({})
|
|
299
|
+
|
|
300
|
+
test.each(hisMethods)(hisMethodsStr, async (method) => {
|
|
301
|
+
const ajax = initAjax({reUseDuration: 300})
|
|
181
302
|
const url = getRandomUrl()
|
|
182
303
|
// 并行发出请求,旧的方法配置只能在ajax[method]的第二个参数写
|
|
183
304
|
const req1 = ajax[method](url)()
|
|
@@ -189,7 +310,11 @@ describe('initAjax test', () => {
|
|
|
189
310
|
expect(res1).toBe(res2)
|
|
190
311
|
})
|
|
191
312
|
|
|
192
|
-
|
|
313
|
+
})
|
|
314
|
+
|
|
315
|
+
describe('wx环境', () => {
|
|
316
|
+
|
|
317
|
+
beforeAll(() => {
|
|
193
318
|
global.wx = {
|
|
194
319
|
request: jest.fn((config: any) => {
|
|
195
320
|
setTimeout(() => {
|
|
@@ -200,52 +325,165 @@ describe('initAjax test', () => {
|
|
|
200
325
|
})
|
|
201
326
|
}
|
|
202
327
|
|
|
203
|
-
|
|
328
|
+
undoAbortController()
|
|
329
|
+
})
|
|
330
|
+
|
|
331
|
+
afterAll(() => {
|
|
332
|
+
delete global.wx
|
|
333
|
+
restoreAbortController()
|
|
334
|
+
})
|
|
335
|
+
|
|
336
|
+
test.each(methods)(methodsStr,async (method: RequestMethods) => {
|
|
337
|
+
|
|
338
|
+
const ajax = initAjax({}) // reUseDuration默认值为300
|
|
204
339
|
const url = getRandomUrl()
|
|
205
340
|
// 并行发出请求
|
|
206
341
|
const req1 = ajax[method](url)()
|
|
207
342
|
const req2 = ajax[method](url)()
|
|
208
343
|
const res1 = await req1
|
|
209
344
|
const res2 = await req2
|
|
210
|
-
|
|
345
|
+
|
|
211
346
|
expect(res1).toBe(res2)
|
|
347
|
+
})
|
|
348
|
+
|
|
349
|
+
test.each(hisMethods)(hisMethodsStr, async (method) => {
|
|
350
|
+
const ajax = initAjax({})
|
|
351
|
+
// 验证微信环境,web和node下为undefined
|
|
352
|
+
expect(ajax.$options.adapter).not.toBeUndefined()
|
|
353
|
+
|
|
354
|
+
const url = getRandomUrl()
|
|
355
|
+
// 并行发出请求,旧的方法配置只能在ajax[method]的第二个参数写
|
|
356
|
+
const req1 = ajax[method](url)()
|
|
357
|
+
const req2 = ajax[method](url)()
|
|
358
|
+
const res1 = await req1
|
|
359
|
+
const res2 = await req2
|
|
360
|
+
|
|
361
|
+
// mock的axios.request每次会返回一个随机数,如果复用上一个请求,两者应该相等
|
|
362
|
+
expect(res1).toBe(res2)
|
|
363
|
+
})
|
|
364
|
+
})
|
|
212
365
|
|
|
213
|
-
|
|
366
|
+
})
|
|
367
|
+
|
|
368
|
+
describe('reUseDuration配置时间小于请求返回的时间,即使前一个请求没有返回,但在reUseDuration之后发出的请求不会复用前一个结果', () => {
|
|
369
|
+
|
|
370
|
+
describe('web或node环境', () => {
|
|
371
|
+
|
|
372
|
+
test.each(methods)(methodsStr, (method, done) => {
|
|
373
|
+
const ajax = initAjax({reUseDuration: 30}) // mock的axios返回时间为50ms
|
|
374
|
+
const url = getRandomUrl()
|
|
375
|
+
|
|
376
|
+
const req1 = ajax[method](url)()
|
|
377
|
+
setTimeout(async () => {
|
|
378
|
+
// 第二个请求在reUseDuration之后,请求返回之前发出
|
|
379
|
+
const req2 = ajax[method](url)()
|
|
380
|
+
const res1 = await req1
|
|
381
|
+
const res2 = await req2
|
|
382
|
+
// mock的axios.request每次会返回一个随机数,两个请求均返回,但结果并不相同
|
|
383
|
+
expect(res1).not.toBeNaN()
|
|
384
|
+
expect(res2).not.toBeNaN()
|
|
385
|
+
expect(res1).not.toBe(res2)
|
|
386
|
+
done()
|
|
387
|
+
}, 40)
|
|
214
388
|
})
|
|
215
389
|
|
|
216
|
-
test.each(hisMethods)(
|
|
390
|
+
test.each(hisMethods)(hisMethodsStr, (method, done) => {
|
|
391
|
+
const ajax = initAjax({reUseDuration: 30})
|
|
392
|
+
const url = getRandomUrl()
|
|
393
|
+
|
|
394
|
+
const req1 = ajax[method](url)()
|
|
395
|
+
setTimeout(async () => {
|
|
396
|
+
// 第二个请求在reUseDuration之后,请求返回之前发出
|
|
397
|
+
const req2 = ajax[method](url)()
|
|
398
|
+
const res1 = await req1
|
|
399
|
+
const res2 = await req2
|
|
400
|
+
|
|
401
|
+
// mock的axios.request每次会返回一个随机数,两个请求均返回,但结果并不相同
|
|
402
|
+
expect(res1).not.toBeNaN()
|
|
403
|
+
expect(res2).not.toBeNaN()
|
|
404
|
+
expect(res1).not.toBe(res2)
|
|
405
|
+
done()
|
|
406
|
+
}, 40)
|
|
407
|
+
})
|
|
217
408
|
|
|
409
|
+
})
|
|
410
|
+
|
|
411
|
+
describe('wx环境', () => {
|
|
412
|
+
|
|
413
|
+
beforeAll(() => {
|
|
218
414
|
global.wx = {
|
|
219
415
|
request: jest.fn((config: any) => {
|
|
220
416
|
setTimeout(() => {
|
|
221
417
|
config.success({
|
|
222
|
-
data:
|
|
418
|
+
data: Math.random()
|
|
223
419
|
}, 50)
|
|
224
420
|
})
|
|
225
421
|
})
|
|
226
422
|
}
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
423
|
+
|
|
424
|
+
undoAbortController()
|
|
425
|
+
})
|
|
230
426
|
|
|
427
|
+
afterAll(() => {
|
|
428
|
+
delete global.wx
|
|
429
|
+
restoreAbortController()
|
|
430
|
+
})
|
|
431
|
+
|
|
432
|
+
test.each(methods)(methodsStr, (method: RequestMethods, done) => {
|
|
433
|
+
|
|
434
|
+
const ajax = initAjax({reUseDuration: 30})
|
|
231
435
|
const url = getRandomUrl()
|
|
232
|
-
|
|
436
|
+
|
|
437
|
+
expect(ajax.$options.adapter).not.toBeUndefined() // 证明是微信环境
|
|
438
|
+
|
|
233
439
|
const req1 = ajax[method](url)()
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
440
|
+
setTimeout(async () => {
|
|
441
|
+
// 第二个请求在reUseDuration之后,请求返回之前发出
|
|
442
|
+
const req2 = ajax[method](url)()
|
|
443
|
+
const res1 = await req1
|
|
444
|
+
const res2 = await req2
|
|
445
|
+
|
|
446
|
+
expect(res1.data).not.toBeNaN()
|
|
447
|
+
expect(res2.data).not.toBeNaN()
|
|
448
|
+
console.log('res2:', res2, res1)
|
|
449
|
+
expect(res1.data).not.toEqual(res2.data)
|
|
450
|
+
done()
|
|
451
|
+
}, 40)
|
|
452
|
+
})
|
|
240
453
|
|
|
241
|
-
|
|
454
|
+
test.each(hisMethods)(hisMethodsStr, (method, done) => {
|
|
455
|
+
|
|
456
|
+
const ajax = initAjax({reUseDuration: 30})
|
|
457
|
+
// 验证微信环境,web和node下为undefined
|
|
458
|
+
expect(ajax.$options.adapter).not.toBeUndefined()
|
|
459
|
+
|
|
460
|
+
const url = getRandomUrl()
|
|
461
|
+
|
|
462
|
+
const req1 = ajax[method](url)()
|
|
463
|
+
setTimeout(async () => {
|
|
464
|
+
// 第二个请求在reUseDuration之后,请求返回之前发出
|
|
465
|
+
const req2 = ajax[method](url)()
|
|
466
|
+
const res1 = await req1
|
|
467
|
+
const res2 = await req2
|
|
468
|
+
|
|
469
|
+
// 返回的对象虽然相同,但是并不是同一个对象,证明不是复用
|
|
470
|
+
expect(res1.data).not.toBeNaN()
|
|
471
|
+
expect(res2.data).not.toBeNaN()
|
|
472
|
+
expect(res1.data).not.toEqual(res2.data)
|
|
473
|
+
done()
|
|
474
|
+
}, 40)
|
|
242
475
|
})
|
|
243
476
|
})
|
|
244
477
|
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
478
|
+
})
|
|
479
|
+
|
|
480
|
+
describe('触发间隔小于300ms,但第二个请求是在第一个请求返回后触发,依然会触发二次网络请求', () => {
|
|
481
|
+
|
|
482
|
+
describe('web或node环境', () => {
|
|
248
483
|
|
|
484
|
+
test.each(methods)(methodsStr, async (method) => {
|
|
485
|
+
const ajax = initAjax({})
|
|
486
|
+
|
|
249
487
|
const url = getRandomUrl()
|
|
250
488
|
// 发出请求,50ms返回结果,见__mocks__/axios.request
|
|
251
489
|
const res1 = await ajax[method](url)()
|
|
@@ -257,10 +495,10 @@ describe('initAjax test', () => {
|
|
|
257
495
|
expect(res2).not.toBeNaN()
|
|
258
496
|
})
|
|
259
497
|
|
|
260
|
-
test.each(hisMethods)(
|
|
498
|
+
test.each(hisMethods)(hisMethodsStr, async (method) => {
|
|
261
499
|
const ajax = initAjax({})
|
|
262
500
|
const url = getRandomUrl()
|
|
263
|
-
|
|
501
|
+
|
|
264
502
|
// 发出请求,50ms返回结果,见__mocks__/axios.request
|
|
265
503
|
const res1 = await ajax[method](url)()
|
|
266
504
|
const res2 = await ajax[method](url)()
|
|
@@ -271,8 +509,10 @@ describe('initAjax test', () => {
|
|
|
271
509
|
expect(res2).not.toBeNaN()
|
|
272
510
|
})
|
|
273
511
|
|
|
274
|
-
|
|
512
|
+
})
|
|
275
513
|
|
|
514
|
+
describe('wx环境', () => {
|
|
515
|
+
beforeAll(() => {
|
|
276
516
|
global.wx = {
|
|
277
517
|
request: jest.fn((config: any) => {
|
|
278
518
|
setTimeout(() => {
|
|
@@ -282,9 +522,17 @@ describe('initAjax test', () => {
|
|
|
282
522
|
})
|
|
283
523
|
})
|
|
284
524
|
}
|
|
525
|
+
undoAbortController()
|
|
526
|
+
})
|
|
285
527
|
|
|
286
|
-
|
|
528
|
+
afterAll(() => {
|
|
529
|
+
delete global.wx
|
|
530
|
+
restoreAbortController()
|
|
531
|
+
})
|
|
287
532
|
|
|
533
|
+
test.each(methods)(methodsStr, async (method) => {
|
|
534
|
+
const ajax = initAjax({})
|
|
535
|
+
|
|
288
536
|
const url = getRandomUrl()
|
|
289
537
|
// 发出请求,50ms返回结果,见__mocks__/axios.request
|
|
290
538
|
const res1 = await ajax[method](url)()
|
|
@@ -295,24 +543,14 @@ describe('initAjax test', () => {
|
|
|
295
543
|
expect(res2.data).toEqual({a:1})
|
|
296
544
|
expect(res1.config.url).toBe(url)
|
|
297
545
|
expect(res2.config.url).toBe(url)
|
|
298
|
-
|
|
299
|
-
delete global.wx
|
|
546
|
+
|
|
300
547
|
})
|
|
301
|
-
|
|
302
|
-
test.each(hisMethods)(
|
|
303
|
-
|
|
304
|
-
global.wx = {
|
|
305
|
-
request: jest.fn((config: any) => {
|
|
306
|
-
setTimeout(() => {
|
|
307
|
-
config.success({
|
|
308
|
-
data: {a: 1}
|
|
309
|
-
}, 50)
|
|
310
|
-
})
|
|
311
|
-
})
|
|
312
|
-
}
|
|
548
|
+
|
|
549
|
+
test.each(hisMethods)(hisMethodsStr, async (method) => {
|
|
550
|
+
|
|
313
551
|
const ajax = initAjax({})
|
|
314
552
|
const url = getRandomUrl()
|
|
315
|
-
|
|
553
|
+
|
|
316
554
|
// 发出请求,50ms返回结果,见__mocks__/axios.request
|
|
317
555
|
const res1 = await ajax[method](url)()
|
|
318
556
|
const res2 = await ajax[method](url)()
|
|
@@ -322,110 +560,124 @@ describe('initAjax test', () => {
|
|
|
322
560
|
expect(res2.data).toEqual({a:1})
|
|
323
561
|
expect(res1.config.url).toBe(url)
|
|
324
562
|
expect(res2.config.url).toBe(url)
|
|
325
|
-
|
|
326
|
-
delete global.wx
|
|
327
563
|
})
|
|
328
564
|
})
|
|
565
|
+
|
|
329
566
|
})
|
|
330
567
|
|
|
331
568
|
describe('validateStatus配置测试', () => {
|
|
332
|
-
const methods: RequestMethods[] = ['get', 'post', 'head', 'patch', 'put', 'delete']
|
|
333
|
-
const hisMethods: OldAjaxMethods[] = ['create', 'deletes', 'query', 'patchWay', 'putWay']
|
|
334
|
-
|
|
335
|
-
// TODO:待补充,需要在__mocks__/axios中模拟validateStatus功能
|
|
336
|
-
// test.each(methods)('web或node环境', async (method) => {
|
|
337
569
|
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
570
|
+
describe('web或node环境', () => {
|
|
571
|
+
// TODO:待补充,需要在__mocks__/axios中模拟validateStatus功能
|
|
572
|
+
})
|
|
573
|
+
|
|
574
|
+
describe('wx环境', () => {
|
|
575
|
+
beforeAll(() => {
|
|
576
|
+
global.wx = {
|
|
577
|
+
request: jest.fn((config: any) => {
|
|
578
|
+
setTimeout(() => {
|
|
579
|
+
config.success({
|
|
580
|
+
// 微信返回400
|
|
581
|
+
statusCode: 400,
|
|
582
|
+
data: {}
|
|
583
|
+
}, 50)
|
|
584
|
+
})
|
|
348
585
|
})
|
|
349
|
-
})
|
|
350
|
-
}
|
|
351
|
-
const ajax = initAjax({
|
|
352
|
-
validateStatus: (status) => {
|
|
353
|
-
// 小于200,或大于300,判为请求失败
|
|
354
|
-
if (status < 200 || status > 300) return false
|
|
355
|
-
return true
|
|
356
586
|
}
|
|
587
|
+
|
|
588
|
+
undoAbortController()
|
|
357
589
|
})
|
|
358
|
-
const fetch = ajax[method](getRandomUrl())
|
|
359
590
|
|
|
360
|
-
|
|
591
|
+
afterAll(() => {
|
|
592
|
+
delete global.wx
|
|
593
|
+
restoreAbortController()
|
|
594
|
+
})
|
|
361
595
|
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
statusCode: 400,
|
|
371
|
-
data: {}
|
|
372
|
-
}, 50)
|
|
373
|
-
})
|
|
596
|
+
test.each(methods)(methodsStr, async (method) => {
|
|
597
|
+
|
|
598
|
+
const ajax = initAjax({
|
|
599
|
+
validateStatus: (status) => {
|
|
600
|
+
// 小于200,或大于300,判为请求失败
|
|
601
|
+
if (status < 200 || status > 300) return false
|
|
602
|
+
return true
|
|
603
|
+
}
|
|
374
604
|
})
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
// 小于200,或大于300,判为请求失败
|
|
379
|
-
if (status < 200 || status > 300) return false
|
|
380
|
-
return true
|
|
381
|
-
}
|
|
605
|
+
const fetch = ajax[method](getRandomUrl())
|
|
606
|
+
|
|
607
|
+
await expect(fetch()).rejects.toHaveProperty('message', 'Request failed with status code 400')
|
|
382
608
|
})
|
|
383
|
-
const fetch = ajax[method](getRandomUrl())
|
|
384
|
-
|
|
385
|
-
await expect(fetch()).rejects.toHaveProperty('message', 'Request failed with status code 400')
|
|
386
609
|
|
|
387
|
-
|
|
610
|
+
test.each(hisMethods)(hisMethodsStr, async (method) => {
|
|
611
|
+
|
|
612
|
+
const ajax = initAjax({
|
|
613
|
+
validateStatus: (status) => {
|
|
614
|
+
// 小于200,或大于300,判为请求失败
|
|
615
|
+
if (status < 200 || status > 300) return false
|
|
616
|
+
return true
|
|
617
|
+
}
|
|
618
|
+
})
|
|
619
|
+
const fetch = ajax[method](getRandomUrl())
|
|
620
|
+
|
|
621
|
+
await expect(fetch()).rejects.toHaveProperty('message', 'Request failed with status code 400')
|
|
622
|
+
})
|
|
388
623
|
})
|
|
389
624
|
})
|
|
390
625
|
|
|
391
626
|
describe('发送请求异常测试', () => {
|
|
392
627
|
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
628
|
+
describe('web或node环境', () => {
|
|
629
|
+
// TODO
|
|
630
|
+
})
|
|
631
|
+
|
|
632
|
+
describe('wx环境', () => {
|
|
633
|
+
beforeAll(() => {
|
|
634
|
+
global.wx = {
|
|
635
|
+
request: jest.fn((config: any) => {
|
|
636
|
+
setTimeout(() => {
|
|
637
|
+
config.fail({
|
|
638
|
+
errMsg: '网络连接错误',
|
|
639
|
+
errno: 1101
|
|
640
|
+
}, 50)
|
|
641
|
+
})
|
|
401
642
|
})
|
|
402
|
-
}
|
|
403
|
-
}
|
|
404
|
-
const ajax = initAjax({})
|
|
643
|
+
}
|
|
405
644
|
|
|
406
|
-
|
|
645
|
+
undoAbortController()
|
|
646
|
+
})
|
|
407
647
|
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
await expect(fetch()).rejects.toHaveProperty('response', undefined)
|
|
648
|
+
afterAll(() => {
|
|
649
|
+
delete global.wx
|
|
650
|
+
restoreAbortController()
|
|
651
|
+
})
|
|
413
652
|
|
|
414
|
-
|
|
653
|
+
test.each([...methods, ...hisMethods])(`${methodsStr}|${hisMethodsStr}`, async (method) => {
|
|
654
|
+
|
|
655
|
+
const ajax = initAjax({})
|
|
656
|
+
|
|
657
|
+
const fetch = ajax[method](getRandomUrl())
|
|
658
|
+
|
|
659
|
+
await expect(fetch()).rejects.toHaveProperty('message', '网络连接错误')
|
|
660
|
+
await expect(fetch()).rejects.toHaveProperty('code', 1101)
|
|
661
|
+
await expect(fetch()).rejects.toHaveProperty('config')
|
|
662
|
+
await expect(fetch()).rejects.toHaveProperty('request')
|
|
663
|
+
await expect(fetch()).rejects.toHaveProperty('response', undefined)
|
|
664
|
+
})
|
|
415
665
|
})
|
|
666
|
+
|
|
416
667
|
})
|
|
417
668
|
|
|
418
|
-
describe('
|
|
669
|
+
describe('cancelLastSameUrl为true,第二个相同URL的get请求,会将第一个get请求cancel掉', () => {
|
|
419
670
|
|
|
420
671
|
describe('web和node环境', () => {
|
|
421
|
-
|
|
672
|
+
|
|
673
|
+
test('两个请求', async () => {
|
|
422
674
|
const ajax = initAjax({})
|
|
423
675
|
const url = getRandomUrl()
|
|
424
676
|
// 发出请求,50ms返回结果,见__mocks__/axios.request
|
|
425
677
|
// 两个请求URL和方法相同,第一个会取消第二个请求
|
|
426
678
|
const res1 = ajax.get(url, {})({a:1}, {cancelLastSameUrl: true})
|
|
427
679
|
const res2 = ajax.get(url, {})({}, {cancelLastSameUrl: true})
|
|
428
|
-
|
|
680
|
+
|
|
429
681
|
// mock的axios.request每次会返回一个随机数
|
|
430
682
|
await expect(res1).rejects.toMatch('请求被取消')
|
|
431
683
|
await expect(res2).resolves.not.toBeNaN()
|
|
@@ -439,7 +691,7 @@ describe('第二个相同URL和方法的get请求,会将第一个get请求canc
|
|
|
439
691
|
const res1 = ajax.get(url, {})({a: 1}, {cancelLastSameUrl: true})
|
|
440
692
|
const res2 = ajax.get(url, {})({a: 2}, {cancelLastSameUrl: true})
|
|
441
693
|
const res3 = ajax.get(url, {})({a: 3}, {cancelLastSameUrl: true})
|
|
442
|
-
|
|
694
|
+
|
|
443
695
|
// mock的axios.request每次会返回一个随机数
|
|
444
696
|
await expect(res1).rejects.toMatch('请求被取消')
|
|
445
697
|
await expect(res2).rejects.toMatch('请求被取消')
|
|
@@ -449,11 +701,11 @@ describe('第二个相同URL和方法的get请求,会将第一个get请求canc
|
|
|
449
701
|
test('使用cancelLastSameUrl,但如果两个请求完全相同,且同时发出,第二个请求返回第一个请求的请求结果,不会取消第一个请求', async () => {
|
|
450
702
|
const ajax = initAjax({})
|
|
451
703
|
const url = getRandomUrl()
|
|
452
|
-
|
|
704
|
+
|
|
453
705
|
// 发出请求,50ms返回结果,见__mocks__/axios.request
|
|
454
706
|
const req1 = ajax.get(url, {})({}, {cancelLastSameUrl: true})
|
|
455
707
|
const req2 = ajax.get(url, {})({}, {cancelLastSameUrl: true})
|
|
456
|
-
|
|
708
|
+
|
|
457
709
|
const res1 = await req1
|
|
458
710
|
const res2 = await req2
|
|
459
711
|
// mock的axios.request每次会返回一个随机数
|
|
@@ -463,6 +715,15 @@ describe('第二个相同URL和方法的get请求,会将第一个get请求canc
|
|
|
463
715
|
|
|
464
716
|
describe('wx环境', () => {
|
|
465
717
|
|
|
718
|
+
beforeAll(() => {
|
|
719
|
+
undoAbortController()
|
|
720
|
+
})
|
|
721
|
+
|
|
722
|
+
afterAll(() => {
|
|
723
|
+
delete global.wx
|
|
724
|
+
restoreAbortController()
|
|
725
|
+
})
|
|
726
|
+
|
|
466
727
|
test('两个请求', async () => {
|
|
467
728
|
global.wx = {
|
|
468
729
|
request: jest.fn((config: any) => {
|
|
@@ -471,17 +732,10 @@ describe('第二个相同URL和方法的get请求,会将第一个get请求canc
|
|
|
471
732
|
data: {}
|
|
472
733
|
}, 50)
|
|
473
734
|
})
|
|
474
|
-
|
|
735
|
+
|
|
475
736
|
return { abort: jest.fn() }
|
|
476
737
|
})
|
|
477
738
|
}
|
|
478
|
-
|
|
479
|
-
const originAbortController = global.AbortController
|
|
480
|
-
|
|
481
|
-
// 微信环境没有AbortController
|
|
482
|
-
Object.defineProperty(global, 'AbortController', {
|
|
483
|
-
get() { return undefined }
|
|
484
|
-
})
|
|
485
739
|
|
|
486
740
|
const ajax = initAjax({})
|
|
487
741
|
const url = getRandomUrl()
|
|
@@ -493,7 +747,7 @@ describe('第二个相同URL和方法的get请求,会将第一个get请求canc
|
|
|
493
747
|
// 两个请求URL和方法相同,第一个会取消第二个请求
|
|
494
748
|
const res1 = ajax.get(url, {})({a:1}, {cancelLastSameUrl: true})
|
|
495
749
|
const res2 = ajax.get(url, {})({}, {cancelLastSameUrl: true})
|
|
496
|
-
|
|
750
|
+
|
|
497
751
|
// mock的axios.request每次会返回一个随机数
|
|
498
752
|
await expect(res1).rejects.toHaveProperty('code', 'ERR_CANCELED')
|
|
499
753
|
await expect(res1).rejects.toHaveProperty('message', '请求被取消!')
|
|
@@ -502,14 +756,8 @@ describe('第二个相同URL和方法的get请求,会将第一个get请求canc
|
|
|
502
756
|
await expect(res1).rejects.toHaveProperty('response')
|
|
503
757
|
// 第二个请求返回结果
|
|
504
758
|
await expect(res2).resolves.toHaveProperty('data', {})
|
|
505
|
-
|
|
506
|
-
delete global.wx
|
|
507
|
-
Object.defineProperty(global, 'AbortController', {
|
|
508
|
-
get() {
|
|
509
|
-
return originAbortController
|
|
510
|
-
}
|
|
511
|
-
})
|
|
512
759
|
})
|
|
760
|
+
|
|
513
761
|
test('三个请求', async () => {
|
|
514
762
|
global.wx = {
|
|
515
763
|
request: jest.fn((config: any) => {
|
|
@@ -518,17 +766,10 @@ describe('第二个相同URL和方法的get请求,会将第一个get请求canc
|
|
|
518
766
|
data: {}
|
|
519
767
|
}, 100)
|
|
520
768
|
})
|
|
521
|
-
|
|
769
|
+
|
|
522
770
|
return { abort: jest.fn() }
|
|
523
771
|
})
|
|
524
772
|
}
|
|
525
|
-
|
|
526
|
-
const originAbortController = global.AbortController
|
|
527
|
-
|
|
528
|
-
// 微信环境没有AbortController
|
|
529
|
-
Object.defineProperty(global, 'AbortController', {
|
|
530
|
-
get() { return undefined }
|
|
531
|
-
})
|
|
532
773
|
|
|
533
774
|
const ajax = initAjax({})
|
|
534
775
|
const url = getRandomUrl()
|
|
@@ -541,7 +782,7 @@ describe('第二个相同URL和方法的get请求,会将第一个get请求canc
|
|
|
541
782
|
const res1 = ajax.get(url, {})({a: 1}, {cancelLastSameUrl: true})
|
|
542
783
|
const res2 = ajax.get(url, {})({a: 2}, {cancelLastSameUrl: true})
|
|
543
784
|
const res3 = ajax.get(url, {})({a: 3}, {cancelLastSameUrl: true})
|
|
544
|
-
|
|
785
|
+
|
|
545
786
|
// mock的axios.request每次会返回一个随机数
|
|
546
787
|
await expect(res1).rejects.toHaveProperty('code', 'ERR_CANCELED')
|
|
547
788
|
await expect(res1).rejects.toHaveProperty('message', '请求被取消!')
|
|
@@ -550,13 +791,6 @@ describe('第二个相同URL和方法的get请求,会将第一个get请求canc
|
|
|
550
791
|
|
|
551
792
|
// 第二个请求返回结果
|
|
552
793
|
await expect(res3).resolves.toHaveProperty('data', {})
|
|
553
|
-
|
|
554
|
-
delete global.wx
|
|
555
|
-
Object.defineProperty(global, 'AbortController', {
|
|
556
|
-
get() {
|
|
557
|
-
return originAbortController
|
|
558
|
-
}
|
|
559
|
-
})
|
|
560
794
|
})
|
|
561
795
|
|
|
562
796
|
test('使用cancelLastSameUrl,但如果两个请求完全相同,且同时发出,第二个请求返回第一个请求的请求结果,不会取消第一个请求', async () => {
|
|
@@ -568,36 +802,21 @@ describe('第二个相同URL和方法的get请求,会将第一个get请求canc
|
|
|
568
802
|
data: {}
|
|
569
803
|
}, 50)
|
|
570
804
|
})
|
|
571
|
-
|
|
805
|
+
|
|
572
806
|
return { abort: jest.fn() }
|
|
573
807
|
})
|
|
574
808
|
}
|
|
575
|
-
|
|
576
|
-
const originAbortController = global.AbortController
|
|
577
|
-
|
|
578
|
-
// 微信环境没有AbortController
|
|
579
|
-
Object.defineProperty(global, 'AbortController', {
|
|
580
|
-
get() { return undefined }
|
|
581
|
-
})
|
|
582
|
-
|
|
583
809
|
const ajax = initAjax({})
|
|
584
810
|
const url = getRandomUrl()
|
|
585
|
-
|
|
811
|
+
|
|
586
812
|
// 发出请求,50ms返回结果,见__mocks__/axios.request
|
|
587
813
|
const req1 = ajax.get(url, {})({}, {cancelLastSameUrl: true})
|
|
588
814
|
const req2 = ajax.get(url, {})({}, {cancelLastSameUrl: true})
|
|
589
|
-
|
|
815
|
+
|
|
590
816
|
const res1 = await req1
|
|
591
817
|
const res2 = await req2
|
|
592
818
|
// mock的axios.request每次会返回一个随机数
|
|
593
819
|
expect(res1).toBe(res2)
|
|
594
|
-
|
|
595
|
-
delete global.wx
|
|
596
|
-
Object.defineProperty(global, 'AbortController', {
|
|
597
|
-
get() {
|
|
598
|
-
return originAbortController
|
|
599
|
-
}
|
|
600
|
-
})
|
|
601
820
|
})
|
|
602
821
|
})
|
|
603
822
|
})
|
|
@@ -623,10 +842,19 @@ describe('mock配置测试', () => {
|
|
|
623
842
|
|
|
624
843
|
describe('wx环境', () => {
|
|
625
844
|
|
|
626
|
-
|
|
845
|
+
beforeAll(() => {
|
|
627
846
|
global.wx = {
|
|
628
847
|
request: jest.fn()
|
|
629
848
|
}
|
|
849
|
+
undoAbortController()
|
|
850
|
+
})
|
|
851
|
+
|
|
852
|
+
afterAll(() => {
|
|
853
|
+
delete global.wx
|
|
854
|
+
restoreAbortController()
|
|
855
|
+
})
|
|
856
|
+
|
|
857
|
+
test.each(methods)('基本功能', async (method) => {
|
|
630
858
|
|
|
631
859
|
const ajax = initAjax({})
|
|
632
860
|
const url = getRandomUrl()
|
|
@@ -634,15 +862,9 @@ describe('mock配置测试', () => {
|
|
|
634
862
|
expect(ajax.$options.adapter).not.toBeUndefined()
|
|
635
863
|
const res1 = ajax[method](url, {})({a:1}, {mock: {mock:true}})
|
|
636
864
|
await expect(res1).resolves.toEqual({mock:true})
|
|
637
|
-
|
|
638
|
-
delete global.wx
|
|
639
865
|
})
|
|
640
866
|
// 历史方法没有兼容该功能
|
|
641
867
|
// test.each(hisMethods)('基本功能(废弃方法)', async (method) => {
|
|
642
|
-
// global.wx = {
|
|
643
|
-
// request: jest.fn()
|
|
644
|
-
// }
|
|
645
|
-
|
|
646
868
|
// const ajax = initAjax({})
|
|
647
869
|
// const url = 'wx/get/mocked/res/duplicate'
|
|
648
870
|
|
|
@@ -650,8 +872,6 @@ describe('mock配置测试', () => {
|
|
|
650
872
|
// // 即将废弃的方法,配置只能在ajax[method]()的第二个参数
|
|
651
873
|
// const res1 = ajax[method](url, {mock: {mock:true}})({a:1})
|
|
652
874
|
// await expect(res1).resolves.toEqual({mock:true})
|
|
653
|
-
|
|
654
|
-
// delete global.wx
|
|
655
875
|
// })
|
|
656
876
|
})
|
|
657
|
-
})
|
|
877
|
+
})
|