Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions src/components/Avatar/Avatar.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,25 @@ describe('Avatar', () => {
).toBeInTheDocument();
});

it('trims alt before using it as the accessible name', () => {
render(<Avatar alt=" Profile photo " name="Ada Lovelace" />);

expect(screen.getByRole('img')).toHaveAccessibleName('Profile photo');
expect(screen.getByRole('img')).toHaveAttribute(
'aria-label',
'Profile photo',
);
});

it.each([
['empty', ''],
['whitespace-only', ' '],
])('falls back to name when alt is %s', (_description, alt) => {
render(<Avatar alt={alt} name="Ada Lovelace" />);

expect(screen.getByRole('img')).toHaveAccessibleName('Ada Lovelace');
});

it('renders an image and falls back to fallbackSrc when it fails', () => {
render(
<Avatar
Expand Down
6 changes: 5 additions & 1 deletion src/components/Avatar/Avatar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,11 @@ export function Avatar({
isGrouped: avatarGroup != null,
isLarge: numericSize >= 96,
});
const accessibleName = alt ?? (showInitials ? name : undefined) ?? 'Avatar';
const normalizedAlt = alt?.trim();
const accessibleName =
(normalizedAlt === '' ? undefined : normalizedAlt) ??
(showInitials ? name : undefined) ??
'Avatar';
const describedBy = isTooltipEnabled
? [ariaDescribedBy, tooltip.describedBy].filter(Boolean).join(' ')
: ariaDescribedBy;
Expand Down