-
-
Notifications
You must be signed in to change notification settings - Fork 161
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix: vest validate all criteria + better TypeSript support + add tests
* test: add components test to ensure TypeScript is working as expected Related to #97 * chore: improve zod resolvers types * chore(eslint): remove warnings * test: add severals tests & improve typings * chore: setup compressed-size * test: update describe
- Loading branch information
Showing
34 changed files
with
1,007 additions
and
628 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }}' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'], | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
export * from './joi'; | ||
export * from './types'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>( | ||
schema: T, | ||
options?: AsyncValidationOptions, | ||
) => <TFieldValues extends FieldValues, TContext>( | ||
values: UnpackNestedValue<TFieldValues>, | ||
context?: TContext, | ||
validateAllFieldCriteria?: boolean, | ||
) => Promise<ResolverResult<TFieldValues>>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
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 { 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(); | ||
}); |
Oops, something went wrong.