48 lines
1.5 KiB
TypeScript
48 lines
1.5 KiB
TypeScript
import { describe, it, expect } from '@jest/globals';
|
|
import {
|
|
validateEmail,
|
|
validateUrl,
|
|
sanitizeString
|
|
} from '../../src/lib/validation';
|
|
|
|
describe('Validation Utilities', () => {
|
|
describe('validateEmail', () => {
|
|
it('should return true for valid emails', () => {
|
|
expect(validateEmail('test@example.com')).toBe(true);
|
|
expect(validateEmail('user.name@domain.co.uk')).toBe(true);
|
|
});
|
|
|
|
it('should return false for invalid emails', () => {
|
|
expect(validateEmail('invalid-email')).toBe(false);
|
|
expect(validateEmail('@example.com')).toBe(false);
|
|
expect(validateEmail('')).toBe(false);
|
|
});
|
|
});
|
|
|
|
describe('validateUrl', () => {
|
|
it('should return true for valid URLs', () => {
|
|
expect(validateUrl('https://example.com')).toBe(true);
|
|
expect(validateUrl('http://localhost:3000')).toBe(true);
|
|
});
|
|
|
|
it('should return false for invalid URLs', () => {
|
|
expect(validateUrl('not-a-url')).toBe(false);
|
|
expect(validateUrl('')).toBe(false);
|
|
});
|
|
});
|
|
|
|
describe('sanitizeString', () => {
|
|
it('should trim whitespace', () => {
|
|
expect(sanitizeString(' test ')).toBe('test');
|
|
});
|
|
|
|
it('should limit string length', () => {
|
|
const longString = 'a'.repeat(2000);
|
|
expect(sanitizeString(longString, 100)).toHaveLength(100);
|
|
});
|
|
|
|
it('should remove HTML tags', () => {
|
|
expect(sanitizeString('test<script>alert(1)</script>test')).toBe('testscriptalert(1)/scripttest');
|
|
});
|
|
});
|
|
}); |