Fix defaults (#403)

* chore: inital work to migrate to Query's alpha package setup

* chore: update deps

* chore: fix build issues

* chore: disable no-children-prop rule

* fix: user input now works

* fix: useForm defaults now work as-expected
This commit is contained in:
Corbin Crutchley
2023-07-04 20:08:03 -07:00
committed by GitHub
parent 74ac9f4b9c
commit 123e7a0f7e
4 changed files with 136 additions and 11 deletions

View File

@@ -0,0 +1,73 @@
import { fireEvent, render } from "@testing-library/react";
import userEvent from "@testing-library/user-event";
import '@testing-library/jest-dom'
import * as React from 'react'
import { createFormFactory } from '..'
const user = userEvent.setup()
describe('useForm', () => {
it('preserves field state', async () => {
type Person = {
firstName: string
lastName: string
}
const formFactory = createFormFactory<Person>()
function Comp() {
const form = formFactory.useForm()
return (
<form.Provider>
<form.Field
name="firstName"
defaultValue={""}
children={(field) => {
return <input data-testid="fieldinput" {...field.getInputProps()} />
}}
/>
</form.Provider>
)
}
const { getByTestId, queryByText } = render(<Comp />)
const input = getByTestId("fieldinput");
expect(queryByText('FirstName')).not.toBeInTheDocument()
await user.type(input, "FirstName")
expect(input).toHaveValue("FirstName")
})
it('should allow default values to be set', async () => {
type Person = {
firstName: string
lastName: string
}
const formFactory = createFormFactory<Person>()
function Comp() {
const form = formFactory.useForm({
defaultValues: {
firstName: 'FirstName',
lastName: 'LastName',
},
})
return (
<form.Provider>
<form.Field
name="firstName"
children={(field) => {
return <p>{field.getInputProps().value}</p>
}}
/>
</form.Provider>
)
}
const { findByText, queryByText } = render(<Comp />)
expect(await findByText('FirstName')).toBeInTheDocument()
expect(queryByText('LastName')).not.toBeInTheDocument()
})
})