2020-04-20 23:28:23 +08:00
|
|
|
/*
|
|
|
|
* @Author: early-autumn
|
|
|
|
* @Date: 2020-04-20 22:42:46
|
|
|
|
* @LastEditors: early-autumn
|
2020-04-21 19:39:07 +08:00
|
|
|
* @LastEditTime: 2020-04-21 19:38:53
|
2020-04-20 23:28:23 +08:00
|
|
|
*/
|
2020-04-21 10:02:11 +08:00
|
|
|
import { CancelAction } from '../../src/types';
|
2020-04-20 23:28:23 +08:00
|
|
|
import dispatchRequest from '../../src/core/dispatchRequest';
|
|
|
|
import CancelToken from '../../src/cancel/CancelToken';
|
|
|
|
import isCancel from '../../src/cancel/isCancel';
|
|
|
|
|
|
|
|
describe('测试 src/core/dispatchRequest.ts', () => {
|
2020-04-21 10:02:11 +08:00
|
|
|
it('默认', () => {
|
|
|
|
dispatchRequest({}).then(undefined, (err) => {
|
2020-04-20 23:28:23 +08:00
|
|
|
expect(err.message).toBe('平台适配失败,您需要参阅文档使用自定义适配器手动适配当前平台');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2020-04-21 10:02:11 +08:00
|
|
|
it('请求失败', () => {
|
|
|
|
dispatchRequest({
|
|
|
|
adapter({ success }): any {
|
2020-04-20 23:28:23 +08:00
|
|
|
success({ status: 200, data: '' });
|
2020-04-21 10:02:11 +08:00
|
|
|
|
|
|
|
return 'task';
|
2020-04-20 23:28:23 +08:00
|
|
|
},
|
|
|
|
validateStatus(status) {
|
|
|
|
return status === -1;
|
|
|
|
},
|
2020-04-21 10:02:11 +08:00
|
|
|
}).then(undefined, (err) => expect(err.response.status).toBe(200));
|
2020-04-20 23:28:23 +08:00
|
|
|
});
|
|
|
|
|
2020-04-21 19:39:07 +08:00
|
|
|
it('自定义错误处理', () => {
|
|
|
|
dispatchRequest({
|
|
|
|
adapter({ fail }): any {
|
|
|
|
fail({});
|
|
|
|
|
|
|
|
return 'task';
|
|
|
|
},
|
|
|
|
errorHandler(error) {
|
|
|
|
error.errorHandler = true;
|
|
|
|
return error;
|
|
|
|
},
|
|
|
|
}).then(undefined, (error) => expect(error.errorHandler).toBe(true));
|
|
|
|
});
|
|
|
|
|
2020-04-20 23:28:23 +08:00
|
|
|
it('取消请求', () => {
|
2020-04-21 10:02:11 +08:00
|
|
|
let cancel: CancelAction;
|
|
|
|
|
|
|
|
dispatchRequest({
|
|
|
|
adapter({ success }) {
|
|
|
|
cancel();
|
|
|
|
setTimeout(() => {
|
|
|
|
success({ status: 200, data: '' });
|
|
|
|
});
|
|
|
|
},
|
|
|
|
cancelToken: new CancelToken(function executor(c) {
|
|
|
|
cancel = c;
|
|
|
|
}),
|
|
|
|
}).then(undefined, (err) => expect(isCancel(err)).toBe(true));
|
2020-04-20 23:28:23 +08:00
|
|
|
});
|
|
|
|
});
|