import { render, screen } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import { describe, expect, it, vi } from 'vitest';
import { Field } from './Field';
describe('Field', () => {
it('links the label to the input via htmlFor / id', () => {
render();
const input = screen.getByLabelText('Email');
expect(input).toBeInTheDocument();
});
it('forwards the provided id when given', () => {
render();
expect(screen.getByLabelText('Email')).toHaveAttribute('id', 'my-email');
});
it('renders a hint when no error is present', () => {
render();
expect(screen.getByText('help text')).toBeInTheDocument();
});
it('renders the error instead of the hint and marks aria-invalid', () => {
render();
expect(screen.queryByText('ignored')).not.toBeInTheDocument();
expect(screen.getByText('bad')).toBeInTheDocument();
expect(screen.getByLabelText('N')).toHaveAttribute('aria-invalid', 'true');
});
it('fires onChange when typing', async () => {
const onChange = vi.fn();
render();
await userEvent.type(screen.getByLabelText('N'), 'a');
expect(onChange).toHaveBeenCalled();
});
});