validation.test.js•1.18 kB
import { describe, it, expect } from 'vitest';
import { validateGetJokeInput, sanitizeBlacklist } from '../../src/validation.js';
const defaults = { defaultCategory: 'programming', lang: 'en' };
describe('validateGetJokeInput', () => {
it('returns defaults when input is undefined', () => {
const result = validateGetJokeInput(undefined, defaults);
expect(result).toEqual({ category: 'programming', lang: 'en', blacklist: [] });
});
it('normalizes category and lang to lowercase', () => {
const result = validateGetJokeInput({ category: 'Programming', lang: 'EN' }, defaults);
expect(result).toEqual({ category: 'programming', lang: 'en', blacklist: [] });
});
it('throws on unsupported category', () => {
expect(() => validateGetJokeInput({ category: 'space' }, defaults)).toThrow('Unsupported category');
});
it('throws on unsupported language', () => {
expect(() => validateGetJokeInput({ lang: 'jp' }, defaults)).toThrow('Unsupported language');
});
it('sanitizes blacklist array', () => {
const result = sanitizeBlacklist(['NSFW', 'religious', 'unknown', 'nsfw']);
expect(result).toEqual(['nsfw', 'religious']);
});
});