Simpl-Resultat/src/components/profile/ProfileSwitcher.wiring.test.ts
le king fu 41f3b15f4d test: pin four cases the reviews of the stack left unpinned
Non-blocking suggestions of the independent reviews of PRs #490 to #493,
applied in a follow-up link above #493 (Max's decision of 2026-10-24):

- locales.keys.test.ts: a variable name that differs by its case only is a
  gap (`{{version}}` against `{{Version}}`, review 27037, H8); the header says
  that `{{- name}}` and `{{name}}` render alike with `escapeValue: false`.
- db.test.ts: a `)` glued to a word ends no path
  (`C:\Users\jean\Projet(2024)final\x.db`, review 27073, R10).
- TemplateActions.delete.test.ts: three templates, the middle one deleted, so
  a handler bound to the first or the last fails (review 27088, R11).
- ProfileSwitcher.wiring.test.ts: a locked profile that has a PIN opens the
  upsell, never the PIN entry; in Base it asks for the PIN (review 27084, R5).
- elementTree.ts: an absence is proved with `find`, never with `holding`
  (review 27088, S2).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-24 09:09:36 -04:00

139 lines
5.7 KiB
TypeScript

// 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. A locked profile that has
// a PIN opens the dialog, never the PIN entry: the lock is checked first, or
// the right PIN would switch to a locked profile.
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: "" },
{ id: "third", name: "Troisième", color: "#8E7CC3", pin_hash: "a-pin-hash", db_filename: "profile_third.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 PinDialog from "./PinDialog";
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);
const pins = (tree: ReactNode) => find(tree, (el) => el.type === PinDialog);
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);
});
it("Free: a locked profile that has a PIN opens the dialog, never the PIN entry", () => {
openMenu();
const menu = profileButtons(render(), "third");
expect(menu).toHaveLength(1);
(menu[0].props.onClick as () => void)();
const opened = render();
expect(dialogs(opened)).toHaveLength(1);
expect(pins(opened)).toHaveLength(0);
expect(ctx.switchProfile).not.toHaveBeenCalled();
});
it("Base: a profile that has a PIN asks for it, no dialog", () => {
ctx.gate = { allowed: true, ready: true };
openMenu();
(profileButtons(render(), "third")[0].props.onClick as () => void)();
const opened = render();
const asked = pins(opened);
expect(asked).toHaveLength(1);
expect(asked[0].props.profileName).toBe("Troisième");
expect(dialogs(opened)).toHaveLength(0);
expect(ctx.switchProfile).not.toHaveBeenCalled();
});
});