import React from 'react';
import { render, fireEvent, screen } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import { axe } from 'jest-axe';
import 'jest-styled-components';
import 'jest-axe/extend-expect';
import 'regenerator-runtime/runtime';
import '@testing-library/jest-dom';
import { Grommet } from '../../Grommet';
import { Box } from '../../Box';
import { RadioButtonGroup } from '..';
describe('RadioButtonGroup', () => {
test('should have no accessibility violations', async () => {
const { container } = render(
,
);
const results = await axe(container);
expect(results).toHaveNoViolations();
expect(container).toMatchSnapshot();
});
test('string options', () => {
const { container } = render(
,
);
expect(container.firstChild).toMatchSnapshot();
});
test('number options', () => {
const { container } = render(
,
);
expect(container.firstChild).toMatchSnapshot();
});
test('boolean options', () => {
const { container } = render(
,
);
expect(container.firstChild).toMatchSnapshot();
});
test('object options just value', () => {
const { container } = render(
,
);
expect(container.firstChild).toMatchSnapshot();
});
test('object options', () => {
const { container } = render(
,
);
expect(container.firstChild).toMatchSnapshot();
});
test('object options disabled', () => {
const { container } = render(
,
);
expect(container.firstChild).toMatchSnapshot();
});
test('defaultValue', () => {
const { container } = render(
,
);
expect(container).toMatchSnapshot();
});
test('children', () => {
const child = ({ checked }: { checked: boolean }) => (
);
const { container } = render(
{child}
,
);
expect(container.firstChild).toMatchSnapshot();
});
test('adding additional props', () => {
const { container } = render(
,
);
expect(container.firstChild).toMatchSnapshot();
});
test('onChange fires with event when passed from props', () => {
const radioGroupOptions = [
{
id: 'ONE',
value: '1',
'data-testid': 'testid-1',
},
{
id: 'TWO',
value: '2',
'data-testid': 'testid-2',
},
];
const onChange = jest.fn((event) => {
expect(event).toBeDefined();
expect(event).toHaveProperty(['target', 'value']);
const { target } = event;
const option = radioGroupOptions.find(
(optn) => target.value === optn.value,
);
expect(option).not.toBeNull();
expect(target.value).toEqual(option?.value);
});
const { getByTestId } = render(
,
);
// Select first radio button
fireEvent.click(getByTestId('testid-1'));
expect(onChange).toBeCalledTimes(1);
// Select first radio button again - should not trigger onChange
fireEvent.click(getByTestId('testid-1'));
expect(onChange).toBeCalledTimes(1);
// Select second radio button
fireEvent.click(getByTestId('testid-2'));
expect(onChange).toBeCalledTimes(2);
});
test('Works with keyboard', async () => {
const user = userEvent.setup();
const radioGroupOptions = [
{
id: 'ONE',
value: '1',
label: 'radio button 1',
},
{
id: 'TWO',
value: '2',
label: 'radio button 2',
},
{
id: 'THREE',
value: '3',
label: 'radio button 3',
},
];
const onChange = jest.fn();
render(
,
);
const middleRadioBtn = screen.getByRole('radio', {
name: 'radio button 2',
});
await user.type(middleRadioBtn, '{arrowDown}');
expect(onChange).toHaveBeenLastCalledWith(
expect.objectContaining({
target: screen.getByRole('radio', { name: 'radio button 3' }),
}),
);
await user.type(middleRadioBtn, '{arrowUp}');
expect(onChange).toHaveBeenLastCalledWith(
expect.objectContaining({
target: screen.getByRole('radio', { name: 'radio button 1' }),
}),
);
});
});