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(accordion): ne pas crasher s'il y a un selector invalid dans l'url #5935

Merged
merged 2 commits into from
Jun 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
73 changes: 73 additions & 0 deletions packages/react-ui/src/Accordion/__snapshots__/test.js.snap
Original file line number Diff line number Diff line change
@@ -1,5 +1,78 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`<Accordion /> do not throw error if preExpended attribut is an invalid query selector 1`] = `
<div>
<div
class="accordion"
data-accordion-component="Accordion"
>
<div>
<div
class="sc-fqkvVR eJjnVN"
data-accordion-component="AccordionItem"
>
<div
class="accordion__heading"
data-accordion-component="AccordionItemHeading"
>
<div
aria-controls="accordion__panel-raa-5"
aria-disabled="false"
aria-expanded="false"
class="sc-iGgWBj ctXAVT"
data-accordion-component="AccordionItemButton"
id="accordion__heading-raa-5"
role="button"
tabindex="0"
>
<svg
aria-hidden="true"
class="sc-eqUAAy eQjYoU"
fill="none"
height="24"
stroke="currentColor"
stroke-linecap="round"
stroke-linejoin="round"
stroke-width="2"
viewBox="0 0 24 24"
width="24"
xmlns="http://www.w3.org/2000/svg"
>
<polyline
points="9 18 15 12 9 6"
/>
</svg>
<div
class="sc-gsFSXq UOsQV"
>
<h3
class="sc-gEvEer gGoEse"
style="margin: 0px;"
>
This is the title
</h3>
</div>
</div>
</div>
<div
aria-hidden="true"
class="sc-dcJsrY dwmfbs"
data-accordion-component="AccordionItemPanel"
hidden=""
id="accordion__panel-raa-5"
>
<div
class="sc-dAbbOL dhcuWT"
>
this is the body
</div>
</div>
</div>
</div>
</div>
</div>
`;

exports[`<Accordion /> renders 1`] = `
<div>
<div
Expand Down
10 changes: 7 additions & 3 deletions packages/react-ui/src/Accordion/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,13 @@ export const Accordion = ({

React.useEffect(() => {
if (props?.preExpanded?.length && props.preExpanded[0]?.length) {
const anchor = document?.querySelector(`#${props.preExpanded[0]}`);
if (anchor) {
anchor.scrollIntoView();
try {
const anchor = document?.querySelector(`#${props.preExpanded[0]}`);
if (anchor) {
anchor.scrollIntoView();
}
} catch (_) {
props.preExpanded = [];
}
}
}, [props.preExpanded]);
Expand Down
11 changes: 11 additions & 0 deletions packages/react-ui/src/Accordion/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,15 @@ describe("<Accordion />", () => {
const { getByText } = render(<Accordion titleLevel={7} items={items} />);
expect(getByText("Ceci est un titre").tagName).toEqual("P");
});
it("do not throw error if preExpended attribut is an invalid query selector", () => {
const items = [{ body: "this is the body", title: "This is the title" }];
const { container } = render(
<Accordion
titleLevel={3}
preExpanded={["#:~:text=Le droit du travail est"]}
items={items}
/>
);
expect(container).toMatchSnapshot();
});
});
Loading