Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix filter button accessibility #10204

Merged
merged 2 commits into from
Sep 13, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 29 additions & 29 deletions packages/ra-ui-materialui/src/list/filter/FilterButton.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -49,26 +49,27 @@ describe('<FilterButton />', () => {

fireEvent.click(await screen.findByLabelText('Add filter'));

let checkboxs: HTMLInputElement[] = screen.getAllByRole('checkbox');
expect(checkboxs).toHaveLength(3);
expect(checkboxs[0].checked).toBe(false);
expect(checkboxs[1].checked).toBe(false);
expect(checkboxs[2].checked).toBe(false);
let checkboxes: HTMLInputElement[] =
screen.getAllByRole('menuitemcheckbox');
expect(checkboxes).toHaveLength(3);
expect(checkboxes[0].getAttribute('aria-checked')).toBe('false');
expect(checkboxes[1].getAttribute('aria-checked')).toBe('false');
expect(checkboxes[2].getAttribute('aria-checked')).toBe('false');

fireEvent.click(checkboxs[0]);
fireEvent.click(checkboxes[0]);

await screen.findByRole('textbox', {
name: 'Title',
});
fireEvent.click(screen.getByLabelText('Add filter'));

checkboxs = screen.getAllByRole('checkbox');
expect(checkboxs).toHaveLength(3);
expect(checkboxs[0].checked).toBe(true);
expect(checkboxs[1].checked).toBe(false);
expect(checkboxs[2].checked).toBe(false);
checkboxes = screen.getAllByRole('menuitemcheckbox');
expect(checkboxes).toHaveLength(3);
expect(checkboxes[0].getAttribute('aria-checked')).toBe('true');
expect(checkboxes[1].getAttribute('aria-checked')).toBe('false');
expect(checkboxes[2].getAttribute('aria-checked')).toBe('false');

fireEvent.click(checkboxs[0]);
fireEvent.click(checkboxes[0]);

await waitFor(
() => {
Expand All @@ -82,20 +83,21 @@ describe('<FilterButton />', () => {
);

fireEvent.click(screen.getByLabelText('Add filter'));
checkboxs = screen.getAllByRole('checkbox');
expect(checkboxs).toHaveLength(3);
expect(checkboxs[0].checked).toBe(false);
expect(checkboxs[1].checked).toBe(false);
expect(checkboxs[2].checked).toBe(false);
checkboxes = screen.getAllByRole('menuitemcheckbox');
expect(checkboxes).toHaveLength(3);
expect(checkboxes[0].getAttribute('aria-checked')).toBe('false');
expect(checkboxes[1].getAttribute('aria-checked')).toBe('false');
expect(checkboxes[2].getAttribute('aria-checked')).toBe('false');
}, 7000);

it('should remove the checked state of the menu item when removing its matching filter', async () => {
render(<Basic />);

fireEvent.click(await screen.findByLabelText('Add filter'));

let checkboxs: HTMLInputElement[] = screen.getAllByRole('checkbox');
fireEvent.click(checkboxs[0]);
let checkboxes: HTMLInputElement[] =
screen.getAllByRole('menuitemcheckbox');
fireEvent.click(checkboxes[0]);

await screen.findByRole('textbox', {
name: 'Title',
Expand All @@ -115,11 +117,11 @@ describe('<FilterButton />', () => {
);

fireEvent.click(screen.getByLabelText('Add filter'));
checkboxs = screen.getAllByRole('checkbox');
expect(checkboxs).toHaveLength(3);
expect(checkboxs[0].checked).toBe(false);
expect(checkboxs[1].checked).toBe(false);
expect(checkboxs[2].checked).toBe(false);
checkboxes = screen.getAllByRole('menuitemcheckbox');
expect(checkboxes).toHaveLength(3);
expect(checkboxes[0].getAttribute('aria-checked')).toBe('false');
expect(checkboxes[1].getAttribute('aria-checked')).toBe('false');
expect(checkboxes[2].getAttribute('aria-checked')).toBe('false');
});

it('should display the filter button if all filters are shown and there is a filter value', () => {
Expand Down Expand Up @@ -191,13 +193,11 @@ describe('<FilterButton />', () => {
await screen.findByText('Add filter');
fireEvent.click(screen.getByText('Add filter'));

await screen.findByText('Title', { selector: 'li > span' });
await screen.findByText('Title', { selector: 'li span' });
expect(screen.queryByDisplayValue('Remove all filters')).toBeNull();

// Then we apply a filter
fireEvent.click(
screen.getByText('Title', { selector: 'li > span' })
);
fireEvent.click(screen.getByText('Title', { selector: 'li span' }));
await screen.findByDisplayValue(
'Accusantium qui nihil voluptatum quia voluptas maxime ab similique'
);
Expand All @@ -224,7 +224,7 @@ describe('<FilterButton />', () => {
await screen.findByText('Add filter');
fireEvent.click(screen.getByText('Add filter'));

await screen.findByText('Title', { selector: 'li > span' });
await screen.findByText('Title', { selector: 'li span' });
expect(screen.queryByDisplayValue('Remove all filters')).toBeNull();

// Then we apply a filter to an alwaysOn filter
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ export const FilterButtonMenuItem = forwardRef<any, FilterButtonMenuItemProps>(
autoFocus={autoFocus}
ref={ref}
disabled={filter.props.disabled}
role="menuitemcheckbox"
aria-checked={displayed}
>
<ListItemIcon>
{displayed ? (
Expand Down
Loading