150 lines
6.6 KiB
TypeScript
150 lines
6.6 KiB
TypeScript
import "@testing-library/jest-dom";
|
|
import { act, cleanup, render, screen } from "@testing-library/react";
|
|
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
|
import { assertNoA11yViolations } from "~/utils/test/a11y";
|
|
|
|
const mocks = vi.hoisted(() => ({
|
|
data: {} as Record<string, unknown>,
|
|
navigate: vi.fn(),
|
|
revalidate: vi.fn(),
|
|
}));
|
|
|
|
vi.mock("react-i18next", () => ({
|
|
useTranslation: () => ({
|
|
t: (key: string) =>
|
|
(
|
|
{
|
|
"billing:productAcquisitionReturn.title": "Checking payment",
|
|
"billing:productAcquisitionReturn.description": "Features are enabled after verification.",
|
|
"billing:productAcquisitionReturn.processing.title": "Confirmation in progress",
|
|
"billing:productAcquisitionReturn.processing.description": "This page refreshes automatically.",
|
|
"billing:productAcquisitionReturn.active.title": "Package is active",
|
|
"billing:productAcquisitionReturn.active.description": "The package is now enabled.",
|
|
"billing:productAcquisitionReturn.refresh": "Check status again",
|
|
"billing:productAcquisitionReturn.restartCheckout": "Start a new payment",
|
|
"billing:productAcquisitionReturn.back": "Back to billing",
|
|
"billing:productAcquisitionReturn.backCustomerPortalDomain": "Back to portal domain",
|
|
"billing:productAcquisitionReturn.status.cancelled.title": "Payment cancelled",
|
|
"billing:productAcquisitionReturn.status.cancelled.description": "Nothing was enabled.",
|
|
"billing:productAcquisitionReturn.status.expired.title": "Payment expired",
|
|
"billing:productAcquisitionReturn.status.expired.description": "Nothing was enabled.",
|
|
"billing:productAcquisitionReturn.status.failed.title": "Payment failed",
|
|
"billing:productAcquisitionReturn.status.failed.description": "Nothing was enabled.",
|
|
"billing:productAcquisitionReturn.status.refunded.title": "Payment refunded",
|
|
"billing:productAcquisitionReturn.status.refunded.description": "The package is no longer active.",
|
|
} as Record<string, string>
|
|
)[key] ?? key,
|
|
}),
|
|
}));
|
|
vi.mock("react-router", async () => {
|
|
const actual = await vi.importActual<typeof import("react-router")>("react-router");
|
|
return {
|
|
...actual,
|
|
useLoaderData: () => mocks.data,
|
|
useNavigate: () => mocks.navigate,
|
|
useRevalidator: () => ({ state: "idle", revalidate: mocks.revalidate }),
|
|
};
|
|
});
|
|
vi.mock("~/locale/i18next.server", () => ({ getTranslations: vi.fn() }));
|
|
vi.mock("~/utils/db/tenants.db.server", () => ({ getTenant: vi.fn() }));
|
|
vi.mock("~/utils/helpers/.server/PermissionsService", () => ({ verifyUserHasPermission: vi.fn() }));
|
|
vi.mock("~/utils/services/.server/urlService", () => ({ getTenantIdFromUrl: vi.fn() }));
|
|
vi.mock("~/utils/session.server", () => ({ getUserInfo: vi.fn() }));
|
|
vi.mock("~/services/billing/product-acquisition.server", () => ({
|
|
completeProductAcquisition: vi.fn(),
|
|
ProductAcquisitionError: class extends Error {},
|
|
}));
|
|
|
|
import ProductAcquisitionReturnRoute from "./subscribe.$tenant.product.$acquisition.success";
|
|
|
|
describe("product acquisition return UI", () => {
|
|
beforeEach(() => {
|
|
vi.clearAllMocks();
|
|
mocks.data = {
|
|
title: "Payment status",
|
|
tenant: { id: "tenant_1", name: "Acme GmbH", slug: "acme" },
|
|
returnTarget: "billing",
|
|
acquisition: { id: "acquisition_1", status: "active", processing: false },
|
|
errorKey: null,
|
|
};
|
|
});
|
|
|
|
afterEach(() => {
|
|
cleanup();
|
|
vi.useRealTimers();
|
|
});
|
|
|
|
it("renders the verified active state and tenant-scoped billing action accessibly", () => {
|
|
const { container } = render(<ProductAcquisitionReturnRoute />);
|
|
|
|
expect(screen.getByRole("heading", { name: "Package is active" })).toBeInTheDocument();
|
|
expect(screen.getByText("The package is now enabled.")).toBeInTheDocument();
|
|
screen.getByRole("button", { name: "Back to billing" }).click();
|
|
expect(mocks.navigate).toHaveBeenCalledWith("/acme/settings/billing");
|
|
assertNoA11yViolations(container);
|
|
});
|
|
|
|
it("keeps a processing payment pollable without claiming activation", () => {
|
|
vi.useFakeTimers();
|
|
mocks.data = {
|
|
...mocks.data,
|
|
acquisition: { id: "acquisition_1", status: "payment_pending", processing: true },
|
|
};
|
|
|
|
const { container } = render(<ProductAcquisitionReturnRoute />);
|
|
|
|
expect(screen.getByRole("heading", { name: "Checking payment" })).toBeInTheDocument();
|
|
expect(screen.getByText("Confirmation in progress")).toBeInTheDocument();
|
|
expect(screen.queryByText("The package is now enabled.")).not.toBeInTheDocument();
|
|
act(() => vi.advanceTimersByTime(2_500));
|
|
expect(mocks.revalidate).toHaveBeenCalledTimes(1);
|
|
screen.getByRole("button", { name: "Check status again" }).click();
|
|
expect(mocks.revalidate).toHaveBeenCalledTimes(2);
|
|
expect(screen.queryByRole("button", { name: "Start a new payment" })).not.toBeInTheDocument();
|
|
assertNoA11yViolations(container);
|
|
});
|
|
|
|
it.each([
|
|
["cancelled", "Payment cancelled"],
|
|
["expired", "Payment expired"],
|
|
["failed", "Payment failed"],
|
|
["refunded", "Payment refunded"],
|
|
] as const)("returns terminal %s payments to billing for a fresh checkout", (status, title) => {
|
|
mocks.data = {
|
|
...mocks.data,
|
|
acquisition: { id: "acquisition_1", status, processing: false },
|
|
};
|
|
|
|
render(<ProductAcquisitionReturnRoute />);
|
|
|
|
expect(screen.getByText(title)).toBeInTheDocument();
|
|
expect(screen.queryByRole("button", { name: "Check status again" })).not.toBeInTheDocument();
|
|
screen.getByRole("button", { name: "Start a new payment" }).click();
|
|
expect(mocks.navigate).toHaveBeenCalledWith("/acme/settings/billing");
|
|
expect(mocks.revalidate).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("returns a terminal portal-domain payment to its originating settings page", () => {
|
|
mocks.data = {
|
|
...mocks.data,
|
|
returnTarget: "customer_portal_domain",
|
|
acquisition: { id: "acquisition_1", status: "failed", processing: false },
|
|
};
|
|
|
|
render(<ProductAcquisitionReturnRoute />);
|
|
|
|
screen.getByRole("button", { name: "Start a new payment" }).click();
|
|
expect(mocks.navigate).toHaveBeenCalledWith("/acme/settings/customer-portal/domain");
|
|
expect(mocks.revalidate).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("returns a customer-portal checkout to the originating domain settings", () => {
|
|
mocks.data = { ...mocks.data, returnTarget: "customer_portal_domain" };
|
|
render(<ProductAcquisitionReturnRoute />);
|
|
|
|
screen.getByRole("button", { name: "Back to portal domain" }).click();
|
|
|
|
expect(mocks.navigate).toHaveBeenCalledWith("/acme/settings/customer-portal/domain");
|
|
});
|
|
});
|