import * as React from 'react'; import CardCheckbox from './CardCheckbox'; import {render, screen} from '@testing-library/react'; import userEvent from '@testing-library/user-event'; import {testA11y} from '../../axe'; describe('', () => { it('renders unchecked checkbox input, without label', () => { const checkbox = render(); const checkboxInput = checkbox.getByRole('checkbox') as HTMLInputElement; expect(checkboxInput.checked).toBe(false); }); it('renders CardRadio with accessible name and checkbox role', () => { const label = 'Option A'; render({label}); const checkboxInput = screen.getByRole('checkbox', { name: label, }) as HTMLInputElement; expect(checkboxInput).toBeInTheDocument(); expect(checkboxInput.checked).toBe(false); }); it('does not allow checking disabled item', () => { render(Option A); userEvent.click(screen.getByLabelText('Option A')); expect(screen.getByLabelText('Option A')).not.toBeChecked(); }); it('checks/unchecks when either checkbox, input or label is clicked or space is pressed', () => { const checkbox = render(my label); const checkboxInput = checkbox.getByRole('checkbox') as HTMLInputElement; expect(checkboxInput.checked).toBe(false); userEvent.click(checkboxInput); expect(checkboxInput).toEqual(document.activeElement); expect(checkboxInput.checked).toBe(true); userEvent.keyboard('{space}'); expect(checkboxInput.checked).toBe(false); userEvent.click(checkbox.getByLabelText('my label')); expect(checkboxInput.checked).toBe(true); }); it('allows aria-labbelledby to override accessible name', () => { render( Option ACustom Label Option A ); const checkboxInput = screen.getByRole('checkbox', { name: 'Custom Label Option A', }) as HTMLInputElement; expect(checkboxInput).toBeInTheDocument(); }); it('allows aria-descriibedby to define accessible description', () => { render( Option ACustom Description Option A ); const checkboxInput = screen.getByRole('checkbox', { description: 'Custom Description Option A', }) as HTMLInputElement; expect(checkboxInput).toBeInTheDocument(); }); it('it renders as checked by default', () => { const checkbox = render( my label ); const checkboxInput = checkbox.getByRole('checkbox') as HTMLInputElement; expect(checkboxInput.checked).toBe(true); }); it('renders as unchecked by default', () => { const checkbox = render( my label ); const checkboxInput = checkbox.getByRole('checkbox') as HTMLInputElement; expect(checkboxInput.checked).toBe(false); }); it('responds to check/uncheck when controlled', () => { const ControlledCheckbox = () => { const [checked, setChecked] = React.useState(false); return ( setChecked(val => !val)} aria-labelledby="label" > ); }; const checkbox = render(); const checkboxInput = checkbox.getByRole('checkbox') as HTMLInputElement; expect(checkboxInput.checked).toBe(false); userEvent.click(checkboxInput); expect(checkboxInput.checked).toBe(true); userEvent.click(checkboxInput); expect(checkboxInput.checked).toBe(false); }); describe('a11y', () => { it('should have no a11y violations when children is provided', async () => { await testA11y( ); }); it('should have no a11y violations when checked', async () => { await testA11y( ); }); it('should have no a11y violations when disabled', async () => { await testA11y( ); }); it('should have no a11y violations when required', async () => { await testA11y( ); }); }); });