@lx-frontend/multi-ajax 0.2.1 → 0.2.3-beta

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 (36) hide show
  1. package/README.md +38 -0
  2. package/build/scripts/patch-axios-post-install.js +1 -6
  3. package/build/utils.js +12 -0
  4. package/build/webpackPlugin/webpack4-replace-plugin.js +51 -0
  5. package/lib/adapters/wx-adapter.js +6 -6
  6. package/lib/adapters/wx-adapter.js.map +1 -1
  7. package/package.json +1 -1
  8. package/src/adapters/wx-adapter.ts +6 -6
  9. package/coverage/coverage-summary.json +0 -12
  10. package/coverage/lcov-report/base.css +0 -224
  11. package/coverage/lcov-report/block-navigation.js +0 -87
  12. package/coverage/lcov-report/favicon.png +0 -0
  13. package/coverage/lcov-report/index.html +0 -131
  14. package/coverage/lcov-report/prettify.css +0 -1
  15. package/coverage/lcov-report/prettify.js +0 -2
  16. package/coverage/lcov-report/sort-arrow-sprite.png +0 -0
  17. package/coverage/lcov-report/sorter.js +0 -196
  18. package/coverage/lcov-report/src/adapters/index.html +0 -131
  19. package/coverage/lcov-report/src/adapters/index.ts.html +0 -100
  20. package/coverage/lcov-report/src/adapters/wx-adapter.ts.html +0 -436
  21. package/coverage/lcov-report/src/cancelLastSameRequest.ts.html +0 -211
  22. package/coverage/lcov-report/src/canceller.ts.html +0 -265
  23. package/coverage/lcov-report/src/compatibleMethods.ts.html +0 -301
  24. package/coverage/lcov-report/src/duplicateRequestPrevent.ts.html +0 -151
  25. package/coverage/lcov-report/src/helpers.ts.html +0 -319
  26. package/coverage/lcov-report/src/index.html +0 -221
  27. package/coverage/lcov-report/src/index.ts.html +0 -211
  28. package/coverage/lcov-report/src/methods.ts.html +0 -475
  29. package/coverage/lcov-report/src/utils.ts.html +0 -334
  30. package/coverage/lcov.info +0 -610
  31. package/tests/duplicateRequestPrevent.test.ts +0 -39
  32. package/tests/helpers.test.ts +0 -73
  33. package/tests/index.test.ts +0 -877
  34. package/tests/jest-setup.ts +0 -4
  35. package/tests/utils.test.ts +0 -68
  36. package/tsconfig.json +0 -17
