-
-
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.
feat: support browser native form control (#177)
* feat: support browser native form control * docs(README): update io-ts example
- Loading branch information
Showing
33 changed files
with
937 additions
and
82 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,68 @@ | ||
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 { classValidatorResolver } from '..'; | ||
import { IsNotEmpty } from 'class-validator'; | ||
|
||
class Schema { | ||
@IsNotEmpty() | ||
username: string; | ||
|
||
@IsNotEmpty() | ||
password: string; | ||
} | ||
|
||
interface Props { | ||
onSubmit: (data: FormData) => void; | ||
} | ||
|
||
function TestComponent({ onSubmit }: Props) { | ||
const { register, handleSubmit } = useForm<Schema>({ | ||
resolver: classValidatorResolver(Schema), | ||
shouldUseNativeValidation: true, | ||
}); | ||
|
||
return ( | ||
<form onSubmit={handleSubmit(onSubmit)}> | ||
<input {...register('username')} placeholder="username" /> | ||
|
||
<input {...register('password')} placeholder="password" /> | ||
|
||
<button type="submit">submit</button> | ||
</form> | ||
); | ||
} | ||
|
||
test("form's native validation with Class Validator", async () => { | ||
const handleSubmit = jest.fn(); | ||
render(<TestComponent onSubmit={handleSubmit} />); | ||
|
||
// username | ||
let usernameField = screen.getByPlaceholderText( | ||
/username/i, | ||
) as HTMLInputElement; | ||
expect(usernameField.validity.valid).toBe(true); | ||
expect(usernameField.validationMessage).toBe(''); | ||
|
||
// password | ||
let passwordField = screen.getByPlaceholderText( | ||
/password/i, | ||
) as HTMLInputElement; | ||
expect(passwordField.validity.valid).toBe(true); | ||
expect(passwordField.validationMessage).toBe(''); | ||
|
||
await act(async () => { | ||
user.click(screen.getByText(/submit/i)); | ||
}); | ||
|
||
// username | ||
usernameField = screen.getByPlaceholderText(/username/i) as HTMLInputElement; | ||
expect(usernameField.validity.valid).toBe(false); | ||
expect(usernameField.validationMessage).toBe('username should not be empty'); | ||
|
||
// password | ||
passwordField = screen.getByPlaceholderText(/password/i) as HTMLInputElement; | ||
expect(passwordField.validity.valid).toBe(false); | ||
expect(passwordField.validationMessage).toBe('password should not be empty'); | ||
}); |
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 |
---|---|---|
@@ -0,0 +1,70 @@ | ||
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 Schema, { Type, string } from 'computed-types'; | ||
import { computedTypesResolver } from '..'; | ||
|
||
const USERNAME_REQUIRED_MESSAGE = 'username field is required'; | ||
const PASSWORD_REQUIRED_MESSAGE = 'password field is required'; | ||
|
||
const schema = Schema({ | ||
username: string.min(2).error(USERNAME_REQUIRED_MESSAGE), | ||
password: string.min(2).error(PASSWORD_REQUIRED_MESSAGE), | ||
}); | ||
|
||
type FormData = Type<typeof schema> & { unusedProperty: string }; | ||
|
||
interface Props { | ||
onSubmit: (data: FormData) => void; | ||
} | ||
|
||
function TestComponent({ onSubmit }: Props) { | ||
const { register, handleSubmit } = useForm<FormData>({ | ||
resolver: computedTypesResolver(schema), | ||
shouldUseNativeValidation: true, | ||
}); | ||
|
||
return ( | ||
<form onSubmit={handleSubmit(onSubmit)}> | ||
<input {...register('username')} placeholder="username" /> | ||
|
||
<input {...register('password')} placeholder="password" /> | ||
|
||
<button type="submit">submit</button> | ||
</form> | ||
); | ||
} | ||
|
||
test("form's native validation with computed-types", async () => { | ||
const handleSubmit = jest.fn(); | ||
render(<TestComponent onSubmit={handleSubmit} />); | ||
|
||
// username | ||
let usernameField = screen.getByPlaceholderText( | ||
/username/i, | ||
) as HTMLInputElement; | ||
expect(usernameField.validity.valid).toBe(true); | ||
expect(usernameField.validationMessage).toBe(''); | ||
|
||
// password | ||
let passwordField = screen.getByPlaceholderText( | ||
/password/i, | ||
) as HTMLInputElement; | ||
expect(passwordField.validity.valid).toBe(true); | ||
expect(passwordField.validationMessage).toBe(''); | ||
|
||
await act(async () => { | ||
user.click(screen.getByText(/submit/i)); | ||
}); | ||
|
||
// username | ||
usernameField = screen.getByPlaceholderText(/username/i) as HTMLInputElement; | ||
expect(usernameField.validity.valid).toBe(false); | ||
expect(usernameField.validationMessage).toBe(USERNAME_REQUIRED_MESSAGE); | ||
|
||
// password | ||
passwordField = screen.getByPlaceholderText(/password/i) as HTMLInputElement; | ||
expect(passwordField.validity.valid).toBe(false); | ||
expect(passwordField.validationMessage).toBe(PASSWORD_REQUIRED_MESSAGE); | ||
}); |
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 |
---|---|---|
@@ -0,0 +1,74 @@ | ||
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 t from 'io-ts'; | ||
import * as tt from 'io-ts-types'; | ||
import { ioTsResolver } from '..'; | ||
|
||
const USERNAME_REQUIRED_MESSAGE = 'username field is required'; | ||
const PASSWORD_REQUIRED_MESSAGE = 'password field is required'; | ||
|
||
const schema = t.type({ | ||
username: tt.withMessage(tt.NonEmptyString, () => USERNAME_REQUIRED_MESSAGE), | ||
password: tt.withMessage(tt.NonEmptyString, () => PASSWORD_REQUIRED_MESSAGE), | ||
}); | ||
|
||
interface FormData { | ||
username: string; | ||
password: string; | ||
} | ||
|
||
interface Props { | ||
onSubmit: (data: FormData) => void; | ||
} | ||
|
||
function TestComponent({ onSubmit }: Props) { | ||
const { register, handleSubmit } = useForm<FormData>({ | ||
resolver: ioTsResolver(schema), | ||
shouldUseNativeValidation: true, | ||
}); | ||
|
||
return ( | ||
<form onSubmit={handleSubmit(onSubmit)}> | ||
<input {...register('username')} placeholder="username" /> | ||
|
||
<input {...register('password')} placeholder="password" /> | ||
|
||
<button type="submit">submit</button> | ||
</form> | ||
); | ||
} | ||
|
||
test("form's native validation with io-ts", async () => { | ||
const handleSubmit = jest.fn(); | ||
render(<TestComponent onSubmit={handleSubmit} />); | ||
|
||
// username | ||
let usernameField = screen.getByPlaceholderText( | ||
/username/i, | ||
) as HTMLInputElement; | ||
expect(usernameField.validity.valid).toBe(true); | ||
expect(usernameField.validationMessage).toBe(''); | ||
|
||
// password | ||
let passwordField = screen.getByPlaceholderText( | ||
/password/i, | ||
) as HTMLInputElement; | ||
expect(passwordField.validity.valid).toBe(true); | ||
expect(passwordField.validationMessage).toBe(''); | ||
|
||
await act(async () => { | ||
user.click(screen.getByText(/submit/i)); | ||
}); | ||
|
||
// username | ||
usernameField = screen.getByPlaceholderText(/username/i) as HTMLInputElement; | ||
expect(usernameField.validity.valid).toBe(false); | ||
expect(usernameField.validationMessage).toBe(USERNAME_REQUIRED_MESSAGE); | ||
|
||
// password | ||
passwordField = screen.getByPlaceholderText(/password/i) as HTMLInputElement; | ||
expect(passwordField.validity.valid).toBe(false); | ||
expect(passwordField.validationMessage).toBe(PASSWORD_REQUIRED_MESSAGE); | ||
}); |
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
Oops, something went wrong.