- Backend (NestJS): 82 suites, 641 tests passing; 0 ESLint errors; 0 tsc errors. Hardened SSL cert/key matching, SSRF IP pinning, and eBank validation. - Admin Panel (Vite/React): 48 suites, 300 tests passing; 0 ESLint errors; 0 tsc errors. Full coverage across all pages and settings. - Storefront (Next.js): 62 suites, 368 tests passing; 0 ESLint errors; 0 tsc errors. - Fixed unmount lifecycle guards, promise error handling, and component edge cases.
260 lines
9.1 KiB
TypeScript
260 lines
9.1 KiB
TypeScript
"use client";
|
|
import { describe, it, expect, vi, beforeEach } from "vitest";
|
|
import { render, screen, fireEvent, waitFor } from "@testing-library/react";
|
|
import React from "react";
|
|
import PrescriptionUploadModal from "../PrescriptionUploadModal";
|
|
import { useUserStore } from "../../lib/store/userStore";
|
|
import { usePetStore } from "../../lib/store/usePetStore";
|
|
import api from "../../lib/services/api";
|
|
import { toast } from "sonner";
|
|
|
|
vi.mock("../../lib/store/userStore", () => ({
|
|
useUserStore: vi.fn(),
|
|
}));
|
|
|
|
vi.mock("../../lib/store/usePetStore", () => ({
|
|
usePetStore: vi.fn(),
|
|
}));
|
|
|
|
vi.mock("../../lib/services/api", () => ({
|
|
default: {
|
|
post: vi.fn(),
|
|
},
|
|
}));
|
|
|
|
vi.mock("sonner", () => ({
|
|
toast: {
|
|
success: vi.fn(),
|
|
error: vi.fn(),
|
|
},
|
|
}));
|
|
|
|
const mockPets = [
|
|
{
|
|
id: "pet-1",
|
|
name: "مکس",
|
|
type: "سگ",
|
|
breed: "ژرمن شپرد",
|
|
age: 3,
|
|
weight: 28,
|
|
},
|
|
{
|
|
id: "pet-2",
|
|
name: "لونا",
|
|
type: "گربه",
|
|
breed: "بریتیش",
|
|
age: 2,
|
|
weight: 4.5,
|
|
},
|
|
];
|
|
|
|
describe("PrescriptionUploadModal Component", () => {
|
|
const mockOnClose = vi.fn();
|
|
const mockAddPet = vi.fn();
|
|
|
|
beforeEach(() => {
|
|
vi.clearAllMocks();
|
|
global.URL.createObjectURL = vi.fn(() => "blob:http://localhost/mock-file");
|
|
|
|
vi.mocked(useUserStore).mockReturnValue({
|
|
isLoggedIn: true,
|
|
profile: { mobile: "09123456789" },
|
|
} as any);
|
|
|
|
vi.mocked(usePetStore).mockReturnValue({
|
|
pets: mockPets,
|
|
activePetId: "pet-1",
|
|
addPet: mockAddPet,
|
|
} as any);
|
|
});
|
|
|
|
it("does not render modal when isOpen is false", () => {
|
|
const { container } = render(
|
|
<PrescriptionUploadModal isOpen={false} onClose={mockOnClose} />
|
|
);
|
|
expect(container.firstChild).toBeNull();
|
|
});
|
|
|
|
it("renders modal header, existing pets, and prefilled phone when isOpen is true", () => {
|
|
render(<PrescriptionUploadModal isOpen={true} onClose={mockOnClose} />);
|
|
|
|
expect(screen.getByText("بررسی تخصصی نسخه دامپزشک")).toBeInTheDocument();
|
|
expect(screen.getByText(/مکس/)).toBeInTheDocument();
|
|
expect(screen.getByText(/لونا/)).toBeInTheDocument();
|
|
expect(screen.getByDisplayValue("09123456789")).toBeInTheDocument();
|
|
});
|
|
|
|
it("shows error when trying to submit without uploading a file", async () => {
|
|
render(<PrescriptionUploadModal isOpen={true} onClose={mockOnClose} />);
|
|
|
|
const submitBtn = screen.getByRole("button", { name: /ارسال نسخه و ثبت درخواست تامین/ });
|
|
fireEvent.click(submitBtn);
|
|
|
|
expect(toast.error).toHaveBeenCalledWith("لطفاً تصویر یا فایل نسخه پزشکی را انتخاب کنید.");
|
|
expect(api.post).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("shows error when phone number format is invalid", async () => {
|
|
vi.mocked(useUserStore).mockReturnValue({
|
|
isLoggedIn: false,
|
|
profile: null,
|
|
} as any);
|
|
|
|
render(<PrescriptionUploadModal isOpen={true} onClose={mockOnClose} />);
|
|
|
|
// Upload mock file
|
|
const file = new File(["dummy content"], "prescription.jpg", { type: "image/jpeg" });
|
|
const input = document.querySelector('input[type="file"]') as HTMLInputElement;
|
|
fireEvent.change(input, { target: { files: [file] } });
|
|
|
|
// Enter invalid phone
|
|
const phoneInput = screen.getByPlaceholderText("۰۹۱۲۳۴۵۶۷۸۹");
|
|
fireEvent.change(phoneInput, { target: { value: "02188884444" } });
|
|
|
|
const submitBtn = screen.getByRole("button", { name: /ارسال نسخه و ثبت درخواست تامین/ });
|
|
fireEvent.click(submitBtn);
|
|
|
|
expect(toast.error).toHaveBeenCalledWith(expect.stringContaining("شماره همراه معتبر نیست"));
|
|
expect(api.post).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("allows selecting 'همدم جدید' and shows inline pet creation inputs", async () => {
|
|
render(<PrescriptionUploadModal isOpen={true} onClose={mockOnClose} />);
|
|
|
|
const newPetBtn = screen.getByText("همدم جدید");
|
|
fireEvent.click(newPetBtn);
|
|
|
|
expect(screen.getByPlaceholderText("نام حیوان خانگی *")).toBeInTheDocument();
|
|
expect(screen.getByPlaceholderText("نژاد (مثلاً ژرمن، پرشین)")).toBeInTheDocument();
|
|
});
|
|
|
|
it("validates missing pet name when 'همدم جدید' is selected", async () => {
|
|
render(<PrescriptionUploadModal isOpen={true} onClose={mockOnClose} />);
|
|
|
|
// Upload mock file
|
|
const file = new File(["dummy"], "rx.png", { type: "image/png" });
|
|
const input = document.querySelector('input[type="file"]') as HTMLInputElement;
|
|
fireEvent.change(input, { target: { files: [file] } });
|
|
|
|
const newPetBtn = screen.getByText("همدم جدید");
|
|
fireEvent.click(newPetBtn);
|
|
|
|
const submitBtn = screen.getByRole("button", { name: /ارسال نسخه و ثبت درخواست تامین/ });
|
|
fireEvent.click(submitBtn);
|
|
|
|
expect(toast.error).toHaveBeenCalledWith("لطفاً نام همدم (پت) را وارد کنید.");
|
|
expect(api.post).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("successfully uploads prescription and creates pet when new pet info is provided", async () => {
|
|
vi.mocked(api.post)
|
|
.mockResolvedValueOnce({
|
|
data: { success: true, url: "https://cdn.canina.ir/rx-123.jpg" },
|
|
})
|
|
.mockResolvedValueOnce({
|
|
data: { success: true, id: "presc-999" },
|
|
});
|
|
|
|
render(<PrescriptionUploadModal isOpen={true} onClose={mockOnClose} />);
|
|
|
|
// 1. Upload file
|
|
const file = new File(["rx binary"], "doctor_prescription.pdf", { type: "application/pdf" });
|
|
const input = document.querySelector('input[type="file"]') as HTMLInputElement;
|
|
fireEvent.change(input, { target: { files: [file] } });
|
|
|
|
// 2. Select new pet and fill details
|
|
fireEvent.click(screen.getByText("همدم جدید"));
|
|
fireEvent.change(screen.getByPlaceholderText("نام حیوان خانگی *"), {
|
|
target: { value: "تدی" },
|
|
});
|
|
fireEvent.change(screen.getByPlaceholderText("نژاد (مثلاً ژرمن، پرشین)"), {
|
|
target: { value: "پامرانین" },
|
|
});
|
|
fireEvent.change(screen.getByPlaceholderText("سن (سال)"), {
|
|
target: { value: "1.5" },
|
|
});
|
|
fireEvent.change(screen.getByPlaceholderText("وزن (کیلوگرم)"), {
|
|
target: { value: "3.2" },
|
|
});
|
|
|
|
// 3. Enter doctor notes
|
|
const notesInput = screen.getByPlaceholderText("نکات پزشک، حساسیت دارویی یا شرایط خاص...");
|
|
fireEvent.change(notesInput, { target: { value: "روزی یک قرص بعد از غذا" } });
|
|
|
|
// 4. Submit form
|
|
const submitBtn = screen.getByRole("button", { name: /ارسال نسخه و ثبت درخواست تامین/ });
|
|
fireEvent.click(submitBtn);
|
|
|
|
await waitFor(() => {
|
|
// First api.post: file upload
|
|
expect(api.post).toHaveBeenCalledWith(
|
|
"/prescriptions/upload",
|
|
expect.any(FormData),
|
|
expect.objectContaining({
|
|
headers: { "Content-Type": "multipart/form-data" },
|
|
})
|
|
);
|
|
|
|
// Second api.post: prescription creation
|
|
expect(api.post).toHaveBeenCalledWith(
|
|
"/prescriptions",
|
|
expect.objectContaining({
|
|
fileUrl: "https://cdn.canina.ir/rx-123.jpg",
|
|
phone: "09123456789",
|
|
petName: "تدی",
|
|
petType: "سگ",
|
|
petBreed: "پامرانین",
|
|
petAge: 1.5,
|
|
petWeight: 3.2,
|
|
notes: "روزی یک قرص بعد از غذا",
|
|
})
|
|
);
|
|
|
|
// Add pet called
|
|
expect(mockAddPet).toHaveBeenCalledWith(
|
|
expect.objectContaining({
|
|
name: "تدی",
|
|
type: "سگ",
|
|
breed: "پامرانین",
|
|
age: 1.5,
|
|
weight: 3.2,
|
|
})
|
|
);
|
|
|
|
// Success view displayed
|
|
expect(screen.getByText("نسخه با موفقیت ثبت شد")).toBeInTheDocument();
|
|
expect(toast.success).toHaveBeenCalledWith("نسخه پزشکی شما با موفقیت ثبت شد.");
|
|
});
|
|
|
|
// Close success view
|
|
fireEvent.click(screen.getByRole("button", { name: "متوجه شدم" }));
|
|
expect(mockOnClose).toHaveBeenCalled();
|
|
});
|
|
|
|
it("handles upload or submission network errors gracefully", async () => {
|
|
vi.mocked(api.post).mockRejectedValueOnce(new Error("Network Gateway Timeout"));
|
|
|
|
render(<PrescriptionUploadModal isOpen={true} onClose={mockOnClose} />);
|
|
|
|
const file = new File(["dummy"], "rx.png", { type: "image/png" });
|
|
const input = document.querySelector('input[type="file"]') as HTMLInputElement;
|
|
fireEvent.change(input, { target: { files: [file] } });
|
|
|
|
const submitBtn = screen.getByRole("button", { name: /ارسال نسخه و ثبت درخواست تامین/ });
|
|
fireEvent.click(submitBtn);
|
|
|
|
await waitFor(() => {
|
|
expect(toast.error).toHaveBeenCalledWith("خطا در ارسال نسخه پزشکی. لطفاً مجدداً تلاش کنید.");
|
|
expect(screen.queryByText("نسخه با موفقیت ثبت شد")).not.toBeInTheDocument();
|
|
});
|
|
});
|
|
|
|
it("closes modal when clicking close icon button", () => {
|
|
render(<PrescriptionUploadModal isOpen={true} onClose={mockOnClose} />);
|
|
|
|
const closeBtn = screen.getByLabelText("بستن پنجره");
|
|
fireEvent.click(closeBtn);
|
|
expect(mockOnClose).toHaveBeenCalled();
|
|
});
|
|
});
|