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>
This commit is contained in:
parent
483f886ac0
commit
c38c0c3a36
2 changed files with 106 additions and 19 deletions
|
|
@ -1,28 +1,32 @@
|
|||
// UpsellDialog — the locked screen in a modal (#369). No jsdom: react-dom/server
|
||||
// renders the markup in vitest's node environment, and calling the component as
|
||||
// a function (UpsellCtas.render.test.tsx pattern) exposes the props it hands
|
||||
// to UpsellGate and to its close button.
|
||||
import { describe, it, expect, vi } from "vitest";
|
||||
import { isValidElement, type ReactElement, type ReactNode } from "react";
|
||||
// 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";
|
||||
|
||||
type Props = Record<string, unknown> & { children?: ReactNode };
|
||||
const harness = hookHarness();
|
||||
|
||||
function find(node: ReactNode, pred: (el: ReactElement<Props>) => boolean): ReactElement<Props>[] {
|
||||
if (Array.isArray(node)) return node.flatMap((n) => find(n, pred));
|
||||
if (!isValidElement(node)) return [];
|
||||
const el = node as ReactElement<Props>;
|
||||
return [...(pred(el) ? [el] : []), ...find(el.props.children, pred)];
|
||||
}
|
||||
/** 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", () => {
|
||||
|
|
@ -42,7 +46,7 @@ describe("UpsellDialog (#369)", () => {
|
|||
|
||||
it("closes on its X button and after 'I already have a key'", () => {
|
||||
const onClose = vi.fn();
|
||||
const tree = UpsellDialog({ feature: "adjustments", onClose });
|
||||
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" });
|
||||
|
|
@ -54,7 +58,78 @@ describe("UpsellDialog (#369)", () => {
|
|||
});
|
||||
|
||||
it("asks for the tier the feature needs, not always Base", () => {
|
||||
const tree = UpsellDialog({ feature: "balance", onClose: vi.fn() });
|
||||
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();
|
||||
});
|
||||
});
|
||||
|
|
|
|||
|
|
@ -1,3 +1,4 @@
|
|||
import { useEffect } from "react";
|
||||
import { useTranslation } from "react-i18next";
|
||||
import { X } from "lucide-react";
|
||||
import { requiredTierFor, type FeatureKey } from "../../shared/entitlements";
|
||||
|
|
@ -9,19 +10,30 @@ interface UpsellDialogProps {
|
|||
}
|
||||
|
||||
/**
|
||||
* The locked screen in a modal, for a gated action inside a free page (#369:
|
||||
* the Répartition button of the Transactions page). Same markup as the upsell
|
||||
* dialog of ProfileSwitcher, which has not moved onto this component yet.
|
||||
* The locked screen in a modal, for a gated action outside a locked route: the
|
||||
* Répartition button of the Transactions page (#369) and a locked profile in
|
||||
* ProfileSwitcher (#451) both open it.
|
||||
*
|
||||
* "I already have a key" navigates to the license card and closes the dialog
|
||||
* through UpsellGate's `onNavigate`, so it does not linger over the destination
|
||||
* page; the purchase button leaves it open (the product page opens in the
|
||||
* browser).
|
||||
* browser). The X button and Escape close it too; a click on the backdrop does
|
||||
* not.
|
||||
*/
|
||||
export default function UpsellDialog({ feature, onClose }: UpsellDialogProps) {
|
||||
const { t } = useTranslation();
|
||||
const requiredTier = requiredTierFor(feature);
|
||||
|
||||
// Escape closes the dialog, as in TransactionDetailModal. The listener
|
||||
// follows the latest onClose: hosts pass an inline arrow.
|
||||
useEffect(() => {
|
||||
function handleEscape(e: KeyboardEvent) {
|
||||
if (e.key === "Escape") onClose();
|
||||
}
|
||||
document.addEventListener("keydown", handleEscape);
|
||||
return () => document.removeEventListener("keydown", handleEscape);
|
||||
}, [onClose]);
|
||||
|
||||
return (
|
||||
<div className="fixed inset-0 z-50 flex items-center justify-center bg-black/50">
|
||||
<div
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue