Skip to content

Commit

Permalink
fix(menu-item): ensure that it does not render children when `submenu…
Browse files Browse the repository at this point in the history
…` is an empty string

Adds invariant to ensure that the component does not render when `children` are passed but `submenu`
is an empty string

fix #7010
  • Loading branch information
edleeks87 committed Oct 21, 2024
1 parent 501337c commit 975d5c6
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/components/menu/menu-item/menu-item.component.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,13 @@ export const MenuItem = ({
"If no text is provided an `ariaLabel` should be given to facilitate accessibility."
);

invariant(
typeof submenu === "boolean" ||
submenu === undefined ||
(children && typeof submenu === "string" && submenu.length),
"You should not pass `children` when `submenu` is an empty string"
);

const {
inFullscreenView,
registerItem,
Expand Down
18 changes: 18 additions & 0 deletions src/components/menu/menu-item/menu-item.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1058,3 +1058,21 @@ describe("when MenuItem has a submenu", () => {
});
});
});

test("should throw when `children` passed and `submenu` is an empty string", () => {
const consoleSpy = jest
.spyOn(global.console, "error")
.mockImplementation(() => {});

expect(() => {
render(
<MenuContext.Provider value={{ ...menuContextValues }}>
<MenuItem submenu="">Item One</MenuItem>
</MenuContext.Provider>
);
}).toThrow(
"You should not pass `children` when `submenu` is an empty string"
);

consoleSpy.mockRestore();
});

0 comments on commit 975d5c6

Please sign in to comment.