import Info from '../Info/Info';
import { Input } from '../Inputs/Input/Input';
import { TextArea } from '../Inputs/TextArea/TextArea';
import { mockMatchMedia, render, screen, userEvent } from '../test-utils';
import Field from './Field';
mockMatchMedia();
describe('Field', () => {
it('should render label', () => {
render(
,
);
expect(screen.getByLabelText(/Phone number/)).toBeInTheDocument();
expect(screen.getByRole('textbox')).not.toHaveAttribute('aria-describedby');
});
it('should render help text if provided', () => {
render(
,
);
const textbox = screen.getByRole('textbox', { description: /This is help text/ });
expect(textbox).toBeInTheDocument();
expect(textbox).not.toBeInvalid();
});
it('should render error text if provided', () => {
render(
,
);
const textbox = screen.getByRole('textbox', { description: 'This is error text' });
expect(textbox).toBeInTheDocument();
expect(textbox).toBeInvalid();
});
it('should shows error text and help text when both are provided', () => {
render(
,
);
expect(screen.getByRole('textbox', { description: /This is error text/ })).toBeInTheDocument();
expect(screen.getByText('This is help text')).toBeInTheDocument();
expect(screen.getByLabelText(/Phone number/)).toHaveAttribute('aria-describedby');
});
it('should shows message and description when both are provided', () => {
render(
,
);
expect(screen.getByRole('textbox', { description: /This is error text/ })).toBeInTheDocument();
expect(screen.getByText('This is help text')).toBeInTheDocument();
expect(screen.getByLabelText(/Phone number/)).toHaveAttribute('aria-describedby');
});
it("should allow for InlinePrompt's StatusIcon override via `messageIconLabel` prop", () => {
const customIconLabel = 'My custom icon label';
render(
,
);
expect(screen.queryByLabelText('Error:')).not.toBeInTheDocument();
expect(screen.getByLabelText(customIconLabel)).toBeInTheDocument();
});
it('should show or hide (Optional) suffix depending on required prop', () => {
render(
,
);
expect(screen.getByText('(Optional)')).toBeInTheDocument();
render(
,
);
expect(screen.getByLabelText('Phone number')).toBeInTheDocument();
});
it('avoids triggering button within label inadvertently', async () => {
const handleClick = jest.fn();
render(
Phone number{' '}
}
>
,
);
const button = screen.getByRole('button');
button.addEventListener('click', handleClick);
const label = screen.getByText('Phone number');
await userEvent.click(label);
expect(handleClick).not.toHaveBeenCalled();
});
it('allows nesting-based label association', async () => {
const handleClick = jest.fn();
render(
,
);
expect(screen.getByRole('textbox')).not.toHaveAttribute('id');
const label = screen.getByText('Phone number');
await userEvent.click(label);
});
it('should show loading spinner when messageLoading is true', () => {
render(
,
);
expect(screen.getByTestId('InlinePrompt_ProcessIndicator')).toBeInTheDocument();
expect(screen.getByText('Processing your request')).toBeInTheDocument();
});
describe('TextArea character count', () => {
it('renders counter when TextArea has maxLength', () => {
render(
,
);
expect(screen.getByText('5/200')).toBeInTheDocument();
});
it('does not render counter when TextArea has no maxLength', () => {
render(
,
);
expect(screen.queryByText(/\/\d+/)).not.toBeInTheDocument();
});
it('includes counter id in aria-describedby of the textarea', () => {
render(
,
);
const textarea = screen.getByRole('textbox');
const counter = screen.getByText('5/200');
expect(textarea).toHaveAttribute('aria-describedby', expect.stringContaining(counter.id));
});
it('does not have role=status below 80% threshold', () => {
render(
,
);
const counter = screen.getByText('5/200');
expect(counter).not.toHaveAttribute('role');
expect(counter).not.toHaveAttribute('aria-live');
});
it('has role=status and aria-live=polite at 80% threshold', () => {
const value = 'x'.repeat(160);
render(
,
);
const counter = screen.getByText('160/200');
expect(counter).toHaveAttribute('role', 'status');
expect(counter).toHaveAttribute('aria-live', 'polite');
expect(counter).toHaveAttribute('aria-atomic', 'true');
});
it('updates counter when text changes', () => {
const { rerender } = render(
,
);
expect(screen.getByText('2/200')).toBeInTheDocument();
rerender(
,
);
expect(screen.getByText('11/200')).toBeInTheDocument();
});
it('provides accessible aria-label on the counter', () => {
render(
,
);
const counter = screen.getByText('5/200');
expect(counter).toHaveAttribute('aria-label', '5 of 200 characters used');
});
});
});