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

[material-ui][Avatar] Improve fallback when children is empty string or boolean #40766

Merged
merged 6 commits into from Jan 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/mui-material/src/Avatar/Avatar.js
Expand Up @@ -183,7 +183,7 @@ const Avatar = React.forwardRef(function Avatar(inProps, ref) {
{...imgProps}
/>
);
} else if (childrenProp != null) {
} else if (childrenProp != null && childrenProp !== '' && typeof childrenProp !== 'boolean') {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can shorten the condition without making any test fail: #40834.

children = childrenProp;
} else if (hasImg && alt) {
children = alt[0];
Expand Down
14 changes: 14 additions & 0 deletions packages/mui-material/src/Avatar/Avatar.test.js
Expand Up @@ -212,6 +212,20 @@ describe('<Avatar />', () => {

expect(avatar).to.have.class(classes.colorDefault);
});

it('should fallback if children is empty string', () => {
const container = render(<Avatar>{''}</Avatar>).container;
const avatar = container.firstChild;

expect(avatar.firstChild).to.have.attribute('data-testid', 'PersonIcon');
});

it('should fallback if children is false', () => {
const container = render(<Avatar>{false}</Avatar>).container;
const avatar = container.firstChild;

expect(avatar.firstChild).to.have.attribute('data-testid', 'PersonIcon');
});
});

it('should not throw error when ownerState is used in styleOverrides', () => {
Expand Down