import { h } from 'preact'; import Switch from '../Switch'; import { fireEvent, render, screen } from 'testing-library'; describe('Switch', () => { test('renders a hidden checkbox', async () => { render(
); const unchecked = screen.queryByTestId('unchecked-switch-input'); expect(unchecked).toHaveAttribute('type', 'checkbox'); expect(unchecked).not.toBeChecked(); const checked = screen.queryByTestId('checked-switch-input'); expect(checked).toHaveAttribute('type', 'checkbox'); expect(checked).toBeChecked(); }); test('calls onChange callback when checked/unchecked', async () => { const handleChange = jest.fn(); const { rerender } = render(); fireEvent.change(screen.queryByTestId('check-input'), { checked: true }); expect(handleChange).toHaveBeenCalledWith('check', true); rerender(); fireEvent.change(screen.queryByTestId('check-input'), { checked: false }); expect(handleChange).toHaveBeenCalledWith('check', false); }); test('renders a label before', async () => { render(); const items = screen.queryAllByTestId(/check-.+/); expect(items[0]).toHaveTextContent('This is the label'); expect(items[1]).toHaveAttribute('data-testid', 'check-input'); }); test('renders a label after', async () => { render(); const items = screen.queryAllByTestId(/check-.+/); expect(items[0]).toHaveAttribute('data-testid', 'check-input'); expect(items[1]).toHaveTextContent('This is the label'); }); });