import { render, screen, fireEvent } from '@testing-library/react';
import { RadioOption } from './RadioOption';
import { describe, it, expect, vi } from 'vitest';
describe('RadioOption', () => {
it('renders the component with the correct label', () => {
const label = 'Test Option';
const { getByText } = render(
);
expect(getByText(label)).toBeInTheDocument();
});
it('calls the onClick handler when clicked', () => {
const onClickMock = vi.fn();
render(
);
const option = screen.getByRole('radio');
fireEvent.click(option);
expect(onClickMock).toHaveBeenCalled();
});
it('does not allow to uncheck modality if checkboxStyle is not defined', () => {
const onClickMock = vi.fn();
render(
);
const option = screen.getByRole('radio');
fireEvent.click(option);
expect(onClickMock).not.toHaveBeenCalled();
});
it('does not allow to uncheck modality if checkboxStyle is false', () => {
const onClickMock = vi.fn();
render(
);
const option = screen.getByRole('radio');
fireEvent.click(option);
expect(onClickMock).not.toHaveBeenCalled();
});
it('allows to uncheck modality if checkboxStyle = true and onUncheck', () => {
const onClickMock = vi.fn();
render(
);
const option = screen.getByRole('radio');
fireEvent.click(option);
expect(onClickMock).toHaveBeenCalled();
});
it('sets the tabIndex to 0 when unchecked', () => {
const { getByRole } = render(
);
expect(getByRole('radio')).toHaveAttribute('tabIndex', '0');
});
it('sets the tabIndex to -1 when checked', () => {
const { getByRole } = render(
);
expect(getByRole('radio')).toHaveAttribute('tabIndex', '-1');
});
it('applies the disabled aria when disabled prop is present', () => {
const { getByRole } = render(
);
expect(getByRole('radio')).toHaveAttribute('aria-disabled');
});
it('applies the checked aria when checked prop is present', () => {
const { getByRole } = render(
);
expect(getByRole('radio')).toHaveAttribute('aria-checked', 'true');
});
it('applies the code-modality class when codeModality prop is present', () => {
const { getByText } = render(
);
expect(getByText('HTML')).toHaveClass('code-modality');
});
it('calls the onKeyDown handler when a key is pressed', () => {
const onKeyDownMock = vi.fn();
render(
);
const option = screen.getByRole('radio');
fireEvent.keyDown(option, { key: 'Enter', code: 'Enter' });
expect(onKeyDownMock).toHaveBeenCalled();
});
it('renders the detail when checked', () => {
const { getByText } = render(
{}}
detailLabel="My detail"
checked
/>
);
expect(getByText('My detail')).toBeInTheDocument();
});
it('does not render the detail when unchecked', () => {
const { queryByText } = render(
{}}
detailLabel="My detail"
/>
);
expect(queryByText('My detail')).toBeNull();
});
it('renders the details when unchecked with the detail always displayed attribute', () => {
const { getByText } = render(
{}}
detailLabel="My detail"
detailAlwaysDisplayed
/>
);
expect(getByText('My detail')).toBeInTheDocument();
});
});