Files
v10/packages/utils/src/predicate/tests/predicate.test.ts
T

240 lines
7.3 KiB
TypeScript

import { describe, expect, it } from 'vitest';
import {
isAbortError,
isBoolean,
isFunction,
isNil,
isNull,
isNumber,
isObject,
isPlainObject,
isPromise,
isString,
isUndefined,
} from '../predicate';
describe('predicate', () => {
describe('isString', () => {
it('returns true for strings', () => {
expect(isString('')).toBe(true);
expect(isString('hello')).toBe(true);
expect(isString('123')).toBe(true);
});
it('returns false for non-strings', () => {
expect(isString(123)).toBe(false);
expect(isString(null)).toBe(false);
expect(isString(undefined)).toBe(false);
expect(isString({})).toBe(false);
expect(isString([])).toBe(false);
expect(isString(true)).toBe(false);
});
});
describe('isNumber', () => {
it('returns true for numbers', () => {
expect(isNumber(0)).toBe(true);
expect(isNumber(123)).toBe(true);
expect(isNumber(-1)).toBe(true);
expect(isNumber(3.14)).toBe(true);
expect(isNumber(Number.NaN)).toBe(true);
expect(isNumber(Infinity)).toBe(true);
});
it('returns false for non-numbers', () => {
expect(isNumber('123')).toBe(false);
expect(isNumber(null)).toBe(false);
expect(isNumber(undefined)).toBe(false);
expect(isNumber({})).toBe(false);
expect(isNumber([])).toBe(false);
expect(isNumber(true)).toBe(false);
});
});
describe('isBoolean', () => {
it('returns true for booleans', () => {
expect(isBoolean(true)).toBe(true);
expect(isBoolean(false)).toBe(true);
});
it('returns false for non-booleans', () => {
expect(isBoolean(0)).toBe(false);
expect(isBoolean(1)).toBe(false);
expect(isBoolean('true')).toBe(false);
expect(isBoolean(null)).toBe(false);
expect(isBoolean(undefined)).toBe(false);
expect(isBoolean({})).toBe(false);
});
});
describe('isFunction', () => {
it('returns true for functions', () => {
expect(isFunction(() => {})).toBe(true);
expect(isFunction(() => {})).toBe(true);
expect(isFunction(async () => {})).toBe(true);
expect(isFunction(class {})).toBe(true);
expect(isFunction(Array.isArray)).toBe(true);
});
it('returns false for non-functions', () => {
expect(isFunction({})).toBe(false);
expect(isFunction([])).toBe(false);
expect(isFunction('function')).toBe(false);
expect(isFunction(null)).toBe(false);
expect(isFunction(undefined)).toBe(false);
});
});
describe('isNull', () => {
it('returns true for null', () => {
expect(isNull(null)).toBe(true);
});
it('returns false for non-null', () => {
expect(isNull(undefined)).toBe(false);
expect(isNull(0)).toBe(false);
expect(isNull('')).toBe(false);
expect(isNull(false)).toBe(false);
expect(isNull({})).toBe(false);
});
});
describe('isUndefined', () => {
it('returns true for undefined', () => {
expect(isUndefined(undefined)).toBe(true);
expect(isUndefined(void 0)).toBe(true);
});
it('returns false for non-undefined', () => {
expect(isUndefined(null)).toBe(false);
expect(isUndefined(0)).toBe(false);
expect(isUndefined('')).toBe(false);
expect(isUndefined(false)).toBe(false);
expect(isUndefined({})).toBe(false);
});
});
describe('isNil', () => {
it('returns true for null and undefined', () => {
expect(isNil(null)).toBe(true);
expect(isNil(undefined)).toBe(true);
});
it('returns false for non-nil values', () => {
expect(isNil(0)).toBe(false);
expect(isNil('')).toBe(false);
expect(isNil(false)).toBe(false);
expect(isNil({})).toBe(false);
expect(isNil([])).toBe(false);
expect(isNil(Number.NaN)).toBe(false);
});
});
describe('isPromise', () => {
it('returns true for promises', () => {
expect(isPromise(Promise.resolve())).toBe(true);
expect(isPromise(Promise.reject().catch(() => {}))).toBe(true);
expect(isPromise(new Promise(() => {}))).toBe(true);
});
it('returns false for non-promises', () => {
expect(isPromise({})).toBe(false);
expect(isPromise({ then: () => {} })).toBe(false); // thenable but not Promise
expect(isPromise(null)).toBe(false);
expect(isPromise(undefined)).toBe(false);
expect(isPromise(() => {})).toBe(false);
});
});
describe('isObject', () => {
it('returns true for objects', () => {
expect(isObject({})).toBe(true);
expect(isObject([])).toBe(true);
expect(isObject(new Date())).toBe(true);
expect(isObject(/regex/)).toBe(true);
expect(isObject(new Map())).toBe(true);
expect(isObject(new Set())).toBe(true);
});
it('returns false for null', () => {
expect(isObject(null)).toBe(false);
});
it('returns false for primitives', () => {
expect(isObject(undefined)).toBe(false);
expect(isObject('string')).toBe(false);
expect(isObject(123)).toBe(false);
expect(isObject(true)).toBe(false);
expect(isObject(Symbol())).toBe(false);
});
it('returns false for functions', () => {
expect(isObject(() => {})).toBe(false);
});
});
describe('isPlainObject', () => {
it('returns true for plain objects', () => {
expect(isPlainObject({})).toBe(true);
expect(isPlainObject({ a: 1 })).toBe(true);
expect(isPlainObject(Object.create(null))).toBe(true);
expect(isPlainObject(new Object())).toBe(true);
});
it('returns false for arrays', () => {
expect(isPlainObject([])).toBe(false);
expect(isPlainObject([1, 2, 3])).toBe(false);
});
it('returns false for class instances', () => {
class Foo {}
expect(isPlainObject(new Foo())).toBe(false);
expect(isPlainObject(new Date())).toBe(false);
expect(isPlainObject(new Map())).toBe(false);
expect(isPlainObject(new Set())).toBe(false);
expect(isPlainObject(/regex/)).toBe(false);
});
it('returns false for primitives', () => {
expect(isPlainObject(null)).toBe(false);
expect(isPlainObject(undefined)).toBe(false);
expect(isPlainObject('string')).toBe(false);
expect(isPlainObject(123)).toBe(false);
expect(isPlainObject(true)).toBe(false);
expect(isPlainObject(Symbol())).toBe(false);
});
it('returns false for functions', () => {
expect(isPlainObject(() => {})).toBe(false);
expect(isPlainObject(() => {})).toBe(false);
});
});
describe('isAbortError', () => {
it('returns true for AbortError', () => {
const error = new DOMException('Aborted', 'AbortError');
expect(isAbortError(error)).toBe(true);
});
it('returns true for custom AbortError', () => {
const error = new Error('Aborted');
error.name = 'AbortError';
expect(isAbortError(error)).toBe(true);
});
it('returns false for other errors', () => {
expect(isAbortError(new Error('test'))).toBe(false);
expect(isAbortError(new TypeError('test'))).toBe(false);
expect(isAbortError(new RangeError('test'))).toBe(false);
});
it('returns false for non-errors', () => {
expect(isAbortError(null)).toBe(false);
expect(isAbortError(undefined)).toBe(false);
expect(isAbortError('AbortError')).toBe(false);
expect(isAbortError({ name: 'AbortError' })).toBe(false);
});
});
});