import { render, screen, fireEvent } from '@testing-library/react'; import { FastFlag } from '@transferwise/icons'; import SwitchOption from '.'; describe('SwitchOption', () => { it('renders the option with the provided content', () => { const mockOnChange = jest.fn(); render( } id="id" title="title" aria-label="title" content="content" checked={false} onChange={mockOnChange} />, ); expect(screen.getByText('title')).toBeInTheDocument(); expect(screen.getByText('content')).toBeInTheDocument(); expect(screen.getByTestId('fast-flag')).toBeInTheDocument(); expect(screen.getAllByRole('switch')[0]).toBeInTheDocument(); }); it('checks the switch when the user interacts with it', () => { // Uses first in array to bypass the fact theres a hidden readonly input const getSwitch = () => screen.getAllByRole('switch')[0]; const mockOnChange = jest.fn(); const { rerender } = render( , ); expect(getSwitch()).toBeInTheDocument(); expect(getSwitch()).not.toBeChecked(); expect(mockOnChange).not.toHaveBeenCalled(); fireEvent.click(getSwitch()); expect(mockOnChange).toHaveBeenCalledWith(true); expect(mockOnChange).toHaveBeenCalledTimes(1); rerender( , ); expect(getSwitch()).toBeChecked(); fireEvent.click(getSwitch()); expect(mockOnChange).toHaveBeenLastCalledWith(false); expect(mockOnChange).toHaveBeenCalledTimes(2); rerender( , ); expect(getSwitch()).not.toBeChecked(); }); it('fires the onClick handler once regardless of where you click on the option', () => { const mockOnChange = jest.fn(); render( , ); expect(mockOnChange).toHaveBeenCalledTimes(0); fireEvent.click(screen.getByText('title')); expect(mockOnChange).toHaveBeenCalledTimes(1); fireEvent.click(screen.getByText('content')); expect(mockOnChange).toHaveBeenCalledTimes(2); fireEvent.click(screen.getAllByRole('switch')[0]); expect(mockOnChange).toHaveBeenCalledTimes(3); }); it('renders aligned with container content', () => { const mockOnChange = jest.fn(); render( , ); expect(document.querySelector('.np-option__container-aligned')).toBeInTheDocument(); }); });