72 lines
2.2 KiB
TypeScript
72 lines
2.2 KiB
TypeScript
import { render, screen } from "@testing-library/react";
|
|
import userEvent from "@testing-library/user-event";
|
|
import { vi } from "vitest";
|
|
|
|
import { FleetActionSelector } from "./FleetActionSelector";
|
|
|
|
const actions = [
|
|
{ id: "sync", label: "Synchronize tables", group: "Synchronization", scopeLabel: "All tables in this database" },
|
|
{
|
|
id: "generate",
|
|
label: "Generate column descriptions",
|
|
group: "Descriptions",
|
|
disabled: true,
|
|
disabledReason: "Choose a metadata model first",
|
|
},
|
|
] as const;
|
|
|
|
test("disables the picker when no rows are selected", () => {
|
|
render(
|
|
<FleetActionSelector
|
|
actions={actions}
|
|
selectedCount={0}
|
|
onRun={vi.fn()}
|
|
onClear={vi.fn()}
|
|
/>,
|
|
);
|
|
|
|
expect(screen.getByRole("combobox", { name: "Batch action" })).toBeDisabled();
|
|
});
|
|
|
|
test("keeps unavailable commands discoverable and explains why Run is disabled", async () => {
|
|
const user = userEvent.setup();
|
|
render(
|
|
<FleetActionSelector
|
|
actions={actions}
|
|
selectedCount={1}
|
|
onRun={vi.fn()}
|
|
onClear={vi.fn()}
|
|
renderContext={(action) => action === "generate" ? <span>Model selection</span> : null}
|
|
/>,
|
|
);
|
|
|
|
await user.selectOptions(screen.getByRole("combobox", { name: "Batch action" }), "generate");
|
|
|
|
expect(screen.getByText("Choose a metadata model first")).toBeVisible();
|
|
expect(screen.getByText("Model selection")).toBeVisible();
|
|
expect(screen.getByRole("button", { name: "Run action" })).toBeDisabled();
|
|
});
|
|
|
|
test("runs an enabled command explicitly and resets the picker", async () => {
|
|
const user = userEvent.setup();
|
|
const onRun = vi.fn().mockResolvedValue(undefined);
|
|
render(
|
|
<FleetActionSelector
|
|
actions={actions}
|
|
selectedCount={2}
|
|
onRun={onRun}
|
|
onClear={vi.fn()}
|
|
/>,
|
|
);
|
|
|
|
const picker = screen.getByRole("combobox", { name: "Batch action" });
|
|
await user.selectOptions(picker, "sync");
|
|
expect(screen.getByText("All tables in this database")).toBeVisible();
|
|
expect(screen.getByText("Action scope")).toBeVisible();
|
|
expect(screen.getByRole("button", { name: "Run action" })).toHaveClass("bg-destructive");
|
|
await user.click(screen.getByRole("button", { name: "Run action" }));
|
|
|
|
expect(onRun).toHaveBeenCalledWith("sync");
|
|
expect(picker).toHaveValue("");
|
|
});
|