77 lines
2.4 KiB
TypeScript
77 lines
2.4 KiB
TypeScript
// @vitest-environment node
|
|
import { describe, expect, it, vi } from "vitest";
|
|
import type { ActionFunctionArgs, LoaderFunctionArgs } from "react-router";
|
|
|
|
import { action, loader, subscriptionSettingsPath } from "./subscribe.$tenant";
|
|
|
|
vi.mock("~/utils/session.server", () => ({
|
|
requireUserId: vi.fn(async () => "user_1"),
|
|
}));
|
|
vi.mock("~/utils/services/.server/urlService", () => ({
|
|
getTenantIdFromUrl: vi.fn(async () => "tenant_1"),
|
|
}));
|
|
|
|
const params = { tenant: "acme" };
|
|
|
|
async function expectRedirect(response: Response, location: string) {
|
|
expect(response.status).toBe(302);
|
|
expect(response.headers.get("Location")).toBe(location);
|
|
}
|
|
|
|
async function expectThrownRedirect(promise: Promise<unknown>, location: string) {
|
|
try {
|
|
await promise;
|
|
throw new Error("Expected redirect response");
|
|
} catch (error) {
|
|
await expectRedirect(error as Response, location);
|
|
}
|
|
}
|
|
|
|
describe("subscribe.$tenant legacy checkout route", () => {
|
|
it("builds the tenant subscription settings path without legacy app prefixes", () => {
|
|
const target = subscriptionSettingsPath("acme");
|
|
|
|
expect(target).toBe("/acme/settings/billing");
|
|
expect(target).not.toContain("/app/acme");
|
|
});
|
|
|
|
it("redirects GET requests to the read-only subscription overview", async () => {
|
|
await expectThrownRedirect(
|
|
loader({
|
|
request: new Request("https://app.example.test/subscribe/acme"),
|
|
params,
|
|
context: {},
|
|
unstable_pattern: "subscribe.$tenant",
|
|
} as LoaderFunctionArgs),
|
|
"/acme/settings/billing"
|
|
);
|
|
});
|
|
|
|
it("preserves the subscription-required context as a read-only overview hint", async () => {
|
|
await expectThrownRedirect(
|
|
loader({
|
|
request: new Request("https://app.example.test/subscribe/acme?error=subscription_required"),
|
|
params,
|
|
context: {},
|
|
unstable_pattern: "subscribe.$tenant",
|
|
} as LoaderFunctionArgs),
|
|
"/acme/settings/billing?subscription_required=1"
|
|
);
|
|
});
|
|
|
|
it("redirects POST requests without starting an in-app plan checkout", async () => {
|
|
await expectThrownRedirect(
|
|
action({
|
|
request: new Request("https://app.example.test/subscribe/acme", {
|
|
method: "POST",
|
|
body: new URLSearchParams([["action", "subscribe"]]),
|
|
}),
|
|
params,
|
|
context: {},
|
|
unstable_pattern: "subscribe.$tenant",
|
|
} as ActionFunctionArgs),
|
|
"/acme/settings/billing"
|
|
);
|
|
});
|
|
});
|