import { render } from "@testing-library/react";
import { describe, it, expect, beforeEach } from "vitest";
import ReferralCapture from "./ReferralCapture";
import { getStoredRef, clearStoredRef } from "@/app/lib/referral";
beforeEach(() => {
clearStoredRef();
window.history.replaceState({}, "", "/");
});
describe("ReferralCapture", () => {
it("persists ?ref= from the URL on mount and renders nothing", () => {
window.history.replaceState({}, "", "/register?ref=MC-42");
const { container } = render();
expect(getStoredRef()).toBe("MC-42");
expect(container).toBeEmptyDOMElement();
});
it("does nothing when there is no ref in the URL", () => {
render();
expect(getStoredRef()).toBe("");
});
it("does not overwrite a previously stored ref when the URL has none", () => {
window.history.replaceState({}, "", "/register?ref=FIRST");
render();
window.history.replaceState({}, "", "/login");
render();
expect(getStoredRef()).toBe("FIRST");
});
});