mirror of
https://github.com/nextui-org/nextui.git
synced 2025-12-08 19:26:11 +00:00
fix(avatar): broken fallbacks (#5519)
This commit is contained in:
parent
5eb686843b
commit
2cb6ecff93
5
.changeset/tiny-ducks-think.md
Normal file
5
.changeset/tiny-ducks-think.md
Normal file
@ -0,0 +1,5 @@
|
||||
---
|
||||
"@heroui/avatar": patch
|
||||
---
|
||||
|
||||
fix broken fallbacks in avatar (#5506)
|
||||
@ -104,4 +104,69 @@ describe("Avatar - fallback + loading strategy", () => {
|
||||
|
||||
expect(container.querySelector("svg")).toBeInTheDocument();
|
||||
});
|
||||
|
||||
test("should render name initials fallback when image fails to load and showFallback is true", async () => {
|
||||
const mock = mocks.image();
|
||||
|
||||
mock.simulate("error");
|
||||
|
||||
const src = "https://avatars.githubusercontent.com/u/30373425";
|
||||
const name = "Junior Garcia";
|
||||
const {container} = render(<Avatar showFallback name={name} src={src} />);
|
||||
|
||||
act(() => {
|
||||
jest.runAllTimers();
|
||||
});
|
||||
|
||||
expect(container.querySelector("span")).toHaveTextContent("Jun");
|
||||
});
|
||||
|
||||
test("should render default avatar fallback when image fails to load with no name and showFallback is true", async () => {
|
||||
const mock = mocks.image();
|
||||
|
||||
mock.simulate("error");
|
||||
|
||||
const src = "https://avatars.githubusercontent.com/u/30373425";
|
||||
const {container} = render(<Avatar showFallback src={src} />);
|
||||
|
||||
act(() => {
|
||||
jest.runAllTimers();
|
||||
});
|
||||
|
||||
expect(container.querySelector("svg")).toBeInTheDocument();
|
||||
});
|
||||
|
||||
test("should not render fallback when image fails to load and showFallback is false", async () => {
|
||||
const mock = mocks.image();
|
||||
|
||||
mock.simulate("error");
|
||||
|
||||
const src = "https://avatars.githubusercontent.com/u/30373425";
|
||||
const name = "Junior Garcia";
|
||||
const {container} = render(<Avatar name={name} showFallback={false} src={src} />);
|
||||
|
||||
act(() => {
|
||||
jest.runAllTimers();
|
||||
});
|
||||
|
||||
expect(container.querySelector("span")).not.toHaveTextContent("Jun");
|
||||
expect(container.querySelector("svg")).not.toBeInTheDocument();
|
||||
});
|
||||
|
||||
test("should not render fallback when image fails to load and showFallback is not passed", async () => {
|
||||
const mock = mocks.image();
|
||||
|
||||
mock.simulate("error");
|
||||
|
||||
const src = "https://avatars.githubusercontent.com/u/30373425";
|
||||
const name = "Junior Garcia";
|
||||
const {container} = render(<Avatar name={name} src={src} />);
|
||||
|
||||
act(() => {
|
||||
jest.runAllTimers();
|
||||
});
|
||||
|
||||
expect(container.querySelector("span")).not.toHaveTextContent("Jun");
|
||||
expect(container.querySelector("svg")).not.toBeInTheDocument();
|
||||
});
|
||||
});
|
||||
|
||||
@ -145,7 +145,7 @@ export function useAvatar(originalProps: UseAvatarProps = {}) {
|
||||
src,
|
||||
onError,
|
||||
ignoreFallback,
|
||||
shouldBypassImageLoad: as !== undefined || !isHeroImage,
|
||||
shouldBypassImageLoad: as !== undefined || (ImgComponent !== "img" && !isHeroImage),
|
||||
});
|
||||
|
||||
const isImgLoaded = imageStatus === "loaded";
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user