2023-04-02 23:27:45 +08:00
|
|
|
import { describe, test, expect } from 'vitest';
|
2023-04-03 21:03:33 +08:00
|
|
|
import { mockAdapter, mockAdapterError } from 'scripts/test.utils';
|
2023-04-09 15:20:10 +08:00
|
|
|
import axios from '@/axios';
|
2023-04-02 23:27:45 +08:00
|
|
|
|
|
|
|
describe('src/axios.ts', () => {
|
|
|
|
test('应该处理成功和失败', () => {
|
|
|
|
axios({
|
2023-04-03 21:03:33 +08:00
|
|
|
adapter: mockAdapter({
|
2023-04-02 23:27:45 +08:00
|
|
|
headers: { type: 'json' },
|
|
|
|
data: { v1: 1 },
|
|
|
|
before: (config) => {
|
2023-04-17 21:31:03 +08:00
|
|
|
expect(config.url).toBe('http://api.com/test/1');
|
2023-04-02 23:27:45 +08:00
|
|
|
},
|
|
|
|
}),
|
|
|
|
baseURL: 'http://api.com',
|
2023-04-03 21:03:33 +08:00
|
|
|
url: 'test/:id',
|
2023-04-02 23:27:45 +08:00
|
|
|
params: {
|
|
|
|
id: 1,
|
|
|
|
},
|
2023-04-03 21:03:33 +08:00
|
|
|
}).then((res) => {
|
|
|
|
expect(res.headers).toEqual({ type: 'json' });
|
|
|
|
expect(res.data).toEqual({ v1: 1 });
|
2023-04-02 23:27:45 +08:00
|
|
|
});
|
|
|
|
|
2023-04-03 21:03:33 +08:00
|
|
|
axios('test/:id', {
|
|
|
|
adapter: mockAdapterError({
|
2023-04-02 23:27:45 +08:00
|
|
|
headers: { type: 'json' },
|
|
|
|
data: { v1: 1 },
|
|
|
|
before: (config) => {
|
2023-04-03 21:03:33 +08:00
|
|
|
expect(config.url).toBe('http://api.com/test/1');
|
2023-04-02 23:27:45 +08:00
|
|
|
},
|
|
|
|
}),
|
|
|
|
baseURL: 'http://api.com',
|
|
|
|
data: {
|
|
|
|
id: 1,
|
|
|
|
},
|
2023-04-03 21:03:33 +08:00
|
|
|
}).catch((err) => {
|
|
|
|
expect(err.response.headers).toEqual({ type: 'json' });
|
|
|
|
expect(err.response.data).toEqual({ v1: 1 });
|
2023-04-02 23:27:45 +08:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|