- 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.
220 lines
6.8 KiB
TypeScript
220 lines
6.8 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 TopUpModal from "../TopUpModal";
|
||
import { useUserStore } from "../../lib/store/userStore";
|
||
import { useSettingsStore } from "../../lib/store/settingsStore";
|
||
import api from "../../lib/services/api";
|
||
import { toast } from "sonner";
|
||
|
||
vi.mock("../../lib/store/userStore", () => ({
|
||
useUserStore: Object.assign(vi.fn(), {
|
||
getState: vi.fn(),
|
||
}),
|
||
}));
|
||
|
||
vi.mock("../../lib/store/settingsStore", () => ({
|
||
useSettingsStore: vi.fn(),
|
||
}));
|
||
|
||
vi.mock("../../lib/services/api", () => ({
|
||
default: {
|
||
post: vi.fn(),
|
||
},
|
||
}));
|
||
|
||
vi.mock("sonner", () => ({
|
||
toast: {
|
||
success: vi.fn(),
|
||
error: vi.fn(),
|
||
},
|
||
}));
|
||
|
||
describe("TopUpModal Component", () => {
|
||
const mockOnClose = vi.fn();
|
||
const mockOnConfirm = vi.fn();
|
||
|
||
beforeEach(() => {
|
||
vi.clearAllMocks();
|
||
|
||
vi.mocked(useUserStore).mockReturnValue({
|
||
isLoggedIn: true,
|
||
profile: { mobile: "09121234567" },
|
||
} as any);
|
||
|
||
(useUserStore as any).getState = vi.fn(() => ({
|
||
isLoggedIn: true,
|
||
}));
|
||
|
||
vi.mocked(useSettingsStore).mockImplementation((selector: any) => {
|
||
return selector({
|
||
getBoolean: vi.fn((key: string, def: boolean) => def),
|
||
});
|
||
});
|
||
});
|
||
|
||
it("does not render when isOpen is false", () => {
|
||
const { container } = render(
|
||
<TopUpModal isOpen={false} onClose={mockOnClose} />
|
||
);
|
||
expect(container.firstChild).toBeNull();
|
||
});
|
||
|
||
it("renders modal with header and preset buttons when isOpen is true", () => {
|
||
render(<TopUpModal isOpen={true} onClose={mockOnClose} />);
|
||
|
||
expect(screen.getByText("شارژ آنلاین کیف پول")).toBeInTheDocument();
|
||
expect(screen.getByText("۵۰۰,۰۰۰ تومان")).toBeInTheDocument();
|
||
expect(screen.getByText("۱,۰۰۰,۰۰۰ تومان")).toBeInTheDocument();
|
||
expect(screen.getByText("۲,۰۰۰,۰۰۰ تومان")).toBeInTheDocument();
|
||
});
|
||
|
||
it("populates initial amount if provided in props", () => {
|
||
render(
|
||
<TopUpModal
|
||
isOpen={true}
|
||
onClose={mockOnClose}
|
||
initialAmount={350000}
|
||
/>
|
||
);
|
||
|
||
expect(screen.getByDisplayValue("350,000")).toBeInTheDocument();
|
||
});
|
||
|
||
it("selects preset amount when preset button is clicked", () => {
|
||
render(<TopUpModal isOpen={true} onClose={mockOnClose} />);
|
||
|
||
const preset1m = screen.getByText("۱,۰۰۰,۰۰۰ تومان");
|
||
fireEvent.click(preset1m);
|
||
|
||
expect(screen.getByDisplayValue("1,000,000")).toBeInTheDocument();
|
||
});
|
||
|
||
it("allows custom amount manual typing", () => {
|
||
render(<TopUpModal isOpen={true} onClose={mockOnClose} />);
|
||
|
||
const input = screen.getByPlaceholderText("۰");
|
||
fireEvent.change(input, { target: { value: "750000" } });
|
||
|
||
expect(screen.getByDisplayValue("750,000")).toBeInTheDocument();
|
||
});
|
||
|
||
it("blocks submission and shows error if user is not logged in", async () => {
|
||
(useUserStore as any).getState = vi.fn(() => ({
|
||
isLoggedIn: false,
|
||
}));
|
||
|
||
render(
|
||
<TopUpModal
|
||
isOpen={true}
|
||
onClose={mockOnClose}
|
||
initialAmount={100000}
|
||
/>
|
||
);
|
||
|
||
const submitBtn = screen.getByRole("button", { name: /اتصال به درگاه و شارژ آنلاین/ });
|
||
fireEvent.click(submitBtn);
|
||
|
||
expect(toast.error).toHaveBeenCalledWith(
|
||
"جهت افزایش موجودی کیف پول لطفاً ابتدا وارد حساب کاربری خود شوید."
|
||
);
|
||
expect(api.post).not.toHaveBeenCalled();
|
||
});
|
||
|
||
it("validates minimum amount (at least 10,000 Tomans)", async () => {
|
||
render(<TopUpModal isOpen={true} onClose={mockOnClose} />);
|
||
|
||
const input = screen.getByPlaceholderText("۰");
|
||
fireEvent.change(input, { target: { value: "5000" } });
|
||
|
||
const submitBtn = screen.getByRole("button", { name: /اتصال به درگاه و شارژ آنلاین/ });
|
||
fireEvent.click(submitBtn);
|
||
|
||
expect(toast.error).toHaveBeenCalledWith("حداقل مبلغ شارژ ۱۰,۰۰۰ تومان است");
|
||
expect(api.post).not.toHaveBeenCalled();
|
||
});
|
||
|
||
it("submits payment request and redirects to gateway paymentUrl on success", async () => {
|
||
const originalLocation = window.location;
|
||
// mock window.location
|
||
delete (window as any).location;
|
||
window.location = { ...originalLocation, href: "" } as any;
|
||
|
||
vi.mocked(api.post).mockResolvedValueOnce({
|
||
data: {
|
||
success: true,
|
||
paymentUrl: "https://gateway.zibal.ir/start/12345678",
|
||
},
|
||
});
|
||
|
||
render(
|
||
<TopUpModal
|
||
isOpen={true}
|
||
onClose={mockOnClose}
|
||
initialAmount={500000}
|
||
/>
|
||
);
|
||
|
||
const submitBtn = screen.getByRole("button", { name: /اتصال به درگاه و شارژ آنلاین/ });
|
||
fireEvent.click(submitBtn);
|
||
|
||
await waitFor(() => {
|
||
expect(api.post).toHaveBeenCalledWith("/payment/zibal/wallet/initiate", {
|
||
amount: 500000,
|
||
});
|
||
expect(toast.success).toHaveBeenCalledWith("در حال انتقال امن به درگاه پرداخت زیبال...");
|
||
expect(window.location.href).toBe("https://gateway.zibal.ir/start/12345678");
|
||
});
|
||
|
||
window.location = originalLocation;
|
||
});
|
||
|
||
it("handles payment gateway initialization failure", async () => {
|
||
vi.mocked(api.post).mockResolvedValueOnce({
|
||
data: { success: false, paymentUrl: null },
|
||
});
|
||
|
||
render(
|
||
<TopUpModal
|
||
isOpen={true}
|
||
onClose={mockOnClose}
|
||
initialAmount={500000}
|
||
/>
|
||
);
|
||
|
||
const submitBtn = screen.getByRole("button", { name: /اتصال به درگاه و شارژ آنلاین/ });
|
||
fireEvent.click(submitBtn);
|
||
|
||
await waitFor(() => {
|
||
expect(toast.error).toHaveBeenCalledWith("خطا در ایجاد تراکنش درگاه پرداخت زیبال");
|
||
});
|
||
});
|
||
|
||
it("shows card-to-card account details when card-to-card setting is enabled", () => {
|
||
vi.mocked(useSettingsStore).mockImplementation((selector: any) => {
|
||
return selector({
|
||
getBoolean: vi.fn((key: string) => key === "payment_card_to_card_enabled"),
|
||
});
|
||
});
|
||
|
||
render(<TopUpModal isOpen={true} onClose={mockOnClose} />);
|
||
|
||
expect(screen.getByText("اطلاعات کارت جهت واریز کارتبهکارت:")).toBeInTheDocument();
|
||
expect(screen.getByText(/۶۲۱۹-۸۶۱۹-۷۴۰۱-۲۰۷۱/)).toBeInTheDocument();
|
||
expect(screen.getByText(/IR-65 0560 6118 2800 5725 1015 01/)).toBeInTheDocument();
|
||
});
|
||
|
||
it("calls onClose when close button is clicked", () => {
|
||
render(<TopUpModal isOpen={true} onClose={mockOnClose} />);
|
||
|
||
// Top X close button
|
||
const closeButtons = screen.getAllByRole("button");
|
||
const closeIconBtn = closeButtons.find(b => b.querySelector("svg.lucide-x"));
|
||
expect(closeIconBtn).toBeDefined();
|
||
|
||
fireEvent.click(closeIconBtn!);
|
||
expect(mockOnClose).toHaveBeenCalled();
|
||
});
|
||
});
|