Файли
openidea/src/ui/Button.test.tsx
T
ntbang0901таNguyenBang 16bf9b53a2 feat(plugins): show a running state on a plugin command's toolbar button (#2371)
A plugin command that takes seconds (an LLM call that fills the commit message) gave no
sign at the button that the click was taken, and a second click started a second run.

Actions may now set `indicatesRunning`: the action runner counts their in-flight runs
and an IconButton bound to such an action shows a spinner, sets aria-busy and ignores
clicks until the run settles. Plugin commands set it, so every toolbar slot that draws
them (editor.title, mainToolbar, commit.message) is covered without a plugin API change
and without features importing each other. The button stays focusable (aria-disabled,
not disabled). Spinner moves to its own module so Button can use it without a cycle
with Feedback, which re-exports it.

Refs #2254

Co-authored-by: NguyenBang <[email protected]>
2026-11-09 22:43:53 +08:00

61 рядки
2.4 KiB
TypeScript

import { afterEach, describe, expect, it, vi } from "vitest";
import { act, cleanup, fireEvent, render, screen } from "@testing-library/react";
import { registerAction } from "@/platform/actions";
import { IconButton } from "./Button";
afterEach(cleanup);
describe("IconButton bound to an action that indicates running", () => {
const setup = () => {
let finish!: () => void;
const run = vi.fn(() => new Promise<void>((resolve) => (finish = resolve)));
const dispose = registerAction({ id: "test.generate", title: "Generate", indicatesRunning: true, run });
return { run, finish: () => finish(), dispose };
};
it("shows a spinner and ignores clicks until the run settles, then goes back to its icon", async () => {
const { run, finish, dispose } = setup();
render(<IconButton icon="plugin" action="test.generate" />);
const button = screen.getByRole("button", { name: "Generate" });
expect(button).not.toHaveAttribute("aria-busy");
expect(button.querySelector('[role="status"]')).toBeNull();
fireEvent.click(button);
expect(await screen.findByRole("status")).toBeInTheDocument();
expect(button).toHaveAttribute("aria-busy", "true");
expect(button).toHaveAttribute("aria-disabled", "true");
expect(button).not.toBeDisabled();
fireEvent.click(button);
fireEvent.click(button);
expect(run).toHaveBeenCalledTimes(1);
await act(async () => finish());
expect(button).not.toHaveAttribute("aria-busy");
expect(screen.queryByRole("status")).toBeNull();
fireEvent.click(button);
expect(run).toHaveBeenCalledTimes(2);
await act(async () => finish());
dispose();
});
it("also shows the run started from elsewhere, such as a shortcut", async () => {
const { finish, dispose } = setup();
render(<IconButton icon="plugin" action="test.generate" />);
const { executeAction } = await import("@/platform/actions");
void executeAction("test.generate");
expect(await screen.findByRole("status")).toBeInTheDocument();
await act(async () => finish());
expect(screen.queryByRole("status")).toBeNull();
dispose();
});
it("leaves other buttons alone", () => {
const dispose = registerAction({ id: "test.plainAction", title: "Plain", run: () => {} });
render(<IconButton icon="plugin" action="test.plainAction" />);
fireEvent.click(screen.getByRole("button", { name: "Plain" }));
expect(screen.queryByRole("status")).toBeNull();
dispose();
});
});