Simpl-Resultat/src/components/shared/ErrorPage.wiring.test.ts
le king fu 2362117cb5 test: pin the UTM label of each product page button (#525)
purchaseUrl.test.ts: per language, new URL(...) keeps exactly the origin and
the path of the contract; the query holds the four keys and no other, in
their order; utm_content is the label for each of the four; the exact address
is pinned; the language fallbacks stay. A free string, or no label, does not
compile (@ts-expect-error, checked by tsc in npm run build).

UpsellCtas.render.test.tsx: both buttons open the URL with its query
(update-locked for PurchaseButton, update-locked-download for
DownloadLatestButton), PurchaseButton opens the label its caller gives, and
UpsellGate passes locked-module. PurchaseButton takes only its two labels and
DownloadLatestButton none (tsc).

New wiring tests under the shared hook harness: ErrorPage opens the buttons by
a click on the update check (invoke mocked, check_entitlement false), then
renders again; UpdateCard in the notEntitled state; LicenseCard's body, read
from the element the card returns, opens license-card.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-02 21:57:24 -05:00

78 lines
3.6 KiB
TypeScript

// ErrorPage — the product page buttons of the free edition and their label (#525).
//
// A static render never shows them: they appear once the update check, run by a
// click, learns that automatic updates are locked (#346). React's hooks come from
// the shared harness (src/test/hookHarness.ts, #409): the page 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 the update check through the props
// of the rendered tree, awaits it, then renders again to read what it opened (the
// harness does not re-render after a setter). No slot is preset, so the test does
// not depend on the order of the page's hooks (docs/architecture.md, « Limites
// connues »). PurchaseButton and DownloadLatestButton stay elements, never called:
// what they open with their label is proved by UpsellCtas.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 tauri = vi.hoisted(() => ({
/** `check_entitlement` answers false: the free edition. */
invoke: vi.fn(async (_command: string, _args?: unknown) => false as unknown),
check: vi.fn(async () => null),
}));
vi.mock("@tauri-apps/api/core", () => ({ invoke: tauri.invoke }));
vi.mock("@tauri-apps/plugin-updater", () => ({ check: tauri.check }));
vi.mock("@tauri-apps/plugin-opener", () => ({ openUrl: vi.fn(() => Promise.resolve()) }));
import { hookHarness } from "../../test/hookHarness";
import { find, texts } from "../../test/elementTree";
import ErrorPage from "./ErrorPage";
import PurchaseButton from "./PurchaseButton";
import DownloadLatestButton from "./DownloadLatestButton";
const harness = hookHarness();
/** One render of the mounted page. */
const render = (): ReactNode => harness.render(() => ErrorPage({}));
/** The update check: the button labelled « Check for updates ». */
function updateCheck(tree: ReactNode) {
const buttons = find(tree, (el) => el.type === "button" && texts(el).includes("t(error.checkUpdate)"));
expect(buttons).toHaveLength(1);
return buttons[0];
}
const purchases = (tree: ReactNode) => find(tree, (el) => el.type === PurchaseButton);
const downloads = (tree: ReactNode) => find(tree, (el) => el.type === DownloadLatestButton);
beforeEach(() => {
harness.clear();
tauri.invoke.mockClear();
tauri.check.mockClear();
});
describe("ErrorPage — updates locked in the free edition (#346, #525)", () => {
it("shows no product page button before the update check", () => {
const tree = render();
expect(purchases(tree)).toHaveLength(0);
expect(downloads(tree)).toHaveLength(0);
});
it("after the check, offers Get Base labelled update-locked and Download the latest version", async () => {
await (updateCheck(render()).props.onClick as () => Promise<void>)();
expect(tauri.invoke).toHaveBeenCalledWith("check_entitlement", { feature: "auto-update" });
// The free edition makes no update check.
expect(tauri.check).not.toHaveBeenCalled();
const opened = render();
expect(texts(opened)).toContain("t(error.updateNotEntitled)");
const purchase = purchases(opened);
expect(purchase).toHaveLength(1);
expect(purchase[0].props.link).toBe("update-locked");
const download = downloads(opened);
expect(download).toHaveLength(1);
// The download button has a label of its own, fixed inside it.
expect(download[0].props).not.toHaveProperty("link");
});
});