Skip to content
Open
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
Expand Up @@ -6,7 +6,6 @@ import { Button } from "@superset/ui/button";
import {
DropdownMenu,
DropdownMenuContent,
DropdownMenuItem,
DropdownMenuTrigger,
} from "@superset/ui/dropdown-menu";
import { toast } from "@superset/ui/sonner";
Expand Down Expand Up @@ -43,6 +42,7 @@ import { useRestartAgentSessions } from "../../hooks/useRestartAgentSessions";
import { useSetDefaultUsageAccount } from "../../hooks/useSetDefaultUsageAccount";
import { LeaderboardCard } from "../LeaderboardCard";
import { UsageHistorySection } from "../UsageHistorySection";
import { AccountMenuItems } from "./components/AccountMenuItems";
import type { SwitchSignInTarget } from "./components/AddAccountDialog";
import { AddAccountDialog } from "./components/AddAccountDialog";
import { RemoveAccountDialog } from "./components/RemoveAccountDialog";
Expand Down Expand Up @@ -243,16 +243,11 @@ function AccountCard({
</Button>
</DropdownMenuTrigger>
<DropdownMenuContent align="end">
{onSwitchSignIn && (
<DropdownMenuItem onClick={onSwitchSignIn}>
<Trans>Switch sign-in…</Trans>
</DropdownMenuItem>
)}
{onRemove && (
<DropdownMenuItem variant="destructive" onClick={onRemove}>
<Trans>Remove…</Trans>
</DropdownMenuItem>
)}
<AccountMenuItems
onSwitchSignIn={onSwitchSignIn}
onRemove={onRemove}
createdByParadev={account.createdByParadev === true}
/>
</DropdownMenuContent>
</DropdownMenu>
)}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import { afterEach, describe, expect, mock, test } from "bun:test";

(
globalThis as { IS_REACT_ACT_ENVIRONMENT?: boolean }
).IS_REACT_ACT_ENVIRONMENT = true;

const { act, cleanup, fireEvent, render, within } = await import(
"@testing-library/react"
);
const { DropdownMenu, DropdownMenuContent } = await import(
"@superset/ui/dropdown-menu"
);
const { AccountMenuItems } = await import("./AccountMenuItems");

afterEach(cleanup);

async function menu({
createdByParadev,
removable = true,
}: {
createdByParadev: boolean;
removable?: boolean;
}) {
const onRemove = mock(() => {});
let view!: ReturnType<typeof render>;
await act(async () => {
view = render(
<DropdownMenu open>
<DropdownMenuContent>
<AccountMenuItems
onSwitchSignIn={() => {}}
onRemove={removable ? onRemove : null}
createdByParadev={createdByParadev}
/>
</DropdownMenuContent>
</DropdownMenu>,
);
});
const ui = within(view.baseElement as HTMLElement);
const removeItem = () =>
ui.queryByText("Remove…")?.closest("[role=menuitem]") ?? null;
return { ui, onRemove, removeItem };
}

describe("a profile Paradev created", () => {
test("can be removed", async () => {
const { onRemove, removeItem } = await menu({ createdByParadev: true });
const item = removeItem();
expect(item?.getAttribute("data-disabled")).toBeNull();
await act(async () => {
fireEvent.click(item as Element);
});
expect(onRemove).toHaveBeenCalledTimes(1);
});
});

// A profile from an earlier build, a dir the user made, or a host too old
// to say: Remove would delete nothing, so the menu says why up front.
describe("a profile Paradev did not create", () => {
test("lists Remove as unavailable, with the reason", async () => {
const { ui, removeItem } = await menu({ createdByParadev: false });
expect(removeItem()?.getAttribute("data-disabled")).not.toBeNull();
expect(
ui.getByText("Paradev only removes profiles it created."),
).toBeDefined();
});
});

describe("the system-default login", () => {
test("has no Remove", async () => {
const { ui, removeItem } = await menu({
createdByParadev: false,
removable: false,
});
expect(removeItem()).toBeNull();
expect(ui.getByText("Switch sign-in…")).toBeDefined();
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { Trans } from "@lingui/react/macro";
import { DropdownMenuItem } from "@superset/ui/dropdown-menu";

interface AccountMenuItemsProps {
onSwitchSignIn: (() => void) | null;
/** Null for the system-default login, which is never removable. */
onRemove: (() => void) | null;
/** Whether the host reports that Paradev created the profile dir. Remove
* deletes no other dir. */
createdByParadev: boolean;
}

/** The items of an account card's "…" menu. */
export function AccountMenuItems({
onSwitchSignIn,
onRemove,
createdByParadev,
}: AccountMenuItemsProps) {
return (
<>
{onSwitchSignIn && (
<DropdownMenuItem onClick={onSwitchSignIn}>
<Trans>Switch sign-in…</Trans>
</DropdownMenuItem>
)}
{onRemove &&
(createdByParadev ? (
<DropdownMenuItem variant="destructive" onClick={onRemove}>
<Trans>Remove…</Trans>
</DropdownMenuItem>
) : (
<DropdownMenuItem disabled className="flex-col items-start gap-0.5">
<span>
<Trans>Remove…</Trans>
</span>
<span className="text-xs text-muted-foreground">
<Trans>Paradev only removes profiles it created.</Trans>
</span>
</DropdownMenuItem>
))}
</>
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { AccountMenuItems } from "./AccountMenuItems";
Loading
Loading