axios-miniprogram/test/adpater/createAdapter.test.ts

360 lines
8.6 KiB
TypeScript
Raw Normal View History

2023-04-05 08:40:00 +08:00
import { describe, test, expect, vi } from 'vitest';
import { noop } from 'scripts/test.utils';
2023-04-22 16:12:32 +08:00
import { AxiosAdapterPlatform, createAdapter } from '@/adpater/createAdapter';
2023-04-05 08:40:00 +08:00
2023-04-22 16:12:32 +08:00
describe('src/adapter/createAdapter.ts', () => {
2023-04-05 08:40:00 +08:00
test('应该抛出异常', () => {
expect(() =>
2023-04-18 11:14:57 +08:00
createAdapter(undefined as unknown as AxiosAdapterPlatform),
2023-04-05 08:40:00 +08:00
).toThrowErrorMatchingInlineSnapshot(
'"[axios-miniprogram]: platform 不是一个 object"',
);
2023-04-09 15:20:10 +08:00
expect(() =>
2023-04-18 11:14:57 +08:00
createAdapter({} as unknown as AxiosAdapterPlatform),
2023-04-09 15:20:10 +08:00
).toThrowErrorMatchingInlineSnapshot(
2023-04-05 08:40:00 +08:00
'"[axios-miniprogram]: request 不是一个 function"',
);
expect(() =>
2023-04-18 11:14:57 +08:00
createAdapter({ request: vi.fn() } as unknown as AxiosAdapterPlatform),
2023-04-05 08:40:00 +08:00
).toThrowErrorMatchingInlineSnapshot(
'"[axios-miniprogram]: upload 不是一个 function"',
);
expect(() =>
2023-04-09 15:20:10 +08:00
createAdapter({
request: vi.fn(),
upload: vi.fn(),
2023-04-18 11:14:57 +08:00
} as unknown as AxiosAdapterPlatform),
2023-04-05 08:40:00 +08:00
).toThrowErrorMatchingInlineSnapshot(
'"[axios-miniprogram]: download 不是一个 function"',
);
});
test('应该可以成功执行对应的平台函数', () => {
const p = {
request: vi.fn(),
upload: vi.fn(),
download: vi.fn(),
};
const a = createAdapter(p);
const r = {
type: 'request' as const,
2023-04-10 18:53:21 +08:00
headers: {
Accept: 'application/json, text/plain, */*',
},
2023-04-05 08:40:00 +08:00
url: 'test',
method: 'GET' as const,
success: noop,
fail: noop,
};
const u = {
type: 'upload' as const,
2023-04-10 18:53:21 +08:00
headers: {
Accept: 'application/json, text/plain, */*',
},
2023-04-05 08:40:00 +08:00
url: 'test',
method: 'POST' as const,
2023-04-10 18:53:21 +08:00
data: {
name: 'file',
filePath: '/path/file',
2023-04-27 21:25:47 +08:00
fileType: 'image',
2023-04-10 18:53:21 +08:00
user: 'test',
id: 1,
},
2023-04-05 08:40:00 +08:00
success: noop,
fail: noop,
};
const d = {
type: 'download' as const,
2023-04-10 18:53:21 +08:00
headers: {
Accept: 'application/json, text/plain, */*',
},
2023-04-05 08:40:00 +08:00
url: 'test',
method: 'GET' as const,
2023-04-10 18:53:21 +08:00
params: {
filePath: '/path/file',
},
2023-04-05 08:40:00 +08:00
success: noop,
fail: noop,
};
expect(p.request).not.toBeCalled();
a(r);
2023-04-27 21:25:47 +08:00
const rOpts = p.request.mock.calls[0][0];
expect(rOpts).toMatchInlineSnapshot(`
2023-04-10 18:53:21 +08:00
{
"fail": [Function],
2023-04-23 14:23:06 +08:00
"header": {
"Accept": "application/json, text/plain, */*",
},
2023-04-10 18:53:21 +08:00
"headers": {
"Accept": "application/json, text/plain, */*",
},
"method": "GET",
"success": [Function],
"type": "request",
"url": "test",
}
`);
2023-04-27 21:25:47 +08:00
expect(rOpts.header).toEqual(r.headers);
2023-04-05 08:40:00 +08:00
2023-04-27 21:25:47 +08:00
expect(p.download).not.toBeCalled();
a(d);
const dOpts = p.download.mock.calls[0][0];
expect(dOpts).toMatchInlineSnapshot(`
2023-04-10 18:53:21 +08:00
{
"fail": [Function],
"filePath": "/path/file",
2023-04-23 14:23:06 +08:00
"header": {
"Accept": "application/json, text/plain, */*",
},
2023-04-10 18:53:21 +08:00
"headers": {
"Accept": "application/json, text/plain, */*",
},
2023-04-27 21:25:47 +08:00
"method": "GET",
2023-04-10 18:53:21 +08:00
"success": [Function],
2023-04-27 21:25:47 +08:00
"type": "download",
2023-04-10 18:53:21 +08:00
"url": "test",
}
`);
2023-04-27 21:25:47 +08:00
expect(dOpts.header).toEqual(d.headers);
expect(dOpts.filePath).toEqual(d.params.filePath);
2023-04-05 08:40:00 +08:00
2023-04-27 21:25:47 +08:00
expect(p.upload).not.toBeCalled();
a(u);
const uOpts = p.upload.mock.calls[0][0];
expect(uOpts).toMatchInlineSnapshot(`
2023-04-10 18:53:21 +08:00
{
"fail": [Function],
2023-04-27 21:25:47 +08:00
"fileName": "file",
2023-04-10 18:53:21 +08:00
"filePath": "/path/file",
2023-04-27 21:25:47 +08:00
"fileType": "image",
"formData": {
"id": 1,
"user": "test",
},
2023-04-23 14:23:06 +08:00
"header": {
"Accept": "application/json, text/plain, */*",
},
2023-04-10 18:53:21 +08:00
"headers": {
"Accept": "application/json, text/plain, */*",
},
2023-04-27 21:25:47 +08:00
"method": "POST",
"name": "file",
2023-04-10 18:53:21 +08:00
"success": [Function],
2023-04-27 21:25:47 +08:00
"type": "upload",
2023-04-10 18:53:21 +08:00
"url": "test",
}
`);
2023-04-27 21:25:47 +08:00
expect(uOpts.header).toEqual(u.headers);
expect(uOpts.name).toEqual(u.data.name);
expect(uOpts.fileName).toEqual(u.data.name);
expect(uOpts.filePath).toEqual(u.data.filePath);
expect(uOpts.fileType).toEqual(u.data.fileType);
expect(uOpts.formData).toEqual({
id: u.data.id,
user: u.data.user,
});
2023-04-10 18:53:21 +08:00
});
test('应该支持转换下载数据', () => {
const p1 = {
request: vi.fn(),
upload: vi.fn(),
download: vi.fn((config) => {
config.success({
tempFilePath: '/path/temp/file',
});
}),
};
const p2 = {
2023-04-10 22:53:15 +08:00
...p1,
download: vi.fn((config) => {
config.success({
apFilePath: '/path/temp/file',
});
}),
};
const p3 = {
...p1,
download: vi.fn((config) => {
config.success({
filePath: config.filePath,
tempFilePath: '/path/temp/file',
});
}),
};
const p4 = {
...p1,
2023-04-10 18:53:21 +08:00
download: vi.fn((config) => {
config.success({
filePath: config.filePath,
apFilePath: '/path/temp/file',
});
}),
};
2023-04-10 22:53:15 +08:00
const c1 = {
2023-04-10 18:53:21 +08:00
type: 'download' as const,
url: 'test',
method: 'GET' as const,
success: (response: any) => {
expect(response.data).toMatchInlineSnapshot(`
{
2023-04-10 22:53:15 +08:00
"filePath": undefined,
2023-08-15 15:43:24 +08:00
"fileSize": undefined,
2023-04-10 18:53:21 +08:00
"tempFilePath": "/path/temp/file",
}
`);
},
fail: noop,
2023-04-10 22:53:15 +08:00
};
const c2 = {
...c1,
2023-04-10 18:53:21 +08:00
success: (response: any) => {
expect(response.data).toMatchInlineSnapshot(`
{
2023-04-10 22:53:15 +08:00
"filePath": undefined,
2023-08-15 15:43:24 +08:00
"fileSize": undefined,
2023-04-10 18:53:21 +08:00
"tempFilePath": "/path/temp/file",
}
`);
},
2023-04-10 22:53:15 +08:00
};
const c3 = {
...c1,
params: {
filePath: '/user/path',
},
success: (response: any) => {
expect(response.data).toMatchInlineSnapshot(`
{
"filePath": "/user/path",
2023-08-15 15:43:24 +08:00
"fileSize": undefined,
2023-04-10 22:53:15 +08:00
"tempFilePath": "/path/temp/file",
}
`);
},
};
const c4 = {
...c1,
params: {
filePath: '/user/path',
},
success: (response: any) => {
expect(response.data).toMatchInlineSnapshot(`
{
"filePath": "/user/path",
2023-08-15 15:43:24 +08:00
"fileSize": undefined,
2023-04-10 22:53:15 +08:00
"tempFilePath": "/path/temp/file",
}
`);
},
};
createAdapter(p1)(c1);
createAdapter(p2)(c2);
createAdapter(p3)(c3);
createAdapter(p4)(c4);
});
test('应该支持转换失败的请求', () => {
const p1 = {
request: vi.fn(({ fail }) => fail({})),
upload: vi.fn(),
download: vi.fn(),
};
const p2 = {
...p1,
request: vi.fn(({ fail }) =>
fail({ status: 500, data: { result: null } }),
),
2023-04-10 22:53:15 +08:00
};
const p3 = {
...p1,
request: vi.fn(({ fail }) =>
fail({ errMsg: 'request:fail', errno: 1000 }),
2023-04-10 22:53:15 +08:00
),
};
const c1 = {
type: 'request' as const,
url: 'test',
method: 'GET' as const,
success: noop,
fail: (response: any) => {
expect(response).toMatchInlineSnapshot(`
{
"data": {
"errMsg": undefined,
"errno": undefined,
},
"headers": undefined,
"status": undefined,
2023-04-10 22:53:15 +08:00
}
`);
},
};
const c2 = {
...c1,
fail: (response: any) => {
expect(response).toMatchInlineSnapshot(`
{
"data": {
"errMsg": undefined,
"errno": undefined,
2023-04-10 22:53:15 +08:00
},
"headers": undefined,
"status": 500,
2023-04-10 22:53:15 +08:00
}
`);
},
};
const c3 = {
...c1,
fail: (response: any) => {
expect(response).toMatchInlineSnapshot(`
{
"data": {
"errMsg": "request:fail",
"errno": 1000,
},
"headers": undefined,
"status": undefined,
2023-04-10 22:53:15 +08:00
}
`);
},
};
createAdapter(p1)(c1);
createAdapter(p2)(c2);
createAdapter(p3)(c3);
2023-04-05 08:40:00 +08:00
});
test('响应数据应该进行JSON.parse', async () => {
const a = createAdapter({
request: vi.fn(),
upload: (options) => {
options.success({
data: '{"id":1}',
});
},
download: vi.fn(),
});
a({
type: 'upload',
url: 'test',
method: 'POST',
dataType: 'json',
responseType: 'text',
data: {},
success(res) {
expect(res.data).toBeTypeOf('object');
// @ts-ignore
expect(res.data.id).toBe(1);
},
fail() {
//
},
});
});
2023-04-05 08:40:00 +08:00
});