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

71 lines
2.1 KiB
TypeScript
Raw Normal View History

2020-11-14 00:44:26 +03:00
import { ChangeEvent, PropsWithChildren } from 'react';
2020-08-22 20:03:25 +03:00
import { mount, ReactWrapper } from 'enzyme';
import { Mock } from 'ts-mockery';
2019-03-17 20:09:10 +03:00
import Checkbox from '../../src/utils/Checkbox';
2020-08-22 20:03:25 +03:00
import { BooleanControlProps } from '../../src/utils/BooleanControl';
2019-03-17 20:09:10 +03:00
describe('<Checkbox />', () => {
2020-08-22 20:03:25 +03:00
let wrapped: ReactWrapper;
2019-03-17 20:09:10 +03:00
2020-08-22 20:03:25 +03:00
const createComponent = (props: PropsWithChildren<BooleanControlProps> = {}) => {
2019-03-17 20:09:10 +03:00
wrapped = mount(<Checkbox {...props} />);
return wrapped;
};
2020-08-22 20:03:25 +03:00
afterEach(() => wrapped?.unmount());
2019-03-17 20:09:10 +03:00
it('includes extra class names when provided', () => {
2022-03-26 14:17:42 +03:00
const classNames = ['foo', 'bar', 'baz'];
2019-03-17 20:09:10 +03:00
expect.assertions(classNames.length);
classNames.forEach((className) => {
const wrapped = createComponent({ className });
2019-03-17 20:09:10 +03:00
expect(wrapped.prop('className')).toContain(className);
});
});
it('marks input as checked if defined', () => {
2022-03-26 14:17:42 +03:00
const checkeds = [true, false];
2019-03-17 20:09:10 +03:00
expect.assertions(checkeds.length);
checkeds.forEach((checked) => {
const wrapped = createComponent({ checked });
2019-03-17 20:09:10 +03:00
const input = wrapped.find('input');
expect(input.prop('checked')).toEqual(checked);
});
});
it('renders provided children inside the label', () => {
2022-03-26 14:17:42 +03:00
const labels = ['foo', 'bar', 'baz'];
2019-03-17 20:09:10 +03:00
expect.assertions(labels.length);
labels.forEach((children) => {
const wrapped = createComponent({ children });
2019-03-17 20:09:10 +03:00
const label = wrapped.find('label');
expect(label.text()).toEqual(children);
});
});
it('changes checked status on input change', () => {
const onChange = jest.fn();
2020-08-22 20:03:25 +03:00
const e = Mock.of<ChangeEvent<HTMLInputElement>>({ target: { checked: false } });
2019-03-17 20:09:10 +03:00
const wrapped = createComponent({ checked: true, onChange });
const input = wrapped.find('input');
2020-08-22 20:03:25 +03:00
(input.prop('onChange') as Function)(e);
2019-03-17 20:09:10 +03:00
expect(onChange).toHaveBeenCalledWith(false, e);
});
2020-11-28 13:45:04 +03:00
it('allows setting inline rendering', () => {
const wrapped = createComponent({ inline: true });
2022-03-05 16:04:01 +03:00
const control = wrapped.find('.form-check');
2020-11-28 13:45:04 +03:00
expect(control.prop('style')).toEqual({ display: 'inline-block' });
});
2019-03-17 20:09:10 +03:00
});