Files

96 lines
3.2 KiB
TypeScript
Raw Permalink Normal View History

2026-10-05 18:10:18 -04:00
/** @vitest-environment jsdom */
2026-10-05 23:28:29 -04:00
import { createRef, useImperativeHandle, type FocusEventHandler, type Ref } from 'react';
2026-10-05 18:10:18 -04:00
import { act, cleanup, fireEvent, render, screen } from '@testing-library/react';
import { afterEach, describe, expect, it, vi } from 'vitest';
import { useAppForm, type AppForm } from '@/components/form';
afterEach(cleanup);
type Values = { name: string; memory: number | null };
function Harness({
2026-10-05 23:28:29 -04:00
formRef,
2026-10-05 18:10:18 -04:00
onNameBlur,
}: {
2026-10-05 23:28:29 -04:00
formRef: Ref<AppForm<Values>>;
onNameBlur?: FocusEventHandler<HTMLInputElement>;
2026-10-05 18:10:18 -04:00
}) {
const form = useAppForm({ defaultValues: { name: '', memory: 512 } as Values });
2026-10-08 16:53:20 -04:00
2026-10-05 23:28:29 -04:00
useImperativeHandle(formRef, () => form, [form]);
2026-10-05 18:10:18 -04:00
return (
<>
<form.AppField
2026-10-08 16:53:20 -04:00
name='name'
2026-10-05 18:10:18 -04:00
validators={{ onChange: ({ value }) => (value === 'bad' ? 'invalid CPU limit.' : undefined) }}
>
{(field) => (
<field.TextField
2026-10-08 16:53:20 -04:00
label='Name'
description='The display name.'
aria-describedby='external-hint'
2026-10-05 18:10:18 -04:00
onBlur={onNameBlur}
/>
)}
</form.AppField>
2026-10-08 16:53:20 -04:00
<form.AppField name='memory'>{(field) => <field.NumberField label='Memory' />}</form.AppField>
2026-10-05 18:10:18 -04:00
</>
);
}
2026-10-05 23:28:29 -04:00
const renderHarness = (onNameBlur?: FocusEventHandler<HTMLInputElement>) => {
const formRef = createRef<AppForm<Values>>();
2026-10-08 16:53:20 -04:00
2026-10-05 23:28:29 -04:00
render(<Harness formRef={formRef} onNameBlur={onNameBlur} />);
2026-10-05 18:10:18 -04:00
2026-10-05 23:28:29 -04:00
return () => formRef.current!;
2026-10-05 18:10:18 -04:00
};
describe('form fields', () => {
it('stores null when a number field is cleared', () => {
const form = renderHarness();
const input = screen.getByRole('textbox', { name: 'Memory' });
fireEvent.change(input, { target: { value: '' } });
fireEvent.blur(input);
expect(form().state.values.memory).toBeNull();
});
it('links the description to the control and keeps caller ids', () => {
renderHarness();
const input = screen.getByRole('textbox', { name: 'Name' });
expect(input).not.toHaveAttribute('aria-invalid');
expect(input).toHaveAccessibleDescription('The display name.');
expect(input.getAttribute('aria-describedby')?.split(' ')).toContain('external-hint');
});
it('marks the control invalid and describes it with the error, preserving its casing', () => {
renderHarness();
const input = screen.getByRole('textbox', { name: 'Name' });
act(() => {
fireEvent.change(input, { target: { value: 'bad' } });
});
expect(input).toHaveAttribute('aria-invalid', 'true');
expect(input).toHaveAccessibleDescription('Invalid CPU limit.');
expect(screen.getByText('Invalid CPU limit.')).toBeInTheDocument();
});
it('runs both the field blur handler and the caller onBlur', () => {
const onBlur = vi.fn();
const form = renderHarness(onBlur);
const input = screen.getByRole('textbox', { name: 'Name' });
fireEvent.focus(input);
fireEvent.blur(input);
expect(onBlur).toHaveBeenCalledOnce();
expect(form().getFieldMeta('name')?.isBlurred).toBe(true);
});
});