import { describe, expect, it, vi, beforeEach } from "vitest";
import { renderWithProviders as render, screen } from "../utils/test-utils";
import "@testing-library/jest-dom/vitest";
import { ConfirmLinkScreen } from "../../app/(app)/_components/ConfirmLinkScreen";
const { navMock } = vi.hoisted(() => ({
navMock: {
push: vi.fn(),
},
}));
vi.mock("next/navigation", () => ({
useRouter: () => ({
push: navMock.push,
replace: vi.fn(),
prefetch: vi.fn(),
refresh: vi.fn(),
back: vi.fn(),
forward: vi.fn(),
}),
useSearchParams: () => new URLSearchParams(),
usePathname: () => "/login/confirm",
}));
const baseProps = {
title: "Open this login link",
description: "Press continue to log in.",
buttonLabel: "Continue",
action: "/api/auth/magic-link/verify?token=abcdefghijkl",
token: "abcdefghijkl",
backHref: "/",
backLabel: "← Back to home",
missingTitle: "This login link is missing",
missingDescription: "Request a new login link from the login page.",
missingHref: "/login",
missingButtonLabel: "Go to log in",
};
describe("ConfirmLinkScreen", () => {
beforeEach(() => {
navMock.push.mockReset();
});
it("posts the token when Continue is pressed", () => {
const { container } = render();
expect(
screen.getByRole("heading", { name: "Open this login link" }),
).toBeInTheDocument();
const form = container.querySelector("form");
expect(form).toHaveAttribute("method", "POST");
expect(form).toHaveAttribute(
"action",
"/api/auth/magic-link/verify?token=abcdefghijkl",
);
expect(container.querySelector('input[name="token"]')).toHaveValue(
"abcdefghijkl",
);
expect(screen.getByRole("button", { name: "Continue" })).toHaveAttribute(
"type",
"submit",
);
});
it("offers a way back when the token is missing", () => {
render();
expect(
screen.getByRole("heading", { name: "This login link is missing" }),
).toBeInTheDocument();
expect(screen.queryByRole("button", { name: "Continue" })).not.toBeInTheDocument();
expect(screen.getByRole("link", { name: "Go to log in" })).toHaveAttribute(
"href",
"/login",
);
});
});