import { render, screen } from "@testing-library/react";
import { MemoryRouter, useLocation } from "react-router-dom";
import { beforeEach, describe, expect, it, vi } from "vitest";
import App from "@/App";
import { GetStartedRouteGate } from "@/features/get-started/GetStartedRouteGate";
const { mockUseSession } = vi.hoisted(() => ({
mockUseSession: vi.fn(),
}));
vi.mock("@/lib/auth-client", () => ({
authClient: {
useSession: mockUseSession,
},
}));
vi.mock("@/features/auth/GuestApp", () => ({
GuestApp: () =>
Guest App
,
}));
vi.mock("@/features/auth/AuthenticatedApp", () => ({
AuthenticatedApp: ({
rootRedirectTarget,
}: {
rootRedirectTarget: string | null;
}) => (
Authenticated App
Root redirect: {rootRedirectTarget ?? "none"}
),
}));
vi.mock("@/features/auth/ResetPasswordApp", () => ({
ResetPasswordApp: () => Reset Password App
,
}));
describe("App routing", () => {
beforeEach(() => {
mockUseSession.mockReset();
mockUseSession.mockReturnValue({ data: null, isPending: false });
});
it("renders the desktop-only overlay on the guest homepage", () => {
render(
,
);
expect(screen.getByText("Guest App")).toBeInTheDocument();
expect(screen.getByTestId("desktop-only-overlay")).toHaveClass(
"hidden",
"max-[499px]:flex",
);
});
it.each([
"/?user_code=ABCD-1234",
"/device?user_code=ABCD-1234",
])("redirects the legacy device entry %s into welcome", async (entry) => {
render(
,
);
expect(
await screen.findByText("/welcome?user_code=ABCD-1234"),
).toBeInTheDocument();
});
it("hands signed-in login visits to authenticated routing", () => {
mockUseSession.mockReturnValue({
data: { user: { id: "user-1" } },
isPending: false,
});
render(
,
);
expect(screen.getByText("Authenticated App")).toBeInTheDocument();
});
});
function CurrentLocation() {
const { pathname, search } = useLocation();
return (
{pathname}
{search}
);
}
it.each([
"/get-started",
"/dashboard/get-started",
])("preserves CLI pairing parameters when redirecting %s to welcome", async (pathname) => {
render(
,
);
expect(
await screen.findByText("/welcome?connect=pairing-id&user_code=ABCD-1234"),
).toBeInTheDocument();
});