Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -593,3 +593,7 @@ src/ingestion/tools/seed/manifest.json
# run, exactly like .env.compose.test-stand and the generated realm.
.artifacts/
coverage.md

# playwright-cli session output: snapshots, console logs and screenshots from
# driving the UI against a stand.
.playwright-cli/
55 changes: 55 additions & 0 deletions src/frontend/src/routes/index.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// @vitest-environment jsdom
/**
* The preview flag can flip while "/" is mounted, and the route's `beforeLoad`
* guard does not run again for that — only the component can send the reader
* into the portal without a reload.
*/
vi.mock("@tanstack/react-router", async () => {
const { portalRouterMock } = await import("@/test/portal-router");
return {
...portalRouterMock(),
redirect: vi.fn(),
createFileRoute: () => (options: Record<string, unknown>) => options,
};
});
vi.mock("@/auth", () => ({
useViewer: () => ({ personId: "person-1" }),
}));
vi.mock("@/screens/dashboard", () => ({
DashboardScreen: () => <div data-testid="dashboard" />,
}));

import { act, render, screen } from "@testing-library/react";
import { beforeEach, describe, expect, it, vi } from "vitest";

import { setPortalEnabled } from "@/lib/portal/portal-store";
import { portalRouter } from "@/test/portal-router";

import { Route } from "./index";

const Component = (Route as unknown as { component: () => React.ReactNode })
.component;

beforeEach(() => {
act(() => {
setPortalEnabled(false);
portalRouter.reset("/");
});
});

describe("/", () => {
it("renders the dashboard while the preview is off", () => {
render(<Component />);
expect(screen.getByTestId("dashboard")).toBeInTheDocument();
expect(portalRouter.navigations).toEqual([]);
});

it("enters the portal when the preview is turned on", () => {
render(<Component />);
act(() => setPortalEnabled(true));
expect(screen.queryByTestId("dashboard")).not.toBeInTheDocument();
expect(portalRouter.navigations).toEqual([
{ to: "/portal", replace: true },
]);
});
});
8 changes: 6 additions & 2 deletions src/frontend/src/routes/index.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { createFileRoute, redirect } from "@tanstack/react-router";
import { Navigate, createFileRoute, redirect } from "@tanstack/react-router";

import { useViewer } from "@/auth";
import { readPortalEnabled } from "@/lib/portal/portal-store";
import { readPortalEnabled, usePortalEnabled } from "@/lib/portal/portal-store";
import { FullScreenLoading } from "@/components/full-screen-loading";
import { DashboardScreen } from "@/screens/dashboard";

Expand All @@ -17,6 +17,10 @@ export const Route = createFileRoute("/")({

function IndexRoute() {
const { personId } = useViewer();
const portal = usePortalEnabled();
// `beforeLoad` only runs on navigation, so a reader who turns the preview on
// while standing here would sit on the dashboard until a reload.
if (portal) return <Navigate to="/portal" replace />;
Comment thread
coderabbitai[bot] marked this conversation as resolved.
// An authenticated session always carries the person id (the gateway JWT
// `sub`); the loading fallback only shows in the brief window before the
// store resolves.
Expand Down
Loading