axios-miniprogram/test/helpers/isTypes.test.ts

77 lines
1.8 KiB
TypeScript

import { describe, test, expect } from 'vitest';
import {
isArray,
isDate,
isPlainObject,
isFunction,
isNull,
isUndefined,
isString,
isPromise,
} from '@/helpers/isTypes';
describe('src/helpers/isTypes.ts', () => {
test('应该能判断是数组', () => {
expect(isArray(new Array(1))).toBeTruthy();
expect(isArray([])).toBeTruthy();
expect(isArray([1])).toBeTruthy();
});
test('应该能判断是普通对象', () => {
expect(isPlainObject(new String())).toBeFalsy();
expect(isPlainObject(new Function())).toBeFalsy();
expect(isPlainObject({ v: 1 })).toBeTruthy();
expect(isPlainObject(new Object())).toBeTruthy();
});
test('应该能判断是日期', () => {
expect(isDate({})).toBeFalsy();
expect(isDate(new Date())).toBeTruthy();
});
test('应该能判断是函数', () => {
expect(isFunction(() => null)).toBeTruthy();
expect(
isFunction(function () {
return;
}),
).toBeTruthy();
expect(isFunction(new Function())).toBeTruthy();
});
test('应该能判断是 Null', () => {
expect(isNull(undefined)).toBeFalsy();
expect(isNull(null)).toBeTruthy();
});
test('应该能判断是 Undefined', () => {
expect(isUndefined(null)).toBeFalsy();
expect(isUndefined(undefined)).toBeTruthy();
});
test('应该能判断是字符串', () => {
expect(isString(new String())).toBeTruthy();
expect(isString('')).toBeTruthy();
expect(isString(``)).toBeTruthy();
});
test('应该能判断是 Promise', () => {
expect(isPromise({})).toBeFalsy();
expect(isPromise(Promise.resolve())).toBeTruthy();
expect(
isPromise(
new Promise(function () {
return;
}),
),
).toBeTruthy();
expect(
isPromise({
then() {
return;
},
}),
).toBeTruthy();
});
});