Skip to content

Commit

Permalink
Merge pull request #1445 from cozy/am-null-child
Browse files Browse the repository at this point in the history
Support ActionMenu having null children
  • Loading branch information
y-lohse committed Apr 28, 2020
2 parents 214b0e0 + 65dbde2 commit 63e20d2
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 2 deletions.
2 changes: 1 addition & 1 deletion react/ActionMenu/Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const hideMenu = () => setState({ menuDisplayed: false });
{state.menuDisplayed &&
<ActionMenu
onClose={hideMenu}>
<ActionMenuItem left={<Icon icon='file' />}>Item 1</ActionMenuItem>
<ActionMenuItem left={<Icon icon='file' />} right={<Icon icon='warning' />}>Item 1</ActionMenuItem>
<ActionMenuItem left={<Icon icon='right' />}>Item 2</ActionMenuItem>
<ActionMenuItem left={<Icon icon='file' />}>
<div>
Expand Down
11 changes: 11 additions & 0 deletions react/ActionMenu/__snapshots__/index.spec.jsx.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`ActionMenu should support null children 1`] = `
<div
className="styles__c-actionmenu___22Fp1 styles__c-actionmenu--inline___1SXZa"
>
<ActionMenuItem>
Item 1
</ActionMenuItem>
</div>
`;
2 changes: 1 addition & 1 deletion react/ActionMenu/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ const ActionMenu = ({
})}
>
{React.Children.map(children, child =>
child.type === ActionMenuHeader && isDesktop ? null : child
child && child.type === ActionMenuHeader && isDesktop ? null : child
)}
</div>
</ActionMenuWrapper>
Expand Down
24 changes: 24 additions & 0 deletions react/ActionMenu/index.spec.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import React from 'react'
import { mount } from 'enzyme'

import ActionMenu, { ActionMenuItem } from './'
import { fixPopperTesting } from '../Popper/testing'

describe('ActionMenu', () => {
fixPopperTesting()

it('should support null children', () => {
const comp = mount(
<ActionMenu onClose={jest.fn()}>
<ActionMenuItem>Item 1</ActionMenuItem>
{null}
</ActionMenu>
)
expect(
comp
.find(ActionMenuItem)
.parent()
.getElement()
).toMatchSnapshot()
})
})
21 changes: 21 additions & 0 deletions react/Popper/testing.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Fixes the error "TypeError: document.createRange is not a function" in jest,
// see https://github.com/mui-org/material-ui/issues/15726#issuecomment-493124813
export const fixPopperTesting = () => {
let createRangeBackup

beforeAll(() => {
createRangeBackup = global.document.createRange
global.document.createRange = jest.fn(() => ({
setStart: () => {},
setEnd: () => {},
commonAncestorContainer: {
nodeName: 'BODY',
ownerDocument: document
}
}))
})

afterAll(() => {
global.document.createRange = createRangeBackup
})
}
3 changes: 3 additions & 0 deletions react/__snapshots__/examples.spec.jsx.snap
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ exports[`ActionMenu should render examples: ActionMenu 1`] = `
<use xlink:href=\\"#file\\"></use>
</svg></div>
<div class=\\"styles__bd___1Uv-F u-mr-1\\">Item 1</div>
<div class=\\"styles__img___3SHpG u-mr-1\\"><svg class=\\"styles__icon___23x3R\\" width=\\"16\\" height=\\"16\\">
<use xlink:href=\\"#warning\\"></use>
</svg></div>
</div>
<div class=\\"styles__media___cSJMp styles__media--top___K9w0I styles__c-actionmenu-item___gODqd\\">
<div class=\\"styles__img___3SHpG u-mh-1\\"><svg class=\\"styles__icon___23x3R\\" width=\\"16\\" height=\\"16\\">
Expand Down

0 comments on commit 63e20d2

Please sign in to comment.