@lx-frontend/multi-ajax 0.1.1 → 0.2.0

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.
Files changed (64) hide show
  1. package/README.md +10 -2
  2. package/__mocks__/axios.ts +49 -0
  3. package/jest.config.ts +24 -0
  4. package/lib/adapters/index.js +7 -0
  5. package/lib/adapters/index.js.map +1 -0
  6. package/lib/adapters/wx-adapter.d.ts +6 -0
  7. package/lib/adapters/wx-adapter.js +81 -0
  8. package/lib/adapters/wx-adapter.js.map +1 -0
  9. package/lib/cancelLastSameRequest.d.ts +9 -0
  10. package/lib/cancelLastSameRequest.js +48 -0
  11. package/lib/cancelLastSameRequest.js.map +1 -0
  12. package/lib/canceller.d.ts +2 -0
  13. package/lib/canceller.js +8 -0
  14. package/lib/canceller.js.map +1 -0
  15. package/lib/compatibleMethods.d.ts +6 -0
  16. package/lib/compatibleMethods.js +60 -0
  17. package/lib/compatibleMethods.js.map +1 -0
  18. package/lib/duplicateRequestPrevent.d.ts +5 -0
  19. package/lib/duplicateRequestPrevent.js +28 -0
  20. package/lib/duplicateRequestPrevent.js.map +1 -0
  21. package/lib/{types/helpers.d.ts → helpers.d.ts} +2 -2
  22. package/lib/helpers.js +42 -0
  23. package/lib/helpers.js.map +1 -0
  24. package/lib/index.d.ts +2 -0
  25. package/lib/index.js +34 -0
  26. package/lib/index.js.map +1 -0
  27. package/lib/methods.d.ts +7 -0
  28. package/lib/methods.js +107 -0
  29. package/lib/methods.js.map +1 -0
  30. package/lib/{types/types.d.ts → types.d.ts} +6 -5
  31. package/lib/types.js +3 -0
  32. package/lib/types.js.map +1 -0
  33. package/lib/{types/utils.d.ts → utils.d.ts} +13 -1
  34. package/lib/utils.js +66 -0
  35. package/lib/utils.js.map +1 -0
  36. package/package.json +14 -22
  37. package/src/adapters/wx-adapter.ts +35 -10
  38. package/src/cancelLastSameRequest.ts +43 -0
  39. package/src/canceller.ts +2 -2
  40. package/src/compatibleMethods.ts +24 -6
  41. package/src/duplicateRequestPrevent.ts +23 -0
  42. package/src/helpers.ts +3 -3
  43. package/src/index.ts +4 -3
  44. package/src/methods.ts +80 -37
  45. package/src/types.ts +7 -6
  46. package/src/utils.ts +28 -6
  47. package/tests/duplicateRequestPrevent.test.ts +39 -0
  48. package/tests/helpers.test.ts +73 -0
  49. package/tests/index.test.ts +657 -0
  50. package/tests/jest-setup.ts +4 -0
  51. package/tests/utils.test.ts +68 -0
  52. package/tsconfig.json +2 -1
  53. package/.babelrc.json +0 -5
  54. package/__tests__/lx-frontend-ajax.test.js +0 -7
  55. package/build/rollup.config.cjs.js +0 -43
  56. package/build/rollup.config.esm.js +0 -40
  57. package/lib/ajax.cjs.js +0 -1
  58. package/lib/ajax.esm.js +0 -1
  59. package/lib/types/adapters/wx-adapter.d.ts +0 -2
  60. package/lib/types/canceller.d.ts +0 -2
  61. package/lib/types/compatibleMethods.d.ts +0 -6
  62. package/lib/types/index.d.ts +0 -2
  63. package/lib/types/methods.d.ts +0 -9
  64. /package/lib/{types/adapters → adapters}/index.d.ts +0 -0
