-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathCheckbox.test.tsx
95 lines (68 loc) · 3.07 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
84
85
86
87
88
89
90
91
92
93
94
95
import '@testing-library/jest-dom';
import { act, render, screen } from '@testing-library/react';
import { Checkbox } from './Checkbox';
import { CheckboxTestWrapper } from './Checkbox.utils';
const id = 'default';
const label = 'this is a label';
const testId = `${id}-container`;
const inputTestId = `${id}-input`;
const attributeAria = 'aria-checked';
const attributeClass = 'class';
const attributeTitle = 'title';
const defaultProps = { id, label, 'data-testid': testId };
describe('Checkbox', () => {
it('Propagates additional HTML properties to main component element', () => {
const testTitle = 'test-title';
render(<Checkbox {...defaultProps} title={testTitle} />);
const checkbox = screen.getByTestId(inputTestId);
expect(checkbox).toBeInTheDocument();
expect(checkbox).toHaveAttribute(attributeTitle, testTitle);
});
it('Calls the provided onChange handler', async () => {
const onChange = vi.fn();
render(<CheckboxTestWrapper {...defaultProps} onChange={onChange} />);
const checkbox = await screen.findByRole(/checkbox/i);
expect(checkbox.getAttribute(attributeClass)).toMatch('a-checkbox');
expect(checkbox.getAttribute(attributeAria)).toMatch('false');
expect(checkbox).not.toBeDisabled();
expect(onChange).not.toHaveBeenCalled();
act(() => checkbox.click());
expect(onChange).toHaveBeenCalled();
// Accessbility attributes updated
expect(checkbox.getAttribute(attributeAria)).toMatch('true');
});
it('Renders helper text that toggles checkbox when clicked', async () => {
const helperText = 'This is optional helper text for the checkbox';
const helperTextOutput = `(${helperText})`;
render(<CheckboxTestWrapper {...defaultProps} helperText={helperText} />);
const checkbox = await screen.findByRole(/checkbox/i);
expect(checkbox.getAttribute(attributeAria)).toMatch('false');
// Clicking helper text correctly updates checkbox
const helper = await screen.findByText(helperTextOutput);
act(() => helper.click());
expect(checkbox.getAttribute(attributeAria)).toMatch('true');
});
it('Renders the "Large" variant', async () => {
render(<Checkbox {...defaultProps} isLarge />);
const largeClass = 'm-form-field__lg-target';
const checkbox = screen.getByTestId(testId);
expect(checkbox.getAttribute(attributeClass)).toMatch(largeClass);
});
it('Is disabled when passing the "disabled" prop', async () => {
render(<Checkbox {...defaultProps} disabled />);
const checkbox = await screen.findByRole(/checkbox/i);
expect(checkbox).toBeDisabled();
});
it('Integrates a provided className', async () => {
const cname = 'extraCname';
render(<Checkbox {...defaultProps} className={cname} />);
const checkbox = screen.getByTestId(testId);
expect(checkbox).toHaveClass(cname);
});
it('Integrates a provided inputClassName', async () => {
const cname = 'extraInputCname';
render(<Checkbox {...defaultProps} inputClassName={cname} />);
const input = screen.getByTestId(`${id}-input`);
expect(input).toHaveClass(cname);
});
});