shlink-web-client/test/utils/Checkbox.test.tsx

45 lines
1.7 KiB
TypeScript
Raw Normal View History

import { render, screen } from '@testing-library/react';
import { Checkbox } from '../../src/utils/Checkbox';
2022-07-10 20:44:49 +03:00
import { renderWithEvents } from '../__helpers__/setUpTest';
2019-03-17 20:09:10 +03:00
describe('<Checkbox />', () => {
it.each([['foo'], ['bar'], ['baz']])('includes extra class names when provided', (className) => {
const { container } = render(<Checkbox className={className} />);
expect(container.firstChild).toHaveAttribute('class', `form-check form-checkbox ${className}`);
2019-03-17 20:09:10 +03:00
});
it.each([[true], [false]])('marks input as checked if defined', (checked) => {
render(<Checkbox checked={checked}>Foo</Checkbox>);
2019-03-17 20:09:10 +03:00
if (checked) {
expect(screen.getByLabelText('Foo')).toBeChecked();
} else {
expect(screen.getByLabelText('Foo')).not.toBeChecked();
}
2019-03-17 20:09:10 +03:00
});
it.each([['foo'], ['bar'], ['baz']])('renders provided children inside the label', (children) => {
render(<Checkbox>{children}</Checkbox>);
expect(screen.getByText(children)).toHaveAttribute('class', 'form-check-label');
2019-03-17 20:09:10 +03:00
});
it.each([[true], [false]])('changes checked status on input change', async (checked) => {
2019-03-17 20:09:10 +03:00
const onChange = jest.fn();
const { user } = renderWithEvents(<Checkbox onChange={onChange} checked={checked}>Foo</Checkbox>);
2019-03-17 20:09:10 +03:00
expect(onChange).not.toHaveBeenCalled();
await user.click(screen.getByLabelText('Foo'));
expect(onChange).toHaveBeenCalledWith(!checked, expect.anything());
2019-03-17 20:09:10 +03:00
});
2020-11-28 13:45:04 +03:00
it.each([[true], [false]])('allows setting inline rendering', (inline) => {
const { container } = render(<Checkbox inline={inline} />);
2020-11-28 13:45:04 +03:00
if (inline) {
expect(container.firstChild).toHaveAttribute('style', 'display: inline-block;');
} else {
expect(container.firstChild).not.toHaveAttribute('style');
}
2020-11-28 13:45:04 +03:00
});
2019-03-17 20:09:10 +03:00
});