import { describe, it, expect, vi, beforeEach } from "vitest"; const portalCreateMock = vi.fn(); vi.mock("stripe", () => ({ default: vi.fn(() => ({ billingPortal: { sessions: { create: portalCreateMock, }, }, })), })); import { createBillingPortalSession } from "../stripe"; describe("createBillingPortalSession", () => { beforeEach(() => { portalCreateMock.mockReset(); }); it("retourne l'URL de la billing portal session", async () => { portalCreateMock.mockResolvedValue({ url: "https://billing.stripe.com/p/session/abc123", }); const result = await createBillingPortalSession({ customerId: "cus_abc", returnUrl: "https://expria.app/dashboard", }); expect(result.url).toBe("https://billing.stripe.com/p/session/abc123"); expect(portalCreateMock).toHaveBeenCalledWith({ customer: "cus_abc", return_url: "https://expria.app/dashboard", }); }); it("throw si customerId vide", async () => { await expect( createBillingPortalSession({ customerId: "", returnUrl: "https://expria.app/dashboard", }), ).rejects.toThrow("customerId requis"); expect(portalCreateMock).not.toHaveBeenCalled(); }); it("throw si returnUrl vide", async () => { await expect( createBillingPortalSession({ customerId: "cus_abc", returnUrl: "", }), ).rejects.toThrow("returnUrl requis"); expect(portalCreateMock).not.toHaveBeenCalled(); }); it("throw si Stripe ne retourne pas d'URL", async () => { portalCreateMock.mockResolvedValue({ url: null }); await expect( createBillingPortalSession({ customerId: "cus_abc", returnUrl: "https://expria.app/dashboard", }), ).rejects.toThrow("URL de billing portal"); }); });