Open History to see the last commit
1import { EnvironmentId, ProjectId, type PullRequestReaction } from "@t3tools/contracts";
2import { expect, it, vi } from "vite-plus/test";
3import { userEvent } from "vite-plus/test/browser";
4
5import { act, render } from "../../../test/render";
6
7const state = vi.hoisted(() => ({ send: vi.fn() }));
8vi.mock("~/state/use-atom-command", () => ({ useAtomCommand: () => state.send }));
9vi.mock("~/state/pullRequests", () => ({ pullRequestEnvironment: { setReaction: {} } }));
10
11import { PullRequestReactionBar } from "./PullRequestReactions";
12
13it("keeps the latest reaction choice when an older toggle fails", async () => {
14 state.send.mockReset();
15 let failFirst = () => {};
16 let finishLater = () => {};
17 state.send
18 .mockReturnValueOnce(new Promise((resolve) => (failFirst = () => resolve({ _tag: "Failure" }))))
19 .mockReturnValue(
20 new Promise(
21 (resolve) => (finishLater = () => resolve({ _tag: "Success", value: undefined })),
22 ),
23 );
24 await using screen = await render(
25 <PullRequestReactionBar
26 environmentId={EnvironmentId.make("test")}
27 reference={{ projectId: ProjectId.make("project"), repository: "owner/repo", number: 1 }}
28 reactions={[{ content: "thumbs-up", count: 1, viewerHasReacted: false, actors: ["other"] }]}
29 canReact={true}
30 onRefresh={() => {}}
31 />,
32 );
33 await userEvent.click(screen.getByRole("button", { name: "thumbs up, 1", exact: true }));
34 await userEvent.click(screen.getByRole("button", { name: "thumbs up, 2", exact: true }));
35 await userEvent.click(screen.getByRole("button", { name: "thumbs up, 1", exact: true }));
36 const latest = screen.getByRole("button", { name: "thumbs up, 2", exact: true });
37 await expect.element(latest).toHaveAttribute("aria-pressed", "true");
38
39 await act(() => failFirst());
40 await expect.element(latest).toHaveAttribute("aria-pressed", "true");
41 await act(() => finishLater());
42});
43
44it("shows a reaction removed on the host after its toggle synced", async () => {
45 state.send.mockReset();
46 state.send.mockResolvedValue({ _tag: "Success", value: undefined });
47 const others: PullRequestReaction = {
48 content: "thumbs-up",
49 count: 1,
50 viewerHasReacted: false,
51 actors: ["other"],
52 };
53 const ui = (reactions: ReadonlyArray<PullRequestReaction>) => (
54 <PullRequestReactionBar
55 environmentId={EnvironmentId.make("test")}
56 reference={{ projectId: ProjectId.make("project"), repository: "owner/repo", number: 1 }}
57 reactions={reactions}
58 canReact={true}
59 onRefresh={() => {}}
60 />
61 );
62 await using screen = await render(ui([others]));
63 await userEvent.click(screen.getByRole("button", { name: "thumbs up, 1", exact: true }));
64 await expect
65 .element(screen.getByRole("button", { name: "thumbs up, 2", exact: true }))
66 .toHaveAttribute("aria-pressed", "true");
67
68 await screen.rerender(ui([{ ...others, count: 2, viewerHasReacted: true }]));
69 await screen.rerender(ui([others]));
70 expect(screen.getByRole("button", { name: "thumbs up, 2", exact: true }).elements().length).toBe(
71 0,
72 );
73 await expect
74 .element(screen.getByRole("button", { name: "thumbs up, 1", exact: true }))
75 .toHaveAttribute("aria-pressed", "false");
76});