2023-04-09 15:20:10 +08:00
|
|
|
import { describe, test, expect } from 'vitest';
|
|
|
|
import Axios from '@/core/Axios';
|
|
|
|
import { CancelToken, isCancel } from '@/core/cancel';
|
|
|
|
import { isAxiosError } from '@/core/createError';
|
|
|
|
import { createAdapter } from '@/adapter';
|
|
|
|
import axios from '@/axios';
|
2023-04-10 15:03:59 +08:00
|
|
|
import defaults from '@/defaults';
|
2023-04-20 21:49:26 +08:00
|
|
|
import { eachMethods } from 'scripts/test.utils';
|
2023-04-05 08:40:00 +08:00
|
|
|
|
|
|
|
describe('src/axios.ts', () => {
|
|
|
|
test('应该有这些静态属性', () => {
|
|
|
|
expect(axios.Axios).toBe(Axios);
|
|
|
|
expect(axios.CancelToken).toBe(CancelToken);
|
2023-04-09 15:20:10 +08:00
|
|
|
expect(axios.create).toBeTypeOf('function');
|
2023-04-05 08:40:00 +08:00
|
|
|
expect(axios.createAdapter).toBe(createAdapter);
|
2023-04-05 13:31:48 +08:00
|
|
|
expect(axios.isCancel).toBe(isCancel);
|
|
|
|
expect(axios.isAxiosError).toBe(isAxiosError);
|
2023-04-07 15:18:05 +08:00
|
|
|
});
|
2023-04-10 15:03:59 +08:00
|
|
|
|
|
|
|
test('应该可以创建实例', () => {
|
2023-04-10 22:53:15 +08:00
|
|
|
const instance = axios.create({
|
2023-04-10 15:03:59 +08:00
|
|
|
baseURL: 'http://api.com',
|
|
|
|
});
|
2023-04-10 18:53:21 +08:00
|
|
|
|
2023-04-10 22:53:15 +08:00
|
|
|
expect(instance.defaults).toEqual({
|
|
|
|
...defaults,
|
|
|
|
baseURL: 'http://api.com',
|
2023-04-10 18:53:21 +08:00
|
|
|
});
|
2023-04-10 22:53:15 +08:00
|
|
|
expect(instance.interceptors).toBeTypeOf('object');
|
|
|
|
expect(instance.getUri).toBeTypeOf('function');
|
|
|
|
expect(instance.fork).toBeTypeOf('function');
|
|
|
|
expect(instance.request).toBeTypeOf('function');
|
2023-04-10 15:03:59 +08:00
|
|
|
|
2023-04-20 21:49:26 +08:00
|
|
|
eachMethods((k) => {
|
2023-04-10 22:53:15 +08:00
|
|
|
expect(instance[k]).toBeTypeOf('function');
|
2023-04-10 15:03:59 +08:00
|
|
|
});
|
|
|
|
});
|
2023-04-05 08:40:00 +08:00
|
|
|
});
|