axios-miniprogram/test/core/cancel.test.ts

137 lines
3.1 KiB
TypeScript
Raw Normal View History

2023-03-29 22:23:36 +08:00
import { describe, test, expect, vi } from 'vitest';
import {
asyncNext,
captureError,
2023-04-03 21:03:33 +08:00
mockAdapter,
2023-03-29 22:23:36 +08:00
noop,
asyncTimeout,
} from 'scripts/test.utils';
import axios from 'src/axios';
2023-04-02 23:27:45 +08:00
import {
Cancel,
isCancel,
CancelToken,
isCancelToken,
} from '../../src/core/cancel';
2023-03-29 22:23:36 +08:00
describe('src/helpers/cancel.ts', () => {
2023-03-29 22:23:36 +08:00
test('应该支持空参数', () => {
2023-04-03 21:03:33 +08:00
const c = new Cancel();
2023-03-29 22:23:36 +08:00
2023-04-03 21:03:33 +08:00
expect(c.message).toBeUndefined();
expect(c.toString()).toBe('Cancel');
2023-03-29 22:23:36 +08:00
});
test('传入参数时应该有正确的返回结果', () => {
2023-04-03 21:03:33 +08:00
const c = new Cancel('error');
2023-03-29 22:23:36 +08:00
2023-04-03 21:03:33 +08:00
expect(c.message).toBe('error');
expect(c.toString()).toBe('Cancel: error');
2023-03-29 22:23:36 +08:00
});
test('应该正确判断 Cancel', () => {
expect(isCancel(undefined)).toBeFalsy();
expect(isCancel({})).toBeFalsy();
expect(new Cancel()).toBeTruthy();
});
test('应该可以取消', () => {
2023-04-03 21:03:33 +08:00
let ca!: () => void;
const ct = new CancelToken((a) => (ca = a));
expect(ct.throwIfRequested()).toBeUndefined();
ca();
expect(() => ct.throwIfRequested()).toThrowError();
2023-03-29 22:23:36 +08:00
});
test('应该抛出正确的异常信息', async () => {
2023-04-03 21:03:33 +08:00
let ca!: (msg: string) => void;
const ct = new CancelToken((a) => (ca = a));
const te = () => ct.throwIfRequested();
ca('stop');
expect(te).toThrowErrorMatchingInlineSnapshot(`
Cancel {
"message": "stop",
}
`);
expect(captureError<Cancel>(te).toString()).toBe('Cancel: stop');
2023-03-29 22:23:36 +08:00
});
test('回调函数应该被异步执行', async () => {
2023-04-03 21:03:33 +08:00
const cb = vi.fn();
let ca!: () => void;
const ct = new CancelToken((a) => (ca = a));
ct.onCancel(cb);
expect(cb).not.toBeCalled();
2023-03-29 22:23:36 +08:00
2023-04-03 21:03:33 +08:00
ca();
2023-03-29 22:23:36 +08:00
2023-04-03 21:03:33 +08:00
expect(cb).not.toBeCalled();
2023-03-29 22:23:36 +08:00
await asyncNext();
2023-04-03 21:03:33 +08:00
expect(cb).toBeCalled();
expect(isCancel(cb.mock.calls[0][0])).toBeTruthy();
2023-03-29 22:23:36 +08:00
});
test('应该正确判断 CancelToken', () => {
expect(isCancelToken(undefined)).toBeFalsy();
expect(isCancelToken({})).toBeFalsy();
expect(isCancelToken(new CancelToken(noop))).toBeTruthy();
});
test('应该有正确返回结果', () => {
2023-04-03 21:03:33 +08:00
const s = CancelToken.source();
2023-03-29 22:23:36 +08:00
2023-04-03 21:03:33 +08:00
expect(s.cancel).toBeTypeOf('function');
expect(isCancelToken(s.token)).toBeTruthy();
2023-03-29 22:23:36 +08:00
});
test('应该可以取消', () => {
2023-04-03 21:03:33 +08:00
const s = CancelToken.source();
2023-03-29 22:23:36 +08:00
2023-04-03 21:03:33 +08:00
expect(s.token.throwIfRequested()).toBeUndefined();
2023-03-29 22:23:36 +08:00
2023-04-03 21:03:33 +08:00
s.cancel();
2023-03-29 22:23:36 +08:00
2023-04-03 21:03:33 +08:00
expect(() => s.token.throwIfRequested()).toThrowError();
2023-03-29 22:23:36 +08:00
});
test('应该可以在请求发出之前取消', async () => {
2023-04-03 21:03:33 +08:00
const cb = vi.fn();
const s = CancelToken.source();
s.cancel();
2023-03-29 22:23:36 +08:00
axios({
2023-04-03 21:03:33 +08:00
adapter: mockAdapter(),
cancelToken: s.token,
}).catch(cb);
2023-03-29 22:23:36 +08:00
await asyncTimeout();
2023-04-03 21:03:33 +08:00
expect(cb).toBeCalled();
expect(isCancel(cb.mock.calls[0][0])).toBeTruthy();
2023-03-29 22:23:36 +08:00
});
test('应该可以在请求发出之后取消', async () => {
2023-04-03 21:03:33 +08:00
const cb = vi.fn();
const s = CancelToken.source();
2023-03-29 22:23:36 +08:00
axios({
2023-04-03 21:03:33 +08:00
adapter: mockAdapter(),
cancelToken: s.token,
}).catch(cb);
s.cancel();
2023-03-29 22:23:36 +08:00
await asyncTimeout();
2023-04-03 21:03:33 +08:00
expect(cb).toBeCalled();
expect(isCancel(cb.mock.calls[0][0])).toBeTruthy();
2023-03-29 22:23:36 +08:00
});
});