Skip to content

Commit 3b14bd2

Browse files
committed
chore: add tests for checkbox
1 parent afef170 commit 3b14bd2

File tree

1 file changed

+62
-0
lines changed

1 file changed

+62
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import React from 'react';
2+
import { render } from '@testing-library/react';
3+
import { Checkbox } from '../';
4+
import userEvent from '@testing-library/user-event';
5+
6+
describe('Checkbox', () => {
7+
test('renders checkbox with label', () => {
8+
const { getByRole, getByLabelText } = render(<Checkbox name="test-checkbox" label="Test Checkbox" readOnly />);
9+
expect(getByRole('checkbox')).toBeInTheDocument();
10+
expect(getByLabelText('Test Checkbox')).toBeInTheDocument();
11+
});
12+
13+
test('renders checked checkbox', () => {
14+
const { getByRole } = render(<Checkbox name="test-checkbox" checked readOnly />);
15+
expect(getByRole('checkbox')).toBeChecked();
16+
});
17+
18+
test('renders unchecked checkbox', () => {
19+
const { getByRole } = render(<Checkbox name="test-checkbox" checked={false} readOnly />);
20+
expect(getByRole('checkbox')).not.toBeChecked();
21+
});
22+
23+
test('renders disabled checkbox', () => {
24+
const { getByRole } = render(<Checkbox name="test-checkbox" disabled />);
25+
expect(getByRole('checkbox')).toBeDisabled();
26+
});
27+
28+
test('renders checkbox with error state', () => {
29+
const { getByRole, getByLabelText } = render(<Checkbox name="test-checkbox" label="Test Checkbox" error={true} readOnly />);
30+
expect(getByRole('checkbox')).toBeInTheDocument();
31+
const checkboxWrapper = getByLabelText('Test Checkbox')?.parentElement?.parentElement;
32+
expect(checkboxWrapper?.querySelector('.deriv-checkbox__label')).toHaveClass('deriv-checkbox__label--error');
33+
});
34+
35+
test('triggers onChange event when clicked', async () => {
36+
const handleChange = jest.fn();
37+
const { getByRole } = render(<Checkbox name="test-checkbox" onChange={handleChange} />);
38+
await userEvent.click(getByRole('checkbox'));
39+
expect(handleChange).toHaveBeenCalledTimes(1);
40+
});
41+
42+
test('applies custom className to checkbox input', () => {
43+
const { getByRole } = render(<Checkbox name="test-checkbox" className="custom-class" readOnly />);
44+
expect(getByRole('checkbox')).toHaveClass('custom-class');
45+
});
46+
47+
test('applies custom labelClassName to label', () => {
48+
const { getByLabelText } = render(
49+
<Checkbox name="test-checkbox" label="Test Checkbox" labelClassName="custom-label-class" readOnly />
50+
);
51+
const checkboxWrapper = getByLabelText('Test Checkbox')?.parentElement?.parentElement;
52+
expect(checkboxWrapper?.querySelector('.deriv-checkbox__label')).toHaveClass('custom-label-class');
53+
});
54+
55+
test('applies custom wrapperClassName to wrapper div', () => {
56+
const { getByLabelText } = render(
57+
<Checkbox name="test-checkbox" label="Test Checkbox" wrapperClassName="custom-wrapper-class" readOnly />
58+
);
59+
const checkboxWrapper = getByLabelText('Test Checkbox')?.parentElement?.parentElement;
60+
expect(checkboxWrapper).toHaveClass('custom-wrapper-class');
61+
});
62+
});

0 commit comments

Comments
 (0)