Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Masonry] Check if container or child exists to prevent error #29452

Merged
merged 10 commits into from
Nov 9, 2021
3 changes: 3 additions & 0 deletions packages/mui-lab/src/Masonry/Masonry.js
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,9 @@ const Masonry = React.forwardRef(function Masonry(inProps, ref) {

React.useEffect(() => {
const handleResize = () => {
if (!masonryRef.current || !masonryRef.current.firstChild) {
hbjORbj marked this conversation as resolved.
Show resolved Hide resolved
return;
}
const parentWidth = masonryRef.current.clientWidth;
const childWidth = masonryRef.current.firstChild.clientWidth;
const firstChildComputedStyle = window.getComputedStyle(masonryRef.current.firstChild);
Expand Down
19 changes: 19 additions & 0 deletions packages/mui-lab/src/Masonry/Masonry.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,25 @@ describe('<Masonry />', () => {
width: `calc(${(100 / columns).toFixed(2)}% - ${theme.spacing(spacing)})`,
});
});

it('should throw console error when children are empty', function test() {
if (!/jsdom/.test(window.navigator.userAgent)) {
this.skip();
}
expect(() => render(<Masonry columns={3} spacing={1} />)).toErrorDev(
'Warning: Failed prop type: The prop `children` is marked as required in `ForwardRef(Masonry)`, but its value is `undefined`.',
);
});

it('should not throw type error when children are empty', function test() {
if (/jsdom/.test(window.navigator.userAgent)) {
this.skip();
}
expect(() => render(<Masonry columns={3} spacing={1} />)).toErrorDev(
'Warning: Failed prop type: The prop `children` is marked as required in `ForwardRef(Masonry)`, but its value is `undefined`.',
);
expect(() => render(<Masonry columns={3} spacing={1} />)).not.to.throw(new TypeError());
});
});

describe('style attribute:', () => {
Expand Down