-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: added test cases for toggle switch component
- Loading branch information
1 parent
3a2d74b
commit 3c5bf0c
Showing
1 changed file
with
65 additions
and
0 deletions.
There are no files selected for viewing
65 changes: 65 additions & 0 deletions
65
src/components/ToggleSwitch/__test__/ToggleSwitch.spec.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import React from 'react'; | ||
import { render, fireEvent } from '@testing-library/react'; | ||
import { ToggleSwitch } from '..'; | ||
|
||
describe('ToggleSwitch Component', () => { | ||
|
||
it('handles onChange event correctly', () => { | ||
const onChange = jest.fn(); | ||
const { getByRole } = render(<ToggleSwitch onChange={onChange} value={false} />); | ||
const toggleSwitch = getByRole('checkbox'); | ||
fireEvent.click(toggleSwitch); | ||
expect(onChange).toHaveBeenCalledTimes(1); | ||
}); | ||
|
||
it('displays correct checked state based on value prop', () => { | ||
const onChange = jest.fn(); | ||
const { getByRole, rerender } = render(<ToggleSwitch onChange={onChange} value={false} />); | ||
let toggleSwitch = getByRole('checkbox'); | ||
expect(toggleSwitch).not.toBeChecked(); | ||
|
||
rerender(<ToggleSwitch onChange={onChange} value={true} />); | ||
toggleSwitch = getByRole('checkbox'); | ||
expect(toggleSwitch).toBeChecked(); | ||
}); | ||
|
||
it('applies wrapperClassName and className correctly', () => { | ||
const onChange = jest.fn(); | ||
const { container } = render( | ||
<ToggleSwitch | ||
onChange={onChange} | ||
value={false} | ||
wrapperClassName="wrapper-class" | ||
className="custom-class" | ||
/> | ||
); | ||
const wrapper = container.querySelector('.deriv-toggle-switch.wrapper-class'); | ||
expect(wrapper).toBeInTheDocument(); | ||
const input = wrapper?.querySelector('input'); | ||
expect(input).toHaveClass('custom-class'); | ||
}); | ||
|
||
it('applies wrapperStyle and style correctly', () => { | ||
const onChange = jest.fn(); | ||
const { container } = render( | ||
<ToggleSwitch | ||
onChange={onChange} | ||
value={false} | ||
wrapperStyle={{ backgroundColor: 'red' }} | ||
style={{ fontSize: '16px' }} | ||
/> | ||
); | ||
const input = container.querySelector('.deriv-toggle-switch input'); | ||
expect(input).toHaveStyle({ fontSize: '16px' }); | ||
const label = input?.parentElement; | ||
expect(label).toHaveStyle({ backgroundColor: 'red' }); | ||
}); | ||
|
||
it('defaults to unchecked when no value prop is provided', () => { | ||
const onChange = jest.fn(); | ||
const { getByRole } = render(<ToggleSwitch onChange={onChange} value={false} />); | ||
const toggleSwitch = getByRole('checkbox'); | ||
expect(toggleSwitch).not.toBeChecked(); | ||
}); | ||
|
||
}); |