-
Notifications
You must be signed in to change notification settings - Fork 1
/
CheckBox.test.tsx
83 lines (79 loc) · 2.7 KB
/
CheckBox.test.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import React, { ReactElement, useState } from 'react';
import { render } from 'react-dom';
import { act, renderIntoDocument } from 'react-dom/test-utils';
import CheckBox from './CheckBox';
it('CheckBox can render', () => {
const onChange = (): void => { };
renderIntoDocument(<CheckBox checked onChange={onChange} />);
renderIntoDocument(<CheckBox checked={false} onChange={onChange} />);
renderIntoDocument(<CheckBox checked onChange={onChange} inverted={false} />);
renderIntoDocument(<CheckBox checked={false} onChange={onChange} inverted={false} />);
renderIntoDocument(<CheckBox checked onChange={onChange} inverted />);
renderIntoDocument(<CheckBox checked={false} onChange={onChange} inverted />);
renderIntoDocument(<CheckBox checked onChange={onChange} disabled={false} />);
renderIntoDocument(<CheckBox checked={false} onChange={onChange} disabled={false} />);
renderIntoDocument(<CheckBox checked onChange={onChange} disabled />);
renderIntoDocument(<CheckBox checked={false} onChange={onChange} disabled />);
});
function StatefulCheckBox({ disabled }: { readonly disabled: boolean }): ReactElement {
const [checked, setChecked] = useState(false);
const onChange = (): void => setChecked((prev) => !prev);
return (
<CheckBox checked={checked} onChange={onChange} disabled={disabled} />
);
}
it('CheckBox can correctly change state.', () => {
const container = document.createElement('div');
document.body.append(container);
act(() => {
render(<StatefulCheckBox disabled={false} />, container);
});
const input = container.querySelector('input');
if (input == null) {
throw new Error();
}
{
// use the existence of span to tell the checked state, same below
const span = container.querySelector('span');
if (span != null) {
throw new Error();
}
}
act(() => {
input.dispatchEvent(new MouseEvent('click', { bubbles: true }));
});
{
const span = container.querySelector('span');
if (span == null) {
throw new Error();
}
}
document.body.removeChild(container);
});
it('CheckBox cannot change state when disabled', () => {
const container = document.createElement('div');
document.body.append(container);
act(() => {
render(<StatefulCheckBox disabled />, container);
});
const input = container.querySelector('input');
if (input == null) {
throw new Error();
}
{
const span = container.querySelector('span');
if (span != null) {
throw new Error();
}
}
act(() => {
input.dispatchEvent(new MouseEvent('click', { bubbles: true }));
});
{
const span = container.querySelector('span');
if (span != null) {
throw new Error();
}
}
document.body.removeChild(container);
});