@@ -1,877 +0,0 @@
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 = 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 = 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
- 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
- })
37
- }
38
-
39
- const methods: RequestMethods[] = ['get', 'post', 'head', 'patch', 'put', 'delete']
40
- const hisMethods: OldAjaxMethods[] = ['create', 'deletes', 'query', 'patchWay', 'putWay']
41
-
42
- const methodsStr = methods.join('|')
43
- const hisMethodsStr = hisMethods.join('|')
44
-
45
- describe('initAjax test', () => {
46
-
47
- test('初始化测试', () => {
48
- const ajax = initAjax({})
49
- expect(ajax.$options).toEqual({});
50
- expect(ajax.$engine).toBeDefined();
51
- expect(ajax.canceller).toBeDefined();
52
- // request, interceptors
53
- expect(ajax.request).not.toBeUndefined()
54
- expect(ajax.interceptors).not.toBeUndefined()
55
- })
56
-
57
- test.each(methods)('ajax方法检测', (method) => {
58
- const ajax = initAjax({})
59
- expect(typeof ajax[method]).toBe('function')
60
- })
61
-
62
- test.each(hisMethods)('ajax兼容方法检测', (method) => {
63
- const ajax = initAjax({})
64
- expect(typeof ajax[method]).toBe('function')
65
- })
66
-
67
- test.each([...methods, ...hisMethods])('ajax[method]调用应该返回函数', (method) => {
68
- const ajax = initAjax({})
69
- expect(typeof ajax[method](getRandomUrl(), {})).toBe('function')
70
- })
71
-
72
- test.each([...methods, ...hisMethods])('ajax[method]()返回的函数再调用应该返回Promise对象', (method) => {
73
- const ajax = initAjax({})
74
- expect(getType(ajax[method](getRandomUrl(), {})())).toBe('promise')
75
- })
76
-
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
-
96
- const ajax = initAjax({})
97
-
98
- // 发出请求
99
- const cancelKey = getRandomKey()
100
- const fetch = ajax[method](getRandomUrl())({}, {cancelKey})
101
- // 取消请求
102
- ajax.canceller[cancelKey]()
103
-
104
- // 模拟的axios直接返回了请求被取消,实际使用可能不是这样
105
- await expect(fetch).rejects.toMatch('请求被取消')
106
- })
107
-
108
- })
109
-
110
- describe('wx环境', () => {
111
- beforeAll(() => {
112
- global.wx = {
113
- request: jest.fn((config: any) => {
114
- setTimeout(() => {
115
- config.success({
116
- data: {}
117
- }, 50)
118
- })
119
-
120
- return { abort: jest.fn() }
121
- })
122
- }
123
- undoAbortController()
124
- })
125
-
126
- afterAll(() => {
127
- delete global.wx
128
- restoreAbortController()
129
- })
130
-
131
- test.each(methods)(methodsStr, async (method) => {
132
-
133
- const ajax = initAjax({})
134
-
135
- // 发出请求
136
- const cancelKey = getRandomKey()
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')
143
- })
144
- })
145
-
146
- })
147
-
148
- describe('多个具有相同cancelKey,但不同参数的请求发出并取消测试', () => {
149
-
150
- describe('web或node环境', () => {
151
-
152
- test.each(methods)(methodsStr, async (method) => {
153
-
154
- const ajax = initAjax({})
155
-
156
- // 发出请求
157
- const cancelKey = getRandomKey()
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})
162
- // 取消请求
163
- ajax.canceller[cancelKey]()
164
-
165
- // 请求都应该被取消
166
- await expect(fetchA).rejects.toMatch('请求被取消')
167
- await expect(fetchB).rejects.toMatch('请求被取消')
168
- await expect(fetchC).rejects.toMatch('请求被取消')
169
- })
170
-
171
- })
172
-
173
- describe('wx环境', () => {
174
- beforeAll(() => {
175
- global.wx = {
176
- request: jest.fn((config: any) => {
177
- setTimeout(() => {
178
- config.success({
179
- data: {}
180
- }, 50)
181
- })
182
-
183
- return { abort: jest.fn() }
184
- })
185
- }
186
-
187
- undoAbortController()
188
- })
189
-
190
- afterAll(() => {
191
- delete global.wx
192
- restoreAbortController()
193
- })
194
-
195
- test.each(methods)(methodsStr, async (method) => {
196
-
197
- const ajax = initAjax({})
198
-
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) => {
219
-
220
- const ajax = initAjax({})
221
-
222
- // 发出请求
223
- const cancelKey = getRandomKey()
224
- // 请求参数不同,且交替发出取消请求,最后一个没有被取消的请求应该正常返回
225
- const fetchA = ajax[method](getRandomUrl())({a:1}, {cancelKey})
226
- ajax.canceller[cancelKey]()
227
- const fetchB = ajax[method](getRandomUrl())({a:2}, {cancelKey})
228
- ajax.canceller[cancelKey]()
229
- const fetchC = ajax[method](getRandomUrl())({a:3}, {cancelKey})
230
- // 取消请求
231
-
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
- })
249
-
250
- return { abort: jest.fn() }
251
- })
252
- }
253
- undoAbortController()
254
- })
255
-
256
- afterAll(() => {
257
- delete global.wx
258
- restoreAbortController()
259
- })
260
-
261
- test.each(methods)(methodsStr, async (method) => {
262
-
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})
289
- const url = getRandomUrl()
290
- // 并行发出请求
291
- const req1 = ajax[method](url)()
292
- const req2 = ajax[method](url)()
293
- const res1 = await req1
294
- const res2 = await req2
295
-
296
- // mock的axios.request每次会返回一个随机数,如果复用上一个请求,两者应该相等
297
- expect(res1).toBe(res2)
298
- })
299
-
300
- test.each(hisMethods)(hisMethodsStr, async (method) => {
301
- const ajax = initAjax({reUseDuration: 300})
302
- const url = getRandomUrl()
303
- // 并行发出请求,旧的方法配置只能在ajax[method]的第二个参数写
304
- const req1 = ajax[method](url)()
305
- const req2 = ajax[method](url)()
306
- const res1 = await req1
307
- const res2 = await req2
308
-
309
- // mock的axios.request每次会返回一个随机数,如果复用上一个请求,两者应该相等
310
- expect(res1).toBe(res2)
311
- })
312
-
313
- })
314
-
315
- describe('wx环境', () => {
316
-
317
- beforeAll(() => {
318
- global.wx = {
319
- request: jest.fn((config: any) => {
320
- setTimeout(() => {
321
- config.success({
322
- data: {a: 1}
323
- }, 50)
324
- })
325
- })
326
- }
327
-
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
339
- const url = getRandomUrl()
340
- // 并行发出请求
341
- const req1 = ajax[method](url)()
342
- const req2 = ajax[method](url)()
343
- const res1 = await req1
344
- const res2 = await req2
345
-
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
- })
365
-
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)
388
- })
389
-
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
- })
408
-
409
- })
410
-
411
- describe('wx环境', () => {
412
-
413
- beforeAll(() => {
414
- global.wx = {
415
- request: jest.fn((config: any) => {
416
- setTimeout(() => {
417
- config.success({
418
- data: Math.random()
419
- }, 50)
420
- })
421
- })
422
- }
423
-
424
- undoAbortController()
425
- })
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})
435
- const url = getRandomUrl()
436
-
437
- expect(ajax.$options.adapter).not.toBeUndefined() // 证明是微信环境
438
-
439
- const req1 = ajax[method](url)()
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
- })
453
-
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)
475
- })
476
- })
477
-
478
- })
479
-
480
- describe('触发间隔小于300ms,但第二个请求是在第一个请求返回后触发,依然会触发二次网络请求', () => {
481
-
482
- describe('web或node环境', () => {
483
-
484
- test.each(methods)(methodsStr, async (method) => {
485
- const ajax = initAjax({})
486
-
487
- const url = getRandomUrl()
488
- // 发出请求,50ms返回结果,见__mocks__/axios.request
489
- const res1 = await ajax[method](url)()
490
- const res2 = await ajax[method](url)()
491
-
492
- // mock的axios.request每次会返回一个随机数
493
- expect(res1).not.toBe(res2)
494
- expect(res1).not.toBeNaN()
495
- expect(res2).not.toBeNaN()
496
- })
497
-
498
- test.each(hisMethods)(hisMethodsStr, async (method) => {
499
- const ajax = initAjax({})
500
- const url = getRandomUrl()
501
-
502
- // 发出请求,50ms返回结果,见__mocks__/axios.request
503
- const res1 = await ajax[method](url)()
504
- const res2 = await ajax[method](url)()
505
-
506
- // mock的axios.request每次会返回一个随机数
507
- expect(res1).not.toBe(res2)
508
- expect(res1).not.toBeNaN()
509
- expect(res2).not.toBeNaN()
510
- })
511
-
512
- })
513
-
514
- describe('wx环境', () => {
515
- beforeAll(() => {
516
- global.wx = {
517
- request: jest.fn((config: any) => {
518
- setTimeout(() => {
519
- config.success({
520
- data: {a: 1}
521
- }, 50)
522
- })
523
- })
524
- }
525
- undoAbortController()
526
- })
527
-
528
- afterAll(() => {
529
- delete global.wx
530
- restoreAbortController()
531
- })
532
-
533
- test.each(methods)(methodsStr, async (method) => {
534
- const ajax = initAjax({})
535
-
536
- const url = getRandomUrl()
537
- // 发出请求,50ms返回结果,见__mocks__/axios.request
538
- const res1 = await ajax[method](url)()
539
- const res2 = await ajax[method](url)()
540
-
541
- expect(res1).not.toBe(res2)
542
- expect(res1.data).toEqual({a:1})
543
- expect(res2.data).toEqual({a:1})
544
- expect(res1.config.url).toBe(url)
545
- expect(res2.config.url).toBe(url)
546
-
547
- })
548
-
549
- test.each(hisMethods)(hisMethodsStr, async (method) => {
550
-
551
- const ajax = initAjax({})
552
- const url = getRandomUrl()
553
-
554
- // 发出请求,50ms返回结果,见__mocks__/axios.request
555
- const res1 = await ajax[method](url)()
556
- const res2 = await ajax[method](url)()
557
-
558
- expect(res1).not.toBe(res2)
559
- expect(res1.data).toEqual({a:1})
560
- expect(res2.data).toEqual({a:1})
561
- expect(res1.config.url).toBe(url)
562
- expect(res2.config.url).toBe(url)
563
- })
564
- })
565
-
566
- })
567
-
568
- describe('validateStatus配置测试', () => {
569
-
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
- })
585
- })
586
- }
587
-
588
- undoAbortController()
589
- })
590
-
591
- afterAll(() => {
592
- delete global.wx
593
- restoreAbortController()
594
- })
595
-
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
- }
604
- })
605
- const fetch = ajax[method](getRandomUrl())
606
-
607
- await expect(fetch()).rejects.toHaveProperty('message', 'Request failed with status code 400')
608
- })
609
-
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
- })
623
- })
624
- })
625
-
626
- describe('发送请求异常测试', () => {
627
-
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
- })
642
- })
643
- }
644
-
645
- undoAbortController()
646
- })
647
-
648
- afterAll(() => {
649
- delete global.wx
650
- restoreAbortController()
651
- })
652
-
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
- })
665
- })
666
-
667
- })
668
-
669
- describe('cancelLastSameUrl为true,第二个相同URL的get请求,会将第一个get请求cancel掉', () => {
670
-
671
- describe('web和node环境', () => {
672
-
673
- test('两个请求', async () => {
674
- const ajax = initAjax({})
675
- const url = getRandomUrl()
676
- // 发出请求,50ms返回结果,见__mocks__/axios.request
677
- // 两个请求URL和方法相同,第一个会取消第二个请求
678
- const res1 = ajax.get(url, {})({a:1}, {cancelLastSameUrl: true})
679
- const res2 = ajax.get(url, {})({}, {cancelLastSameUrl: true})
680
-
681
- // mock的axios.request每次会返回一个随机数
682
- await expect(res1).rejects.toMatch('请求被取消')
683
- await expect(res2).resolves.not.toBeNaN()
684
- })
685
-
686
- test('连续三个请求', async () => {
687
- const ajax = initAjax({})
688
- const url = getRandomUrl()
689
- // 发出请求,50ms返回结果,见__mocks__/axios.request
690
- // 两个请求URL和方法相同,第一个会取消第二个请求
691
- const res1 = ajax.get(url, {})({a: 1}, {cancelLastSameUrl: true})
692
- const res2 = ajax.get(url, {})({a: 2}, {cancelLastSameUrl: true})
693
- const res3 = ajax.get(url, {})({a: 3}, {cancelLastSameUrl: true})
694
-
695
- // mock的axios.request每次会返回一个随机数
696
- await expect(res1).rejects.toMatch('请求被取消')
697
- await expect(res2).rejects.toMatch('请求被取消')
698
- await expect(res3).resolves.not.toBeNaN()
699
- })
700
-
701
- test('使用cancelLastSameUrl,但如果两个请求完全相同,且同时发出,第二个请求返回第一个请求的请求结果,不会取消第一个请求', async () => {
702
- const ajax = initAjax({})
703
- const url = getRandomUrl()
704
-
705
- // 发出请求,50ms返回结果,见__mocks__/axios.request
706
- const req1 = ajax.get(url, {})({}, {cancelLastSameUrl: true})
707
- const req2 = ajax.get(url, {})({}, {cancelLastSameUrl: true})
708
-
709
- const res1 = await req1
710
- const res2 = await req2
711
- // mock的axios.request每次会返回一个随机数
712
- expect(res1).toBe(res2)
713
- })
714
- })
715
-
716
- describe('wx环境', () => {
717
-
718
- beforeAll(() => {
719
- undoAbortController()
720
- })
721
-
722
- afterAll(() => {
723
- delete global.wx
724
- restoreAbortController()
725
- })
726
-
727
- test('两个请求', async () => {
728
- global.wx = {
729
- request: jest.fn((config: any) => {
730
- setTimeout(() => {
731
- config.success({
732
- data: {}
733
- }, 50)
734
- })
735
-
736
- return { abort: jest.fn() }
737
- })
738
- }
739
-
740
- const ajax = initAjax({})
741
- const url = getRandomUrl()
742
-
743
- // 验证微信环境
744
- expect(ajax.$options.adapter).not.toBeUndefined()
745
- expect(typeof ajax.$options.adapter).toBe('function')
746
- // 发出请求,50ms返回结果,见__mocks__/axios.request
747
- // 两个请求URL和方法相同,第一个会取消第二个请求
748
- const res1 = ajax.get(url, {})({a:1}, {cancelLastSameUrl: true})
749
- const res2 = ajax.get(url, {})({}, {cancelLastSameUrl: true})
750
-
751
- // mock的axios.request每次会返回一个随机数
752
- await expect(res1).rejects.toHaveProperty('code', 'ERR_CANCELED')
753
- await expect(res1).rejects.toHaveProperty('message', '请求被取消!')
754
- await expect(res1).rejects.toHaveProperty('config')
755
- await expect(res1).rejects.toHaveProperty('request')
756
- await expect(res1).rejects.toHaveProperty('response')
757
- // 第二个请求返回结果
758
- await expect(res2).resolves.toHaveProperty('data', {})
759
- })
760
-
761
- test('三个请求', async () => {
762
- global.wx = {
763
- request: jest.fn((config: any) => {
764
- setTimeout(() => {
765
- config.success({
766
- data: {}
767
- }, 100)
768
- })
769
-
770
- return { abort: jest.fn() }
771
- })
772
- }
773
-
774
- const ajax = initAjax({})
775
- const url = getRandomUrl()
776
-
777
- // 验证微信环境
778
- expect(ajax.$options.adapter).not.toBeUndefined()
779
- expect(typeof ajax.$options.adapter).toBe('function')
780
- // 发出请求,50ms返回结果,见__mocks__/axios.request
781
- // 两个请求URL和方法相同,第一个会取消第二个请求
782
- const res1 = ajax.get(url, {})({a: 1}, {cancelLastSameUrl: true})
783
- const res2 = ajax.get(url, {})({a: 2}, {cancelLastSameUrl: true})
784
- const res3 = ajax.get(url, {})({a: 3}, {cancelLastSameUrl: true})
785
-
786
- // mock的axios.request每次会返回一个随机数
787
- await expect(res1).rejects.toHaveProperty('code', 'ERR_CANCELED')
788
- await expect(res1).rejects.toHaveProperty('message', '请求被取消!')
789
- await expect(res2).rejects.toHaveProperty('code', 'ERR_CANCELED')
790
- await expect(res2).rejects.toHaveProperty('message', '请求被取消!')
791
-
792
- // 第二个请求返回结果
793
- await expect(res3).resolves.toHaveProperty('data', {})
794
- })
795
-
796
- test('使用cancelLastSameUrl,但如果两个请求完全相同,且同时发出,第二个请求返回第一个请求的请求结果,不会取消第一个请求', async () => {
797
-
798
- global.wx = {
799
- request: jest.fn((config: any) => {
800
- setTimeout(() => {
801
- config.success({
802
- data: {}
803
- }, 50)
804
- })
805
-
806
- return { abort: jest.fn() }
807
- })
808
- }
809
- const ajax = initAjax({})
810
- const url = getRandomUrl()
811
-
812
- // 发出请求,50ms返回结果,见__mocks__/axios.request
813
- const req1 = ajax.get(url, {})({}, {cancelLastSameUrl: true})
814
- const req2 = ajax.get(url, {})({}, {cancelLastSameUrl: true})
815
-
816
- const res1 = await req1
817
- const res2 = await req2
818
- // mock的axios.request每次会返回一个随机数
819
- expect(res1).toBe(res2)
820
- })
821
- })
822
- })
823
-
824
- describe('mock配置测试', () => {
825
- describe('web或node环境', () => {
826
- test.each(methods)('基本功能', async (method) => {
827
- const ajax = initAjax({})
828
- const url = getRandomUrl()
829
-
830
- const res1 = ajax[method](url, {})({a:1}, {mock: {mock:true}})
831
- await expect(res1).resolves.toEqual({mock:true})
832
- })
833
- // 历史方法没有兼容该功能
834
- // test.each(hisMethods)('基本功能(废弃的方法)', async (method) => {
835
- // const ajax = initAjax({})
836
- // const url = 'web/get/mocked/res/duplicate'
837
-
838
- // const res1 = ajax[method](url, {mock: {mock:true}})({a:1})
839
- // await expect(res1).resolves.toEqual({mock:true})
840
- // })
841
- })
842
-
843
- describe('wx环境', () => {
844
-
845
- beforeAll(() => {
846
- global.wx = {
847
- request: jest.fn()
848
- }
849
- undoAbortController()
850
- })
851
-
852
- afterAll(() => {
853
- delete global.wx
854
- restoreAbortController()
855
- })
856
-
857
- test.each(methods)('基本功能', async (method) => {
858
-
859
- const ajax = initAjax({})
860
- const url = getRandomUrl()
861
-
862
- expect(ajax.$options.adapter).not.toBeUndefined()
863
- const res1 = ajax[method](url, {})({a:1}, {mock: {mock:true}})
864
- await expect(res1).resolves.toEqual({mock:true})
865
- })
866
- // 历史方法没有兼容该功能
867
- // test.each(hisMethods)('基本功能(废弃方法)', async (method) => {
868
- // const ajax = initAjax({})
869
- // const url = 'wx/get/mocked/res/duplicate'
870
-
871
- // expect(ajax.$options.adapter).not.toBeUndefined()
872
- // // 即将废弃的方法,配置只能在ajax[method]()的第二个参数
873
- // const res1 = ajax[method](url, {mock: {mock:true}})({a:1})
874
- // await expect(res1).resolves.toEqual({mock:true})
875
- // })
876
- })
877
- })