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

64 lines
1.8 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', () => {
const classNames = [ 'foo', 'bar', 'baz' ];
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', () => {
const checkeds = [ true, false ];
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', () => {
const labels = [ 'foo', 'bar', 'baz' ];
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);
});
});