180 lines
6.0 KiB
TypeScript
180 lines
6.0 KiB
TypeScript
import { render, screen, fireEvent, waitFor } from "@testing-library/react";
|
|
import {
|
|
useGetUnConfirmedReservedAppointmentsCount,
|
|
useRejectReservedAppointment
|
|
} from "@/apps/new-ui/services/ReservedAppointment";
|
|
import { useGetCustomerProfilePicture } from "@/apps/new-ui/services/Customer";
|
|
import { useConfirmReservedAppointment } from "@/apps/new-ui/services/NewTest";
|
|
import { showWarningAlert } from "@/apps/new-ui/components/CustomAlert";
|
|
import toast from "react-hot-toast";
|
|
import AppointmentRequest from "@/apps/new-ui/pages/reserved-appointments/+components/AppointmentRequest";
|
|
import { IReservedAppointment } from "@/apps/new-ui/Utils/types";
|
|
|
|
jest.mock("@/apps/new-ui/pages/CustomerClub/index.css", () => ({}));
|
|
|
|
jest.mock("../src/apps/new-ui/Utils/env.ts", () => ({
|
|
ENV: {
|
|
API_URL: "https://api.test",
|
|
},
|
|
}));
|
|
|
|
// --- Mocks ---
|
|
jest.mock("@/apps/new-ui/services/ReservedAppointment", () => ({
|
|
useGetUnConfirmedReservedAppointmentsCount: jest.fn(),
|
|
useRejectReservedAppointment: jest.fn(),
|
|
}));
|
|
|
|
jest.mock("@/apps/new-ui/services/Customer", () => ({
|
|
useGetCustomerProfilePicture: jest.fn(),
|
|
}));
|
|
|
|
jest.mock("@/apps/new-ui/services/NewTest", () => ({
|
|
useConfirmReservedAppointment: jest.fn(),
|
|
}));
|
|
|
|
jest.mock("@/apps/new-ui/components/CustomAlert", () => ({
|
|
showWarningAlert: jest.fn(),
|
|
}));
|
|
|
|
jest.mock("react-hot-toast", () => ({
|
|
success: jest.fn(),
|
|
error: jest.fn(),
|
|
}));
|
|
|
|
jest.mock("@/apps/new-ui/assets/profilePhoto.svg", () => "mock-avatar");
|
|
|
|
// کامپوننت فرزند را ساده میکنیم تا فقط تست والد را انجام دهیم
|
|
jest.mock("@/apps/new-ui/pages/reserved-appointments/+components/AppointmentDetail.tsx", () => {
|
|
return ({ isOpen }: { isOpen: boolean }) => isOpen ? <div data-testid="appointment-detail">Detail Modal</div> : null;
|
|
});
|
|
|
|
describe("AppointmentRequest Component", () => {
|
|
const mockRefetch = jest.fn();
|
|
const mockCloseOverlay = jest.fn();
|
|
|
|
const mockAppointments: IReservedAppointment[] = [
|
|
{
|
|
id: 1,
|
|
name: "کاربر تست",
|
|
phone_number: "09120000000",
|
|
service: "کوتاهی مو",
|
|
jalali_time: "14:30",
|
|
jalali_date: "1402/10/01", // اضافه شد
|
|
creation_time: "2024-01-15T10:00:00Z", // اضافه شد
|
|
description: "توضیح تست",
|
|
confrimed: false,
|
|
},
|
|
];
|
|
|
|
|
|
const mockMutateReject = jest.fn();
|
|
const mockRefetchUnconfirmed = jest.fn();
|
|
const mockMutateConfirm = jest.fn();
|
|
const mockMutateGetProfile = jest.fn();
|
|
|
|
beforeEach(() => {
|
|
jest.clearAllMocks();
|
|
|
|
(useRejectReservedAppointment as jest.Mock).mockReturnValue({ mutate: mockMutateReject });
|
|
(useGetUnConfirmedReservedAppointmentsCount as jest.Mock).mockReturnValue({ refetch: mockRefetchUnconfirmed });
|
|
(useConfirmReservedAppointment as jest.Mock).mockReturnValue({ mutateAsync: mockMutateConfirm });
|
|
(useGetCustomerProfilePicture as jest.Mock).mockReturnValue({
|
|
mutateAsync: mockMutateGetProfile.mockResolvedValue({
|
|
data: { "09120000000": { customer_type: "VIP", profile_picture: "pic.jpg" } }
|
|
}),
|
|
});
|
|
});
|
|
|
|
test("به درستی لیست درخواستها را رندر میکند", async () => {
|
|
render(
|
|
<AppointmentRequest
|
|
selectedDateAppointment={mockAppointments}
|
|
refetch={mockRefetch}
|
|
closeOverlay={mockCloseOverlay}
|
|
/>
|
|
);
|
|
|
|
expect(screen.getByText("کاربر تست")).toBeInTheDocument();
|
|
expect(screen.getByText("کوتاهی مو")).toBeInTheDocument();
|
|
expect(screen.getByText("14:30")).toBeInTheDocument();
|
|
expect(screen.getByText("تایید نشده")).toBeInTheDocument();
|
|
});
|
|
|
|
test("با کلیک روی 'باز کردن'، مودال جزئیات باز میشود", () => {
|
|
render(
|
|
<AppointmentRequest
|
|
selectedDateAppointment={mockAppointments}
|
|
refetch={mockRefetch}
|
|
closeOverlay={mockCloseOverlay}
|
|
/>
|
|
);
|
|
|
|
const openBtn = screen.getByText("باز کردن");
|
|
fireEvent.click(openBtn);
|
|
|
|
expect(mockCloseOverlay).toHaveBeenCalled();
|
|
expect(screen.getByTestId("appointment-detail")).toBeInTheDocument();
|
|
});
|
|
|
|
test("عملکرد دکمه تایید نوبت را تست میکند", async () => {
|
|
mockMutateConfirm.mockResolvedValueOnce({}); // شبیهسازی موفقیت API
|
|
|
|
render(
|
|
<AppointmentRequest
|
|
selectedDateAppointment={mockAppointments}
|
|
refetch={mockRefetch}
|
|
closeOverlay={mockCloseOverlay}
|
|
/>
|
|
);
|
|
|
|
const confirmBtn = screen.getByText("تایید");
|
|
fireEvent.click(confirmBtn);
|
|
|
|
expect(mockMutateConfirm).toHaveBeenCalledWith(1);
|
|
|
|
await waitFor(() => {
|
|
expect(toast.success).toHaveBeenCalledWith(expect.any(String));
|
|
expect(mockRefetchUnconfirmed).toHaveBeenCalled();
|
|
expect(mockCloseOverlay).toHaveBeenCalled();
|
|
});
|
|
});
|
|
|
|
test("عملکرد دکمه رد نوبت و نمایش Alert هشدار را تست میکند", () => {
|
|
render(
|
|
<AppointmentRequest
|
|
selectedDateAppointment={mockAppointments}
|
|
refetch={mockRefetch}
|
|
closeOverlay={mockCloseOverlay}
|
|
/>
|
|
);
|
|
|
|
const rejectBtn = screen.getByText("رد");
|
|
fireEvent.click(rejectBtn);
|
|
|
|
// بررسی نمایش آلرت
|
|
expect(showWarningAlert).toHaveBeenCalled();
|
|
|
|
// شبیهسازی کلیک روی تاییدِ آلرت (اجرای کالبک)
|
|
const callback = (showWarningAlert as jest.Mock).mock.calls[0][1];
|
|
callback();
|
|
|
|
expect(mockMutateReject).toHaveBeenCalledWith(1);
|
|
});
|
|
|
|
test("اگر نوبت قبلاً تایید شده باشد، دکمههای تایید/رد نمایش داده نمیشوند", () => {
|
|
const confirmedApt = [{ ...mockAppointments[0], confrimed: true }];
|
|
|
|
render(
|
|
<AppointmentRequest
|
|
selectedDateAppointment={confirmedApt}
|
|
refetch={mockRefetch}
|
|
closeOverlay={mockCloseOverlay}
|
|
/>
|
|
);
|
|
|
|
expect(screen.getByText("تایید شده")).toBeInTheDocument();
|
|
expect(screen.queryByText("تایید")).not.toBeInTheDocument();
|
|
expect(screen.queryByText("رد")).not.toBeInTheDocument();
|
|
});
|
|
});
|