refactor(profile): open the ProfileSwitcher upsell in UpsellDialog (#451)
The switcher's own copy of the upsell window (same layout, same feature, same required tier, onNavigate closing it) becomes <UpsellDialog feature="multi-profile" />, which also gives it the dialog role, aria-modal, an accessible name, a named close button and Escape. The X and requiredTierFor imports go; UpsellGate's comment names UpsellDialog as the modal host. ProfileSwitcher.wiring.test.ts, on the shared hook harness, opens the menu and clicks the locked profile through the rendered tree (no slot preset): one UpsellDialog element for multi-profile, no UpsellGate of its own, the menu closed, and onClose closes it; in Base the same click switches profile and opens nothing. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
c38c0c3a36
commit
3ec1f48b6f
3 changed files with 117 additions and 29 deletions
|
|
@ -1,11 +1,10 @@
|
|||
import { useState, useRef, useEffect } from "react";
|
||||
import { useTranslation } from "react-i18next";
|
||||
import { ChevronDown, Lock, Settings, X } from "lucide-react";
|
||||
import { ChevronDown, Lock, Settings } from "lucide-react";
|
||||
import { useProfile } from "../../contexts/ProfileContext";
|
||||
import { useEntitlement } from "../../hooks/useEntitlement";
|
||||
import { requiredTierFor } from "../../shared/entitlements";
|
||||
import { isProfileSwitchLocked } from "../../shared/profileGate";
|
||||
import UpsellGate from "../shared/UpsellGate";
|
||||
import UpsellDialog from "../shared/UpsellDialog";
|
||||
import PinDialog from "./PinDialog";
|
||||
import ProfileFormModal from "./ProfileFormModal";
|
||||
import type { Profile } from "../../services/profileService";
|
||||
|
|
@ -139,27 +138,7 @@ export default function ProfileSwitcher() {
|
|||
<ProfileFormModal onClose={() => setShowManage(false)} />
|
||||
)}
|
||||
|
||||
{showUpsell && (
|
||||
<div className="fixed inset-0 z-50 flex items-center justify-center bg-black/50">
|
||||
<div className="bg-[var(--card)] rounded-xl shadow-xl w-full max-w-md border border-[var(--border)]">
|
||||
<div className="flex justify-end p-3 pb-0">
|
||||
<button
|
||||
onClick={() => setShowUpsell(false)}
|
||||
className="text-[var(--muted-foreground)] hover:text-[var(--foreground)]"
|
||||
>
|
||||
<X size={18} />
|
||||
</button>
|
||||
</div>
|
||||
<div className="px-4 pb-8">
|
||||
<UpsellGate
|
||||
feature="multi-profile"
|
||||
requiredTier={requiredTierFor("multi-profile")}
|
||||
onNavigate={() => setShowUpsell(false)}
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
{showUpsell && <UpsellDialog feature="multi-profile" onClose={() => setShowUpsell(false)} />}
|
||||
</>
|
||||
);
|
||||
}
|
||||
|
|
|
|||
109
src/components/profile/ProfileSwitcher.wiring.test.ts
Normal file
109
src/components/profile/ProfileSwitcher.wiring.test.ts
Normal file
|
|
@ -0,0 +1,109 @@
|
|||
// ProfileSwitcher — a locked profile opens the shared upsell dialog (#451).
|
||||
//
|
||||
// A static render shows neither the menu nor a window: they open on a click,
|
||||
// through the switcher's own state. React's hooks come from the shared harness
|
||||
// (src/test/hookHarness.ts, #409): the switcher runs as a plain function, its
|
||||
// state survives from one call to the next as between two renders of a mounted
|
||||
// component, and the test clicks through the props of the rendered tree (the
|
||||
// toggle, then a profile of the menu), then renders again to read what opened.
|
||||
// No slot is preset, so the test does not depend on the order of the
|
||||
// switcher's hooks. No effect is run: `commit()` would add the switcher's
|
||||
// mousedown listener to a `document` the node environment does not have.
|
||||
// UpsellDialog stays an element, never called: what it renders (UpsellGate,
|
||||
// Escape) is proved by UpsellDialog.render.test.tsx.
|
||||
import { describe, it, expect, vi, beforeEach } from "vitest";
|
||||
import type { ReactNode } from "react";
|
||||
|
||||
vi.mock("react", async (orig) => (await import("../../test/hookHarness")).fakeReact(await orig()));
|
||||
vi.mock("react-i18next", async () => (await import("../../test/i18nMock")).i18nMock());
|
||||
|
||||
const ctx = vi.hoisted(() => ({
|
||||
profiles: [
|
||||
{ id: "default", name: "Principal", color: "#4A90A4", pin_hash: null, db_filename: "simpl_resultat.db", created_at: "" },
|
||||
{ id: "second", name: "Second", color: "#C17767", pin_hash: null, db_filename: "profile_second.db", created_at: "" },
|
||||
],
|
||||
/** What `useEntitlement("multi-profile")` returns: Free by default. */
|
||||
gate: { allowed: false, ready: true },
|
||||
switchProfile: vi.fn(),
|
||||
}));
|
||||
vi.mock("../../hooks/useEntitlement", () => ({ useEntitlement: () => ctx.gate }));
|
||||
vi.mock("../../contexts/ProfileContext", () => ({
|
||||
useProfile: () => ({
|
||||
profiles: ctx.profiles,
|
||||
activeProfile: ctx.profiles[0],
|
||||
switchProfile: ctx.switchProfile,
|
||||
updateProfile: vi.fn(),
|
||||
}),
|
||||
}));
|
||||
|
||||
import { ChevronDown } from "lucide-react";
|
||||
import { hookHarness } from "../../test/hookHarness";
|
||||
import { find, holding } from "../../test/elementTree";
|
||||
import ProfileSwitcher from "./ProfileSwitcher";
|
||||
import UpsellDialog from "../shared/UpsellDialog";
|
||||
import UpsellGate from "../shared/UpsellGate";
|
||||
|
||||
const harness = hookHarness();
|
||||
|
||||
/** One render of the mounted switcher. */
|
||||
const render = (): ReactNode => harness.render(() => ProfileSwitcher());
|
||||
|
||||
/** Clicks the toggle, the button that holds the chevron: the menu opens at the next render. */
|
||||
function openMenu() {
|
||||
const toggles = holding(render(), ChevronDown);
|
||||
expect(toggles).toHaveLength(1);
|
||||
(toggles[0].props.onClick as () => void)();
|
||||
}
|
||||
|
||||
/** The menu's button for a profile (keyed by its id). */
|
||||
const profileButtons = (tree: ReactNode, id: string) =>
|
||||
find(tree, (el) => el.type === "button" && el.key === id);
|
||||
|
||||
const dialogs = (tree: ReactNode) => find(tree, (el) => el.type === UpsellDialog);
|
||||
const gates = (tree: ReactNode) => find(tree, (el) => el.type === UpsellGate);
|
||||
|
||||
beforeEach(() => {
|
||||
harness.clear();
|
||||
ctx.gate = { allowed: false, ready: true };
|
||||
ctx.switchProfile.mockReset();
|
||||
});
|
||||
|
||||
describe("ProfileSwitcher — the upsell of a locked profile (#451)", () => {
|
||||
it("opens nothing before a click", () => {
|
||||
const tree = render();
|
||||
expect(dialogs(tree)).toHaveLength(0);
|
||||
expect(gates(tree)).toHaveLength(0);
|
||||
});
|
||||
|
||||
it("Free: a locked profile opens the shared dialog for multi-profile, not a locked screen of its own", () => {
|
||||
openMenu();
|
||||
const menu = profileButtons(render(), "second");
|
||||
expect(menu).toHaveLength(1);
|
||||
expect(menu[0].props.title).toBe("t(nav.locked)");
|
||||
(menu[0].props.onClick as () => void)();
|
||||
|
||||
const opened = render();
|
||||
const shown = dialogs(opened);
|
||||
expect(shown).toHaveLength(1);
|
||||
expect(shown[0].props.feature).toBe("multi-profile");
|
||||
expect(gates(opened)).toHaveLength(0);
|
||||
expect(ctx.switchProfile).not.toHaveBeenCalled();
|
||||
// The menu closed before the dialog opened.
|
||||
expect(profileButtons(opened, "second")).toHaveLength(0);
|
||||
|
||||
// The dialog's close hands control back to the switcher.
|
||||
(shown[0].props.onClose as () => void)();
|
||||
expect(dialogs(render())).toHaveLength(0);
|
||||
});
|
||||
|
||||
it("Base: the same click switches profile, no dialog", () => {
|
||||
ctx.gate = { allowed: true, ready: true };
|
||||
openMenu();
|
||||
const menu = profileButtons(render(), "second");
|
||||
expect(menu[0].props.title).toBeUndefined();
|
||||
(menu[0].props.onClick as () => void)();
|
||||
|
||||
expect(ctx.switchProfile).toHaveBeenCalledWith("second");
|
||||
expect(dialogs(render())).toHaveLength(0);
|
||||
});
|
||||
});
|
||||
|
|
@ -12,11 +12,11 @@ interface UpsellGateProps {
|
|||
requiredTier: Edition;
|
||||
/**
|
||||
* Called right after "I already have a key" navigates to the license card. Lets
|
||||
* modal hosts (the ProfileSwitcher upsell dialog, ProfileFormModal) close
|
||||
* themselves — the Sidebar stays mounted across navigations, so an uncontrolled
|
||||
* modal would linger on top of the destination page. The purchase button never
|
||||
* calls it: the product page opens in the browser and the host stays open.
|
||||
* Optional: route-level usage ignores it.
|
||||
* modal hosts (UpsellDialog, ProfileFormModal) close themselves — both open from
|
||||
* the Sidebar's ProfileSwitcher, and the Sidebar stays mounted across
|
||||
* navigations, so an uncontrolled modal would linger on top of the destination
|
||||
* page. The purchase button never calls it: the product page opens in the
|
||||
* browser and the host stays open. Optional: route-level usage ignores it.
|
||||
*/
|
||||
onNavigate?: () => void;
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue