189 lines
8.8 KiB
TypeScript
189 lines
8.8 KiB
TypeScript
import { fireEvent, screen } from "@testing-library/react";
|
|
import { beforeEach, describe, expect, it, vi } from "vitest";
|
|
import { renderWithProviders } from "../../test/utils";
|
|
|
|
vi.mock("../../hooks/useSubscription", () => ({
|
|
useSubscription: () => ({ hasFeature: () => true }),
|
|
}));
|
|
|
|
import { useAuthStore } from "../../stores/authStore";
|
|
import Sidebar from "./Sidebar";
|
|
|
|
describe("Sidebar — expandable نوبتها menu", () => {
|
|
beforeEach(() => {
|
|
useAuthStore.setState({
|
|
primaryRole: "admin",
|
|
dbUuid: "c1",
|
|
userName: "ادمین",
|
|
availableContexts: [],
|
|
context: null,
|
|
} as any);
|
|
});
|
|
|
|
it("keeps نوبتها always expanded regardless of route and click", () => {
|
|
renderWithProviders(<Sidebar />, { route: "/admin/dashboard" });
|
|
|
|
// منوی والد به شکل دکمه
|
|
const parent = screen.getByRole("button", { name: /نوبتها/ });
|
|
expect(parent).toBeInTheDocument();
|
|
|
|
// حتی خارج از مسیر نوبتها، زیرمنوها همیشه دیده میشوند
|
|
expect(screen.getByText("نوبت ها")).toBeInTheDocument();
|
|
expect(screen.getByText("افزودن نوبت")).toBeInTheDocument();
|
|
|
|
// کلیک روی والد آن را جمع نمیکند
|
|
fireEvent.click(parent);
|
|
expect(screen.getByText("نوبت ها")).toBeInTheDocument();
|
|
expect(screen.getByText("افزودن نوبت")).toBeInTheDocument();
|
|
});
|
|
|
|
it("auto-expands when a child route is active", () => {
|
|
renderWithProviders(<Sidebar />, { route: "/admin/appointments/new" });
|
|
// چون «افزودن نوبت» فعال است، منو باید خودکار باز باشد
|
|
expect(screen.getByText("افزودن نوبت")).toBeInTheDocument();
|
|
expect(screen.getByText("نوبت ها")).toBeInTheDocument();
|
|
});
|
|
});
|
|
|
|
describe("Sidebar — خدمات در منوی اصلی (نه تنظیمات)", () => {
|
|
it("shows خدمات in the clinic main menu linking to /admin/clinic-services", () => {
|
|
useAuthStore.setState({
|
|
primaryRole: "clinic",
|
|
dbUuid: "c1",
|
|
userName: "کلینیک",
|
|
availableContexts: [],
|
|
context: null,
|
|
} as any);
|
|
renderWithProviders(<Sidebar />, { route: "/admin/dashboard" });
|
|
expect(screen.getByText("سرویس ها").closest("a")).toHaveAttribute(
|
|
"href",
|
|
"/admin/clinic-services",
|
|
);
|
|
});
|
|
|
|
it("shows خدمات in the doctor main menu linking to /admin/clinic-services", () => {
|
|
useAuthStore.setState({
|
|
primaryRole: "doctor",
|
|
dbUuid: null,
|
|
userName: "پزشک",
|
|
availableContexts: [],
|
|
context: null,
|
|
} as any);
|
|
renderWithProviders(<Sidebar />, { route: "/admin/dashboard" });
|
|
expect(screen.getByText("سرویس ها").closest("a")).toHaveAttribute(
|
|
"href",
|
|
"/admin/clinic-services",
|
|
);
|
|
});
|
|
});
|
|
|
|
describe("Sidebar — گِیت منوی منشی بر اساس مجوز", () => {
|
|
const setSecretary = (resources: Record<string, Record<string, boolean>>) =>
|
|
useAuthStore.setState({
|
|
primaryRole: "secretary",
|
|
dbUuid: "c1",
|
|
userName: "منشی",
|
|
availableContexts: [],
|
|
context: { scope: "clinic", permissions: { resources } },
|
|
} as any);
|
|
|
|
it("منشیِ بدون مجوز، سرویسها/انبار/تگها را نمیبیند", () => {
|
|
setSecretary({ appointments: { view: true } });
|
|
renderWithProviders(<Sidebar />, { route: "/admin/dashboard" });
|
|
expect(screen.queryByText("سرویس ها")).not.toBeInTheDocument();
|
|
expect(screen.queryByText("انبارداری")).not.toBeInTheDocument();
|
|
expect(screen.queryByText("تگها")).not.toBeInTheDocument();
|
|
});
|
|
|
|
it("با مجوز services.view، آیتم «سرویس ها» به /admin/clinic-services میرود", () => {
|
|
setSecretary({ services: { view: true } });
|
|
renderWithProviders(<Sidebar />, { route: "/admin/dashboard" });
|
|
expect(screen.getByText("سرویس ها").closest("a")).toHaveAttribute(
|
|
"href",
|
|
"/admin/clinic-services",
|
|
);
|
|
});
|
|
|
|
it("انبارداری با مجوز inventory.view در سایدبار اصلی میآید", () => {
|
|
setSecretary({ inventory: { view: true } });
|
|
renderWithProviders(<Sidebar />, { route: "/admin/dashboard" });
|
|
expect(screen.getByText("انبارداری").closest("a")).toHaveAttribute(
|
|
"href",
|
|
"/admin/inventory",
|
|
);
|
|
});
|
|
|
|
it("آیتم «تنظیمات» همیشه برای منشی نمایش داده میشود", () => {
|
|
setSecretary({ appointments: { view: true } });
|
|
renderWithProviders(<Sidebar />, { route: "/admin/dashboard" });
|
|
// «تنظیمات» هم عنوان بخش است هم آیتم؛ فقط لینکِ آیتم را میسنجیم.
|
|
expect(screen.getByRole("link", { name: "تنظیمات" })).toHaveAttribute(
|
|
"href",
|
|
"/admin/account-settings",
|
|
);
|
|
});
|
|
|
|
it("منابعِ زیرمجموعهٔ تنظیمات (staff/discounts/sms/tags/appointment/clinic_doctors) در سایدبار اصلی نمیآیند", () => {
|
|
// حتی با همهٔ مجوزها، اینها باید فقط داخل صفحهٔ تنظیمات باشند نه nav اصلی.
|
|
setSecretary({
|
|
staff: { view: true },
|
|
discounts: { view: true },
|
|
sms: { view: true },
|
|
tags: { view: true },
|
|
appointment_settings: { view: true },
|
|
clinic_doctors: { view: true },
|
|
});
|
|
renderWithProviders(<Sidebar />, { route: "/admin/dashboard" });
|
|
expect(screen.queryByText("پرسنل")).not.toBeInTheDocument();
|
|
expect(screen.queryByText("تخفیفها")).not.toBeInTheDocument();
|
|
expect(screen.queryByText("پیامکها")).not.toBeInTheDocument();
|
|
expect(screen.queryByText("تگها")).not.toBeInTheDocument();
|
|
expect(screen.queryByText("تنظیمات نوبتدهی")).not.toBeInTheDocument();
|
|
expect(screen.queryByText("پزشکان کلینیک")).not.toBeInTheDocument();
|
|
});
|
|
|
|
/**
|
|
* «منابع» از منوی تنظیمات به سایدبار اصلی منتقل شد. گِیتش عوض نشده — همان
|
|
* `appointment_settings.view` که در منوی تنظیمات داشت.
|
|
*/
|
|
it("«منابع» در سایدبار اصلی میآید و به مجوز appointment_settings گِیت است", () => {
|
|
setSecretary({ appointment_settings: { view: true } });
|
|
renderWithProviders(<Sidebar />, { route: "/admin/dashboard" });
|
|
expect(screen.getByText("منابع").closest("a")).toHaveAttribute("href", "/admin/resources");
|
|
});
|
|
|
|
it("منشیِ بدون مجوز، «منابع» را نمیبیند", () => {
|
|
setSecretary({ appointments: { view: true } });
|
|
renderWithProviders(<Sidebar />, { route: "/admin/dashboard" });
|
|
expect(screen.queryByText("منابع")).not.toBeInTheDocument();
|
|
});
|
|
});
|
|
|
|
describe("Sidebar — گِیت منوی پزشکِ عضوِ کلینیک بر اساس مجوز", () => {
|
|
const setMemberDoctor = (resources: Record<string, Record<string, boolean>>) =>
|
|
useAuthStore.setState({
|
|
primaryRole: "doctor",
|
|
dbUuid: "c1",
|
|
userName: "دکتر عضو",
|
|
availableContexts: [],
|
|
context: { scope: "clinic", permissions: { resources } },
|
|
} as any);
|
|
|
|
it("پزشکِ عضو فقط منابعِ مجاز را در سایدبار میبیند + «تنظیمات» همیشه", () => {
|
|
setMemberDoctor({ appointments: { view: true }, services: { view: true } });
|
|
renderWithProviders(<Sidebar />, { route: "/admin/dashboard" });
|
|
expect(screen.getByText("نوبتهای من")).toBeInTheDocument();
|
|
expect(screen.getByText("سرویس ها").closest("a")).toHaveAttribute("href", "/admin/clinic-services");
|
|
expect(screen.getByRole("link", { name: "تنظیمات" })).toHaveAttribute("href", "/admin/account-settings");
|
|
// بدون مجوز → پنهان
|
|
expect(screen.queryByText("انبارداری")).not.toBeInTheDocument();
|
|
expect(screen.queryByText("پرداختها")).not.toBeInTheDocument();
|
|
});
|
|
|
|
it("پزشکِ عضو بدونِ مجوزِ نوبت، آیتم نوبت را نمیبیند", () => {
|
|
setMemberDoctor({ services: { view: true } });
|
|
renderWithProviders(<Sidebar />, { route: "/admin/dashboard" });
|
|
expect(screen.queryByText("نوبتهای من")).not.toBeInTheDocument();
|
|
});
|
|
});
|