-
-
Notifications
You must be signed in to change notification settings - Fork 204
V2 - TypeScript/Tests improvements + Vest fix #110
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
627cc39
2721382
87992b0
e4c750a
0cfd849
dc229cd
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| name: Compressed Size | ||
|
|
||
| on: [pull_request] | ||
|
|
||
| jobs: | ||
| build: | ||
| runs-on: ubuntu-latest | ||
|
|
||
| steps: | ||
| - uses: actions/checkout@v2 | ||
| - uses: preactjs/compressed-size-action@v2 | ||
| with: | ||
| repo-token: '${{ secrets.GITHUB_TOKEN }}' |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,10 +1,11 @@ | ||
| module.exports = { | ||
| preset: 'ts-jest', | ||
| testEnvironment: 'node', | ||
| testEnvironment: 'jsdom', | ||
| restoreMocks: true, | ||
| testMatch: ['**/__tests__/**/*.+(js|jsx|ts|tsx)'], | ||
| transformIgnorePatterns: ['[/\\\\]node_modules[/\\\\].+\\.(js|jsx)$'], | ||
| moduleNameMapper: { | ||
| '^@hookform/resolvers$': '<rootDir>/src', | ||
| }, | ||
| setupFilesAfterEnv: ['@testing-library/jest-dom/extend-expect'], | ||
| }; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| import React from 'react'; | ||
| import { render, screen, act } from '@testing-library/react'; | ||
| import user from '@testing-library/user-event'; | ||
| import { useForm } from 'react-hook-form'; | ||
| import * as Joi from 'joi'; | ||
| import { joiResolver } from '..'; | ||
|
|
||
| const schema = Joi.object({ | ||
| username: Joi.string().required(), | ||
| password: Joi.string().required(), | ||
| }); | ||
|
|
||
| interface FormData { | ||
| username: string; | ||
| password: string; | ||
| } | ||
|
|
||
| interface Props { | ||
| onSubmit: (data: FormData) => void; | ||
| } | ||
|
|
||
| function TestComponent({ onSubmit }: Props) { | ||
| const { register, errors, handleSubmit } = useForm<FormData>({ | ||
| resolver: joiResolver(schema), // Useful to check TypeScript regressions | ||
| }); | ||
|
|
||
| return ( | ||
| <form onSubmit={handleSubmit(onSubmit)}> | ||
| <input name="username" ref={register} /> | ||
| {errors.username && <span role="alert">{errors.username.message}</span>} | ||
|
|
||
| <input name="password" ref={register} /> | ||
| {errors.password && <span role="alert">{errors.password.message}</span>} | ||
|
|
||
| <button type="submit">submit</button> | ||
| </form> | ||
| ); | ||
| } | ||
|
|
||
| test("form's validation with Joi and TypeScript's integration", async () => { | ||
| const handleSubmit = jest.fn(); | ||
| render(<TestComponent onSubmit={handleSubmit} />); | ||
|
|
||
| expect(screen.queryAllByRole(/alert/i)).toHaveLength(0); | ||
|
|
||
| await act(async () => { | ||
| user.click(screen.getByText(/submit/i)); | ||
| }); | ||
|
|
||
| expect( | ||
| screen.getByText(/"username" is not allowed to be empty/i), | ||
| ).toBeInTheDocument(); | ||
| expect( | ||
| screen.getByText(/"password" is not allowed to be empty/i), | ||
| ).toBeInTheDocument(); | ||
| expect(handleSubmit).not.toHaveBeenCalled(); | ||
| }); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,31 +3,67 @@ import { joiResolver } from '..'; | |
|
|
||
| const schema = Joi.object({ | ||
| username: Joi.string().alphanum().min(3).max(30).required(), | ||
|
|
||
| password: Joi.string().pattern(new RegExp('^[a-zA-Z0-9]{3,30}$')), | ||
|
|
||
| password: Joi.string().pattern(new RegExp('^[a-zA-Z0-9]{3,30}$')).required(), | ||
| repeatPassword: Joi.ref('password'), | ||
|
|
||
| accessToken: [Joi.string(), Joi.number()], | ||
|
|
||
| birthYear: Joi.number().integer().min(1900).max(2013), | ||
|
|
||
| email: Joi.string().email({ | ||
| minDomainSegments: 2, | ||
| tlds: { allow: ['com', 'net'] }, | ||
| }), | ||
| tags: Joi.array().items(Joi.string()).required(), | ||
| enabled: Joi.boolean().required(), | ||
| }); | ||
|
|
||
| interface Data { | ||
| username: string; | ||
| password: string; | ||
| repeatPassword: string; | ||
| accessToken?: number | string; | ||
| birthYear?: number; | ||
| email?: string; | ||
| tags: string[]; | ||
| enabled: boolean; | ||
| } | ||
|
|
||
| describe('joiResolver', () => { | ||
| it('should return correct value', async () => { | ||
| const data = { username: 'abc', birthYear: 1994 }; | ||
| expect(await joiResolver(schema)(data)).toEqual({ | ||
| values: data, | ||
| errors: {}, | ||
| }); | ||
| it('should return values from joiResolver when validation pass', async () => { | ||
| const data: Data = { | ||
| username: 'Doe', | ||
| password: 'Password123', | ||
| repeatPassword: 'Password123', | ||
| birthYear: 2000, | ||
| email: 'john@doe.com', | ||
| tags: ['tag1', 'tag2'], | ||
| enabled: true, | ||
| }; | ||
|
|
||
| const result = await joiResolver(schema)(data); | ||
|
|
||
| expect(result).toEqual({ errors: {}, values: data }); | ||
| }); | ||
|
|
||
| it('should return errors', async () => { | ||
| expect(await joiResolver(schema)({})).toMatchSnapshot(); | ||
| it('should return a single error from joiResolver when validation fails', async () => { | ||
| const data = { | ||
| password: '___', | ||
| email: '', | ||
| birthYear: 'birthYear', | ||
| }; | ||
|
|
||
| const result = await joiResolver(schema)(data); | ||
|
|
||
| expect(result).toMatchSnapshot(); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Minor: (don't have to change as well) maybe be specific with the error? I normally use snapshots for a successful responses. |
||
| }); | ||
|
|
||
| it('should return all the errors from joiResolver when validation fails with `validateAllFieldCriteria` set to true', async () => { | ||
| const data = { | ||
| password: '___', | ||
| email: '', | ||
| birthYear: 'birthYear', | ||
| }; | ||
|
|
||
| const result = await joiResolver(schema)(data, undefined, true); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. just a quick note, going to change this maybe call it
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ok, sound good to me. I'll change it in another PR. Does
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Yes.
Yes, That would be awesome. I am not sure how easy it is going to be for all resolvers. |
||
|
|
||
| expect(result).toMatchSnapshot(); | ||
| }); | ||
| }); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1,2 @@ | ||
| export * from './joi'; | ||
| export * from './types'; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| import { | ||
| FieldValues, | ||
| ResolverResult, | ||
| UnpackNestedValue, | ||
| } from 'react-hook-form'; | ||
| import type { AsyncValidationOptions, Schema } from 'joi'; | ||
|
|
||
| export type Resolver = <T extends Schema>( | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. here is the type in the react-hook-form repo: I think maybe it's better to leave that at
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's not the same type From react-hook-form: export type Resolver<
TFieldValues extends FieldValues = FieldValues,
TContext extends object = object
> = (
values: UnpackNestedValue<TFieldValues>,
context?: TContext,
validateAllFieldCriteria?: boolean,
) => Promise<ResolverResult<TFieldValues>> | ResolverResult<TFieldValues>;And from resolvers: export type Resolver = <T extends Schema>(
schema: T,
options?: AsyncValidationOptions,
) => <TFieldValues extends FieldValues, TContext>(
values: UnpackNestedValue<TFieldValues>,
context?: TContext,
validateAllFieldCriteria?: boolean,
) => Promise<ResolverResult<TFieldValues>>;The generic type isn't placed at the same point. This update improve resolver's return type. But I'm open for a better idea 😉 I tried to centralized but it breaks severals things: react-hook-form/react-hook-form#3821
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. all good, I realize that after reviewing the rest of the resolvers. |
||
| schema: T, | ||
| options?: AsyncValidationOptions, | ||
| ) => <TFieldValues extends FieldValues, TContext>( | ||
| values: UnpackNestedValue<TFieldValues>, | ||
| context?: TContext, | ||
| validateAllFieldCriteria?: boolean, | ||
| ) => Promise<ResolverResult<TFieldValues>>; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| import React from 'react'; | ||
jorisre marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| import { render, screen, act } from '@testing-library/react'; | ||
| import user from '@testing-library/user-event'; | ||
| import { useForm } from 'react-hook-form'; | ||
| import { object, string, Infer, size } from 'superstruct'; | ||
| import { superstructResolver } from '..'; | ||
|
|
||
| const schema = object({ | ||
| username: size(string(), 2), | ||
| password: size(string(), 6), | ||
| }); | ||
|
|
||
| type FormData = Infer<typeof schema>; | ||
|
|
||
| interface Props { | ||
| onSubmit: (data: FormData) => void; | ||
| } | ||
|
|
||
| function TestComponent({ onSubmit }: Props) { | ||
| const { register, errors, handleSubmit } = useForm<FormData>({ | ||
| resolver: superstructResolver(schema), // Useful to check TypeScript regressions | ||
| }); | ||
|
|
||
| return ( | ||
| <form onSubmit={handleSubmit(onSubmit)}> | ||
| <input name="username" ref={register} /> | ||
| {errors.username && <span role="alert">{errors.username.message}</span>} | ||
|
|
||
| <input name="password" ref={register} /> | ||
| {errors.password && <span role="alert">{errors.password.message}</span>} | ||
|
|
||
| <button type="submit">submit</button> | ||
| </form> | ||
| ); | ||
| } | ||
|
|
||
| test("form's validation with Superstruct and TypeScript's integration", async () => { | ||
| const handleSubmit = jest.fn(); | ||
| render(<TestComponent onSubmit={handleSubmit} />); | ||
|
|
||
| expect(screen.queryAllByRole(/alert/i)).toHaveLength(0); | ||
|
|
||
| await act(async () => { | ||
| user.click(screen.getByText(/submit/i)); | ||
| }); | ||
|
|
||
| expect( | ||
| screen.getByText( | ||
| /Expected a string with a length of `2` but received one with a length of `0`/i, | ||
| ), | ||
| ).toBeInTheDocument(); | ||
| expect( | ||
| screen.getByText( | ||
| /Expected a string with a length of `6` but received one with a length of `0`/i, | ||
| ), | ||
| ).toBeInTheDocument(); | ||
| expect(handleSubmit).not.toHaveBeenCalled(); | ||
| }); | ||
Uh oh!
There was an error while loading. Please reload this page.