axios-miniprogram/test/adapter.test.ts

318 lines
7.4 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-09 15:20:10 +08:00
import { AxiosPlatform, createAdapter } from '@/adapter';
2023-04-05 08:40:00 +08:00
describe('src/adapter.ts', () => {
test('应该抛出异常', () => {
expect(() =>
2023-04-09 15:20:10 +08:00
createAdapter(undefined as unknown as AxiosPlatform),
2023-04-05 08:40:00 +08:00
).toThrowErrorMatchingInlineSnapshot(
'"[axios-miniprogram]: platform 不是一个 object"',
);
2023-04-09 15:20:10 +08:00
expect(() =>
createAdapter({} as unknown as AxiosPlatform),
).toThrowErrorMatchingInlineSnapshot(
2023-04-05 08:40:00 +08:00
'"[axios-miniprogram]: request 不是一个 function"',
);
expect(() =>
2023-04-09 15:20:10 +08:00
createAdapter({ request: vi.fn() } as unknown as AxiosPlatform),
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(),
} as unknown as AxiosPlatform),
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',
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-10 18:53:21 +08:00
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",
}
`);
2023-04-05 08:40:00 +08:00
expect(p.upload).not.toBeCalled();
a(u);
2023-04-10 18:53:21 +08:00
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",
}
`);
2023-04-05 08:40:00 +08:00
expect(p.download).not.toBeCalled();
a(d);
2023-04-10 18:53:21 +08:00
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({
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-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-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",
"tempFilePath": "/path/temp/file",
}
`);
},
};
const c4 = {
...c1,
params: {
filePath: '/user/path',
},
success: (response: any) => {
expect(response.data).toMatchInlineSnapshot(`
{
"filePath": "/user/path",
"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(`
{
"headers": {},
2023-04-10 22:53:15 +08:00
"status": 400,
"statusText": "Fail Adapter",
2023-04-10 22:53:15 +08:00
}
`);
},
};
const c2 = {
...c1,
fail: (response: any) => {
expect(response).toMatchInlineSnapshot(`
{
"data": {
"result": null,
},
"headers": {},
"status": 500,
2023-04-10 22:53:15 +08:00
"statusText": "OK",
}
`);
},
};
const c3 = {
...c1,
fail: (response: any) => {
expect(response).toMatchInlineSnapshot(`
{
"data": {
"errMsg": "request:fail",
"errno": 1000,
},
2023-04-10 22:53:15 +08:00
"headers": {},
"status": 400,
"statusText": "Fail Adapter",
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
});
});