|
| 1 | +import { FieldValues, Resolver, SubmitHandler, useForm } from 'react-hook-form'; |
| 2 | +import { z } from 'zod'; |
1 | 3 | import { zodResolver } from '..'; |
2 | 4 | import { fields, invalidData, schema, validData } from './__fixtures__/data'; |
3 | 5 |
|
@@ -89,4 +91,86 @@ describe('zodResolver', () => { |
89 | 91 |
|
90 | 92 | await expect(promise).rejects.toThrow('custom error'); |
91 | 93 | }); |
| 94 | + |
| 95 | + /** |
| 96 | + * Type inference tests |
| 97 | + */ |
| 98 | + it('should correctly infer the output type from a zod schema', () => { |
| 99 | + const resolver = zodResolver(z.object({ id: z.number() })); |
| 100 | + |
| 101 | + expectTypeOf(resolver).toEqualTypeOf< |
| 102 | + Resolver<FieldValues, any, { id: number }> |
| 103 | + >(); |
| 104 | + }); |
| 105 | + |
| 106 | + it('should correctly infer the output type from a zod schema when a different input type is specified', () => { |
| 107 | + const schema = z.object({ id: z.string() }); |
| 108 | + const resolver = zodResolver<{ id: string }, any, z.output<typeof schema>>( |
| 109 | + schema, |
| 110 | + ); |
| 111 | + |
| 112 | + expectTypeOf(resolver).toEqualTypeOf< |
| 113 | + Resolver<{ id: string }, any, { id: string }> |
| 114 | + >(); |
| 115 | + }); |
| 116 | + |
| 117 | + it('should correctly infer the output type from a zod schema when a different input type is specified (only input type is specified)', () => { |
| 118 | + const resolver = zodResolver<{ id: string }>(z.object({ id: z.string() })); |
| 119 | + |
| 120 | + expectTypeOf(resolver).toEqualTypeOf< |
| 121 | + Resolver<{ id: string }, any, { id: string }> |
| 122 | + >(); |
| 123 | + }); |
| 124 | + |
| 125 | + it('should correctly infer the output type from a zod schema when different input and output types are specified', () => { |
| 126 | + const resolver = zodResolver< |
| 127 | + { id: string }, |
| 128 | + { context: any }, |
| 129 | + { id: boolean } |
| 130 | + >(z.object({ id: z.number() })); |
| 131 | + |
| 132 | + expectTypeOf(resolver).toEqualTypeOf< |
| 133 | + Resolver<{ id: string }, { context: any }, { id: boolean }> |
| 134 | + >(); |
| 135 | + }); |
| 136 | + |
| 137 | + it('should correctly infer the output type from a Zod schema for the handleSubmit function in useForm', () => { |
| 138 | + const { handleSubmit } = useForm({ |
| 139 | + resolver: zodResolver(z.object({ id: z.number() })), |
| 140 | + }); |
| 141 | + |
| 142 | + expectTypeOf(handleSubmit).parameter(0).toEqualTypeOf< |
| 143 | + SubmitHandler<{ |
| 144 | + id: number; |
| 145 | + }> |
| 146 | + >(); |
| 147 | + }); |
| 148 | + |
| 149 | + it('should correctly infer the output type from a Zod schema when a different input type is specified for the handleSubmit function in useForm', () => { |
| 150 | + const { handleSubmit } = useForm({ |
| 151 | + resolver: zodResolver<{ id: number }>(z.object({ id: z.string() })), |
| 152 | + }); |
| 153 | + |
| 154 | + expectTypeOf(handleSubmit).parameter(0).toEqualTypeOf< |
| 155 | + SubmitHandler<{ |
| 156 | + id: string; |
| 157 | + }> |
| 158 | + >(); |
| 159 | + }); |
| 160 | + |
| 161 | + it('should correctly infer the output type from a Zod schema when different input and output types are specified for the handleSubmit function in useForm', () => { |
| 162 | + const resolver = zodResolver<{ id: string }, any, { id: boolean }>( |
| 163 | + z.object({ id: z.number() }), |
| 164 | + ); |
| 165 | + |
| 166 | + const { handleSubmit } = useForm({ |
| 167 | + resolver, |
| 168 | + }); |
| 169 | + |
| 170 | + expectTypeOf(handleSubmit).parameter(0).toEqualTypeOf< |
| 171 | + SubmitHandler<{ |
| 172 | + id: boolean; |
| 173 | + }> |
| 174 | + >(); |
| 175 | + }); |
92 | 176 | }); |
0 commit comments