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); + }); + }); });