Skip to content

Commit

Permalink
Components: Stop rendering MenuItemsGroup when no menu items provided
Browse files Browse the repository at this point in the history
  • Loading branch information
gziolo committed Feb 20, 2018
1 parent 97ed1bc commit 4a5fb14
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 4 deletions.
16 changes: 12 additions & 4 deletions components/menu-items/menu-items-group.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,24 @@ import './style.scss';
import { NavigableMenu } from '../navigable-container';
import withInstanceId from '../higher-order/with-instance-id';

function MenuItemsGroup( {
label,
export function MenuItemsGroup( {
children,
instanceId,
className = '',
filterName,
instanceId,
label,
} ) {
const childrenArray = Children.toArray( children );
const menuItems = filterName ?
applyFilters( filterName, childrenArray ) :
childrenArray;

if ( ! Array.isArray( menuItems ) || ! menuItems.length ) {
return null;
}

const labelId = `components-menu-items-group-label-${ instanceId }`;
const classNames = classnames( className, 'components-menu-items-group' );
const menuItems = filterName ? applyFilters( filterName, Children.toArray( children ) ) : children;

return (
<div className={ classNames }>
Expand Down
24 changes: 24 additions & 0 deletions components/menu-items/test/__snapshots__/menu-items-group.js.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`MenuItemsGroup should match snapshot 1`] = `
<div
className="components-menu-items-group"
>
<div
className="components-menu-items-group__label"
id="components-menu-items-group-label-1"
>
My group
</div>
<NavigableMenu
aria-labelledby="components-menu-items-group-label-1"
orientation="vertical"
>
<p
key=".0"
>
My item
</p>
</NavigableMenu>
</div>
`;
30 changes: 30 additions & 0 deletions components/menu-items/test/menu-items-group.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/**
* External dependencies
*/
import { shallow } from 'enzyme';

/**
* Internal dependencies
*/
import { MenuItemsGroup } from '../menu-items-group';

describe( 'MenuItemsGroup', () => {
test( 'should render null when no children provided', () => {
const wrapper = shallow( <MenuItemsGroup /> );

expect( wrapper.html() ).toBe( null );
} );

test( 'should match snapshot', () => {
const wrapper = shallow(
<MenuItemsGroup
label="My group"
instanceId="1"
>
<p>My item</p>
</MenuItemsGroup>
);

expect( wrapper ).toMatchSnapshot();
} );
} );

0 comments on commit 4a5fb14

Please sign in to comment.