2023-03-03 22:08:54 +00:00
|
|
|
import { act, cleanup, fireEvent, render } from '@testing-library/react'
|
|
|
|
import React from 'react'
|
2023-01-31 00:26:27 +00:00
|
|
|
import { beforeEach, describe, expect, test } from 'vitest'
|
|
|
|
|
|
|
|
import { useForm } from '../lib'
|
2023-03-03 22:08:54 +00:00
|
|
|
import { Insight, Util } from './shared'
|
2023-01-31 00:26:27 +00:00
|
|
|
|
|
|
|
beforeEach(cleanup)
|
|
|
|
|
|
|
|
describe('Field', () => {
|
|
|
|
test('Basic Form', async () => {
|
2023-03-03 22:08:54 +00:00
|
|
|
function Component() {
|
2023-01-31 00:26:27 +00:00
|
|
|
const form = useForm({ username: '', password: '' })
|
|
|
|
const { field } = form
|
|
|
|
return (
|
|
|
|
<form
|
|
|
|
onSubmit={(e) => {
|
|
|
|
e.preventDefault()
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
<input data-testid="username" {...field('username')} />
|
|
|
|
<input data-testid="password" {...field('password')} />
|
|
|
|
<button data-testid="submit" type="submit">
|
|
|
|
Go
|
|
|
|
</button>
|
|
|
|
<Insight.Portal data={form} />
|
|
|
|
</form>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2023-03-03 22:08:54 +00:00
|
|
|
render(<Component />)
|
2023-01-31 00:26:27 +00:00
|
|
|
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')
|
|
|
|
})
|
|
|
|
|
2023-03-03 22:08:54 +00:00
|
|
|
test.skip('Checkbox', async () => {
|
|
|
|
function Component() {
|
|
|
|
const { field, form } = useForm({ cool: false })
|
2023-01-31 00:26:27 +00:00
|
|
|
return (
|
2023-03-03 22:08:54 +00:00
|
|
|
<form>
|
|
|
|
<input
|
|
|
|
data-testid="field"
|
|
|
|
type="checkbox"
|
|
|
|
{...field('cool', {
|
|
|
|
setter: 'checked',
|
|
|
|
getter: 'onChange',
|
|
|
|
extractor: (e) => e.target.checked,
|
|
|
|
})}
|
|
|
|
/>
|
2023-01-31 00:26:27 +00:00
|
|
|
<Insight.Portal data={form} />
|
2023-03-03 22:08:54 +00:00
|
|
|
</form>
|
2023-01-31 00:26:27 +00:00
|
|
|
)
|
|
|
|
}
|
2023-03-03 22:08:54 +00:00
|
|
|
|
2023-01-31 00:26:27 +00:00
|
|
|
render(<Component />)
|
2023-03-03 22:08:54 +00:00
|
|
|
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 (
|
|
|
|
<>
|
|
|
|
<select data-testid="field" {...field('letter')}>
|
|
|
|
<option value="a">A</option>
|
|
|
|
<option value="b">B</option>
|
|
|
|
<option value="c">C</option>
|
|
|
|
</select>
|
|
|
|
<Insight.Portal data={form} />
|
|
|
|
</>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
render(<Component />)
|
|
|
|
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 })
|
2023-01-31 00:26:27 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
test('Field sync', async () => {
|
|
|
|
const value = 'foo'
|
2023-03-03 22:08:54 +00:00
|
|
|
function Component() {
|
2023-01-31 00:26:27 +00:00
|
|
|
const { field, form } = useForm({ name: '' })
|
|
|
|
return (
|
|
|
|
<form>
|
|
|
|
<input {...field('name')} data-testid="a" />
|
|
|
|
<input {...field('name')} data-testid="b" />
|
|
|
|
<Insight.Portal data={form} />
|
|
|
|
</form>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
render(<Component />)
|
|
|
|
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)
|
|
|
|
})
|
|
|
|
})
|