feat: enhance testing and admin page functionality

- Mocked `useRouter` from `next/navigation` in tests to improve routing behavior during testing.
- Updated admin page tests to reflect changes in title display and removed unnecessary back link check.
- Refactored admin page header to improve accessibility and layout, displaying month and year more clearly.
- Removed unused imports and components to streamline code and enhance maintainability.
This commit is contained in:
2026-03-06 11:03:46 +03:00
parent a3152a4545
commit 53a899ea26
5 changed files with 51 additions and 34 deletions

View File

@@ -93,13 +93,12 @@ describe("AdminPage", () => {
});
});
it("shows admin title and back link when allowed and data loaded", async () => {
it("shows admin title (month/year) when allowed and data loaded", async () => {
mockFetchForAdmin();
render(<AdminPage />);
await waitFor(() => {
expect(screen.getByRole("heading", { name: /admin|админка/i })).toBeInTheDocument();
});
expect(screen.getByRole("link", { name: /back to calendar|назад к календарю/i })).toBeInTheDocument();
});
it("shows access denied when fetchAdminMe returns is_admin false", async () => {

View File

@@ -6,10 +6,8 @@
"use client";
import Link from "next/link";
import { useAdminPage, AdminDutyList, ReassignSheet } from "@/components/admin";
import { useTranslation } from "@/i18n/use-translation";
import { Button } from "@/components/ui/button";
import { AccessDeniedScreen } from "@/components/states/AccessDeniedScreen";
import { LoadingState } from "@/components/states/LoadingState";
import { ErrorState } from "@/components/states/ErrorState";
@@ -47,24 +45,28 @@ export default function AdminPage() {
<p className="text-muted-foreground">
{admin.adminAccessDeniedDetail ?? t("admin.access_denied")}
</p>
<Button asChild variant="outline">
<Link href="/">{t("admin.back_to_calendar")}</Link>
</Button>
</div>
</div>
);
}
const month = admin.currentMonth.getMonth();
const year = admin.currentMonth.getFullYear();
return (
<div className={PAGE_WRAPPER_CLASS}>
<header className="sticky top-0 z-10 flex items-center justify-between border-b bg-[var(--header-bg)] py-3">
<h1 className="text-lg font-semibold">
{t("admin.title")} {monthName(admin.currentMonth.getMonth())}{" "}
{admin.currentMonth.getFullYear()}
<header className="sticky top-0 z-10 flex flex-col items-center border-b bg-[var(--header-bg)] py-3">
<h1
className="m-0 flex flex-col items-center justify-center gap-0 leading-none"
aria-label={`${t("admin.title")}, ${monthName(month)} ${year}`}
>
<span className="text-xs font-normal leading-none text-muted">
{year}
</span>
<span className="text-[1.1rem] font-semibold leading-tight sm:text-[1.25rem]">
{monthName(month)}
</span>
</h1>
<Button asChild variant="ghost" size="sm">
<Link href="/">{t("admin.back_to_calendar")}</Link>
</Button>
</header>
{admin.successMessage && (

View File

@@ -10,6 +10,10 @@ import { resetAppStore } from "@/test/test-utils";
import { useAppStore } from "@/store/app-store";
import { useTelegramAuth } from "@/hooks/use-telegram-auth";
vi.mock("next/navigation", () => ({
useRouter: () => ({ push: vi.fn(), replace: vi.fn(), prefetch: vi.fn() }),
}));
vi.mock("@/hooks/use-telegram-auth", () => ({
useTelegramAuth: vi.fn(),
}));