forked from binary-com/deriv-app
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[WALL] Farhan/WALL-2884/Unit test for ToggleSwitch component (binary-…
…com#12167) * chore: ✨ create unit test for toggleswitch component * chore: ✨ update test case
- Loading branch information
1 parent
a2261a2
commit fd39334
Showing
1 changed file
with
35 additions
and
0 deletions.
There are no files selected for viewing
35 changes: 35 additions & 0 deletions
35
packages/wallets/src/components/ToggleSwitch/__tests__/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,35 @@ | ||
import React, { createRef, useState } from 'react'; | ||
import { render, screen } from '@testing-library/react'; | ||
import ToggleSwitch from '../ToggleSwitch'; | ||
|
||
describe('ToggleSwitch', () => { | ||
it('should render the toggle switch with disabled state', () => { | ||
render(<ToggleSwitch onChange={jest.fn()} value={false} />); | ||
expect(screen.getByRole('checkbox')).not.toBeChecked(); | ||
}); | ||
|
||
it('should render the toggle switch with enabled state', () => { | ||
render(<ToggleSwitch onChange={jest.fn()} value={true} />); | ||
expect(screen.getByRole('checkbox')).toBeChecked(); | ||
}); | ||
|
||
it('should call onChange toggle', () => { | ||
const onChangeMock = jest.fn(); | ||
render(<ToggleSwitch onChange={onChangeMock} value={false} />); | ||
screen.getByRole('checkbox').click(); | ||
expect(onChangeMock).toHaveBeenCalledTimes(1); | ||
}); | ||
|
||
it('should render toggle switch with ref and update the state when clicking on it', () => { | ||
const ref = createRef<HTMLInputElement>(); | ||
const ToggleSwitchWrapper = () => { | ||
const [isChecked, setIsChecked] = useState(false); | ||
return <ToggleSwitch onChange={e => setIsChecked(e.target.checked)} ref={ref} value={isChecked} />; | ||
}; | ||
render(<ToggleSwitchWrapper />); | ||
expect(ref.current).not.toBeNull(); | ||
expect(ref.current).not.toBeChecked(); | ||
ref.current?.click(); | ||
expect(ref.current).toBeChecked(); | ||
}); | ||
}); |