Sidemenu: Refactor SideMenuDropDown (#38554)

* SideMenuDropDown: Refactor to be more component-ey + rewrite tests in RTL

* SideMenuDropDown: Rename childLinks -> items

* Rename mockChildLinks -> mockItems as well
This commit is contained in:
Ashley Harrison 2021-08-26 14:08:27 +01:00 committed by GitHub
parent b231afd50f
commit a583f7e160
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 61 additions and 142 deletions

View File

@ -1,50 +1,51 @@
import React from 'react';
import { shallow } from 'enzyme';
import { render, screen } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import { BrowserRouter } from 'react-router-dom';
import SideMenuDropDown from './SideMenuDropDown';
const setup = (propOverrides?: object) => {
const props = Object.assign(
describe('SideMenuDropDown', () => {
const mockHeaderText = 'MyHeaderText';
const mockHeaderUrl = '/route';
const mockOnHeaderClick = jest.fn();
const mockItems = [
{
link: {
text: 'link',
},
text: 'First link',
},
propOverrides
);
{
text: 'Second link',
},
];
return shallow(<SideMenuDropDown {...props} />);
};
describe('Render', () => {
it('should render component', () => {
const wrapper = setup();
expect(wrapper).toMatchSnapshot();
it('displays the header text', () => {
render(<SideMenuDropDown headerText={mockHeaderText} />);
const text = screen.getByText(mockHeaderText);
expect(text).toBeInTheDocument();
});
it('should render children', () => {
const wrapper = setup({
link: {
text: 'link',
children: [{ id: 1 }, { id: 2 }, { id: 3 }],
},
});
expect(wrapper).toMatchSnapshot();
it('attaches the link to the header text if provided', () => {
render(
<BrowserRouter>
<SideMenuDropDown headerText={mockHeaderText} headerUrl={mockHeaderUrl} />
</BrowserRouter>
);
const link = screen.getByRole('link', { name: mockHeaderText });
expect(link).toBeInTheDocument();
});
it('should not render hideFromMenu children', () => {
const wrapper = setup({
link: {
text: 'link',
children: [
{ id: 1, hideFromMenu: false },
{ id: 2, hideFromMenu: true },
{ id: 3, hideFromMenu: false },
],
},
});
it('calls the onHeaderClick function when the header is clicked', () => {
render(<SideMenuDropDown headerText={mockHeaderText} onHeaderClick={mockOnHeaderClick} />);
const text = screen.getByText(mockHeaderText);
expect(text).toBeInTheDocument();
userEvent.click(text);
expect(mockOnHeaderClick).toHaveBeenCalled();
});
expect(wrapper).toMatchSnapshot();
it('displays the items', () => {
render(<SideMenuDropDown headerText={mockHeaderText} items={mockItems} />);
mockItems.forEach(({ text }) => {
const childItem = screen.getByText(text);
expect(childItem).toBeInTheDocument();
});
});
});

View File

@ -1,44 +1,41 @@
import React, { FC } from 'react';
import { filter } from 'lodash';
import React from 'react';
import DropDownChild from './DropDownChild';
import { NavModelItem } from '@grafana/data';
import { IconName, Link } from '@grafana/ui';
interface Props {
link: NavModelItem;
items?: NavModelItem[];
headerText: string;
headerUrl?: string;
onHeaderClick?: () => void;
}
const SideMenuDropDown: FC<Props> = (props) => {
const { link, onHeaderClick } = props;
let childrenLinks: NavModelItem[] = [];
if (link.children) {
childrenLinks = filter(link.children, (item) => !item.hideFromMenu);
}
const linkContent = <span className="sidemenu-item-text">{link.text}</span>;
const anchor = link.url ? (
<Link href={link.url} onClick={onHeaderClick} className="side-menu-header-link">
{linkContent}
const SideMenuDropDown = ({ items = [], headerText, headerUrl, onHeaderClick }: Props) => {
const headerContent = <span className="sidemenu-item-text">{headerText}</span>;
const header = headerUrl ? (
<Link href={headerUrl} onClick={onHeaderClick} className="side-menu-header-link">
{headerContent}
</Link>
) : (
<a onClick={onHeaderClick} className="side-menu-header-link">
{linkContent}
{headerContent}
</a>
);
return (
<ul className="dropdown-menu dropdown-menu--sidemenu" role="menu">
<li className="side-menu-header">{anchor}</li>
{childrenLinks.map((child, index) => (
<DropDownChild
key={`${child.url}-${index}`}
isDivider={child.divider}
icon={child.icon as IconName}
text={child.text}
url={child.url}
/>
))}
<li className="side-menu-header">{header}</li>
{items
.filter((item) => !item.hideFromMenu)
.map((child, index) => (
<DropDownChild
key={`${child.url}-${index}`}
isDivider={child.divider}
icon={child.icon as IconName}
text={child.text}
url={child.url}
/>
))}
</ul>
);
};

View File

@ -43,7 +43,7 @@ const TopSectionItem: FC<Props> = ({ link, onClick }) => {
return (
<div className="sidemenu-item dropdown">
{anchor}
<SideMenuDropDown link={link} onHeaderClick={onClick} />
<SideMenuDropDown items={link.children} headerText={link.text} headerUrl={link.url} onHeaderClick={onClick} />
</div>
);
};

View File

@ -1,79 +0,0 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`Render should not render hideFromMenu children 1`] = `
<ul
className="dropdown-menu dropdown-menu--sidemenu"
role="menu"
>
<li
className="side-menu-header"
>
<a
className="side-menu-header-link"
>
<span
className="sidemenu-item-text"
>
link
</span>
</a>
</li>
<DropDownChild
key="undefined-0"
/>
<DropDownChild
key="undefined-1"
/>
</ul>
`;
exports[`Render should render children 1`] = `
<ul
className="dropdown-menu dropdown-menu--sidemenu"
role="menu"
>
<li
className="side-menu-header"
>
<a
className="side-menu-header-link"
>
<span
className="sidemenu-item-text"
>
link
</span>
</a>
</li>
<DropDownChild
key="undefined-0"
/>
<DropDownChild
key="undefined-1"
/>
<DropDownChild
key="undefined-2"
/>
</ul>
`;
exports[`Render should render component 1`] = `
<ul
className="dropdown-menu dropdown-menu--sidemenu"
role="menu"
>
<li
className="side-menu-header"
>
<a
className="side-menu-header-link"
>
<span
className="sidemenu-item-text"
>
link
</span>
</a>
</li>
</ul>
`;