import { render, screen } from '@testing-library/react'; import userEvent from '@testing-library/user-event'; import { Checkbox } from '../Checkbox'; import styles from '@patternfly/react-styles/css/components/Check/check'; test(`Renders with only the class ${styles.checkInput} on the check by default`, () => { render(); expect(screen.getByRole('checkbox')).toHaveClass(styles.checkInput, { exact: true }); }); test(`Renders with only the classes ${styles.check} and ${styles.modifiers.standalone} on the check wrapper by default`, () => { render(); expect(screen.getByRole('checkbox').parentElement).toHaveClass(`${styles.check} ${styles.modifiers.standalone}`, { exact: true }); }); test('Renders with additional classes passed via className', () => { render(); expect(screen.getByRole('checkbox').parentElement).toHaveClass('test-class'); }); test('Renders with additional classes passed via inputClassName', () => { render(); expect(screen.getByRole('checkbox')).toHaveClass('test-class'); }); test('Does not set the checkbox as invalid by default', () => { render(); expect(screen.getByRole('checkbox')).toBeValid(); }); test('Sets the checkbox as invalid when isValid is false', () => { render(); expect(screen.getByRole('checkbox')).toBeInvalid(); }); test('Does not set the checkbox as disabled by default', () => { render(); expect(screen.getByRole('checkbox')).not.toBeDisabled(); }); test(`Sets the checkbox as disabled when isDisabled is passed`, () => { render(); expect(screen.getByRole('checkbox')).toBeDisabled(); }); test('Sets the label as disabled when isDisabled and label are passed', () => { render(); expect(screen.getByLabelText('test label')).toBeDisabled(); }); test('Does not set the checkbox as required by default', () => { render(); expect(screen.getByRole('checkbox')).not.toBeRequired(); }); test(`Sets the checkbox as required when isRequired is passed`, () => { render(); expect(screen.getByRole('checkbox')).toBeRequired(); }); test('Does not set the checkbox as checked by default', () => { render(); expect(screen.getByRole('checkbox')).not.toBeChecked(); }); test(`Sets the checkbox as checked when isChecked is passed`, () => { render(); expect(screen.getByRole('checkbox')).toBeChecked(); }); test(`Sets the checkbox as checked when checked is passed`, () => { render(); expect(screen.getByRole('checkbox')).toBeChecked(); }); test(`Calls onChange when the checkbox is clicked`, async () => { const user = userEvent.setup(); const onChange = jest.fn(); render(); await user.click(screen.getByRole('checkbox')); expect(onChange).toHaveBeenCalledTimes(1); }); test('Does not call onChange when the checkbox is not clicked', () => { const onChange = jest.fn(); render(); expect(onChange).not.toHaveBeenCalled(); }); test(`Calls onChange with the event and the checked value when the checkbox is clicked`, async () => { const user = userEvent.setup(); const onChange = jest.fn(); render(); await user.click(screen.getByRole('checkbox')); expect(onChange).toHaveBeenCalledWith(expect.objectContaining({ type: 'change' }), true); }); test(`Calls onChange with the event and the checked value when the checkbox is clicked and isChecked is passed`, async () => { const user = userEvent.setup(); const onChange = jest.fn(); render(); await user.click(screen.getByRole('checkbox')); expect(onChange).toHaveBeenCalledWith(expect.objectContaining({ type: 'change' }), false); }); test('Does not render a label by default', () => { render(); expect(screen.queryByLabelText(/\w+/)).not.toBeInTheDocument(); }); test('Renders a label when label is passed', () => { render(); expect(screen.getByLabelText('test label')).toBeVisible(); }); test('Associates the label with the checkbox', () => { render(); expect(screen.getByRole('checkbox')).toHaveAccessibleName('test label'); }); test('Does not render an asterisk when a label is passed but isRequired is not', () => { render(); expect(screen.queryByText('*')).not.toBeInTheDocument(); }); test('Renders an asterisk when isRequired and a label are passed', () => { render(); expect(screen.getByText('*')).toBeVisible(); }); test(`Wraps the required asterisk in the ${styles.checkLabelRequired} className`, () => { render(); expect(screen.getByText('*')).toHaveClass(styles.checkLabelRequired, { exact: true }); }); test('Renders with the provided id', () => { render(); expect(screen.getByRole('checkbox')).toHaveAttribute('id', 'test-id'); }); test('Does not render an aria-label by default', () => { render(); expect(screen.getByRole('checkbox')).not.toHaveAttribute('aria-label'); }); test('Sets the name to the passed aria-label', () => { render(); expect(screen.getByRole('checkbox')).toHaveAccessibleName('test aria-label'); }); test('Does not render a description by default', () => { render(); expect(screen.queryByText(/\w+/)).not.toBeInTheDocument(); }); test('Renders a description when description is passed', () => { render(); expect(screen.getByText('test description')).toBeVisible(); }); test(`Renders the passed description with the ${styles.checkDescription} className`, () => { render(); expect(screen.getByText('test description')).toHaveClass(styles.checkDescription, { exact: true }); }); test('Does not render a body by default', () => { render(); expect(screen.queryByText(/\w+/)).not.toBeInTheDocument(); }); test('Renders a body when body is passed', () => { render(); expect(screen.getByText('test body')).toBeVisible(); }); test(`Renders the passed body with the ${styles.checkBody} className`, () => { render(); expect(screen.getByText('test body')).toHaveClass(styles.checkBody, { exact: true }); }); test('Renders the check wrapper as a div by default', () => { render(); expect(screen.getByRole('checkbox').parentElement?.tagName).toBe('DIV'); }); test('Renders with the provided component', () => { render(); expect(screen.getByRole('checkbox').parentElement?.tagName).toBe('SPAN'); }); test('Renders with the label wrapper if isLabelWrapped is provided', () => { render(); expect(screen.getByRole('checkbox').parentElement?.tagName).toBe('LABEL'); }); test('Renders with span element around the inner label text if isLabelWrapped is provided', () => { const labelText = 'test checkbox label'; render(); expect(screen.getByText(labelText).tagName).toBe('SPAN'); }); test('Renders with the provided component although isLabelWrapped is provided', () => { render(); expect(screen.getByRole('checkbox').parentElement?.tagName).toBe('H3'); }); test('Renders with the label wrapper if component is set to label', () => { render(); expect(screen.getByRole('checkbox').parentElement?.tagName).toBe('LABEL'); }); test('Renders with span element around the inner label text if component is set to label', () => { const labelText = 'test checkbox label'; render(); expect(screen.getByText(labelText).tagName).toBe('SPAN'); }); test('Renders label before checkbox input if labelPosition is "start"', () => { render(); const wrapper = screen.getByRole('checkbox').parentElement!; expect(wrapper.children[0].tagName).toBe('LABEL'); expect(wrapper.children[1].tagName).toBe('INPUT'); }); test(`Spreads additional props`, () => { render(); expect(screen.getByTestId('test-id')).toBeInTheDocument(); }); test(`Sets the checkbox as checked by default when defaultChecked is passed`, () => { render(); expect(screen.getByRole('checkbox')).toBeChecked(); }); test('Matches snapshot', () => { const { asFragment } = render(); expect(asFragment()).toMatchSnapshot(); }); test('Sets aria-describedby when description is provided', () => { render(); const checkbox = screen.getByRole('checkbox'); const descriptionElement = screen.getByText('test description'); expect(checkbox).toHaveAttribute('aria-describedby', descriptionElement.id); expect(descriptionElement).toHaveAttribute('id'); }); test('Sets custom aria-describedby when provided', () => { render(); const checkbox = screen.getByRole('checkbox'); expect(checkbox).toHaveAttribute('aria-describedby', 'custom-id'); }); test('Does not set aria-describedby when no description is provided', () => { render(); const checkbox = screen.getByRole('checkbox'); expect(checkbox).not.toHaveAttribute('aria-describedby'); }); test('Does not set aria-describedby when description is provided but aria-describedby is empty string', () => { render(); const checkbox = screen.getByRole('checkbox'); expect(checkbox).not.toHaveAttribute('aria-describedby'); });