add tests

This commit is contained in:
Artur Bień
2020-02-07 21:57:45 +01:00
parent 3616a9d66e
commit f54014e7a0
2 changed files with 78 additions and 0 deletions

View File

@@ -117,6 +117,7 @@ const Radio = ({
</CheckboxComponent>
{label && <span>{label}</span>}
<StyledInput
disabled={disabled}
onChange={disabled ? undefined : onChange}
readOnly={disabled}
type='radio'

View File

@@ -0,0 +1,77 @@
import React from 'react';
import { renderWithTheme } from '../../../test/utils';
import Radio from './Radio';
describe('<Radio />', () => {
describe('label', () => {
it('renders', () => {
const labelText = 'Swag';
const { getByLabelText } = renderWithTheme(<Radio label={labelText} />);
expect(getByLabelText(labelText)).toBeInTheDocument();
});
});
describe('prop: onChange', () => {
it('should be called when Radio is clicked', () => {
const handleChange = jest.fn(event => event.target.checked);
const { getByRole } = renderWithTheme(
<Radio onChange={handleChange} value='swag' />
);
getByRole('radio').click();
expect(handleChange).toHaveBeenCalledTimes(1);
});
});
describe('prop: disabled', () => {
it('should disable radio', () => {
const handleChange = jest.fn();
const { getByRole } = renderWithTheme(
<Radio disabled onChange={handleChange} />
);
const checkbox = getByRole('radio');
expect(checkbox).toHaveAttribute('disabled');
checkbox.click();
expect(handleChange).not.toHaveBeenCalled();
});
it('should be overridden by props', () => {
const { getByRole, rerender } = renderWithTheme(<Radio disabled />);
rerender(<Radio disabled={false} />);
const checkbox = getByRole('radio');
expect(checkbox).not.toHaveAttribute('disabled');
});
});
describe('controlled', () => {
it('should check the radio', () => {
const { getByRole, rerender } = renderWithTheme(
<Radio checked={false} />
);
rerender(<Radio checked />);
const checkbox = getByRole('radio');
expect(checkbox.checked).toBe(true);
expect(getByRole('radio')).toHaveAttribute('checked');
expect(getByRole('presentation').firstChild).toHaveAttribute(
'data-testid',
'checkmarkIcon'
);
});
it('should uncheck the checkbox', () => {
const { getByRole, rerender } = renderWithTheme(<Radio checked />);
rerender(<Radio checked={false} />);
const checkbox = getByRole('radio');
expect(checkbox.checked).toBe(false);
expect(getByRole('radio')).not.toHaveAttribute('checked');
expect(getByRole('presentation').firstChild).toBeNull();
// check if proper icon was rendered
});
});
});