Added test suite
All checks were successful
Build / Build NPM Project (push) Successful in 1m16s
Build / Tag Version (push) Successful in 14s
Build / Publish Documentation (push) Successful in 53s

This commit is contained in:
2025-05-14 16:30:42 -04:00
parent cf122ef9e8
commit fec373ca4c
32 changed files with 1719 additions and 310 deletions

View File

@ -1,18 +1,44 @@
import {fn} from '../src';
import {fn, gravatar, escapeRegex, md5} from '../src';
describe('Miscellanies Utilities', () => {
describe('Misc Utilities', () => {
describe('fn', () => {
test('async', async () => {
const test = {a: Math.random()};
const resp = fn(test, 'return a;', true);
expect(resp instanceof Promise).toBeTruthy();
expect(await resp).toEqual(test['a']);
it('should execute a stringified function with arguments', () => {
const result = fn({ x: 2, y: 3 }, 'return x + y;');
expect(result).toBe(5);
});
test('sync', async () => {
const test = {a: Math.random()};
const resp = fn(test, 'return a;3');
expect(resp).toEqual(test['a']);
it('should execute an async function if async=true', async () => {
const asyncFn = 'return await Promise.resolve(x * y);';
const result = await fn({ x: 3, y: 4 }, asyncFn, true);
expect(result).toBe(12);
});
it('should work with no arguments', () => {
const result = fn({}, 'return 42;');
expect(result).toBe(42);
});
});
describe('gravatar', () => {
it('should return empty string if email is falsy', () => {
expect(gravatar('')).toBe('');
});
it('should build correct gravatar url', () => {
const email = 'test@example.com';
expect(gravatar(email)).toContain(`https://www.gravatar.com/avatar/${md5(email)}`);
});
});
describe('escapeRegex', () => {
it('should escape all special regex characters', () => {
const special = '.*+?^${}()|[]\\';
const escaped = escapeRegex(special);
expect(escaped).toBe('\\.\\*\\+\\?\\^\\$\\{\\}\\(\\)\\|\\[\\]\\\\');
});
it('should return original string if nothing to escape', () => {
const normal = 'abc123';
const escaped = escapeRegex(normal);
expect(escaped).toBe('abc123');
});
});
});