import { render, screen, fireEvent } from "@testing-library/react";
import { useGetAllActiveSessions } from "@/apps/new-ui/services/Service";
import ActiveAccounts from "@/apps/new-ui/pages/ActiveAccounts";
jest.mock("@/apps/new-ui/services/Service", () => ({
useGetAllActiveSessions: jest.fn(),
}));
// Mock Accordion (مانع ارور UI شود)
jest.mock("@/components/ui/accordion", () => ({
Accordion: ({ children }: any) =>
{children}
,
AccordionItem: ({ children }: any) => {children}
,
AccordionTrigger: ({ children, ...props }: any) => (
),
AccordionContent: ({ children }: any) => (
{children}
),
}));
describe("ActiveAccounts Component", () => {
beforeEach(() => {
jest.clearAllMocks();
});
test("shows loading state", () => {
(useGetAllActiveSessions as jest.Mock).mockReturnValue({
data: null,
isPending: true,
});
render();
expect(
screen.getByText("در حال بارگیری لیست دستگاهها...")
).toBeInTheDocument();
});
test("shows empty state when sessions = []", () => {
(useGetAllActiveSessions as jest.Mock).mockReturnValue({
data: [],
isPending: false,
});
render();
expect(screen.getByText("وضعیت خلوت")).toBeInTheDocument();
// نسخه درست:
expect(
screen.getByText(/در حال حاضر هیچ نشست فعالی برای نمایش وجود ندارد/)
).toBeInTheDocument();
});
test("renders sessions when data exists", () => {
(useGetAllActiveSessions as jest.Mock).mockReturnValue({
data: [
{
id: 1,
user_id: 10,
device_id: "Mozilla/5.0 Chrome/119",
ip_address: "192.168.1.10",
last_activity: "1404-12-11T17:53:58",
login_time: "1404-12-11T15:00:00",
},
],
isPending: false,
});
render();
expect(screen.getByTestId("accordion")).toBeInTheDocument();
// مدل دستگاه در خروجی ظاهر شود
expect(screen.getByText(/Chrome/)).toBeInTheDocument();
// آیپی
expect(screen.getByText("192.168.1.10")).toBeInTheDocument();
// تاریخ فرمت شده
expect(
screen.getByText("1404/12/11 ساعت 17:53:58")
).toBeInTheDocument();
});
test("accordion opens and shows content", () => {
(useGetAllActiveSessions as jest.Mock).mockReturnValue({
data: [
{
id: 20,
user_id: 33,
device_id: "Mozilla/5.0 Firefox/121",
ip_address: "8.8.8.8",
last_activity: "1404-10-01T09:20:40",
login_time: "1404-10-01T08:00:00",
},
],
isPending: false,
});
render();
const trigger = screen.getByTestId("accordion-trigger");
expect(trigger).toBeInTheDocument();
fireEvent.click(trigger);
expect(screen.getByTestId("accordion-content")).toBeInTheDocument();
expect(screen.getByText("8.8.8.8")).toBeInTheDocument();
});
});