/// import { h, defineComponent, ref } from 'vue-demi' import { render, waitFor } from '@testing-library/vue' import '@testing-library/jest-dom' import { createFormFactory, type FieldApi, provideFormContext, useForm, } from '../index' import userEvent from '@testing-library/user-event' import * as React from 'react' const user = userEvent.setup() describe('useForm', () => { it('preserved field state', async () => { type Person = { firstName: string lastName: string } const formFactory = createFormFactory() const Comp = defineComponent(() => { const form = formFactory.useForm() provideFormContext({ formApi: form }) return () => ( {({ field, }: { field: FieldApi }) => ( field.handleChange((e.target as HTMLInputElement).value) } /> )} ) }) 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() const Comp = defineComponent(() => { const form = formFactory.useForm({ defaultValues: { firstName: 'FirstName', lastName: 'LastName', }, }) form.provideFormContext() return () => ( {({ field, }: { field: FieldApi }) =>

{field.state.value}

}
) }) const { findByText, queryByText } = render(Comp) expect(await findByText('FirstName')).toBeInTheDocument() expect(queryByText('LastName')).not.toBeInTheDocument() }) it('should handle submitting properly', async () => { const Comp = defineComponent(() => { const submittedData = ref<{ firstName: string }>() const form = useForm({ defaultValues: { firstName: 'FirstName', }, onSubmit: (data) => { submittedData.value = data }, }) form.provideFormContext() return () => ( {({ field, }: { field: FieldApi<{ firstName: string }, 'firstName', never, never> }) => { return ( field.handleChange((e.target as HTMLInputElement).value) } placeholder={'First name'} /> ) }} {submittedData.value && (

Submitted data: {submittedData.value.firstName}

)}
) }) const { findByPlaceholderText, getByText } = render(Comp) const input = await findByPlaceholderText('First name') await user.clear(input) await user.type(input, 'OtherName') await user.click(getByText('Submit')) await waitFor(() => expect(getByText('Submitted data: OtherName')).toBeInTheDocument(), ) }) })