Skip to content

Commit 3c5bf0c

Browse files
test: added test cases for toggle switch component
1 parent 3a2d74b commit 3c5bf0c

File tree

1 file changed

+65
-0
lines changed

1 file changed

+65
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
import React from 'react';
2+
import { render, fireEvent } from '@testing-library/react';
3+
import { ToggleSwitch } from '..';
4+
5+
describe('ToggleSwitch Component', () => {
6+
7+
it('handles onChange event correctly', () => {
8+
const onChange = jest.fn();
9+
const { getByRole } = render(<ToggleSwitch onChange={onChange} value={false} />);
10+
const toggleSwitch = getByRole('checkbox');
11+
fireEvent.click(toggleSwitch);
12+
expect(onChange).toHaveBeenCalledTimes(1);
13+
});
14+
15+
it('displays correct checked state based on value prop', () => {
16+
const onChange = jest.fn();
17+
const { getByRole, rerender } = render(<ToggleSwitch onChange={onChange} value={false} />);
18+
let toggleSwitch = getByRole('checkbox');
19+
expect(toggleSwitch).not.toBeChecked();
20+
21+
rerender(<ToggleSwitch onChange={onChange} value={true} />);
22+
toggleSwitch = getByRole('checkbox');
23+
expect(toggleSwitch).toBeChecked();
24+
});
25+
26+
it('applies wrapperClassName and className correctly', () => {
27+
const onChange = jest.fn();
28+
const { container } = render(
29+
<ToggleSwitch
30+
onChange={onChange}
31+
value={false}
32+
wrapperClassName="wrapper-class"
33+
className="custom-class"
34+
/>
35+
);
36+
const wrapper = container.querySelector('.deriv-toggle-switch.wrapper-class');
37+
expect(wrapper).toBeInTheDocument();
38+
const input = wrapper?.querySelector('input');
39+
expect(input).toHaveClass('custom-class');
40+
});
41+
42+
it('applies wrapperStyle and style correctly', () => {
43+
const onChange = jest.fn();
44+
const { container } = render(
45+
<ToggleSwitch
46+
onChange={onChange}
47+
value={false}
48+
wrapperStyle={{ backgroundColor: 'red' }}
49+
style={{ fontSize: '16px' }}
50+
/>
51+
);
52+
const input = container.querySelector('.deriv-toggle-switch input');
53+
expect(input).toHaveStyle({ fontSize: '16px' });
54+
const label = input?.parentElement;
55+
expect(label).toHaveStyle({ backgroundColor: 'red' });
56+
});
57+
58+
it('defaults to unchecked when no value prop is provided', () => {
59+
const onChange = jest.fn();
60+
const { getByRole } = render(<ToggleSwitch onChange={onChange} value={false} />);
61+
const toggleSwitch = getByRole('checkbox');
62+
expect(toggleSwitch).not.toBeChecked();
63+
});
64+
65+
});

0 commit comments

Comments
 (0)