fix(ui): map a cleared Team dropdown back to an empty string in the auto-router form

This commit is contained in:
ryan-crabbe-berri 2026-09-01 15:50:19 -07:00
parent 55d638412b
commit 4acc1d15fb
2 changed files with 39 additions and 12 deletions

View file

@ -101,18 +101,24 @@ vi.mock("./build_complexity_router_config", async (importOriginal) => {
});
// A real TeamDropdown fetches teams and renders an antd Select; the wiring under test is
// whether team_id is registered, validated and forwarded, so a plain control stands in.
// whether team_id is registered, validated and forwarded, so a plain control stands in. The
// clear button mirrors the real dropdown's x, which emits null rather than a string.
vi.mock("../common_components/team_dropdown", () => ({
default: ({ value, onChange }: { value?: string; onChange?: (next: string) => void }) => (
<select
data-testid="team-dropdown"
value={value ?? ""}
onChange={(event) => onChange?.(event.target.value)}
aria-label="Select Team"
>
<option value="">none</option>
<option value="team-1">team-1</option>
</select>
default: ({ value, onChange }: { value?: string; onChange?: (next: string | null) => void }) => (
<>
<select
data-testid="team-dropdown"
value={value ?? ""}
onChange={(event) => onChange?.(event.target.value)}
aria-label="Select Team"
>
<option value="">none</option>
<option value="team-1">team-1</option>
</select>
<button type="button" data-testid="team-dropdown-clear" onClick={() => onChange?.(null)}>
clear team
</button>
</>
),
}));
@ -354,6 +360,25 @@ describe("AddAutoRouterTab", () => {
expect(handleAddAutoRouterSubmit).not.toHaveBeenCalled();
});
// The shared dropdown emits null on clear while this form's schema wants a string, so the
// form maps null back to "": the user sees the pick-a-team message, not a zod type error.
it("treats a team picked and then cleared like no team at all", async () => {
const user = userEvent.setup();
vi.mocked(getMissingTiersError).mockReturnValue(null);
renderWithProviders(
<AddAutoRouterTab handleOk={vi.fn()} accessToken="token" userRole="Internal User" createScope="team-required" />,
);
await user.type(screen.getByPlaceholderText(/smart_router/i), "team-scoped-router");
await user.selectOptions(screen.getByTestId("team-dropdown"), "team-1");
await user.click(screen.getByTestId("team-dropdown-clear"));
await user.click(screen.getByRole("button", { name: /add auto router/i }));
expect(await screen.findByText("Please select a team to continue")).toBeInTheDocument();
expect(handleAddAutoRouterSubmit).not.toHaveBeenCalled();
});
it("defaults a new router to session affinity off, matching the backend field default", async () => {
const user = userEvent.setup();
vi.mocked(getMissingTiersError).mockReturnValue(null);

View file

@ -548,7 +548,9 @@ const AddAutoRouterTab: React.FC<AddAutoRouterTabProps> = ({
"Select the team this auto router belongs to. Only keys for this team will be able to call it.",
)}
>
{({ id, value, onChange }) => <TeamDropdown id={id} value={value} onChange={onChange} />}
{({ id, value, onChange }) => (
<TeamDropdown id={id} value={value} onChange={(next) => onChange(next ?? "")} />
)}
</FormField>
)}