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
719588ffe2
commit
1a0070b32e
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
|
// UpsellDialog — the locked screen in a modal (#369), closed on Escape (#451).
|
||||||
// renders the markup in vitest's node environment, and calling the component as
|
// No jsdom: react-dom/server renders the markup in vitest's node environment.
|
||||||
// a function (UpsellCtas.render.test.tsx pattern) exposes the props it hands
|
// React's hooks come from the shared harness (src/test/hookHarness.ts, #409):
|
||||||
// to UpsellGate and to its close button.
|
// the dialog runs as a plain function inside `harness.render`, which exposes
|
||||||
import { describe, it, expect, vi } from "vitest";
|
// the props it hands to UpsellGate and to its close button, and `commit()`
|
||||||
import { isValidElement, type ReactElement, type ReactNode } from "react";
|
// 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 { renderToStaticMarkup } from "react-dom/server";
|
||||||
import { MemoryRouter } from "react-router-dom";
|
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("@tauri-apps/plugin-opener", () => ({ openUrl: vi.fn(() => Promise.resolve()) }));
|
||||||
vi.mock("react-i18next", async () => (await import("../../test/i18nMock")).i18nMock());
|
vi.mock("react-i18next", async () => (await import("../../test/i18nMock")).i18nMock());
|
||||||
// The locked screen reads one thing from the license context: a revoked key (#365).
|
// The locked screen reads one thing from the license context: a revoked key (#365).
|
||||||
vi.mock("../../contexts/LicenseContext", () => ({ useLicenseContext: () => ({ revoked: false }) }));
|
vi.mock("../../contexts/LicenseContext", () => ({ useLicenseContext: () => ({ revoked: false }) }));
|
||||||
|
|
||||||
|
import { hookHarness } from "../../test/hookHarness";
|
||||||
|
import { find } from "../../test/elementTree";
|
||||||
import UpsellDialog from "./UpsellDialog";
|
import UpsellDialog from "./UpsellDialog";
|
||||||
import UpsellGate from "./UpsellGate";
|
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>[] {
|
/** One render of the mounted dialog. */
|
||||||
if (Array.isArray(node)) return node.flatMap((n) => find(n, pred));
|
const render = (onClose: () => void, feature: "adjustments" | "balance" = "adjustments") =>
|
||||||
if (!isValidElement(node)) return [];
|
harness.render(() => UpsellDialog({ feature, onClose }));
|
||||||
const el = node as ReactElement<Props>;
|
|
||||||
return [...(pred(el) ? [el] : []), ...find(el.props.children, pred)];
|
beforeEach(() => harness.clear());
|
||||||
}
|
|
||||||
|
|
||||||
describe("UpsellDialog (#369)", () => {
|
describe("UpsellDialog (#369)", () => {
|
||||||
it("shows the locked screen of the feature in a named modal dialog", () => {
|
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'", () => {
|
it("closes on its X button and after 'I already have a key'", () => {
|
||||||
const onClose = vi.fn();
|
const onClose = vi.fn();
|
||||||
const tree = UpsellDialog({ feature: "adjustments", onClose });
|
const tree = render(onClose);
|
||||||
const gates = find(tree, (el) => el.type === UpsellGate);
|
const gates = find(tree, (el) => el.type === UpsellGate);
|
||||||
expect(gates).toHaveLength(1);
|
expect(gates).toHaveLength(1);
|
||||||
expect(gates[0].props).toMatchObject({ feature: "adjustments", requiredTier: "base" });
|
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", () => {
|
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");
|
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 { useTranslation } from "react-i18next";
|
||||||
import { X } from "lucide-react";
|
import { X } from "lucide-react";
|
||||||
import { requiredTierFor, type FeatureKey } from "../../shared/entitlements";
|
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 locked screen in a modal, for a gated action outside a locked route: the
|
||||||
* the Répartition button of the Transactions page). Same markup as the upsell
|
* Répartition button of the Transactions page (#369) and a locked profile in
|
||||||
* dialog of ProfileSwitcher, which has not moved onto this component yet.
|
* ProfileSwitcher (#451) both open it.
|
||||||
*
|
*
|
||||||
* "I already have a key" navigates to the license card and closes the dialog
|
* "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
|
* 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
|
* 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) {
|
export default function UpsellDialog({ feature, onClose }: UpsellDialogProps) {
|
||||||
const { t } = useTranslation();
|
const { t } = useTranslation();
|
||||||
const requiredTier = requiredTierFor(feature);
|
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 (
|
return (
|
||||||
<div className="fixed inset-0 z-50 flex items-center justify-center bg-black/50">
|
<div className="fixed inset-0 z-50 flex items-center justify-center bg-black/50">
|
||||||
<div
|
<div
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue