Skip to content

Commit 7a63028

Browse files
committed
feat(toolbar): forwardRef in toolbar, add tests
1 parent 1e7b371 commit 7a63028

File tree

2 files changed

+41
-17
lines changed

2 files changed

+41
-17
lines changed

src/components/Toolbar/Toolbar.js

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,30 +6,25 @@ const StyledToolbar = styled.div`
66
position: relative;
77
display: flex;
88
align-items: center;
9-
padding: ${props => (props.noPadding ? '0px' : '4px')};
9+
padding: ${props => (props.noPadding ? '0' : '4px')};
1010
`;
1111

12-
const Toolbar = ({ children, className, style, noPadding, ...otherProps }) => (
13-
<StyledToolbar
14-
noPadding={noPadding}
15-
className={className}
16-
style={style}
17-
{...otherProps}
18-
>
19-
{children}
20-
</StyledToolbar>
21-
);
12+
const Toolbar = React.forwardRef(function Toolbar(props, ref) {
13+
const { children, noPadding, ...otherProps } = props;
14+
return (
15+
<StyledToolbar noPadding={noPadding} ref={ref} {...otherProps}>
16+
{children}
17+
</StyledToolbar>
18+
);
19+
});
2220

2321
Toolbar.defaultProps = {
24-
noPadding: false,
25-
style: {},
26-
className: ''
22+
children: null,
23+
noPadding: false
2724
};
2825

2926
Toolbar.propTypes = {
30-
style: propTypes.shape([propTypes.string, propTypes.number]),
31-
className: propTypes.string,
32-
children: propTypes.node.isRequired,
27+
children: propTypes.node,
3328
noPadding: propTypes.bool
3429
};
3530

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import React from 'react';
2+
import { render } from '@testing-library/react';
3+
4+
import Toolbar from './Toolbar';
5+
6+
describe('<Toolbar />', () => {
7+
it('should render', () => {
8+
const { container } = render(<Toolbar />);
9+
const toolbar = container.firstChild;
10+
11+
expect(toolbar).toBeInTheDocument();
12+
});
13+
14+
it('should render children', () => {
15+
const { container } = render(<Toolbar>A nice app bar</Toolbar>);
16+
const toolbar = container.firstChild;
17+
18+
expect(toolbar).toHaveTextContent('A nice app bar');
19+
});
20+
21+
describe('prop: noPadding', () => {
22+
it('should apply 0 padding', () => {
23+
const { container } = render(<Toolbar noPadding />);
24+
const toolbar = container.firstChild;
25+
26+
expect(toolbar).toHaveStyleRule('padding', '0');
27+
});
28+
});
29+
});

0 commit comments

Comments
 (0)