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
|
|
|
|