@@ -0,0 +1,657 @@
1
+ import initAjax from '../lib/index'
2
+ import { getType } from '../lib/utils'
3
+ import type { RequestMethods } from '../lib/types.d'
4
+ import type { OldAjaxMethods } from '../lib/compatibleMethods.d'
5
+
6
+ // 生成随机URL,避免测试用例之间的干扰
7
+ function getRandomUrl(len: number = 4) {
8
+ const aCode = 'a'.charCodeAt(0)
9
+ const getIdx = () => Math.floor(Math.random() * 26) + aCode
10
+ return new Array(len).fill(null)
11
+ .map(() => `${String.fromCharCode(getIdx())}${String.fromCharCode(getIdx())}`)
12
+ .join('/')
13
+ }
14
+
15
+ function getRandomKey(len: number = 4) {
16
+ const aCode = 'a'.charCodeAt(0)
17
+ const getIdx = () => Math.floor(Math.random() * 26) + aCode
18
+ return new Array(len).fill(null)
19
+ .map(() => String.fromCharCode(getIdx()))
20
+ .join('')
21
+ }
22
+
23
+ jest.mock('axios')
24
+
25
+ declare global {
26
+ var wx: any
27
+ }
28
+
29
+ const methods: RequestMethods[] = ['get', 'post', 'head', 'patch', 'put', 'delete']
30
+ const hisMethods: OldAjaxMethods[] = ['create', 'deletes', 'query', 'patchWay', 'putWay']
31
+
32
+ describe('initAjax test', () => {
33
+ const methods: RequestMethods[] = ['get', 'post', 'head', 'patch', 'put', 'delete']
34
+ const hisMethods: OldAjaxMethods[] = ['create', 'deletes', 'query', 'patchWay', 'putWay']
35
+ test('初始化测试', () => {
36
+ const ajax = initAjax({})
37
+ expect(ajax.$options).toEqual({});
38
+ expect(ajax.$engine).toBeDefined();
39
+ expect(ajax.canceller).toBeDefined();
40
+ // request, interceptors
41
+ expect(ajax.request).not.toBeUndefined()
42
+ expect(ajax.interceptors).not.toBeUndefined()
43
+ })
44
+
45
+ test.each(methods)('ajax方法检测', (method) => {
46
+ const ajax = initAjax({})
47
+ expect(typeof ajax[method]).toBe('function')
48
+ })
49
+
50
+ test.each(hisMethods)('ajax兼容方法检测', (method) => {
51
+ const ajax = initAjax({})
52
+ expect(typeof ajax[method]).toBe('function')
53
+ })
54
+
55
+ test.each([...methods, ...hisMethods])('ajax[method]调用应该返回函数', (method) => {
56
+ const ajax = initAjax({})
57
+ expect(typeof ajax[method](getRandomUrl(), {})).toBe('function')
58
+ })
59
+
60
+ test.each([...methods, ...hisMethods])('ajax[method]()返回的函数再调用应该返回Promise对象', (method) => {
61
+ const ajax = initAjax({})
62
+ expect(getType(ajax[method](getRandomUrl(), {})())).toBe('promise')
63
+ })
64
+
65
+ describe('请求配置中如果有cancelKey,则ajax.canceller应该有对应的取消函数', () => {
66
+
67
+ test.each(methods)('web或node环境', (method) => {
68
+ const ajax = initAjax({})
69
+ // 发出请求
70
+ const cancelKey = getRandomKey()
71
+ const res = ajax[method](getRandomUrl(), {})({}, { cancelKey })
72
+
73
+ expect(typeof ajax.canceller[cancelKey]).toBe('function')
74
+ })
75
+
76
+ test.each(methods)('wx环境', (method) => {
77
+ global.wx = {
78
+ request: jest.fn((config: any) => {
79
+ setTimeout(() => {
80
+ config.success({
81
+ data: {a: 1}
82
+ }, 50)
83
+ })
84
+ })
85
+ }
86
+
87
+ const ajax = initAjax({})
88
+ // 验证微信环境
89
+ expect(ajax.$options.adapter).not.toBeUndefined()
90
+ // 发出请求
91
+ const cancelKey = getRandomKey()
92
+ const res = ajax[method](getRandomUrl(), {})({}, {cancelKey})
93
+
94
+ expect(getType(ajax.canceller[cancelKey])).toBe('function')
95
+
96
+ delete global.wx
97
+ })
98
+ })
99
+
100
+ describe('取消请求测试', () => {
101
+
102
+ test.each(methods)('web或node环境',async (method) => {
103
+
104
+ const ajax = initAjax({})
105
+
106
+ // 发出请求
107
+ const cancelKey = getRandomKey()
108
+ const fetch = ajax[method](getRandomUrl())({}, {cancelKey})
109
+ // 取消请求
110
+ ajax.canceller[cancelKey]()
111
+
112
+ // 模拟的axios直接返回了请求被取消,实际使用可能不是这样
113
+ await expect(fetch).rejects.toMatch('请求被取消')
114
+ // await expect(fetch).rejects.toHaveProperty('code', 'ERR_CANCELED')
115
+ // 请求取消后,取消函数被删除
116
+ expect(ajax.canceller[cancelKey]).toBeUndefined()
117
+
118
+ })
119
+
120
+ test.each(methods)('wx环境', async (method) => {
121
+
122
+ global.wx = {
123
+ request: jest.fn((config: any) => {
124
+ setTimeout(() => {
125
+ config.success({
126
+ data: {}
127
+ }, 50)
128
+ })
129
+
130
+ return { abort: jest.fn() }
131
+ })
132
+ }
133
+
134
+ const originAbortController = global.AbortController
135
+
136
+ // 微信环境没有AbortController
137
+ Object.defineProperty(global, 'AbortController', {
138
+ get() {
139
+ return undefined
140
+ }
141
+ })
142
+
143
+ const ajax = initAjax({})
144
+
145
+ // 发出请求
146
+ const cancelKey = getRandomKey()
147
+ const fetch = ajax[method](getRandomUrl())({}, {cancelKey})
148
+ // 取消请求
149
+ ajax.canceller[cancelKey]()
150
+
151
+ await expect(fetch).rejects.toHaveProperty('message', '请求被取消!')
152
+ await expect(fetch).rejects.toHaveProperty('code', 'ERR_CANCELED')
153
+ // 请求取消后,取消函数被删除
154
+ expect(ajax.canceller[cancelKey]).toBeUndefined()
155
+
156
+ delete global.wx
157
+ Object.defineProperty(global, 'AbortController', {
158
+ get() {
159
+ return originAbortController
160
+ }
161
+ })
162
+ })
163
+ })
164
+
165
+ describe('300ms内的并行触发的重复请求不会触发二次网络请求,后一个请求复用前一个结果', () => {
166
+ test.each(methods)('web或node环境', async (method) => {
167
+ const ajax = initAjax({})
168
+ const url = getRandomUrl()
169
+ // 并行发出请求
170
+ const req1 = ajax[method](url)()
171
+ const req2 = ajax[method](url)()
172
+ const res1 = await req1
173
+ const res2 = await req2
174
+
175
+ // mock的axios.request每次会返回一个随机数,如果复用上一个请求,两者应该相等
176
+ expect(res1).toBe(res2)
177
+ })
178
+
179
+ test.each(hisMethods)('web或node环境(即将废弃的方法)', async (method) => {
180
+ const ajax = initAjax({})
181
+ const url = getRandomUrl()
182
+ // 并行发出请求,旧的方法配置只能在ajax[method]的第二个参数写
183
+ const req1 = ajax[method](url)()
184
+ const req2 = ajax[method](url)()
185
+ const res1 = await req1
186
+ const res2 = await req2
187
+
188
+ // mock的axios.request每次会返回一个随机数,如果复用上一个请求,两者应该相等
189
+ expect(res1).toBe(res2)
190
+ })
191
+
192
+ test.each(methods)('wx环境',async (method: RequestMethods) => {
193
+ global.wx = {
194
+ request: jest.fn((config: any) => {
195
+ setTimeout(() => {
196
+ config.success({
197
+ data: {a: 1}
198
+ }, 50)
199
+ })
200
+ })
201
+ }
202
+
203
+ const ajax = initAjax({})
204
+ const url = getRandomUrl()
205
+ // 并行发出请求
206
+ const req1 = ajax[method](url)()
207
+ const req2 = ajax[method](url)()
208
+ const res1 = await req1
209
+ const res2 = await req2
210
+
211
+ expect(res1).toBe(res2)
212
+
213
+ delete global.wx
214
+ })
215
+
216
+ test.each(hisMethods)('wx环境(即将废弃的方法)', async (method) => {
217
+
218
+ global.wx = {
219
+ request: jest.fn((config: any) => {
220
+ setTimeout(() => {
221
+ config.success({
222
+ data: {a: 1}
223
+ }, 50)
224
+ })
225
+ })
226
+ }
227
+ const ajax = initAjax({})
228
+ // 验证微信环境,web和node下为undefined
229
+ expect(ajax.$options.adapter).not.toBeUndefined()
230
+
231
+ const url = getRandomUrl()
232
+ // 并行发出请求,旧的方法配置只能在ajax[method]的第二个参数写
233
+ const req1 = ajax[method](url)()
234
+ const req2 = ajax[method](url)()
235
+ const res1 = await req1
236
+ const res2 = await req2
237
+
238
+ // mock的axios.request每次会返回一个随机数,如果复用上一个请求,两者应该相等
239
+ expect(res1).toBe(res2)
240
+
241
+ delete global.wx
242
+ })
243
+ })
244
+
245
+ describe('触发间隔小于300ms,但第二个请求是在第一个请求返回后触发,依然会触发二次网络请求', () => {
246
+ test.each(methods)('web或node环境', async (method) => {
247
+ const ajax = initAjax({})
248
+
249
+ const url = getRandomUrl()
250
+ // 发出请求,50ms返回结果,见__mocks__/axios.request
251
+ const res1 = await ajax[method](url)()
252
+ const res2 = await ajax[method](url)()
253
+
254
+ // mock的axios.request每次会返回一个随机数
255
+ expect(res1).not.toBe(res2)
256
+ expect(res1).not.toBeNaN()
257
+ expect(res2).not.toBeNaN()
258
+ })
259
+
260
+ test.each(hisMethods)('web或node环境(即将废弃方法)', async (method) => {
261
+ const ajax = initAjax({})
262
+ const url = getRandomUrl()
263
+
264
+ // 发出请求,50ms返回结果,见__mocks__/axios.request
265
+ const res1 = await ajax[method](url)()
266
+ const res2 = await ajax[method](url)()
267
+
268
+ // mock的axios.request每次会返回一个随机数
269
+ expect(res1).not.toBe(res2)
270
+ expect(res1).not.toBeNaN()
271
+ expect(res2).not.toBeNaN()
272
+ })
273
+
274
+ test.each(methods)('wx环境', async (method) => {
275
+
276
+ global.wx = {
277
+ request: jest.fn((config: any) => {
278
+ setTimeout(() => {
279
+ config.success({
280
+ data: {a: 1}
281
+ }, 50)
282
+ })
283
+ })
284
+ }
285
+
286
+ const ajax = initAjax({})
287
+
288
+ const url = getRandomUrl()
289
+ // 发出请求,50ms返回结果,见__mocks__/axios.request
290
+ const res1 = await ajax[method](url)()
291
+ const res2 = await ajax[method](url)()
292
+
293
+ expect(res1).not.toBe(res2)
294
+ expect(res1.data).toEqual({a:1})
295
+ expect(res2.data).toEqual({a:1})
296
+ expect(res1.config.url).toBe(url)
297
+ expect(res2.config.url).toBe(url)
298
+
299
+ delete global.wx
300
+ })
301
+
302
+ test.each(hisMethods)('wx环境(即将废弃方法)', async (method) => {
303
+
304
+ global.wx = {
305
+ request: jest.fn((config: any) => {
306
+ setTimeout(() => {
307
+ config.success({
308
+ data: {a: 1}
309
+ }, 50)
310
+ })
311
+ })
312
+ }
313
+ const ajax = initAjax({})
314
+ const url = getRandomUrl()
315
+
316
+ // 发出请求,50ms返回结果,见__mocks__/axios.request
317
+ const res1 = await ajax[method](url)()
318
+ const res2 = await ajax[method](url)()
319
+
320
+ expect(res1).not.toBe(res2)
321
+ expect(res1.data).toEqual({a:1})
322
+ expect(res2.data).toEqual({a:1})
323
+ expect(res1.config.url).toBe(url)
324
+ expect(res2.config.url).toBe(url)
325
+
326
+ delete global.wx
327
+ })
328
+ })
329
+ })
330
+
331
+ 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
+
338
+ // })
339
+ test.each(methods)('wx环境', async (method) => {
340
+ global.wx = {
341
+ request: jest.fn((config: any) => {
342
+ setTimeout(() => {
343
+ config.success({
344
+ // 微信返回400
345
+ statusCode: 400,
346
+ data: {}
347
+ }, 50)
348
+ })
349
+ })
350
+ }
351
+ const ajax = initAjax({
352
+ validateStatus: (status) => {
353
+ // 小于200,或大于300,判为请求失败
354
+ if (status < 200 || status > 300) return false
355
+ return true
356
+ }
357
+ })
358
+ const fetch = ajax[method](getRandomUrl())
359
+
360
+ await expect(fetch()).rejects.toHaveProperty('message', 'Request failed with status code 400')
361
+
362
+ delete global.wx
363
+ })
364
+ test.each(hisMethods)('wx环境(即将废弃方法)', async (method) => {
365
+ global.wx = {
366
+ request: jest.fn((config: any) => {
367
+ setTimeout(() => {
368
+ config.success({
369
+ // 微信返回400
370
+ statusCode: 400,
371
+ data: {}
372
+ }, 50)
373
+ })
374
+ })
375
+ }
376
+ const ajax = initAjax({
377
+ validateStatus: (status) => {
378
+ // 小于200,或大于300,判为请求失败
379
+ if (status < 200 || status > 300) return false
380
+ return true
381
+ }
382
+ })
383
+ const fetch = ajax[method](getRandomUrl())
384
+
385
+ await expect(fetch()).rejects.toHaveProperty('message', 'Request failed with status code 400')
386
+
387
+ delete global.wx
388
+ })
389
+ })
390
+
391
+ describe('发送请求异常测试', () => {
392
+
393
+ test.each([...methods, ...hisMethods])('wx环境', async (method) => {
394
+ global.wx = {
395
+ request: jest.fn((config: any) => {
396
+ setTimeout(() => {
397
+ config.fail({
398
+ errMsg: '网络连接错误',
399
+ errno: 1101
400
+ }, 50)
401
+ })
402
+ })
403
+ }
404
+ const ajax = initAjax({})
405
+
406
+ const fetch = ajax[method](getRandomUrl())
407
+
408
+ await expect(fetch()).rejects.toHaveProperty('message', '网络连接错误')
409
+ await expect(fetch()).rejects.toHaveProperty('code', 1101)
410
+ await expect(fetch()).rejects.toHaveProperty('config')
411
+ await expect(fetch()).rejects.toHaveProperty('request')
412
+ await expect(fetch()).rejects.toHaveProperty('response', undefined)
413
+
414
+ delete global.wx
415
+ })
416
+ })
417
+
418
+ describe('第二个相同URL和方法的get请求,会将第一个get请求cancel掉', () => {
419
+
420
+ describe('web和node环境', () => {
421
+ test('基本功能', async () => {
422
+ const ajax = initAjax({})
423
+ const url = getRandomUrl()
424
+ // 发出请求,50ms返回结果,见__mocks__/axios.request
425
+ // 两个请求URL和方法相同,第一个会取消第二个请求
426
+ const res1 = ajax.get(url, {})({a:1}, {cancelLastSameUrl: true})
427
+ const res2 = ajax.get(url, {})({}, {cancelLastSameUrl: true})
428
+
429
+ // mock的axios.request每次会返回一个随机数
430
+ await expect(res1).rejects.toMatch('请求被取消')
431
+ await expect(res2).resolves.not.toBeNaN()
432
+ })
433
+
434
+ test('连续三个请求', async () => {
435
+ const ajax = initAjax({})
436
+ const url = getRandomUrl()
437
+ // 发出请求,50ms返回结果,见__mocks__/axios.request
438
+ // 两个请求URL和方法相同,第一个会取消第二个请求
439
+ const res1 = ajax.get(url, {})({a: 1}, {cancelLastSameUrl: true})
440
+ const res2 = ajax.get(url, {})({a: 2}, {cancelLastSameUrl: true})
441
+ const res3 = ajax.get(url, {})({a: 3}, {cancelLastSameUrl: true})
442
+
443
+ // mock的axios.request每次会返回一个随机数
444
+ await expect(res1).rejects.toMatch('请求被取消')
445
+ await expect(res2).rejects.toMatch('请求被取消')
446
+ await expect(res3).resolves.not.toBeNaN()
447
+ })
448
+
449
+ test('使用cancelLastSameUrl,但如果两个请求完全相同,且同时发出,第二个请求返回第一个请求的请求结果,不会取消第一个请求', async () => {
450
+ const ajax = initAjax({})
451
+ const url = getRandomUrl()
452
+
453
+ // 发出请求,50ms返回结果,见__mocks__/axios.request
454
+ const req1 = ajax.get(url, {})({}, {cancelLastSameUrl: true})
455
+ const req2 = ajax.get(url, {})({}, {cancelLastSameUrl: true})
456
+
457
+ const res1 = await req1
458
+ const res2 = await req2
459
+ // mock的axios.request每次会返回一个随机数
460
+ expect(res1).toBe(res2)
461
+ })
462
+ })
463
+
464
+ describe('wx环境', () => {
465
+
466
+ test('两个请求', async () => {
467
+ global.wx = {
468
+ request: jest.fn((config: any) => {
469
+ setTimeout(() => {
470
+ config.success({
471
+ data: {}
472
+ }, 50)
473
+ })
474
+
475
+ return { abort: jest.fn() }
476
+ })
477
+ }
478
+
479
+ const originAbortController = global.AbortController
480
+
481
+ // 微信环境没有AbortController
482
+ Object.defineProperty(global, 'AbortController', {
483
+ get() { return undefined }
484
+ })
485
+
486
+ const ajax = initAjax({})
487
+ const url = getRandomUrl()
488
+
489
+ // 验证微信环境
490
+ expect(ajax.$options.adapter).not.toBeUndefined()
491
+ expect(typeof ajax.$options.adapter).toBe('function')
492
+ // 发出请求,50ms返回结果,见__mocks__/axios.request
493
+ // 两个请求URL和方法相同,第一个会取消第二个请求
494
+ const res1 = ajax.get(url, {})({a:1}, {cancelLastSameUrl: true})
495
+ const res2 = ajax.get(url, {})({}, {cancelLastSameUrl: true})
496
+
497
+ // mock的axios.request每次会返回一个随机数
498
+ await expect(res1).rejects.toHaveProperty('code', 'ERR_CANCELED')
499
+ await expect(res1).rejects.toHaveProperty('message', '请求被取消!')
500
+ await expect(res1).rejects.toHaveProperty('config')
501
+ await expect(res1).rejects.toHaveProperty('request')
502
+ await expect(res1).rejects.toHaveProperty('response')
503
+ // 第二个请求返回结果
504
+ await expect(res2).resolves.toHaveProperty('data', {})
505
+
506
+ delete global.wx
507
+ Object.defineProperty(global, 'AbortController', {
508
+ get() {
509
+ return originAbortController
510
+ }
511
+ })
512
+ })
513
+ test('三个请求', async () => {
514
+ global.wx = {
515
+ request: jest.fn((config: any) => {
516
+ setTimeout(() => {
517
+ config.success({
518
+ data: {}
519
+ }, 100)
520
+ })
521
+
522
+ return { abort: jest.fn() }
523
+ })
524
+ }
525
+
526
+ const originAbortController = global.AbortController
527
+
528
+ // 微信环境没有AbortController
529
+ Object.defineProperty(global, 'AbortController', {
530
+ get() { return undefined }
531
+ })
532
+
533
+ const ajax = initAjax({})
534
+ const url = getRandomUrl()
535
+
536
+ // 验证微信环境
537
+ expect(ajax.$options.adapter).not.toBeUndefined()
538
+ expect(typeof ajax.$options.adapter).toBe('function')
539
+ // 发出请求,50ms返回结果,见__mocks__/axios.request
540
+ // 两个请求URL和方法相同,第一个会取消第二个请求
541
+ const res1 = ajax.get(url, {})({a: 1}, {cancelLastSameUrl: true})
542
+ const res2 = ajax.get(url, {})({a: 2}, {cancelLastSameUrl: true})
543
+ const res3 = ajax.get(url, {})({a: 3}, {cancelLastSameUrl: true})
544
+
545
+ // mock的axios.request每次会返回一个随机数
546
+ await expect(res1).rejects.toHaveProperty('code', 'ERR_CANCELED')
547
+ await expect(res1).rejects.toHaveProperty('message', '请求被取消!')
548
+ await expect(res2).rejects.toHaveProperty('code', 'ERR_CANCELED')
549
+ await expect(res2).rejects.toHaveProperty('message', '请求被取消!')
550
+
551
+ // 第二个请求返回结果
552
+ await expect(res3).resolves.toHaveProperty('data', {})
553
+
554
+ delete global.wx
555
+ Object.defineProperty(global, 'AbortController', {
556
+ get() {
557
+ return originAbortController
558
+ }
559
+ })
560
+ })
561
+
562
+ test('使用cancelLastSameUrl,但如果两个请求完全相同,且同时发出,第二个请求返回第一个请求的请求结果,不会取消第一个请求', async () => {
563
+
564
+ global.wx = {
565
+ request: jest.fn((config: any) => {
566
+ setTimeout(() => {
567
+ config.success({
568
+ data: {}
569
+ }, 50)
570
+ })
571
+
572
+ return { abort: jest.fn() }
573
+ })
574
+ }
575
+
576
+ const originAbortController = global.AbortController
577
+
578
+ // 微信环境没有AbortController
579
+ Object.defineProperty(global, 'AbortController', {
580
+ get() { return undefined }
581
+ })
582
+
583
+ const ajax = initAjax({})
584
+ const url = getRandomUrl()
585
+
586
+ // 发出请求,50ms返回结果,见__mocks__/axios.request
587
+ const req1 = ajax.get(url, {})({}, {cancelLastSameUrl: true})
588
+ const req2 = ajax.get(url, {})({}, {cancelLastSameUrl: true})
589
+
590
+ const res1 = await req1
591
+ const res2 = await req2
592
+ // mock的axios.request每次会返回一个随机数
593
+ expect(res1).toBe(res2)
594
+
595
+ delete global.wx
596
+ Object.defineProperty(global, 'AbortController', {
597
+ get() {
598
+ return originAbortController
599
+ }
600
+ })
601
+ })
602
+ })
603
+ })
604
+
605
+ describe('mock配置测试', () => {
606
+ describe('web或node环境', () => {
607
+ test.each(methods)('基本功能', async (method) => {
608
+ const ajax = initAjax({})
609
+ const url = getRandomUrl()
610
+
611
+ const res1 = ajax[method](url, {})({a:1}, {mock: {mock:true}})
612
+ await expect(res1).resolves.toEqual({mock:true})
613
+ })
614
+ // 历史方法没有兼容该功能
615
+ // test.each(hisMethods)('基本功能(废弃的方法)', async (method) => {
616
+ // const ajax = initAjax({})
617
+ // const url = 'web/get/mocked/res/duplicate'
618
+
619
+ // const res1 = ajax[method](url, {mock: {mock:true}})({a:1})
620
+ // await expect(res1).resolves.toEqual({mock:true})
621
+ // })
622
+ })
623
+
624
+ describe('wx环境', () => {
625
+
626
+ test.each(methods)('基本功能', async (method) => {
627
+ global.wx = {
628
+ request: jest.fn()
629
+ }
630
+
631
+ const ajax = initAjax({})
632
+ const url = getRandomUrl()
633
+
634
+ expect(ajax.$options.adapter).not.toBeUndefined()
635
+ const res1 = ajax[method](url, {})({a:1}, {mock: {mock:true}})
636
+ await expect(res1).resolves.toEqual({mock:true})
637
+
638
+ delete global.wx
639
+ })
640
+ // 历史方法没有兼容该功能
641
+ // test.each(hisMethods)('基本功能(废弃方法)', async (method) => {
642
+ // global.wx = {
643
+ // request: jest.fn()
644
+ // }
645
+
646
+ // const ajax = initAjax({})
647
+ // const url = 'wx/get/mocked/res/duplicate'
648
+
649
+ // expect(ajax.$options.adapter).not.toBeUndefined()
650
+ // // 即将废弃的方法,配置只能在ajax[method]()的第二个参数
651
+ // const res1 = ajax[method](url, {mock: {mock:true}})({a:1})
652
+ // await expect(res1).resolves.toEqual({mock:true})
653
+
654
+ // delete global.wx
655
+ // })
656
+ })
657
+ })
@@ -0,0 +1,4 @@
1
+ jest.spyOn(console, 'log').mockReturnValue();
2
+ jest.spyOn(console, 'info').mockReturnValue();
3
+ jest.spyOn(console, 'warn').mockReturnValue();
4
+ jest.spyOn(console, 'error').mockReturnValue();