import { act, cleanup, fireEvent, render } from '@testing-library/react'
import React from 'react'
import { beforeEach, describe, expect, test } from 'vitest'
import { useForm } from '../lib'
import { Insight, Util } from './shared'
beforeEach(cleanup)
describe('Field', () => {
test('Basic Form', async () => {
function Component() {
const form = useForm({ username: '', password: '' })
const { field } = form
return (
)
}
render()
async function inputIntoForm(id: string, value: string) {
const node = await Util.find(id)
await act(() => {
Util.writeToField(node, value)
})
await Insight.verify({ form: { [id]: value } })
}
await inputIntoForm('username', 'foo')
await inputIntoForm('password', 'bar')
})
test.skip('Checkbox', async () => {
function Component() {
const { field, form } = useForm({ cool: false })
return (
)
}
render()
const field = await Util.find('field')
expect(field.checked).toBe(false)
await Insight.verify({ cool: false })
await act(() => {
// Bugged for now
fireEvent.click(field)
})
expect(field.checked).toBe(true)
await Insight.verify({ cool: true })
})
test('Select', async () => {
function Component() {
const { form, field } = useForm({ letter: '' })
return (
<>
>
)
}
render()
const field = await Util.find('field')
const value = 'b'
await act(() => {
fireEvent.change(field, { target: { value } })
})
expect(field.value).toBe(value)
await Insight.verify({ letter: value })
})
test('Field sync', async () => {
const value = 'foo'
function Component() {
const { field, form } = useForm({ name: '' })
return (
)
}
render()
const a = await Util.find('a')
const b = await Util.find('b')
await act(() => {
Util.writeToField(a, value)
})
await Insight.verify({ name: value })
expect(a.value).toBe(b.value)
})
})