import { render, screen } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import { Radio } from '../Radio';
const props = {
onChange: jest.fn()
};
describe('Radio', () => {
test('controlled', () => {
const { asFragment } = render();
expect(asFragment()).toMatchSnapshot();
});
test('uncontrolled', () => {
const { asFragment } = render();
expect(asFragment()).toMatchSnapshot();
});
test('isDisabled', () => {
const { asFragment } = render();
expect(asFragment()).toMatchSnapshot();
});
test('labelPosition is "start"', () => {
const { asFragment } = render(
);
expect(asFragment()).toMatchSnapshot();
});
test('isLabelWrapped', () => {
const { asFragment } = render(
);
expect(asFragment()).toMatchSnapshot();
});
test('label is string', () => {
const { asFragment } = render();
expect(asFragment()).toMatchSnapshot();
});
test('label is function', () => {
const functionLabel = () =>
Header
;
const { asFragment } = render(
);
expect(asFragment()).toMatchSnapshot();
});
test('label is node', () => {
const { asFragment } = render(
Header} id="check" isChecked aria-label="check" name="check" />
);
expect(asFragment()).toMatchSnapshot();
});
test('passing class', () => {
const { asFragment } = render(
);
expect(asFragment()).toMatchSnapshot();
});
test('passing HTML attribute', () => {
const { asFragment } = render(
);
expect(asFragment()).toMatchSnapshot();
});
test('Radio passes value and event to onChange handler', async () => {
const user = userEvent.setup();
render();
await user.click(screen.getByRole('radio'));
expect(props.onChange).toHaveBeenCalledWith(expect.any(Object), true);
});
test('Radio description', () => {
render();
expect(screen.getByText('Text description...')).toBeInTheDocument();
});
test('Radio body', () => {
render();
expect(screen.getByText('Text body...')).toBeInTheDocument();
});
test('should throw console error when no id is given', () => {
const myMock = jest.fn();
global.console = { ...global.console, error: myMock };
render();
expect(myMock).toHaveBeenCalled();
});
test('Renders with the label wrapper if isLabelWrapped is provided', () => {
render();
expect(screen.getByRole('radio').parentElement?.tagName).toBe('LABEL');
});
test('Renders with span element around the inner label text if isLabelWrapped is provided', () => {
const labelText = 'test radio label';
render();
expect(screen.getByText(labelText).tagName).toBe('SPAN');
});
test('Renders with the provided component although isLabelWrapped is provided', () => {
render();
expect(screen.getByRole('radio').parentElement?.tagName).toBe('H3');
});
test('Renders with the label wrapper if component is set to label', () => {
render();
expect(screen.getByRole('radio').parentElement?.tagName).toBe('LABEL');
});
test('Renders with span element around the inner label text if component is set to label', () => {
const labelText = 'test radio label';
render();
expect(screen.getByText(labelText).tagName).toBe('SPAN');
});
test('Renders label before radio input if labelPosition is "start"', () => {
render();
const wrapper = screen.getByRole('radio').parentElement!;
expect(wrapper.children[0].tagName).toBe('LABEL');
expect(wrapper.children[1].tagName).toBe('INPUT');
});
test('Sets aria-describedby when description is provided', () => {
render();
const radio = screen.getByRole('radio');
const descriptionElement = screen.getByText('test description');
expect(radio).toHaveAttribute('aria-describedby', descriptionElement.id);
expect(descriptionElement).toHaveAttribute('id');
});
test('Sets custom aria-describedby when provided', () => {
render(
);
const radio = screen.getByRole('radio');
expect(radio).toHaveAttribute('aria-describedby', 'custom-id');
});
test('Does not set aria-describedby when no description is provided', () => {
render();
const radio = screen.getByRole('radio');
expect(radio).not.toHaveAttribute('aria-describedby');
});
test('Does not set aria-describedby when description is provided but aria-describedby is empty string', () => {
render(
);
const radio = screen.getByRole('radio');
expect(radio).not.toHaveAttribute('aria-describedby');
});
});