test: 增加测试覆盖率
parent
d57b82c74e
commit
4ba1249146
|
@ -32,7 +32,6 @@ function tryToggleProgressUpdate(
|
||||||
progressUpdate(onDownloadProgress);
|
progressUpdate(onDownloadProgress);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
default:
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -38,6 +38,9 @@ describe('src/adapter.ts', () => {
|
||||||
const a = createAdapter(p);
|
const a = createAdapter(p);
|
||||||
const r = {
|
const r = {
|
||||||
type: 'request' as const,
|
type: 'request' as const,
|
||||||
|
headers: {
|
||||||
|
Accept: 'application/json, text/plain, */*',
|
||||||
|
},
|
||||||
url: 'test',
|
url: 'test',
|
||||||
method: 'GET' as const,
|
method: 'GET' as const,
|
||||||
success: noop,
|
success: noop,
|
||||||
|
@ -45,31 +48,159 @@ describe('src/adapter.ts', () => {
|
||||||
};
|
};
|
||||||
const u = {
|
const u = {
|
||||||
type: 'upload' as const,
|
type: 'upload' as const,
|
||||||
|
headers: {
|
||||||
|
Accept: 'application/json, text/plain, */*',
|
||||||
|
},
|
||||||
url: 'test',
|
url: 'test',
|
||||||
method: 'POST' as const,
|
method: 'POST' as const,
|
||||||
data: {},
|
data: {
|
||||||
|
name: 'file',
|
||||||
|
filePath: '/path/file',
|
||||||
|
user: 'test',
|
||||||
|
id: 1,
|
||||||
|
},
|
||||||
success: noop,
|
success: noop,
|
||||||
fail: noop,
|
fail: noop,
|
||||||
};
|
};
|
||||||
const d = {
|
const d = {
|
||||||
type: 'download' as const,
|
type: 'download' as const,
|
||||||
|
headers: {
|
||||||
|
Accept: 'application/json, text/plain, */*',
|
||||||
|
},
|
||||||
url: 'test',
|
url: 'test',
|
||||||
method: 'GET' as const,
|
method: 'GET' as const,
|
||||||
params: { filePath: '' },
|
params: {
|
||||||
|
filePath: '/path/file',
|
||||||
|
},
|
||||||
success: noop,
|
success: noop,
|
||||||
fail: noop,
|
fail: noop,
|
||||||
};
|
};
|
||||||
|
|
||||||
expect(p.request).not.toBeCalled();
|
expect(p.request).not.toBeCalled();
|
||||||
a(r);
|
a(r);
|
||||||
expect(p.request).toBeCalled();
|
expect(p.request.mock.calls[0][0]).toMatchInlineSnapshot(`
|
||||||
|
{
|
||||||
|
"fail": [Function],
|
||||||
|
"header": {
|
||||||
|
"Accept": "application/json, text/plain, */*",
|
||||||
|
},
|
||||||
|
"headers": {
|
||||||
|
"Accept": "application/json, text/plain, */*",
|
||||||
|
},
|
||||||
|
"method": "GET",
|
||||||
|
"success": [Function],
|
||||||
|
"type": "request",
|
||||||
|
"url": "test",
|
||||||
|
}
|
||||||
|
`);
|
||||||
|
|
||||||
expect(p.upload).not.toBeCalled();
|
expect(p.upload).not.toBeCalled();
|
||||||
a(u);
|
a(u);
|
||||||
expect(p.upload).toBeCalled();
|
expect(p.upload.mock.calls[0][0]).toMatchInlineSnapshot(`
|
||||||
|
{
|
||||||
|
"data": {
|
||||||
|
"filePath": "/path/file",
|
||||||
|
"id": 1,
|
||||||
|
"name": "file",
|
||||||
|
"user": "test",
|
||||||
|
},
|
||||||
|
"fail": [Function],
|
||||||
|
"fileName": "file",
|
||||||
|
"filePath": "/path/file",
|
||||||
|
"fileType": undefined,
|
||||||
|
"formData": {
|
||||||
|
"id": 1,
|
||||||
|
"user": "test",
|
||||||
|
},
|
||||||
|
"header": {
|
||||||
|
"Accept": "application/json, text/plain, */*",
|
||||||
|
},
|
||||||
|
"headers": {
|
||||||
|
"Accept": "application/json, text/plain, */*",
|
||||||
|
},
|
||||||
|
"method": "POST",
|
||||||
|
"name": "file",
|
||||||
|
"success": [Function],
|
||||||
|
"type": "upload",
|
||||||
|
"url": "test",
|
||||||
|
}
|
||||||
|
`);
|
||||||
|
|
||||||
expect(p.download).not.toBeCalled();
|
expect(p.download).not.toBeCalled();
|
||||||
a(d);
|
a(d);
|
||||||
expect(p.download).toBeCalled();
|
expect(p.download.mock.calls[0][0]).toMatchInlineSnapshot(`
|
||||||
|
{
|
||||||
|
"fail": [Function],
|
||||||
|
"filePath": "/path/file",
|
||||||
|
"header": {
|
||||||
|
"Accept": "application/json, text/plain, */*",
|
||||||
|
},
|
||||||
|
"headers": {
|
||||||
|
"Accept": "application/json, text/plain, */*",
|
||||||
|
},
|
||||||
|
"method": "GET",
|
||||||
|
"params": {
|
||||||
|
"filePath": "/path/file",
|
||||||
|
},
|
||||||
|
"success": [Function],
|
||||||
|
"type": "download",
|
||||||
|
"url": "test",
|
||||||
|
}
|
||||||
|
`);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('应该支持转换下载数据', () => {
|
||||||
|
const p1 = {
|
||||||
|
request: vi.fn(),
|
||||||
|
upload: vi.fn(),
|
||||||
|
download: vi.fn((config) => {
|
||||||
|
config.success({
|
||||||
|
filePath: config.filePath,
|
||||||
|
tempFilePath: '/path/temp/file',
|
||||||
|
});
|
||||||
|
}),
|
||||||
|
};
|
||||||
|
const p2 = {
|
||||||
|
request: vi.fn(),
|
||||||
|
upload: vi.fn(),
|
||||||
|
download: vi.fn((config) => {
|
||||||
|
config.success({
|
||||||
|
filePath: config.filePath,
|
||||||
|
apFilePath: '/path/temp/file',
|
||||||
|
});
|
||||||
|
}),
|
||||||
|
};
|
||||||
|
|
||||||
|
createAdapter(p1)({
|
||||||
|
type: 'download' as const,
|
||||||
|
url: 'test',
|
||||||
|
method: 'GET' as const,
|
||||||
|
params: { filePath: '/test/file' },
|
||||||
|
success: (response: any) => {
|
||||||
|
expect(response.data).toMatchInlineSnapshot(`
|
||||||
|
{
|
||||||
|
"filePath": "/test/file",
|
||||||
|
"tempFilePath": "/path/temp/file",
|
||||||
|
}
|
||||||
|
`);
|
||||||
|
},
|
||||||
|
fail: noop,
|
||||||
|
});
|
||||||
|
|
||||||
|
createAdapter(p2)({
|
||||||
|
type: 'download' as const,
|
||||||
|
url: 'test',
|
||||||
|
method: 'GET' as const,
|
||||||
|
params: { filePath: '/test/file' },
|
||||||
|
success: (response: any) => {
|
||||||
|
expect(response.data).toMatchInlineSnapshot(`
|
||||||
|
{
|
||||||
|
"filePath": "/test/file",
|
||||||
|
"tempFilePath": "/path/temp/file",
|
||||||
|
}
|
||||||
|
`);
|
||||||
|
},
|
||||||
|
fail: noop,
|
||||||
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
|
@ -17,12 +17,19 @@ describe('src/axios.ts', () => {
|
||||||
});
|
});
|
||||||
|
|
||||||
test('应该可以创建实例', () => {
|
test('应该可以创建实例', () => {
|
||||||
const i1 = axios.create();
|
|
||||||
const i2 = axios.create({
|
const i2 = axios.create({
|
||||||
baseURL: 'http://api.com',
|
baseURL: 'http://api.com',
|
||||||
});
|
});
|
||||||
expect(i1.defaults).toEqual(defaults);
|
|
||||||
expect(i2.defaults).toEqual({ ...defaults, baseURL: 'http://api.com' });
|
expect(i2.defaults).toEqual({ ...defaults, baseURL: 'http://api.com' });
|
||||||
|
expect(i2.interceptors).toBeTypeOf('object');
|
||||||
|
expect(i2.getUri).toBeTypeOf('function');
|
||||||
|
expect(i2.fork).toBeTypeOf('function');
|
||||||
|
expect(i2.request).toBeTypeOf('function');
|
||||||
|
|
||||||
|
[...Axios.as, ...Axios.asp, ...Axios.asd].forEach((k) => {
|
||||||
|
expect(i2[k]).toBeTypeOf('function');
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
test('创建的实例应该有这些实例属性及方法', () => {
|
test('创建的实例应该有这些实例属性及方法', () => {
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
import { describe, test, expect, beforeAll, afterAll } from 'vitest';
|
import { describe, test, expect, beforeAll, afterAll } from 'vitest';
|
||||||
import { mockAdapter } from 'scripts/test.utils';
|
import { mockAdapter } from 'scripts/test.utils';
|
||||||
import Axios from '@/core/Axios';
|
import Axios from '@/core/Axios';
|
||||||
|
import AxiosDomain from '@/core/AxiosDomain';
|
||||||
import defaults from '@/defaults';
|
import defaults from '@/defaults';
|
||||||
import axios from '@/axios';
|
import axios from '@/axios';
|
||||||
|
|
||||||
|
@ -121,5 +122,6 @@ describe('src/axios.ts', () => {
|
||||||
baseURL: 'test',
|
baseURL: 'test',
|
||||||
});
|
});
|
||||||
expect(a.defaults.baseURL).toBe('http://api.com/test');
|
expect(a.defaults.baseURL).toBe('http://api.com/test');
|
||||||
|
expect(a instanceof AxiosDomain).toBeTruthy();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
import { describe, test, expect } from 'vitest';
|
import { describe, test, expect, vi } from 'vitest';
|
||||||
import { mockAdapter } from 'scripts/test.utils';
|
import { mockAdapter } from 'scripts/test.utils';
|
||||||
import Axios from '@/core/Axios';
|
import Axios from '@/core/Axios';
|
||||||
import AxiosDomain from '@/core/AxiosDomain';
|
import AxiosDomain from '@/core/AxiosDomain';
|
||||||
|
@ -116,18 +116,201 @@ describe('src/core/Axios.ts', () => {
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test('应该支持添加/移除请求拦截器', async () => {
|
||||||
|
const cb = vi.fn((v) => v);
|
||||||
|
|
||||||
|
const id = axios.interceptors.request.use(cb);
|
||||||
|
await axios.request('/test', {
|
||||||
|
adapter: mockAdapter(),
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(cb.mock.calls.length).toBe(1);
|
||||||
|
|
||||||
|
await axios.request('/test', {
|
||||||
|
adapter: mockAdapter(),
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(cb.mock.calls.length).toBe(2);
|
||||||
|
|
||||||
|
axios.interceptors.request.eject(id);
|
||||||
|
await axios.request('/test', {
|
||||||
|
adapter: mockAdapter(),
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(cb.mock.calls.length).toBe(2);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('添加多个请求拦截器时应该按添加顺序从后往前依次执行', () => {
|
||||||
|
const axios = new Axios();
|
||||||
|
|
||||||
|
const cb1 = vi.fn((v) => {
|
||||||
|
expect(v.data.index).toBe(2);
|
||||||
|
v.data.index = 3;
|
||||||
|
return v;
|
||||||
|
});
|
||||||
|
const cb2 = vi.fn((v) => {
|
||||||
|
expect(v.data.index).toBe(1);
|
||||||
|
v.data.index = 2;
|
||||||
|
return v;
|
||||||
|
});
|
||||||
|
const cb3 = vi.fn((v) => {
|
||||||
|
expect(v.data.index).toBe(0);
|
||||||
|
v.data.index = 1;
|
||||||
|
return v;
|
||||||
|
});
|
||||||
|
|
||||||
|
axios.interceptors.request.use(cb1);
|
||||||
|
axios.interceptors.request.use(cb2);
|
||||||
|
axios.interceptors.request.use(cb3);
|
||||||
|
|
||||||
|
axios.request('/test', {
|
||||||
|
adapter: (config) => {
|
||||||
|
expect(config.data!.index).toBe(3);
|
||||||
|
},
|
||||||
|
data: {
|
||||||
|
index: 0,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
test('请求拦截器应该支持抛出异常', async () => {
|
||||||
|
const axios = new Axios();
|
||||||
|
const c = { adapter: vi.fn(), url: 'test' };
|
||||||
|
const body = (v: any) => {
|
||||||
|
throw { ...v, throw: true };
|
||||||
|
};
|
||||||
|
const res1 = vi.fn(body);
|
||||||
|
const rej1 = vi.fn(body);
|
||||||
|
const res2 = vi.fn(body);
|
||||||
|
const rej2 = vi.fn(body);
|
||||||
|
|
||||||
|
axios.interceptors.request.use(res1, rej1);
|
||||||
|
axios.interceptors.request.use(res2, rej2);
|
||||||
|
|
||||||
|
try {
|
||||||
|
await axios.request(c);
|
||||||
|
} catch (err) {
|
||||||
|
expect(err).toEqual({ ...c, throw: true });
|
||||||
|
}
|
||||||
|
|
||||||
|
expect(c.adapter).not.toBeCalled();
|
||||||
|
expect(res1).not.toBeCalled();
|
||||||
|
expect(rej1).toBeCalled();
|
||||||
|
expect(rej1.mock.calls[0][0]).toEqual({ ...c, throw: true });
|
||||||
|
expect(res2).toBeCalled();
|
||||||
|
expect(res2.mock.calls[0][0]).toEqual(c);
|
||||||
|
expect(rej2).not.toBeCalled();
|
||||||
|
});
|
||||||
|
|
||||||
|
test('应该支持添加/移除响应拦截器', async () => {
|
||||||
|
const cb = vi.fn((v) => v);
|
||||||
|
|
||||||
|
const id = axios.interceptors.response.use(cb);
|
||||||
|
await axios.request('/test', {
|
||||||
|
adapter: mockAdapter(),
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(cb.mock.calls.length).toBe(1);
|
||||||
|
|
||||||
|
await axios.request('/test', {
|
||||||
|
adapter: mockAdapter(),
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(cb.mock.calls.length).toBe(2);
|
||||||
|
|
||||||
|
axios.interceptors.response.eject(id);
|
||||||
|
await axios.request('/test', {
|
||||||
|
adapter: mockAdapter(),
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(cb.mock.calls.length).toBe(2);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('添加多个响应拦截器时应该按添加顺序从前往后依次执行', async () => {
|
||||||
|
const axios = new Axios();
|
||||||
|
|
||||||
|
const cb1 = vi.fn((v) => {
|
||||||
|
expect(v.data.index).toBe(0);
|
||||||
|
v.data.index = 1;
|
||||||
|
return v;
|
||||||
|
});
|
||||||
|
const cb2 = vi.fn((v) => {
|
||||||
|
expect(v.data.index).toBe(1);
|
||||||
|
v.data.index = 2;
|
||||||
|
return v;
|
||||||
|
});
|
||||||
|
const cb3 = vi.fn((v) => {
|
||||||
|
expect(v.data.index).toBe(2);
|
||||||
|
v.data.index = 3;
|
||||||
|
return v;
|
||||||
|
});
|
||||||
|
|
||||||
|
axios.interceptors.response.use(cb1);
|
||||||
|
axios.interceptors.response.use(cb2);
|
||||||
|
axios.interceptors.response.use(cb3);
|
||||||
|
|
||||||
|
const response = await axios.request<{ index: 0 }>('/test', {
|
||||||
|
adapter: mockAdapter({ data: { index: 0 } }),
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(response.data.index).toBe(3);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('响应拦截器应该支持抛出异常', async () => {
|
||||||
|
const axios = new Axios();
|
||||||
|
const c = { adapter: vi.fn(mockAdapter()), url: 'test' };
|
||||||
|
const body = () => {
|
||||||
|
throw { throw: true };
|
||||||
|
};
|
||||||
|
const res1 = vi.fn(body);
|
||||||
|
const rej1 = vi.fn(body);
|
||||||
|
const res2 = vi.fn(body);
|
||||||
|
const rej2 = vi.fn(body);
|
||||||
|
|
||||||
|
axios.interceptors.response.use(res1, rej1);
|
||||||
|
axios.interceptors.response.use(res2, rej2);
|
||||||
|
|
||||||
|
try {
|
||||||
|
await axios.request(c);
|
||||||
|
} catch (err) {
|
||||||
|
expect(err).toEqual({ throw: true });
|
||||||
|
}
|
||||||
|
expect(c.adapter).toBeCalled();
|
||||||
|
expect(res1).toBeCalled();
|
||||||
|
expect(rej1).not.toBeCalled();
|
||||||
|
expect(res2).not.toBeCalled();
|
||||||
|
expect(rej2).toBeCalled();
|
||||||
|
});
|
||||||
|
|
||||||
test('应该可以获取 URI', () => {
|
test('应该可以获取 URI', () => {
|
||||||
expect(
|
expect(axios.getUri({ url: 'test' })).toBe('test');
|
||||||
axios.getUri({
|
expect(axios.getUri({ url: 'test', params: { id: 1 } })).toBe('test?id=1');
|
||||||
url: 'test',
|
expect(axios.getUri({ url: 'test', paramsSerializer: () => 'id=1' })).toBe(
|
||||||
}),
|
'test?id=1',
|
||||||
).toBe('test');
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
test('应该可以派生领域', () => {
|
test('应该可以派生领域', () => {
|
||||||
const a = axios.fork({
|
const a1 = axios.fork({ baseURL: 'test' });
|
||||||
baseURL: 'test',
|
const a2 = new Axios().fork({ baseURL: 'test' });
|
||||||
});
|
|
||||||
expect(a.defaults.baseURL).toBe('http://api.com/test');
|
expect(a1.defaults.baseURL).toBe('http://api.com/test');
|
||||||
|
expect(a1 instanceof AxiosDomain).toBeTruthy();
|
||||||
|
expect(a2.defaults.baseURL).toBe('/test');
|
||||||
|
});
|
||||||
|
|
||||||
|
test('基于当前实例派生的领域应该可以复用当前实例上的中间件', async () => {
|
||||||
|
const axios = new Axios();
|
||||||
|
const req = vi.fn((v) => v);
|
||||||
|
const res = vi.fn((v) => v);
|
||||||
|
|
||||||
|
axios.interceptors.request.use(req);
|
||||||
|
axios.interceptors.response.use(res);
|
||||||
|
|
||||||
|
const a = axios.fork({ baseURL: 'test' });
|
||||||
|
await a.request('test', { adapter: mockAdapter() });
|
||||||
|
|
||||||
|
expect(req).toBeCalled();
|
||||||
|
expect(res).toBeCalled();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
|
@ -97,6 +97,18 @@ describe('src/helpers/cancel.ts', () => {
|
||||||
expect(() => s.token.throwIfRequested()).toThrowError();
|
expect(() => s.token.throwIfRequested()).toThrowError();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test('多次取消时应该只有第一次会生效', () => {
|
||||||
|
const s = CancelToken.source();
|
||||||
|
|
||||||
|
expect(s.token.throwIfRequested()).toBeUndefined();
|
||||||
|
|
||||||
|
s.cancel('1');
|
||||||
|
s.cancel('2');
|
||||||
|
s.cancel('3');
|
||||||
|
|
||||||
|
expect(() => s.token.throwIfRequested()).toThrowError('1');
|
||||||
|
});
|
||||||
|
|
||||||
test('应该可以在请求发出之前取消', async () => {
|
test('应该可以在请求发出之前取消', async () => {
|
||||||
const cb = vi.fn();
|
const cb = vi.fn();
|
||||||
const s = CancelToken.source();
|
const s = CancelToken.source();
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
import { describe, test, expect, vi } from 'vitest';
|
import { describe, test, expect, vi } from 'vitest';
|
||||||
import {
|
import {
|
||||||
asyncNext,
|
asyncNext,
|
||||||
|
asyncTimeout,
|
||||||
mockAdapter,
|
mockAdapter,
|
||||||
mockAdapterError,
|
mockAdapterError,
|
||||||
mockAdapterFail,
|
mockAdapterFail,
|
||||||
|
@ -119,13 +120,17 @@ describe('src/core/dispatchRequest.ts', () => {
|
||||||
...defaults,
|
...defaults,
|
||||||
adapter: mockAdapterError(),
|
adapter: mockAdapterError(),
|
||||||
url: 'test',
|
url: 'test',
|
||||||
errorHandler: e1,
|
errorHandler: async (err: unknown) => {
|
||||||
|
e1(err);
|
||||||
|
},
|
||||||
};
|
};
|
||||||
const c2 = {
|
const c2 = {
|
||||||
...defaults,
|
...defaults,
|
||||||
adapter: mockAdapterFail(),
|
adapter: mockAdapterFail(),
|
||||||
url: 'test',
|
url: 'test',
|
||||||
errorHandler: e2,
|
errorHandler: async (err: unknown) => {
|
||||||
|
e2(err);
|
||||||
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
|
|
@ -1,5 +1,7 @@
|
||||||
import { describe, test, expect, vi } from 'vitest';
|
import { describe, test, expect, vi } from 'vitest';
|
||||||
import {
|
import {
|
||||||
|
asyncNext,
|
||||||
|
asyncTimeout,
|
||||||
mockAdapter,
|
mockAdapter,
|
||||||
mockAdapterError,
|
mockAdapterError,
|
||||||
mockAdapterFail,
|
mockAdapterFail,
|
||||||
|
@ -184,4 +186,88 @@ describe('src/core/request.ts', () => {
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test('应该可以监听上传进度', () => {
|
||||||
|
const on = vi.fn();
|
||||||
|
const cb = vi.fn();
|
||||||
|
|
||||||
|
request({
|
||||||
|
adapter: () => ({
|
||||||
|
onProgressUpdate: on,
|
||||||
|
}),
|
||||||
|
url: 'test',
|
||||||
|
method: 'post',
|
||||||
|
upload: true,
|
||||||
|
onUploadProgress: cb,
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(on).toBeCalled();
|
||||||
|
expect(on.mock.calls[0][0]).toBe(cb);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('取消请求时应该可以取消监听上传进度', async () => {
|
||||||
|
const on = vi.fn();
|
||||||
|
const cb = vi.fn();
|
||||||
|
const { cancel, token } = axios.CancelToken.source();
|
||||||
|
|
||||||
|
cancel();
|
||||||
|
|
||||||
|
await request({
|
||||||
|
adapter: () => ({
|
||||||
|
offProgressUpdate: on,
|
||||||
|
}),
|
||||||
|
url: 'test',
|
||||||
|
method: 'post',
|
||||||
|
upload: true,
|
||||||
|
onUploadProgress: cb,
|
||||||
|
cancelToken: token,
|
||||||
|
}).catch((err) => {
|
||||||
|
expect(axios.isCancel(err)).toBeTruthy();
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(on).toBeCalled();
|
||||||
|
expect(on.mock.calls[0][0]).toBe(cb);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('应该可以监听下载进度', () => {
|
||||||
|
const on = vi.fn();
|
||||||
|
const cb = vi.fn();
|
||||||
|
|
||||||
|
request({
|
||||||
|
adapter: () => ({
|
||||||
|
onProgressUpdate: on,
|
||||||
|
}),
|
||||||
|
url: 'test',
|
||||||
|
method: 'get',
|
||||||
|
download: true,
|
||||||
|
onDownloadProgress: cb,
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(on).toBeCalled();
|
||||||
|
expect(on.mock.calls[0][0]).toBe(cb);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('取消请求时应该可以取消监听下载进度', async () => {
|
||||||
|
const on = vi.fn();
|
||||||
|
const cb = vi.fn();
|
||||||
|
const { cancel, token } = axios.CancelToken.source();
|
||||||
|
|
||||||
|
cancel();
|
||||||
|
|
||||||
|
await request({
|
||||||
|
adapter: () => ({
|
||||||
|
offProgressUpdate: on,
|
||||||
|
}),
|
||||||
|
url: 'test',
|
||||||
|
method: 'get',
|
||||||
|
download: true,
|
||||||
|
onDownloadProgress: cb,
|
||||||
|
cancelToken: token,
|
||||||
|
}).catch((err) => {
|
||||||
|
expect(axios.isCancel(err)).toBeTruthy();
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(on).toBeCalled();
|
||||||
|
expect(on.mock.calls[0][0]).toBe(cb);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
|
@ -25,6 +25,11 @@ describe('src/helpers/buildURL.ts', () => {
|
||||||
});
|
});
|
||||||
|
|
||||||
test('应该对数组进行系列化', () => {
|
test('应该对数组进行系列化', () => {
|
||||||
|
expect(
|
||||||
|
buildURL('/test', {
|
||||||
|
arr: [],
|
||||||
|
}),
|
||||||
|
).toBe('/test');
|
||||||
expect(
|
expect(
|
||||||
buildURL('/test', {
|
buildURL('/test', {
|
||||||
arr: [1, 2],
|
arr: [1, 2],
|
||||||
|
@ -33,6 +38,11 @@ describe('src/helpers/buildURL.ts', () => {
|
||||||
});
|
});
|
||||||
|
|
||||||
test('应该对对象进行系列化', () => {
|
test('应该对对象进行系列化', () => {
|
||||||
|
expect(
|
||||||
|
buildURL('/test', {
|
||||||
|
obj: {},
|
||||||
|
}),
|
||||||
|
).toBe('/test');
|
||||||
expect(
|
expect(
|
||||||
buildURL('/test', {
|
buildURL('/test', {
|
||||||
obj: {
|
obj: {
|
||||||
|
|
Loading…
Reference in New Issue