axios-miniprogram/test/request/dispatchRequest.test.ts

184 lines
3.8 KiB
TypeScript
Raw Normal View History

2023-05-04 10:39:52 +08:00
import { describe, test, expect, vi } from 'vitest'
import { asyncNext, mockAdapter } from 'scripts/test.utils'
2023-04-18 19:50:40 +08:00
import {
2023-05-04 10:39:52 +08:00
PLAIN_METHODS,
WITH_DATA_METHODS,
WITH_PARAMS_METHODS,
} from '@/constants/methods'
import { dispatchRequest } from '@/request/dispatchRequest'
2023-04-09 15:20:10 +08:00
2023-05-04 10:39:52 +08:00
import axios from '@/axios'
import _defaults from '@/defaults'
2023-04-21 18:09:32 +08:00
describe('src/request/dispatchRequest.ts', () => {
2023-05-04 10:39:52 +08:00
const defaults = {
..._defaults,
adapter: mockAdapter(),
baseURL: 'http://api.com',
method: 'get' as const,
headers: {},
}
test('应该抛出异常', () => {
expect(() => dispatchRequest({})).toThrowErrorMatchingInlineSnapshot(
'"[axios-miniprogram]: adapter 不是一个 function"',
)
expect(() =>
dispatchRequest({ adapter: mockAdapter() }),
).toThrowErrorMatchingInlineSnapshot(
'"[axios-miniprogram]: url 不是一个 string"',
)
expect(() =>
dispatchRequest({ adapter: mockAdapter(), url: '/' }),
).toThrowErrorMatchingInlineSnapshot(
'"[axios-miniprogram]: method 不是一个 string"',
)
expect(() =>
dispatchRequest({ adapter: mockAdapter(), url: '/', method: 'get' }),
).not.toThrowError()
})
test('坏的适配器应该抛出异常', () => {
expect(
dispatchRequest({
adapter: () => {
throw 'bad adapter'
},
url: '/',
method: 'get',
}).catch((e) => ({ ...e })),
).resolves.toMatchInlineSnapshot(`
{
"config": {
"adapter": [Function],
"headers": {},
2023-04-27 10:34:36 +08:00
"method": "get",
2023-04-25 15:06:19 +08:00
"url": "/",
},
"request": undefined,
"response": {
"config": {
"adapter": [Function],
"headers": {},
2023-04-27 10:34:36 +08:00
"method": "get",
2023-04-25 15:06:19 +08:00
"url": "/",
},
"data": undefined,
"headers": {},
"isFail": true,
"request": undefined,
"status": 400,
"statusText": "Bad Adapter",
},
}
2023-05-04 10:39:52 +08:00
`)
})
test('应该支持拉平请求头', () => {
const c = {
...defaults,
url: 'test',
headers: {
common: {
h1: 1,
},
get: {
h2: 2,
},
h3: 3,
},
}
dispatchRequest(c)
expect(c.headers).toEqual({
h1: 1,
h2: 2,
h3: 3,
})
})
test.each(WITH_DATA_METHODS)('%s 方法应该支持转换请求数据', (k) => {
const c = {
...defaults,
url: 'test',
method: k,
data: {},
transformRequest: () => ({ id: 1 }),
}
dispatchRequest(c)
expect(c.data).toEqual({ id: 1 })
})
test('不能带数据的请求方法应该删除数据', () => {
const c = {
...defaults,
url: 'test',
data: {},
transformRequest: () => ({ id: 1 }),
}
;[...PLAIN_METHODS, ...WITH_PARAMS_METHODS].forEach((k) => {
const s = { ...c, method: k }
dispatchRequest(s)
expect(s.data).toBeUndefined()
})
})
test('应该支持转换响应数据', async () => {
const c = {
...defaults,
url: 'test',
transformResponse: () => ({ result: 1 }),
}
const r = await dispatchRequest(c)
expect(r.data).toEqual({ result: 1 })
})
test('请求发送前取消请求应该抛出异常', async () => {
const cb = vi.fn()
const { cancel, token } = axios.CancelToken.source()
const c = {
...defaults,
url: 'test',
cancelToken: token,
}
cancel()
try {
dispatchRequest(c)
} catch (err) {
cb(err)
}
expect(cb).toBeCalled()
expect(axios.isCancel(cb.mock.calls[0][0])).toBeTruthy()
})
test('请求发送后取消请求应该抛出异常', async () => {
const cb = vi.fn()
const { cancel, token } = axios.CancelToken.source()
const c = {
...defaults,
url: 'test',
cancelToken: token,
}
const p = dispatchRequest(c).catch(cb)
await asyncNext()
expect(cb).not.toBeCalled()
cancel()
await p
expect(cb).toBeCalled()
expect(axios.isCancel(cb.mock.calls[0][0])).toBeTruthy()
})
})