|
| 1 | +import { z } from 'zod'; |
| 2 | + |
| 3 | +describe('Hook Validations', () => { |
| 4 | + it('should pass for valid input', () => { |
| 5 | + const schema = z.object({ |
| 6 | + name: z.string().min(1), |
| 7 | + age: z.number().min(0), |
| 8 | + }); |
| 9 | + const input = { name: 'John Doe', age: 30 }; |
| 10 | + const parsed = schema.safeParse(input); |
| 11 | + expect(parsed.success).toBe(true); |
| 12 | + }); |
| 13 | + |
| 14 | + it('should fail for missing name', () => { |
| 15 | + const schema = z.object({ |
| 16 | + name: z.string().min(1), |
| 17 | + age: z.number().min(0), |
| 18 | + }); |
| 19 | + const input = { age: 30 }; |
| 20 | + const parsed = schema.safeParse(input); |
| 21 | + expect(parsed.success).toBe(false); |
| 22 | + expect(parsed.error).toBeDefined(); |
| 23 | + }); |
| 24 | + |
| 25 | + it('should fail for empty name', () => { |
| 26 | + const schema = z.object({ |
| 27 | + name: z.string().min(1), |
| 28 | + age: z.number().min(0), |
| 29 | + }); |
| 30 | + const input = { name: '', age: 30 }; |
| 31 | + const parsed = schema.safeParse(input); |
| 32 | + expect(parsed.success).toBe(false); |
| 33 | + expect(parsed.error).toBeDefined(); |
| 34 | + }); |
| 35 | + |
| 36 | + it('should fail for negative age', () => { |
| 37 | + const schema = z.object({ |
| 38 | + name: z.string().min(1), |
| 39 | + age: z.number().min(0), |
| 40 | + }); |
| 41 | + const input = { name: 'John Doe', age: -5 }; |
| 42 | + const parsed = schema.safeParse(input); |
| 43 | + expect(parsed.success).toBe(false); |
| 44 | + expect(parsed.error).toBeDefined(); |
| 45 | + }); |
| 46 | +}); |
0 commit comments