import { render, screen } from '@testing-library/react';
import SelectList from './SelectList';
describe('', () => {
const options = [
{ label: 'option1', value: 'value1' },
{ label: 'option2', value: 'value2' },
{ label: 'option3', value: 'value3' },
].map(({ label, value }: any) => );
it('renders an error message', () => {
render(
{options}
,
);
expect(screen.getByText('Error message')).toBeVisible();
});
it('renders a name', () => {
const { container } = render(
{options}
,
);
// eslint-disable-next-line testing-library/no-container, testing-library/no-node-access
expect(container.querySelector('[name="select_name"]')).toBeVisible();
});
it('handles errorMessage prop change', () => {
const handleChange = jest.fn<
[
{
event: React.ChangeEvent;
value: string;
},
],
// @ts-expect-error - TS2344 - Type 'undefined' does not satisfy the constraint 'any[]'.
undefined
>();
const { rerender } = render(
{options}
,
);
expect(() => {
screen.getByText('Error message');
}).toThrow('Unable to find an element with the text: Error message');
rerender(
{options}
,
);
expect(screen.getByText('Error message')).toBeVisible();
});
it('handles disabled', () => {
const { container } = render(
{options}
,
);
// eslint-disable-next-line testing-library/no-container, testing-library/no-node-access
expect(container.querySelector('select[disabled]')).toBeVisible();
});
it('renders a placeholder', () => {
const { container } = render(
{options}
,
);
// eslint-disable-next-line testing-library/no-container, testing-library/no-node-access
expect(container.querySelector('option')).toBeDisabled();
});
it('handles disabled options', () => {
const { container } = render(
{options}
,
);
// eslint-disable-next-line testing-library/no-container, testing-library/no-node-access
expect(container.querySelector('option[value="value4"]')).toBeDisabled();
});
it('renders a label', () => {
render(
{options}
,
);
expect(screen.getByText('Label for the select list')).toBeVisible();
});
it('renders helper text', () => {
render(
{options}
,
);
expect(screen.getByText('Helper text for the select list')).toBeVisible();
});
it('hides the helper text when an error message is shown', () => {
render(
{options}
,
);
expect(() => {
screen.getByText('Helper text for the select list');
}).toThrow('Unable to find an element with the text: Helper text for the select list');
});
describe('size', () => {
it('adds a "medium" classname by default', () => {
const { container } = render(
{options}
,
);
// eslint-disable-next-line testing-library/no-container, testing-library/no-node-access
expect(container.querySelector('.medium')).toBeVisible();
});
it('adds a "large" classname when size is set to "lg"', () => {
const { container } = render(
{options}
,
);
// eslint-disable-next-line testing-library/no-container, testing-library/no-node-access
expect(container.querySelector('.large')).toBeVisible();
});
});
it('handles disabling option group', () => {
const { container } = render(
{}}>
{options}
,
);
// eslint-disable-next-line testing-library/no-container, testing-library/no-node-access
expect(container.querySelector('optgroup[label="Foo group"]')).toBeDisabled();
// eslint-disable-next-line testing-library/no-container, testing-library/no-node-access
expect(container.querySelector('option[value="Foo-value1"]')).toBeDisabled();
// eslint-disable-next-line testing-library/no-container, testing-library/no-node-access
expect(container.querySelector('option[value="Foo-value2"]')).toBeDisabled();
});
});