|
| 1 | +/** |
| 2 | + * @fileoverview Unit tests for validation utilities and schemas |
| 3 | + * @module utils/validation.test |
| 4 | + */ |
| 5 | + |
| 6 | +import { describe, expect, it } from 'vitest'; |
| 7 | +import { dateSchema, timestampSchema, urlSchema, validateInput } from './validation.js'; |
| 8 | + |
| 9 | +/** |
| 10 | + * Test suite for validation schemas |
| 11 | + */ |
| 12 | +describe('validation schemas', () => { |
| 13 | + /** |
| 14 | + * Tests for URL validation schema |
| 15 | + */ |
| 16 | + describe('urlSchema', () => { |
| 17 | + it('should accept valid URLs', () => { |
| 18 | + expect(urlSchema.parse('https://example.com')).toBe('https://example.com'); |
| 19 | + expect(urlSchema.parse('http://example.com/path')).toBe('http://example.com/path'); |
| 20 | + }); |
| 21 | + |
| 22 | + it('should reject invalid URLs', () => { |
| 23 | + expect(() => urlSchema.parse('not a url')).toThrow(); |
| 24 | + expect(() => urlSchema.parse('example.com')).toThrow(); |
| 25 | + }); |
| 26 | + }); |
| 27 | + |
| 28 | + /** |
| 29 | + * Tests for date validation schema (YYYY-MM-DD format) |
| 30 | + */ |
| 31 | + describe('dateSchema', () => { |
| 32 | + it('should accept valid dates', () => { |
| 33 | + expect(dateSchema.parse('2024-01-01')).toBe('2024-01-01'); |
| 34 | + expect(dateSchema.parse('2024-12-31')).toBe('2024-12-31'); |
| 35 | + }); |
| 36 | + |
| 37 | + it('should reject invalid dates', () => { |
| 38 | + expect(() => dateSchema.parse('2024-1-1')).toThrow(); |
| 39 | + expect(() => dateSchema.parse('01-01-2024')).toThrow(); |
| 40 | + expect(() => dateSchema.parse('2024/01/01')).toThrow(); |
| 41 | + }); |
| 42 | + }); |
| 43 | + |
| 44 | + /** |
| 45 | + * Tests for timestamp validation schema (YYYYMMDDHHmmss format) |
| 46 | + */ |
| 47 | + describe('timestampSchema', () => { |
| 48 | + it('should accept valid timestamps', () => { |
| 49 | + expect(timestampSchema.parse('20240101120000')).toBe('20240101120000'); |
| 50 | + }); |
| 51 | + |
| 52 | + it('should reject invalid timestamps', () => { |
| 53 | + expect(() => timestampSchema.parse('2024-01-01')).toThrow(); |
| 54 | + expect(() => timestampSchema.parse('202401011200')).toThrow(); |
| 55 | + }); |
| 56 | + }); |
| 57 | +}); |
| 58 | + |
| 59 | +/** |
| 60 | + * Test suite for the validateInput utility function |
| 61 | + */ |
| 62 | +describe('validateInput', () => { |
| 63 | + it('should return parsed value for valid input', () => { |
| 64 | + const result = validateInput(urlSchema, 'https://example.com'); |
| 65 | + expect(result).toBe('https://example.com'); |
| 66 | + }); |
| 67 | + |
| 68 | + it('should throw formatted error for invalid input', () => { |
| 69 | + expect(() => validateInput(urlSchema, 'invalid')).toThrow('Validation failed'); |
| 70 | + }); |
| 71 | + |
| 72 | + it('should re-throw non-ZodError errors', () => { |
| 73 | + // Create a schema that throws a non-ZodError |
| 74 | + const faultySchema = { |
| 75 | + parse: () => { |
| 76 | + throw new Error('Not a ZodError'); |
| 77 | + }, |
| 78 | + // biome-ignore lint/suspicious/noExplicitAny: Testing error handling |
| 79 | + } as any; |
| 80 | + |
| 81 | + expect(() => validateInput(faultySchema, 'input')).toThrow('Not a ZodError'); |
| 82 | + }); |
| 83 | +}); |
0 commit comments