import Info from '../info/Info';
import { Input } from '../inputs/Input';
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();
});
});