From 81784a0ac84c174df0057247bfff83b7d5e662b3 Mon Sep 17 00:00:00 2001 From: Denis Zavershinskiy Date: Tue, 21 Mar 2023 17:16:37 +0700 Subject: [PATCH] [ListItemButton] Respect LinkComponent (#34159) --- .../src/ListItemButton/ListItemButton.js | 3 +- .../src/ListItemButton/ListItemButton.test.js | 108 ++++++++++++++++++ 2 files changed, 110 insertions(+), 1 deletion(-) diff --git a/packages/mui-material/src/ListItemButton/ListItemButton.js b/packages/mui-material/src/ListItemButton/ListItemButton.js index aecdb0ca25fbcf..f636f5f0f25767 100644 --- a/packages/mui-material/src/ListItemButton/ListItemButton.js +++ b/packages/mui-material/src/ListItemButton/ListItemButton.js @@ -181,7 +181,8 @@ const ListItemButton = React.forwardRef(function ListItemButton(inProps, ref) { ', () => { @@ -140,4 +141,111 @@ describe('', () => { expect(!!heading).to.equal(true); }); }); + + describe('prop: LinkComponent', () => { + const href = 'example.com'; + const customLinkId = 'customLink'; + const CustomLink = React.forwardRef((props, ref) => { + // eslint-disable-next-line jsx-a11y/anchor-has-content + return ; + }); + + it('should rendered as LinkComponent when href is provided', () => { + const { container, getByTestId } = render( + , + ); + const button = container.firstChild; + + expect(getByTestId(customLinkId)).not.to.equal(null); + expect(button).to.have.property('nodeName', 'A'); + expect(button).to.have.attribute('href', href); + }); + + it('should ignore LinkComponent is component is provided', () => { + const { container, queryByTestId } = render( + , + ); + const button = container.firstChild; + + expect(queryByTestId(customLinkId)).to.equal(null); + expect(button).to.have.property('nodeName', 'H1'); + expect(button).to.have.attribute('href', href); + }); + + it('should rendered as LinkComponent (from theme) when href is provided', () => { + const theme = createTheme({ + components: { + MuiListItemButton: { + defaultProps: { + LinkComponent: CustomLink, + }, + }, + }, + }); + const { container, getByTestId } = render( + + , + , + ); + const button = container.firstChild; + + expect(getByTestId(customLinkId)).not.to.equal(null); + expect(button).to.have.property('nodeName', 'A'); + expect(button).to.have.attribute('href', href); + }); + + it('should rendered as LinkComponent (from theme MuiButtonBase) when href is provided', () => { + const theme = createTheme({ + components: { + MuiButtonBase: { + defaultProps: { + LinkComponent: CustomLink, + }, + }, + }, + }); + const { container, getByTestId } = render( + + , + , + ); + const button = container.firstChild; + + expect(getByTestId(customLinkId)).not.to.equal(null); + expect(button).to.have.property('nodeName', 'A'); + expect(button).to.have.attribute('href', href); + }); + + it('should prefer LinkComponent from MuiListItemButton over MuiButtonBase', () => { + const WrongCustomLink = React.forwardRef((props, ref) => { + // eslint-disable-next-line jsx-a11y/anchor-has-content + return ; + }); + + const theme = createTheme({ + components: { + MuiListItemButton: { + defaultProps: { + LinkComponent: CustomLink, + }, + }, + MuiButtonBase: { + defaultProps: { + LinkComponent: WrongCustomLink, + }, + }, + }, + }); + const { container, getByTestId } = render( + + , + , + ); + const button = container.firstChild; + + expect(getByTestId(customLinkId)).not.to.equal(null); + expect(button).to.have.property('nodeName', 'A'); + expect(button).to.have.attribute('href', href); + }); + }); });