Skip to content

Commit 1f5187e

Browse files
committed
fix(Avatar): fall back to initials when imageUrl is an empty string
An empty string passed `typeof imageUrl === 'string'`, so the component rendered `<img src="">`, which makes the browser attempt to load the current document URL as an image. Add a truthy check so empty strings fall through to the initials/icon fallback path.
1 parent 88c0ccc commit 1f5187e

2 files changed

Lines changed: 9 additions & 1 deletion

File tree

src/components/Avatar/Avatar.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ export const Avatar = ({
6666
return initials;
6767
}, [nameString, size]);
6868

69-
const showImage = typeof imageUrl === 'string' && !error;
69+
const showImage = typeof imageUrl === 'string' && imageUrl && !error;
7070

7171
return (
7272
<div

src/components/Avatar/__tests__/Avatar.test.tsx

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,14 @@ describe('Avatar', () => {
5757
expect(queryByTestId(AVATAR_IMG_TEST_ID)).not.toBeInTheDocument();
5858
});
5959

60+
it('should render fallback when imageUrl is an empty string', () => {
61+
const { getByTestId, queryByTestId } = render(
62+
<Avatar imageUrl='' size='md' userName='frank N. Stein' />,
63+
);
64+
expect(queryByTestId(AVATAR_IMG_TEST_ID)).not.toBeInTheDocument();
65+
expect(getByTestId(AVATAR_FALLBACK_TEST_ID)).toHaveTextContent('fS');
66+
});
67+
6068
it('should call onClick prop on user click', () => {
6169
const onClick = vi.fn();
6270

0 commit comments

Comments
 (0)