import { describe, it, expect, vi, beforeEach } from 'vitest';
import { render, screen, fireEvent } from '@testing-library/react';
import { RadioGroup } from '../radio-group';
describe('RadioGroup', () => {
const options = [
{ value: 'option1', label: 'Option 1' },
{ value: 'option2', label: 'Option 2' },
{ value: 'option3', label: 'Option 3' },
];
const defaultProps = {
name: 'test-radio',
options,
value: 'option1',
onChange: vi.fn(),
};
beforeEach(() => {
vi.clearAllMocks();
});
it('should render all radio options', () => {
render();
expect(screen.getByLabelText('Option 1')).toBeInTheDocument();
expect(screen.getByLabelText('Option 2')).toBeInTheDocument();
expect(screen.getByLabelText('Option 3')).toBeInTheDocument();
});
it('should apply custom className', () => {
const { container } = render(
);
expect(container.firstChild).toHaveClass('custom-class');
});
it('should check the correct option', () => {
render();
expect(screen.getByLabelText('Option 1')).toBeChecked();
expect(screen.getByLabelText('Option 2')).not.toBeChecked();
});
it('should call onChange when option is selected', () => {
render();
fireEvent.click(screen.getByLabelText('Option 2'));
expect(defaultProps.onChange).toHaveBeenCalledWith('option2');
});
it('should render with vertical orientation by default', () => {
const { container } = render();
expect(container.firstChild).toHaveClass('flex-col', 'gap-2');
});
it('should render with horizontal orientation', () => {
const { container } = render(
);
expect(container.firstChild).toHaveClass('flex-row', 'gap-4');
});
it('should disable specific options', () => {
const optionsWithDisabled = [
{ value: 'option1', label: 'Option 1' },
{ value: 'option2', label: 'Option 2', disabled: true },
];
render();
expect(screen.getByLabelText('Option 2')).toBeDisabled();
});
it('should use the correct name for all radios', () => {
render();
const radios = screen.getAllByRole('radio');
radios.forEach((radio) => {
expect(radio).toHaveAttribute('name', 'test-radio');
});
});
it('should generate unique ids for each option', () => {
render();
expect(screen.getByLabelText('Option 1')).toHaveAttribute(
'id',
'test-radio-option1'
);
expect(screen.getByLabelText('Option 2')).toHaveAttribute(
'id',
'test-radio-option2'
);
});
it('should forward ref', () => {
const ref = { current: null };
render();
expect(ref.current).not.toBeNull();
});
it('should spread additional props', () => {
const { container } = render(
);
expect(container.firstChild).toHaveAttribute('data-testid', 'radio-group');
});
it('should have flex class', () => {
const { container } = render();
expect(container.firstChild).toHaveClass('flex');
});
});