import { render, screen } from '@testing-library/react'; import user from '@testing-library/user-event'; import Switch from './index'; import { personalStroke as PersonalIcon, businessStroke as BusinessIcon, } from '../Icon/icons'; const getProps = (overrides = {}) => ({ title: 'Profile Switch', name: 'profile-switch', checkedIcon: , uncheckedIcon: , ...overrides, }); describe('Switch', () => { it('should render', () => { const { asFragment } = render(); expect(asFragment()).toMatchSnapshot(); }); it('should switch to true if defaultChecked = true and work uncontrolled', () => { render(); const checkbox = screen.getByRole('checkbox'); expect(checkbox.checked).toBe(true); user.click(checkbox); expect(checkbox.checked).toBe(false); }); it('should switch state as controlled by the checked prop', () => { const { rerender } = render(); const checkbox = screen.getByRole('checkbox'); expect(checkbox.checked).toBe(true); rerender(); expect(checkbox.checked).toBe(false); }); it('should disable the switch when disabled = true', () => { const { asFragment } = render(); expect(asFragment()).toMatchSnapshot(); const checkbox = screen.getByRole('checkbox'); user.click(checkbox); expect(checkbox.checked).toBe(false); }); it('should trigger onChange when toggled', () => { const props = getProps({ onChange: jest.fn() }); render(); const checkbox = screen.getByRole('checkbox'); user.click(checkbox); expect(props.onChange).toHaveBeenCalledTimes(1); }); });