axios-miniprogram/scripts/test.utils.ts

127 lines
2.5 KiB
TypeScript
Raw Normal View History

import { test } from 'vitest';
import {
requestMethodNames,
requestMethodWithParamsNames,
requestMethodWithDataNames,
} from '@/core/AxiosDomain';
import { AxiosAdapterRequestConfig } from '@/adapter';
2023-04-02 23:27:45 +08:00
2023-03-29 22:23:36 +08:00
export function asyncNext() {
return Promise.resolve().then;
}
export function asyncTimeout(delay = 0) {
2023-04-01 23:11:55 +08:00
return new Promise((resolve) => setTimeout(resolve, delay));
2023-03-29 22:23:36 +08:00
}
2023-04-03 21:03:33 +08:00
export function captureError<T = any>(fn: () => void) {
2023-03-29 22:23:36 +08:00
try {
fn();
2023-04-01 23:11:55 +08:00
throw new Error('without Error');
2023-03-29 22:23:36 +08:00
} catch (err) {
return err as T;
}
}
2023-04-03 21:03:33 +08:00
export function checkStack(error: Error) {
if (error.stack) {
return error.stack.indexOf('at') === error.stack.indexOf('at /');
}
return true;
}
2023-03-29 22:23:36 +08:00
export function noop() {
return;
}
2023-04-09 15:20:10 +08:00
export function mockResponse(
2023-03-29 22:23:36 +08:00
status: number,
statusText: string,
headers: AnyObject,
data: AnyObject,
) {
return {
status,
statusText,
headers,
data,
};
}
2023-04-02 23:27:45 +08:00
export interface MockAdapterOptions {
headers?: AnyObject;
data?: AnyObject;
delay?: number;
before?: (config: AxiosAdapterRequestConfig) => void;
after?: () => void;
}
2023-04-09 21:01:43 +08:00
function mockAdapterBase(
2023-04-03 21:03:33 +08:00
type: 'success' | 'error' | 'fail' = 'success',
2023-04-02 23:27:45 +08:00
options: MockAdapterOptions = {},
) {
2023-04-09 21:01:43 +08:00
const {
headers = {},
data = {
result: null,
},
delay = 0,
before,
after,
} = options;
2023-04-02 23:27:45 +08:00
return (config: AxiosAdapterRequestConfig) => {
2023-04-09 15:20:10 +08:00
let canceled = false;
2023-04-02 23:27:45 +08:00
before?.(config);
2023-04-09 15:20:10 +08:00
2023-04-02 23:27:45 +08:00
setTimeout(() => {
2023-04-09 15:20:10 +08:00
if (!canceled) {
switch (type) {
case 'success':
config.success(mockResponse(200, 'OK', headers, data));
break;
case 'error':
config.success(mockResponse(500, 'ERROR', headers, data));
break;
case 'fail':
config.fail(mockResponse(400, 'FAIL', headers, data));
break;
}
after?.();
2023-04-02 23:27:45 +08:00
}
}, delay);
2023-04-09 15:20:10 +08:00
return {
abort() {
canceled = true;
},
};
2023-04-02 23:27:45 +08:00
};
}
2023-04-03 21:03:33 +08:00
export function mockAdapter(options: MockAdapterOptions = {}) {
return mockAdapterBase('success', options);
}
export function mockAdapterError(options: MockAdapterOptions = {}) {
return mockAdapterBase('error', options);
2023-04-02 23:27:45 +08:00
}
export function mockAdapterFail(options: MockAdapterOptions = {}) {
2023-04-03 21:03:33 +08:00
return mockAdapterBase('fail', options);
2023-04-02 23:27:45 +08:00
}
export const methods = [
...requestMethodNames,
...requestMethodWithParamsNames,
...requestMethodWithDataNames,
];
export const testEachMethods = test.each(methods);
export function eachMethods(cb: (k: (typeof methods)[number]) => void) {
methods.forEach(cb);
}