|
| 1 | +import { Resolver, SubmitHandler, useForm } from 'react-hook-form'; |
1 | 2 | import { standardSchemaResolver } from '..';
|
2 | 3 | import {
|
3 | 4 | customSchema,
|
|
6 | 7 | schema,
|
7 | 8 | validData,
|
8 | 9 | } from './__fixtures__/data';
|
| 10 | +import { z } from 'zod'; |
9 | 11 |
|
10 | 12 | const shouldUseNativeValidation = false;
|
11 | 13 |
|
@@ -71,4 +73,73 @@ describe('standardSchemaResolver', () => {
|
71 | 73 |
|
72 | 74 | expect(result).toMatchSnapshot();
|
73 | 75 | });
|
| 76 | + |
| 77 | + /** |
| 78 | + * Type inference tests |
| 79 | + */ |
| 80 | + it('should correctly infer the output type from a standardSchema schema', () => { |
| 81 | + const resolver = standardSchemaResolver(z.object({ id: z.number() })); |
| 82 | + |
| 83 | + expectTypeOf(resolver).toEqualTypeOf< |
| 84 | + Resolver<{ id: number }, unknown, { id: number }> |
| 85 | + >(); |
| 86 | + }); |
| 87 | + |
| 88 | + it('should correctly infer the output type from a standardSchema schema using a transform', () => { |
| 89 | + const resolver = standardSchemaResolver( |
| 90 | + z.object({ id: z.number().transform((val) => String(val)) }), |
| 91 | + ); |
| 92 | + |
| 93 | + expectTypeOf(resolver).toEqualTypeOf< |
| 94 | + Resolver<{ id: number }, unknown, { id: string }> |
| 95 | + >(); |
| 96 | + }); |
| 97 | + |
| 98 | + it('should correctly infer the output type from a standardSchema schema when a different input type is specified', () => { |
| 99 | + const schema = z.object({ id: z.number() }).transform(({ id }) => { |
| 100 | + return { id: String(id) }; |
| 101 | + }); |
| 102 | + |
| 103 | + const resolver = standardSchemaResolver< |
| 104 | + { id: number }, |
| 105 | + any, |
| 106 | + z.output<typeof schema> |
| 107 | + >(schema); |
| 108 | + |
| 109 | + expectTypeOf(resolver).toEqualTypeOf< |
| 110 | + Resolver<{ id: number }, any, { id: string }> |
| 111 | + >(); |
| 112 | + }); |
| 113 | + |
| 114 | + it('should correctly infer the output type from a standardSchema schema for the handleSubmit function in useForm', () => { |
| 115 | + const schema = z.object({ id: z.number() }); |
| 116 | + |
| 117 | + const form = useForm({ |
| 118 | + resolver: standardSchemaResolver(schema), |
| 119 | + }); |
| 120 | + |
| 121 | + expectTypeOf(form.watch('id')).toEqualTypeOf<number>(); |
| 122 | + |
| 123 | + expectTypeOf(form.handleSubmit).parameter(0).toEqualTypeOf< |
| 124 | + SubmitHandler<{ |
| 125 | + id: number; |
| 126 | + }> |
| 127 | + >(); |
| 128 | + }); |
| 129 | + |
| 130 | + it('should correctly infer the output type from a standardSchema schema with a transform for the handleSubmit function in useForm', () => { |
| 131 | + const schema = z.object({ id: z.number().transform((val) => String(val)) }); |
| 132 | + |
| 133 | + const form = useForm({ |
| 134 | + resolver: standardSchemaResolver(schema), |
| 135 | + }); |
| 136 | + |
| 137 | + expectTypeOf(form.watch('id')).toEqualTypeOf<number>(); |
| 138 | + |
| 139 | + expectTypeOf(form.handleSubmit).parameter(0).toEqualTypeOf< |
| 140 | + SubmitHandler<{ |
| 141 | + id: string; |
| 142 | + }> |
| 143 | + >(); |
| 144 | + }); |
74 | 145 | });
|
0 commit comments