Skip to content

Commit 4352a26

Browse files
renovate[bot]Stefan3002
authored andcommitted
fix: a11y bug: aria-expanded not supported here
1 parent 1e63d1c commit 4352a26

File tree

8 files changed

+67
-31
lines changed

8 files changed

+67
-31
lines changed

src/components/Chip/Chip.test.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ describe("Chip ", () => {
4141
);
4242
expect(
4343
within(screen.getByTestId("chip")).getByRole("button", {
44-
name: Label.Dismiss,
44+
name: `${Label.Dismiss} Bob`,
4545
}),
4646
).toBeInTheDocument();
4747
});
@@ -59,7 +59,7 @@ describe("Chip ", () => {
5959
const dismissButton = within(screen.getByTestId("chip")).getByRole(
6060
"button",
6161
{
62-
name: Label.Dismiss,
62+
name: `${Label.Dismiss} Bob`,
6363
},
6464
);
6565
await userEvent.click(dismissButton);
@@ -209,7 +209,7 @@ describe("Chip ", () => {
209209
const dismissButton = within(screen.getByTestId("chip")).getByRole(
210210
"button",
211211
{
212-
name: Label.Dismiss,
212+
name: `${Label.Dismiss} Bob`,
213213
},
214214
);
215215
await userEvent.click(dismissButton);

src/components/Chip/Chip.tsx

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,8 +150,15 @@ const Chip = ({
150150
return (
151151
<span {...props} className={chipClassName}>
152152
{chipContent}
153-
<button className="p-chip__dismiss" onClick={onDismiss} type="button">
154-
<i className="p-icon--close">{Label.Dismiss}</i>
153+
<button
154+
className="p-chip__dismiss"
155+
onClick={onDismiss}
156+
type="button"
157+
aria-label={`Dismiss ${value}`}
158+
>
159+
<i className="p-icon--close" aria-hidden="true">
160+
{Label.Dismiss}
161+
</i>
155162
</button>
156163
</span>
157164
);

src/components/SearchAndFilter/FilterPanelSection/FilterPanelSection.test.tsx

Lines changed: 35 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,11 @@ const sampleData = {
1010
chips: [{ value: "us-east1" }, { value: "us-east2" }, { value: "us-east3" }],
1111
};
1212

13+
const manyChipsData = {
14+
...sampleData,
15+
chips: Array.from({ length: 30 }, (_, i) => ({ value: `us-east${i + 1}` })),
16+
};
17+
1318
describe("Filter panel section", () => {
1419
it("renders", () => {
1520
render(
@@ -100,25 +105,45 @@ describe("Filter panel section", () => {
100105
});
101106

102107
it("all chips are shown when counter is clicked", async () => {
108+
// Jest is unaware of layout so we must mock the offsetTop and offsetHeight
109+
// of the chips to force the overflow counter to show.
110+
Object.defineProperty(HTMLElement.prototype, "offsetHeight", {
111+
configurable: true,
112+
value: 40,
113+
});
114+
Object.defineProperty(HTMLElement.prototype, "offsetTop", {
115+
configurable: true,
116+
value: 100,
117+
});
103118
render(
104119
<FilterPanelSection
105120
searchData={[]}
106121
searchTerm=""
107122
toggleSelected={jest.fn()}
108-
data={sampleData}
123+
data={manyChipsData}
109124
sectionHidden={false}
110125
/>,
111126
);
127+
128+
const counter = document.querySelector(
129+
".p-filter-panel-section__counter",
130+
) as HTMLElement;
131+
expect(counter).toBeInTheDocument();
132+
133+
await userEvent.click(counter);
134+
112135
expect(
113-
document.querySelector(".p-filter-panel-section__chips"),
114-
).toHaveAttribute("aria-expanded", "false");
115-
await userEvent.click(
116-
// Use a query selector because the element's text is split up over
117-
// multiple elements so it can't be selected by its content.
118-
document.querySelector(".p-filter-panel-section__counter") as HTMLElement,
119-
);
136+
document.querySelector(".p-filter-panel-section__counter"),
137+
).not.toBeInTheDocument();
138+
120139
expect(
121-
document.querySelector(".p-filter-panel-section__chips"),
122-
).toHaveAttribute("aria-expanded", "true");
140+
screen.getByRole("button", { name: "us-east1" }),
141+
).toBeInTheDocument();
142+
expect(
143+
screen.getByRole("button", { name: "us-east15" }),
144+
).toBeInTheDocument();
145+
expect(
146+
screen.getByRole("button", { name: "us-east30" }),
147+
).toBeInTheDocument();
123148
});
124149
});

src/components/SearchAndFilter/FilterPanelSection/FilterPanelSection.tsx

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -104,11 +104,7 @@ const FilterPanelSection = ({
104104
}}
105105
/>
106106
)}
107-
<div
108-
className="p-filter-panel-section__chips"
109-
aria-expanded={expanded}
110-
ref={chipWrapper}
111-
>
107+
<div className="p-filter-panel-section__chips" ref={chipWrapper}>
112108
{chips?.map((chip) => {
113109
// If search term has been added to input, only matching chips
114110
// should display

src/components/SearchAndFilter/FilterPanelSection/__snapshots__/FilterPanelSection.test.tsx.snap

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ exports[`Filter panel section renders 1`] = `
1010
Regions
1111
</h3>
1212
<div
13-
aria-expanded="false"
1413
class="p-filter-panel-section__chips"
1514
>
1615
<button

src/components/SearchAndFilter/SearchAndFilter.test.tsx

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,6 @@ const sampleData = [
2828
];
2929
const getPanel = () => document.querySelector(".p-search-and-filter__panel");
3030

31-
const getSearchContainer = () =>
32-
document.querySelector(".p-search-and-filter__search-container");
33-
3431
describe("Search and filter", () => {
3532
it("renders", async () => {
3633
const returnSearchData = jest.fn();
@@ -155,14 +152,27 @@ describe("Search and filter", () => {
155152
returnSearchData={returnSearchData}
156153
/>,
157154
);
158-
expect(getSearchContainer()).toHaveAttribute("aria-expanded", "false");
159155
await userEvent.click(
160156
screen.getByRole("searchbox", { name: Label.SearchAndFilter }),
161157
);
162158
await userEvent.click(screen.getByRole("button", { name: "us-east1" }));
163-
await userEvent.click(screen.getByRole("button", { name: "+1" }));
159+
const counter = screen.getByRole("button", { name: "+1" });
160+
await userEvent.click(counter);
164161

165-
expect(getSearchContainer()).toHaveAttribute("aria-expanded", "true");
162+
// After expanding, the container should indicate it is expanded.
163+
expect(
164+
document.querySelector(".p-search-and-filter__search-container"),
165+
).toHaveAttribute("data-expanded", "true");
166+
167+
expect(
168+
screen.getByRole("button", { name: "us-east1" }),
169+
).toBeInTheDocument();
170+
expect(
171+
screen.getByRole("button", { name: "us-east2" }),
172+
).toBeInTheDocument();
173+
expect(
174+
screen.getByRole("button", { name: "us-east3" }),
175+
).toBeInTheDocument();
166176
});
167177

168178
it("search prompt appears when search field has search term", async () => {
@@ -388,7 +398,7 @@ describe("Search and filter", () => {
388398
// Dismiss the Cloud: Google filter chip
389399
const cloudChip: HTMLElement = screen.getByText("CLOUD").closest(".p-chip");
390400
const dismissButton = within(cloudChip).getByRole("button", {
391-
name: "Dismiss",
401+
name: "Dismiss Google",
392402
});
393403
await userEvent.click(dismissButton);
394404

src/components/SearchAndFilter/SearchAndFilter.tsx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import React, { useState, useEffect, useRef, KeyboardEvent } from "react";
2-
32
import FilterPanelSection from "./FilterPanelSection";
43
import Chip from "../Chip";
54
import { overflowingChipsCount, isChipInArray } from "./utils";
@@ -241,7 +240,7 @@ const SearchAndFilter = ({
241240
>
242241
<div
243242
className="p-search-and-filter__search-container"
244-
aria-expanded={searchBoxExpanded}
243+
data-expanded={searchBoxExpanded}
245244
data-active={searchContainerActive || searchData.length === 0}
246245
data-empty={searchData.length <= 0}
247246
ref={searchContainerRef}

src/components/SearchAndFilter/__snapshots__/SearchAndFilter.test.tsx.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@ exports[`Search and filter renders 1`] = `
66
data-testid="searchandfilter"
77
>
88
<div
9-
aria-expanded="false"
109
class="p-search-and-filter__search-container"
1110
data-active="true"
1211
data-empty="true"
12+
data-expanded="false"
1313
>
1414
<form
1515
class="p-search-and-filter__box"

0 commit comments

Comments
 (0)