diff --git a/src/components/profile/ProfileSwitcher.tsx b/src/components/profile/ProfileSwitcher.tsx index 918780e..570101a 100644 --- a/src/components/profile/ProfileSwitcher.tsx +++ b/src/components/profile/ProfileSwitcher.tsx @@ -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() { setShowManage(false)} /> )} - {showUpsell && ( -
-
-
- -
-
- setShowUpsell(false)} - /> -
-
-
- )} + {showUpsell && setShowUpsell(false)} />} ); } diff --git a/src/components/profile/ProfileSwitcher.wiring.test.ts b/src/components/profile/ProfileSwitcher.wiring.test.ts new file mode 100644 index 0000000..2799c39 --- /dev/null +++ b/src/components/profile/ProfileSwitcher.wiring.test.ts @@ -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); + }); +}); diff --git a/src/components/shared/UpsellGate.tsx b/src/components/shared/UpsellGate.tsx index fb3d3df..19f7daf 100644 --- a/src/components/shared/UpsellGate.tsx +++ b/src/components/shared/UpsellGate.tsx @@ -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; }