import { render, screen, fireEvent } from '../test-utils';
import Chips from '.';
const choiceProps = {
chips: [
{
value: 'accounting',
label: 'Accounting',
},
{
value: 'payroll',
label: 'Payroll',
},
{
value: 'reporting',
label: 'Reporting',
},
{
value: 'payments',
label: 'Payments',
},
],
selected: 'payroll',
onChange: jest.fn(),
'aria-label': 'Please select a category',
};
const filterProps = {
chips: [
{
value: 'accounting',
label: 'Accounting',
},
{
value: 'payroll',
label: 'Payroll',
},
{
value: 'reporting',
label: 'Reporting',
},
{
value: 'payments',
label: 'Payments',
},
],
onChange: jest.fn(),
'aria-label': 'Filter by categories',
selected: ['payments'],
multiple: true,
};
describe('Chips', () => {
describe('Choice Chips', () => {
it(`will pass previous state, updated state, and selected value on select`, () => {
render();
const accounting = screen.getByText('Accounting');
fireEvent.click(accounting);
expect(choiceProps.onChange).toHaveBeenCalledWith({
selectedValue: 'accounting',
isEnabled: true,
});
});
});
describe('Filter Chips', () => {
it(`will pass previous state, updated state, and selected value on select`, () => {
render();
const accounting = screen.getByText('Accounting');
fireEvent.click(accounting);
expect(filterProps.onChange).toHaveBeenCalledWith({
selectedValue: 'accounting',
isEnabled: true,
});
});
it(`will pass previous state, updated state, and deselected value on deselect`, () => {
render();
fireEvent.click(screen.getByRole('checkbox', { name: 'Payments' }));
expect(filterProps.onChange).toHaveBeenCalledWith({
selectedValue: 'payments',
isEnabled: false,
});
});
it(`toggles only on Enter and Space`, () => {
const onChange = jest.fn();
render();
const payments = screen.getByRole('checkbox', { name: 'Payments' });
fireEvent.keyPress(payments, { key: 'a', code: 'KeyA', charCode: 97 });
expect(onChange).not.toHaveBeenCalled();
fireEvent.keyPress(payments, { key: 'Enter', code: 'Enter', charCode: 13 });
fireEvent.keyPress(payments, { key: ' ', code: 'Space', charCode: 32 });
expect(onChange).toHaveBeenCalledTimes(2);
expect(onChange).toHaveBeenCalledWith({ selectedValue: 'payments', isEnabled: false });
});
it(`doesn't show a remove button on selected chips`, () => {
render();
expect(screen.queryByRole('button')).not.toBeInTheDocument();
});
});
});