Simpl-Resultat/src/components/shared/UpsellDialog.render.test.tsx
le king fu c38c0c3a36 feat(upsell): close UpsellDialog on Escape (#451)
A keydown listener on the document, added once the dialog is mounted and
removed when it goes away, closes it on Escape, as TransactionDetailModal
does; it follows the latest onClose, since hosts pass an inline arrow.
UpsellDialog.render.test.tsx moves onto the shared hook harness (a
direct call of the component would now hit an Invalid hook call) and the
shared find of src/test/elementTree.ts, and pins the listener: added at
commit and not during the render, Escape closes once, any other key does
not, the same function is removed at unmount, and a new onClose replaces
the old listener.

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

135 lines
5.5 KiB
TypeScript

// UpsellDialog — the locked screen in a modal (#369), closed on Escape (#451).
// No jsdom: react-dom/server renders the markup in vitest's node environment.
// React's hooks come from the shared harness (src/test/hookHarness.ts, #409):
// the dialog runs as a plain function inside `harness.render`, which exposes
// the props it hands to UpsellGate and to its close button, and `commit()`
// runs its Escape effect against a stubbed `document` (the node environment
// has none), whose listener the test then calls with a key.
import { describe, it, expect, vi, beforeEach, afterEach } from "vitest";
import { renderToStaticMarkup } from "react-dom/server";
import { MemoryRouter } from "react-router-dom";
vi.mock("react", async (orig) => (await import("../../test/hookHarness")).fakeReact(await orig()));
vi.mock("@tauri-apps/plugin-opener", () => ({ openUrl: vi.fn(() => Promise.resolve()) }));
vi.mock("react-i18next", async () => (await import("../../test/i18nMock")).i18nMock());
// The locked screen reads one thing from the license context: a revoked key (#365).
vi.mock("../../contexts/LicenseContext", () => ({ useLicenseContext: () => ({ revoked: false }) }));
import { hookHarness } from "../../test/hookHarness";
import { find } from "../../test/elementTree";
import UpsellDialog from "./UpsellDialog";
import UpsellGate from "./UpsellGate";
const harness = hookHarness();
/** One render of the mounted dialog. */
const render = (onClose: () => void, feature: "adjustments" | "balance" = "adjustments") =>
harness.render(() => UpsellDialog({ feature, onClose }));
beforeEach(() => harness.clear());
describe("UpsellDialog (#369)", () => {
it("shows the locked screen of the feature in a named modal dialog", () => {
const html = renderToStaticMarkup(
<MemoryRouter>
<UpsellDialog feature="adjustments" onClose={vi.fn()} />
</MemoryRouter>,
);
expect(html).toContain('role="dialog"');
expect(html).toContain('aria-modal="true"');
expect(html).toContain('aria-label="t(upsell.title[tier=t(license.editions.base)])"');
expect(html).toContain("t(upsell.features.adjustments)");
expect(html).toContain("t(upsell.ctaGet[tier=t(license.editions.base)])");
expect(html).toContain("t(upsell.ctaHaveKey)");
expect(html).toContain('aria-label="t(common.close)"');
});
it("closes on its X button and after 'I already have a key'", () => {
const onClose = vi.fn();
const tree = render(onClose);
const gates = find(tree, (el) => el.type === UpsellGate);
expect(gates).toHaveLength(1);
expect(gates[0].props).toMatchObject({ feature: "adjustments", requiredTier: "base" });
expect(gates[0].props.onNavigate).toBe(onClose);
const close = find(tree, (el) => el.type === "button" && el.props["aria-label"] === "t(common.close)");
expect(close).toHaveLength(1);
(close[0].props.onClick as () => void)();
expect(onClose).toHaveBeenCalledTimes(1);
});
it("asks for the tier the feature needs, not always Base", () => {
const tree = render(vi.fn(), "balance");
expect(find(tree, (el) => el.type === UpsellGate)[0].props.requiredTier).toBe("premium");
});
});
describe("UpsellDialog — Escape (#451)", () => {
type KeyListener = (e: Pick<KeyboardEvent, "key">) => void;
const doc = {
addEventListener: vi.fn<(type: string, listener: KeyListener) => void>(),
removeEventListener: vi.fn<(type: string, listener: KeyListener) => void>(),
};
beforeEach(() => {
doc.addEventListener.mockReset();
doc.removeEventListener.mockReset();
vi.stubGlobal("document", doc);
});
afterEach(() => {
vi.unstubAllGlobals();
});
/** Mounts the dialog (one render, then its effects) and returns the one listener it added. */
function mount(onClose: () => void): KeyListener {
render(onClose);
harness.commit();
expect(doc.addEventListener).toHaveBeenCalledTimes(1);
const [type, listener] = doc.addEventListener.mock.calls[0];
expect(type).toBe("keydown");
return listener;
}
it("listens to keydown on the document once mounted, not during the render", () => {
render(vi.fn());
expect(doc.addEventListener).not.toHaveBeenCalled();
harness.commit();
expect(doc.addEventListener).toHaveBeenCalledWith("keydown", expect.any(Function));
});
it("Escape closes it, once", () => {
const onClose = vi.fn();
mount(onClose)({ key: "Escape" });
expect(onClose).toHaveBeenCalledTimes(1);
});
it("any other key leaves it open", () => {
const onClose = vi.fn();
const listener = mount(onClose);
for (const key of ["Enter", " ", "Tab", "a"]) listener({ key });
expect(onClose).not.toHaveBeenCalled();
});
it("removes that very listener when it goes away", () => {
const listener = mount(vi.fn());
expect(doc.removeEventListener).not.toHaveBeenCalled();
harness.unmount();
expect(doc.removeEventListener).toHaveBeenCalledTimes(1);
expect(doc.removeEventListener).toHaveBeenCalledWith("keydown", listener);
});
it("follows the host's latest onClose: the old listener goes, the new one closes", () => {
const first = vi.fn();
const second = vi.fn();
const old = mount(first);
render(second);
harness.commit();
expect(doc.removeEventListener).toHaveBeenCalledWith("keydown", old);
expect(doc.addEventListener).toHaveBeenCalledTimes(2);
const [, latest] = doc.addEventListener.mock.calls[1];
expect(latest).not.toBe(old);
latest({ key: "Escape" });
expect(second).toHaveBeenCalledTimes(1);
expect(first).not.toHaveBeenCalled();
});
});