Files
co-lite/solvway-admin-portal/app/components/app/AppShell.test.tsx
T
ngqileandClaude Opus 5.5 5ee51d1913 chore: copy kept Tastefully modules and portals into Co-Lite
Backend modules from tastefully 8bd93d15: core-libs, core-session,
api-gateway, mobile-api, services-auth, services-system,
services-counter, services-member, services-log, services-email.
solvway-admin-portal from babyboo f64d096ea, solvway-customer-portal
from tastefully 8bd93d15. Copied as tracked files only.

Literal secrets replaced with environment placeholders:
- services-email: SMTP_PASSWORD, MAILGUN_API_KEY, SENDGRID_API_KEY,
  MAILJET_API_KEY, MAILJET_SECRET_KEY (optional, empty default)
- services-system: JUMIO_TOKEN, JUMIO_SECRET (optional)
- services-auth: USER_AUTH_API_SECRET (required); commented JWT secret removed
- core-libs: JWT_TOKEN default removed (required), XXL_JOB_TOKEN default
  removed, ipgeolocation key read from IPGEOLOCATION_API_KEY
- services-system EkycAppConfig: commented Jumio credentials removed

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01EFUzfj5akPxyikzGc2iAHM
2026-11-26 15:55:45 +08:00

75 lines
2.8 KiB
TypeScript

import { render, screen, waitFor } from "@testing-library/react";
import { describe, it, expect, vi, beforeEach } from "vitest";
const nav = vi.hoisted(() => ({ replace: vi.fn() }));
let mockPathname = "/";
vi.mock("next/navigation", () => ({
useRouter: () => ({ replace: nav.replace }),
usePathname: () => mockPathname,
}));
const auth = vi.hoisted(() => ({ name: null as string | null, loading: true }));
vi.mock("@/app/lib/AdminAuthContext", () => ({
useAdminAuth: () => auth,
}));
vi.mock("./Sidebar", () => ({ default: () => <div data-testid="sidebar" /> }));
vi.mock("./Header", () => ({ default: () => <div data-testid="header" /> }));
import AppShell from "./AppShell";
beforeEach(() => {
nav.replace.mockReset();
mockPathname = "/";
auth.name = null;
auth.loading = true;
});
describe("AppShell", () => {
it("renders nothing (no flash) while session hydration is loading", () => {
auth.loading = true;
auth.name = null;
const { container } = render(<AppShell>content</AppShell>);
expect(container).toBeEmptyDOMElement();
});
// Ruling R3: session hydration (/api/auth/session via the context) found no
// session — either a genuinely absent cookie, or the expired-but-present-
// cookie case the edge middleware's presence-only check cannot catch.
it("redirects to /login when hydration finishes with no session", async () => {
auth.loading = false;
auth.name = null;
render(<AppShell>content</AppShell>);
await waitFor(() => expect(nav.replace).toHaveBeenCalledWith("/login"));
});
it("renders the authenticated shell (sidebar, header, children) once signed in", () => {
auth.loading = false;
auth.name = "Admin User";
render(<AppShell>content</AppShell>);
expect(screen.getByTestId("sidebar")).toBeInTheDocument();
expect(screen.getByTestId("header")).toBeInTheDocument();
expect(screen.getByText("content")).toBeInTheDocument();
expect(nav.replace).not.toHaveBeenCalled();
});
it("shows the Containerverse credit under the content on small screens (the sidebar is hidden there)", () => {
auth.loading = false;
auth.name = "Admin User";
render(<AppShell>content</AppShell>);
const link = screen.getByRole("link", { name: "Containerverse Sdn. Bhd." });
expect(link).toHaveAttribute("href", "https://containerverse.dev/");
expect(link.closest("[data-testid='mobile-credit']")).toHaveClass("lg:hidden");
});
it("does not redirect on a public page even with no session", () => {
mockPathname = "/login";
auth.loading = false;
auth.name = null;
render(<AppShell>content</AppShell>);
expect(nav.replace).not.toHaveBeenCalled();
expect(screen.getByText("content")).toBeInTheDocument();
expect(screen.queryByTestId("sidebar")).not.toBeInTheDocument();
});
});