Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 13 additions & 4 deletions app/components/form/fields/NumberField.browser.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@ function Harness({
const form = useForm({ defaultValues: { count: defaultValue } })
const [submitted, setSubmitted] = useState<number>()
return (
<form onSubmit={form.handleSubmit(({ count }) => setSubmitted(count))}>
<form
aria-label="Count form"
onSubmit={form.handleSubmit(({ count }) => setSubmitted(count))}
>
<NumberField
name="count"
label="Count"
Expand All @@ -38,23 +41,29 @@ function Harness({
)
}

// Error text lookups are scoped to the form because the error is also
// announced through react-aria's live region, a copy appended to the body
// that lingers for 7s. A body-wide getByText would match both and stall.

test('rejects a fractional value already in form state', async () => {
const screen = await render(<Harness defaultValue={2.5} />)
const form = screen.getByRole('form', { name: 'Count form' })
await screen.getByRole('button', { name: 'Save' }).click()
await expect.element(screen.getByText('Must be a whole number')).toBeVisible()
await expect.element(form.getByText('Must be a whole number')).toBeVisible()
await expect.element(screen.getByText('Submitted: none')).toBeVisible()
})

test('keeps a typed fraction, blocks submit, and clears the error when corrected', async () => {
const screen = await render(<Harness />)
const form = screen.getByRole('form', { name: 'Count form' })
const input = screen.getByRole('textbox', { name: 'Count' })
await input.fill('2.5')
await screen.getByRole('button', { name: 'Save' }).click()
await expect.element(input).toHaveValue('2.5')
await expect.element(screen.getByText('Must be a whole number')).toBeVisible()
await expect.element(form.getByText('Must be a whole number')).toBeVisible()
await expect.element(screen.getByText('Submitted: none')).toBeVisible()
await input.fill('3')
await expect.element(screen.getByText('Must be a whole number')).not.toBeInTheDocument()
await expect.element(form.getByText('Must be a whole number')).not.toBeInTheDocument()
await screen.getByRole('button', { name: 'Save' }).click()
await expect.element(screen.getByText('Submitted: 3')).toBeVisible()
})
Expand Down
Loading