import * as React from 'react';
import Chip from './Chip';
import {screen, render} from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import {testA11y} from '../../axe';
describe('', () => {
it('has an accessible description', () => {
const description = 'This is a secription';
render(
);
expect(screen.getByRole('radio', {description})).toBeInTheDocument();
});
describe('single select', () => {
it('renders uncontrolled chip with accessible name and radio role', () => {
const label = 'physics';
render({label});
const chipInput = screen.getByRole('radio', {
name: label,
}) as HTMLInputElement;
expect(chipInput).toBeInTheDocument();
expect(chipInput.checked).toBe(false);
expect(chipInput.getAttribute('value')).toBe(label);
});
it('checks/unchecks when either label is clicked or space is pressed', () => {
const labels = ['physics', 'math'];
const chip = render(
{labels.map(label => (
{label}
))}
);
const chipInputs = screen.getAllByRole(
'radio'
) as Array;
expect(chipInputs[0].checked).toBe(false);
expect(chipInputs[1].checked).toBe(false);
userEvent.click(chip.getByLabelText(labels[0]));
expect(chipInputs[0]).toEqual(document.activeElement);
expect(chipInputs[0].checked).toBe(true);
expect(chipInputs[1].checked).toBe(false);
chipInputs[1].focus();
userEvent.keyboard('{space}');
expect(chipInputs[0].checked).toBe(false);
expect(chipInputs[1].checked).toBe(true);
chipInputs[0].focus();
userEvent.keyboard('{enter}');
expect(chipInputs[0].checked).toBe(false);
expect(chipInputs[1].checked).toBe(true);
});
it('renders as checked', () => {
const label = 'physics';
const chip = render(
{label}
);
const chipInput = chip.getByRole('radio') as HTMLInputElement;
expect(chipInput.checked).toBe(true);
});
it('does not allow checking disabled chip', () => {
const onChange = jest.fn();
const label = 'physics';
const chip = render(
{label}
);
const chipInput = screen.getByRole('radio') as HTMLInputElement;
expect(chipInput.checked).toBe(false);
userEvent.click(chipInput);
expect(onChange).not.toHaveBeenCalled();
expect(chipInput.checked).toBe(false);
userEvent.click(chip.getByLabelText(label));
expect(onChange).not.toHaveBeenCalled();
expect(chipInput.checked).toBe(false);
});
});
describe('multi select', () => {
it('renders uncontrolled chip with accessible name and radio role', () => {
const label = 'physics';
render(
{label}
);
const chipInput = screen.getByRole('checkbox', {
name: label,
}) as HTMLInputElement;
expect(chipInput).toBeInTheDocument();
expect(chipInput.checked).toBe(false);
expect(chipInput.getAttribute('value')).toBe(label);
});
it('checks/unchecks when either label is clicked or space/enter is pressed', () => {
const labels = ['physics', 'math'];
const chip = render(
{labels.map(label => (
{label}
))}
);
const chipInputs = screen.getAllByRole(
'checkbox'
) as Array;
expect(chipInputs[0].checked).toBe(false);
expect(chipInputs[1].checked).toBe(false);
userEvent.click(chip.getByLabelText(labels[0]));
expect(chipInputs[0]).toEqual(document.activeElement);
expect(chipInputs[0].checked).toBe(true);
expect(chipInputs[1].checked).toBe(false);
chipInputs[1].focus();
expect(chipInputs[1]).toEqual(document.activeElement);
userEvent.keyboard('{space}');
expect(chipInputs[0].checked).toBe(true);
expect(chipInputs[1].checked).toBe(true);
chipInputs[0].focus();
userEvent.keyboard('{space}');
expect(chipInputs[0].checked).toBe(false);
expect(chipInputs[1].checked).toBe(true);
});
it('renders as checked', () => {
const label = 'physics';
const chip = render(
{label}
);
const chipInput = chip.getByRole('checkbox') as HTMLInputElement;
expect(chipInput.checked).toBe(true);
});
it('does not allow checking disabled chip', () => {
const onChange = jest.fn();
const label = 'physics';
const chip = render(
{label}
);
const chipInput = screen.getByRole('checkbox') as HTMLInputElement;
expect(chipInput.checked).toBe(false);
userEvent.click(chipInput);
expect(onChange).not.toHaveBeenCalled();
expect(chipInput.checked).toBe(false);
userEvent.click(chip.getByLabelText(label));
expect(onChange).not.toHaveBeenCalled();
expect(chipInput.checked).toBe(false);
});
});
describe('a11y', () => {
it('should have no a11y violations when it has a description', async () => {
await testA11y(
);
});
describe('single select', () => {
it('should have no a11y violations when children, name and value are provided', async () => {
await testA11y(item);
});
it('should have no a11y violations when checked', async () => {
await testA11y(
item
);
});
it('should have no a11y violations when disabled', async () => {
await testA11y(
item
);
});
it('should have no a11y violations when required', async () => {
await testA11y(
item
);
});
});
describe('multi select', () => {
it('should have no a11y violations when children, name and value are provided', async () => {
await testA11y(
item
);
});
it('should have no a11y violations when checked', async () => {
await testA11y(
item
);
});
it('should have no a11y violations when disabled', async () => {
await testA11y(
item
);
});
it('should have no a11y violations when required', async () => {
await testA11y(
item
);
});
});
});
});