Passed
Pull Request — main (#353)
by Alejandro
41:20 queued 16:16
created

test/utils/Checkbox.test.tsx   A

Complexity

Total Complexity 1
Complexity/F 0

Size

Lines of Code 71
Function Count 0

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
wmc 1
eloc 60
mnd 1
bc 1
fnc 0
dl 0
loc 71
rs 10
bpm 0
cpm 0
noi 0
c 0
b 0
f 0
1
import { ChangeEvent, PropsWithChildren } from 'react';
2
import { mount, ReactWrapper } from 'enzyme';
3
import { Mock } from 'ts-mockery';
4
import Checkbox from '../../src/utils/Checkbox';
5
import { BooleanControlProps } from '../../src/utils/BooleanControl';
6
7
describe('<Checkbox />', () => {
8
  let wrapped: ReactWrapper;
9
10
  const createComponent = (props: PropsWithChildren<BooleanControlProps> = {}) => {
11
    wrapped = mount(<Checkbox {...props} />);
12
13
    return wrapped;
14
  };
15
16
  afterEach(() => wrapped?.unmount());
17
18
  it('includes extra class names when provided', () => {
19
    const classNames = [ 'foo', 'bar', 'baz' ];
20
21
    expect.assertions(classNames.length);
22
    classNames.forEach((className) => {
23
      const wrapped = createComponent({ className });
24
25
      expect(wrapped.prop('className')).toContain(className);
26
    });
27
  });
28
29
  it('marks input as checked if defined', () => {
30
    const checkeds = [ true, false ];
31
32
    expect.assertions(checkeds.length);
33
    checkeds.forEach((checked) => {
34
      const wrapped = createComponent({ checked });
35
      const input = wrapped.find('input');
36
37
      expect(input.prop('checked')).toEqual(checked);
38
    });
39
  });
40
41
  it('renders provided children inside the label', () => {
42
    const labels = [ 'foo', 'bar', 'baz' ];
43
44
    expect.assertions(labels.length);
45
    labels.forEach((children) => {
46
      const wrapped = createComponent({ children });
47
      const label = wrapped.find('label');
48
49
      expect(label.text()).toEqual(children);
50
    });
51
  });
52
53
  it('changes checked status on input change', () => {
54
    const onChange = jest.fn();
55
    const e = Mock.of<ChangeEvent<HTMLInputElement>>({ target: { checked: false } });
56
    const wrapped = createComponent({ checked: true, onChange });
57
    const input = wrapped.find('input');
58
59
    (input.prop('onChange') as Function)(e);
60
61
    expect(onChange).toHaveBeenCalledWith(false, e);
62
  });
63
64
  it('allows setting inline rendering', () => {
65
    const wrapped = createComponent({ inline: true });
66
    const control = wrapped.find('.custom-control');
67
68
    expect(control.prop('style')).toEqual({ display: 'inline-block' });
69
  });
70
});
71