-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: methodFactory第二个参数接收函数可以改配置;新增use
- Loading branch information
1 parent
6b0e1c8
commit 7a7fd00
Showing
3 changed files
with
86 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import axios, { AxiosRequestConfig } from 'axios'; | ||
import { AxiosRequestTemplate } from '../src'; | ||
import { mockAxiosResponse } from './utils'; | ||
|
||
jest.mock('axios'); | ||
const map = new Map<string, Function>(); | ||
const mockCreate = (/*config: AxiosRequestConfig*/) => { | ||
// console.log(config); | ||
return function (config) { | ||
const { url, cancelToken } = config; | ||
return new Promise((res, rej) => { | ||
map.set(cancelToken, rej); | ||
if (/^\/use/.test(url)) { | ||
setTimeout(() => | ||
res(mockAxiosResponse(config, { code: 200, data: config, msg: 'success' })), | ||
); | ||
return; | ||
} | ||
}); | ||
}; | ||
}; | ||
(axios.CancelToken.source as any).mockImplementation(() => { | ||
const token = Math.floor(Math.random() * 0xffffffffff).toString(16); | ||
return { | ||
token, | ||
cancel(msg?: string) { | ||
map.get(token)?.(msg); | ||
}, | ||
}; | ||
}); | ||
|
||
(axios as any).create.mockImplementation(mockCreate); | ||
describe('use', () => { | ||
const req = new AxiosRequestTemplate(); | ||
test('empty', async () => { | ||
const request = req.use({}); | ||
const res = await request<AxiosRequestConfig>({ url: '/use' }); | ||
expect(res.data.url).toBe('/use'); | ||
expect(res.data.method).toBe('get'); | ||
}); | ||
test('url', async () => { | ||
const post = req.use({ requestConfig: { url: '/use/', method: 'post' } }); | ||
const res = await post<AxiosRequestConfig>({ url: 'test' }); | ||
expect(res.data.url).toBe('/use/test'); | ||
expect(res.data.method).toBe('post'); | ||
const res2 = await post<AxiosRequestConfig>({ url: 'test2' }); | ||
expect(res2.data.url).toBe('/use/test2'); | ||
expect(res2.data.method).toBe('post'); | ||
}); | ||
test('methodFactory url', async () => { | ||
const post = req.methodFactory('post', (configs) => { | ||
configs.requestConfig.url = '/use/' + configs.requestConfig.url; | ||
}); | ||
const res = await post<AxiosRequestConfig>({ url: 'test' }); | ||
expect(res.data.url).toBe('/use/test'); | ||
expect(res.data.method).toBe('post'); | ||
const res2 = await post<AxiosRequestConfig>({ url: 'test2' }); | ||
expect(res2.data.url).toBe('/use/test2'); | ||
}); | ||
test('data', async () => { | ||
const post = req.use({ requestConfig: { url: '/use', method: 'post', data: { a: 1 } } }); | ||
const res = await post<AxiosRequestConfig>({ url: 'test' }); | ||
expect(res.data.data).toEqual({ a: 1 }); | ||
const res2 = await post<AxiosRequestConfig>({ url: 'test', data: { b: 2 } }); | ||
expect(res2.data.data).toEqual({ a: 1, b: 2 }); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters