import { describe, it, expect, vi, afterEach } from 'vitest'; import { FormControl, FormGroup } from '@angular/forms'; import { futureDateValidator, atLeastOneFilledValidator, trimmedRequiredValidator } from '@app/shared/utils/form-validators.util'; describe('form-validators.util', () => { afterEach(() => { vi.useRealTimers(); }); describe('futureDateValidator', () => { it('should return null for empty value', () => { const control = new FormControl(''); const result = futureDateValidator(control); expect(result).toBeNull(); }); it('should return null for future date', () => { vi.useFakeTimers(); vi.setSystemTime(new Date('2026-05-13T12:00:00')); const control = new FormControl('2030-01-01'); const result = futureDateValidator(control); expect(result).toBeNull(); }); it('should return error for past date', () => { vi.useFakeTimers(); vi.setSystemTime(new Date('2026-05-13T12:00:00')); const control = new FormControl('2020-01-01'); const result = futureDateValidator(control); expect(result).toEqual({ futureDate: { value: '2020-01-01' } }); }); it('should return error for today', () => { vi.useFakeTimers(); vi.setSystemTime(new Date('2026-05-13T12:00:00')); const control = new FormControl('2026-05-13'); const result = futureDateValidator(control); expect(result).toEqual({ futureDate: { value: '2026-05-13' } }); }); }); describe('atLeastOneFilledValidator', () => { it('should return null when at least one field has value', () => { const group = new FormGroup({ title: new FormControl('Hello'), description: new FormControl(''), }); const result = atLeastOneFilledValidator('title', 'description')(group); expect(result).toBeNull(); }); it('should return error when all fields are empty', () => { const group = new FormGroup({ title: new FormControl(''), description: new FormControl(''), }); const result = atLeastOneFilledValidator('title', 'description')(group); expect(result).toEqual({ atLeastOneFilled: { fields: ['title', 'description'] } }); }); it('should return error when all fields are whitespace only', () => { const group = new FormGroup({ title: new FormControl(' '), description: new FormControl(' '), }); const result = atLeastOneFilledValidator('title', 'description')(group); expect(result).toEqual({ atLeastOneFilled: { fields: ['title', 'description'] } }); }); }); describe('trimmedRequiredValidator', () => { it('should return null for non-empty trimmed string', () => { const control = new FormControl('hello'); const result = trimmedRequiredValidator(control); expect(result).toBeNull(); }); it('should return error for empty string', () => { const control = new FormControl(''); const result = trimmedRequiredValidator(control); expect(result).toEqual({ trimmedRequired: { value: '' } }); }); it('should return error for whitespace only', () => { const control = new FormControl(' '); const result = trimmedRequiredValidator(control); expect(result).toEqual({ trimmedRequired: { value: ' ' } }); }); it('should return error for non-string value', () => { const control = new FormControl(null); const result = trimmedRequiredValidator(control); expect(result).toEqual({ trimmedRequired: { value: null } }); }); }); });