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
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { act } from "react";
import { create, type ReactTestRenderer } from "react-test-renderer";
import { afterEach, expect, it, vi } from "vite-plus/test";

import { PullRequestActorAvatar } from "./pullRequestPresentation";

let renderer: ReactTestRenderer | undefined;

afterEach(async () => {
await act(async () => renderer?.unmount());
vi.unstubAllGlobals();
});

it("falls back to the actor initial when a remote avatar fails", async () => {
vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true);
await act(async () => {
renderer = create(
<PullRequestActorAvatar
actor={{ login: "octocat", name: null, avatarUrl: "https://ghe.example/octocat.png" }}
/>,
);
});

await act(async () => renderer!.root.findByType("img").props.onError());

expect(renderer!.root.findAllByType("img")).toHaveLength(0);
expect(renderer!.root.findByType("span").children).toEqual(["O"]);
});
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import {
TriangleAlertIcon,
UserCheckIcon,
} from "lucide-react";
import { Children, isValidElement, type ReactNode } from "react";
import { Children, isValidElement, type ReactNode, useState } from "react";

import { cn } from "~/lib/utils";

Expand Down Expand Up @@ -346,8 +346,9 @@ export function PullRequestActorAvatar({
}) {
const login = actor?.login ?? "ghost";
const avatarUrl = actor?.avatarUrl ?? null;
return avatarUrl === null ? (
// Not every host reports an avatar, so the initial stands in where none arrives.
const [failedAvatarUrl, setFailedAvatarUrl] = useState<string | null>(null);
return avatarUrl === null || failedAvatarUrl === avatarUrl ? (
// Not every host reports an avatar, and a private host may refuse the browser's request.
<span
aria-hidden
className={cn(
Expand All @@ -364,6 +365,7 @@ export function PullRequestActorAvatar({
src={avatarUrl}
loading="lazy"
className={cn("size-4 shrink-0 rounded-full bg-muted object-cover", className)}
onError={() => setFailedAvatarUrl(avatarUrl)}
/>
);
}
Expand Down
Loading