- 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.
275 lines
10 KiB
TypeScript
275 lines
10 KiB
TypeScript
"use client";
|
|
import React from "react";
|
|
import { describe, it, expect, vi, beforeEach } from "vitest";
|
|
import { render, screen, fireEvent, waitFor } from "@testing-library/react";
|
|
import AuthModal from "../AuthModal";
|
|
import { authService } from "../../lib/services/authService";
|
|
import { useUserStore } from "../../lib/store/userStore";
|
|
import { useSettingsStore } from "../../lib/store/settingsStore";
|
|
import { toast } from "sonner";
|
|
|
|
vi.mock("../../lib/services/authService", () => ({
|
|
authService: {
|
|
sendOtp: vi.fn(),
|
|
verifyOtp: vi.fn(),
|
|
login: vi.fn(),
|
|
register: vi.fn(),
|
|
},
|
|
}));
|
|
|
|
vi.mock("../../lib/store/userStore", () => ({
|
|
useUserStore: vi.fn(),
|
|
}));
|
|
|
|
vi.mock("../../lib/store/settingsStore", () => ({
|
|
useSettingsStore: vi.fn((selector) => {
|
|
const state = {
|
|
getText: (_k: string, def: string) => def,
|
|
getBoolean: (_k: string, def: boolean = true) => def,
|
|
};
|
|
if (typeof selector === "function") {
|
|
return selector(state);
|
|
}
|
|
return state;
|
|
}),
|
|
}));
|
|
|
|
vi.mock("sonner", () => ({
|
|
toast: {
|
|
success: vi.fn(),
|
|
error: vi.fn(),
|
|
info: vi.fn(),
|
|
},
|
|
}));
|
|
|
|
// Mock OtpInput5
|
|
vi.mock("../OtpInput5", () => ({
|
|
default: ({
|
|
value,
|
|
onChange,
|
|
onComplete,
|
|
disabled,
|
|
}: {
|
|
value: string;
|
|
onChange: (val: string) => void;
|
|
onComplete?: (code: string) => void;
|
|
disabled?: boolean;
|
|
}) => (
|
|
<div data-testid="otp-input-container">
|
|
<input
|
|
data-testid="mock-otp-input"
|
|
type="text"
|
|
maxLength={5}
|
|
value={value}
|
|
disabled={disabled}
|
|
onChange={(e) => {
|
|
const val = e.target.value;
|
|
onChange(val);
|
|
if (val.length === 5 && onComplete) {
|
|
onComplete(val);
|
|
}
|
|
}}
|
|
/>
|
|
</div>
|
|
),
|
|
}));
|
|
|
|
describe("AuthModal Component", () => {
|
|
const mockOnClose = vi.fn();
|
|
const mockFetchProfile = vi.fn();
|
|
|
|
beforeEach(() => {
|
|
vi.clearAllMocks();
|
|
|
|
vi.mocked(useUserStore).mockReturnValue({
|
|
fetchProfile: mockFetchProfile,
|
|
isLoggedIn: false,
|
|
} as any);
|
|
});
|
|
|
|
it("does not render when isOpen is false", () => {
|
|
const { container } = render(<AuthModal isOpen={false} onClose={mockOnClose} />);
|
|
expect(container.firstChild).toBeNull();
|
|
});
|
|
|
|
it("renders modal heading and login OTP view when isOpen is true", () => {
|
|
render(<AuthModal isOpen={true} onClose={mockOnClose} />);
|
|
|
|
expect(screen.getByText("احراز هویت کنینا")).toBeInTheDocument();
|
|
expect(screen.getByPlaceholderText("۰۹۱۲۳۴۵۶۷۸۹")).toBeInTheDocument();
|
|
expect(screen.getByText("دریافت کد تایید و ورود")).toBeInTheDocument();
|
|
});
|
|
|
|
it("calls onClose when clicking close button or backdrop", () => {
|
|
const { container } = render(<AuthModal isOpen={true} onClose={mockOnClose} />);
|
|
|
|
// Click backdrop
|
|
const backdrop = container.querySelector(".fixed.inset-0.bg-medical-gray-900\\/60");
|
|
if (backdrop) {
|
|
fireEvent.click(backdrop);
|
|
expect(mockOnClose).toHaveBeenCalled();
|
|
}
|
|
|
|
mockOnClose.mockClear();
|
|
|
|
// Click close button (the round close button at header)
|
|
const closeBtn = container.querySelector("button.rounded-full");
|
|
expect(closeBtn).toBeInTheDocument();
|
|
if (closeBtn) {
|
|
fireEvent.click(closeBtn);
|
|
expect(mockOnClose).toHaveBeenCalled();
|
|
}
|
|
});
|
|
|
|
it("validates mobile number on OTP request submission", async () => {
|
|
render(<AuthModal isOpen={true} onClose={mockOnClose} />);
|
|
|
|
const phoneInput = screen.getByPlaceholderText("۰۹۱۲۳۴۵۶۷۸۹");
|
|
// Enter invalid number
|
|
fireEvent.change(phoneInput, { target: { value: "0912123" } });
|
|
|
|
// Try submitting
|
|
const submitBtn = screen.getByText("دریافت کد تایید و ورود").closest("button");
|
|
expect(submitBtn).toBeDisabled();
|
|
});
|
|
|
|
it("sends OTP and navigates to OTP verification step on valid phone submission", async () => {
|
|
vi.mocked(authService.sendOtp).mockResolvedValue({ success: true } as any);
|
|
|
|
render(<AuthModal isOpen={true} onClose={mockOnClose} />);
|
|
|
|
const phoneInput = screen.getByPlaceholderText("۰۹۱۲۳۴۵۶۷۸۹");
|
|
fireEvent.change(phoneInput, { target: { value: "09123456789" } });
|
|
|
|
const submitBtn = screen.getByText("دریافت کد تایید و ورود").closest("button")!;
|
|
expect(submitBtn).not.toBeDisabled();
|
|
fireEvent.click(submitBtn);
|
|
|
|
await waitFor(() => {
|
|
expect(authService.sendOtp).toHaveBeenCalledWith("09123456789");
|
|
expect(toast.success).toHaveBeenCalledWith("کد تایید با موفقیت پیامک شد");
|
|
});
|
|
|
|
// Subview changed to otp-verify
|
|
await waitFor(() => {
|
|
expect(screen.getByText("تایید شماره تلفن")).toBeInTheDocument();
|
|
expect(screen.getByTestId("mock-otp-input")).toBeInTheDocument();
|
|
});
|
|
});
|
|
|
|
it("allows navigating back to phone edit from OTP step", async () => {
|
|
vi.mocked(authService.sendOtp).mockResolvedValue({ success: true } as any);
|
|
|
|
render(<AuthModal isOpen={true} onClose={mockOnClose} />);
|
|
|
|
const phoneInput = screen.getByPlaceholderText("۰۹۱۲۳۴۵۶۷۸۹");
|
|
fireEvent.change(phoneInput, { target: { value: "09123456789" } });
|
|
fireEvent.click(screen.getByText("دریافت کد تایید و ورود").closest("button")!);
|
|
|
|
await waitFor(() => {
|
|
expect(screen.getByText("تایید شماره تلفن")).toBeInTheDocument();
|
|
});
|
|
|
|
// Click back button inside OTP view
|
|
const backBtn = screen.getByText("تایید شماره تلفن").previousSibling as HTMLElement;
|
|
fireEvent.click(backBtn);
|
|
|
|
// Should return to main login view
|
|
await waitFor(() => {
|
|
expect(screen.getByPlaceholderText("۰۹۱۲۳۴۵۶۷۸۹")).toBeInTheDocument();
|
|
});
|
|
});
|
|
|
|
it("verifies OTP and completes login on 5-digit code entry", async () => {
|
|
vi.mocked(authService.sendOtp).mockResolvedValue({ success: true } as any);
|
|
vi.mocked(authService.verifyOtp).mockResolvedValue({ success: true, token: "jwt-123" } as any);
|
|
|
|
render(<AuthModal isOpen={true} onClose={mockOnClose} />);
|
|
|
|
// Step 1: Request OTP
|
|
const phoneInput = screen.getByPlaceholderText("۰۹۱۲۳۴۵۶۷۸۹");
|
|
fireEvent.change(phoneInput, { target: { value: "09123456789" } });
|
|
fireEvent.click(screen.getByText("دریافت کد تایید و ورود").closest("button")!);
|
|
|
|
await waitFor(() => {
|
|
expect(screen.getByTestId("mock-otp-input")).toBeInTheDocument();
|
|
});
|
|
|
|
// Step 2: Enter 5 digit code
|
|
const otpInput = screen.getByTestId("mock-otp-input");
|
|
fireEvent.change(otpInput, { target: { value: "12345" } });
|
|
|
|
await waitFor(() => {
|
|
expect(authService.verifyOtp).toHaveBeenCalledWith("09123456789", "12345");
|
|
expect(mockFetchProfile).toHaveBeenCalled();
|
|
expect(toast.success).toHaveBeenCalledWith("ورود با موفقیت انجام شد");
|
|
expect(mockOnClose).toHaveBeenCalled();
|
|
});
|
|
});
|
|
|
|
it("keeps phone entry retryable after OTP delivery fails", async () => {
|
|
vi.mocked(authService.sendOtp).mockRejectedValueOnce(new Error('SMS unavailable'));
|
|
render(<AuthModal isOpen={true} onClose={mockOnClose} />);
|
|
fireEvent.change(screen.getByPlaceholderText('۰۹۱۲۳۴۵۶۷۸۹'), { target: { value: '09128887766' } });
|
|
fireEvent.click(screen.getByText('دریافت کد تایید و ورود'));
|
|
await waitFor(() => expect(toast.error).toHaveBeenCalledWith('SMS unavailable'));
|
|
expect(screen.queryByTestId('mock-otp-input')).not.toBeInTheDocument();
|
|
expect(screen.getByText('دریافت کد تایید و ورود').closest('button')).not.toBeDisabled();
|
|
expect(mockFetchProfile).not.toHaveBeenCalled();
|
|
expect(mockOnClose).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("allows corrected OTP after verification rejection", async () => {
|
|
vi.mocked(authService.sendOtp).mockResolvedValueOnce({ success: true } as never);
|
|
vi.mocked(authService.verifyOtp).mockRejectedValueOnce(new Error('Invalid code')).mockResolvedValueOnce({ success: true } as never);
|
|
render(<AuthModal isOpen={true} onClose={mockOnClose} />);
|
|
fireEvent.change(screen.getByPlaceholderText('۰۹۱۲۳۴۵۶۷۸۹'), { target: { value: '09127776655' } });
|
|
fireEvent.click(screen.getByText('دریافت کد تایید و ورود'));
|
|
const input = await screen.findByTestId('mock-otp-input');
|
|
fireEvent.change(input, { target: { value: '00000' } });
|
|
await waitFor(() => expect(toast.error).toHaveBeenCalledWith('Invalid code'));
|
|
expect(mockOnClose).not.toHaveBeenCalled();
|
|
expect(mockFetchProfile).not.toHaveBeenCalled();
|
|
expect(input).not.toBeDisabled();
|
|
fireEvent.change(input, { target: { value: '54321' } });
|
|
await waitFor(() => expect(mockOnClose).toHaveBeenCalledTimes(1));
|
|
expect(authService.verifyOtp).toHaveBeenLastCalledWith('09127776655', '54321');
|
|
});
|
|
|
|
it("handles countdown timer and displays remaining cooldown seconds", async () => {
|
|
vi.mocked(authService.sendOtp).mockResolvedValue({ success: true } as any);
|
|
|
|
render(<AuthModal isOpen={true} onClose={mockOnClose} />);
|
|
|
|
const phoneInput = screen.getByPlaceholderText("۰۹۱۲۳۴۵۶۷۸۹");
|
|
fireEvent.change(phoneInput, { target: { value: "09120000001" } });
|
|
const btn = screen.getByText("دریافت کد تایید و ورود").closest("button")!;
|
|
fireEvent.click(btn);
|
|
|
|
await waitFor(() => {
|
|
expect(screen.getByText(/امکان ارسال مجدد کد تا/)).toBeInTheDocument();
|
|
});
|
|
});
|
|
|
|
it("supports switching between Login and Register tabs", async () => {
|
|
render(<AuthModal isOpen={true} onClose={mockOnClose} />);
|
|
|
|
// Switch to Register tab
|
|
const registerTab = screen.getByText("ثبتنام کاربر جدید");
|
|
fireEvent.click(registerTab);
|
|
|
|
await waitFor(() => {
|
|
expect(screen.getByPlaceholderText("نام...")).toBeInTheDocument();
|
|
expect(screen.getByPlaceholderText("نام خانوادگی...")).toBeInTheDocument();
|
|
});
|
|
|
|
// Switch back to Login tab
|
|
const loginTab = screen.getByText("ورود به حساب");
|
|
fireEvent.click(loginTab);
|
|
|
|
await waitFor(() => {
|
|
expect(screen.getByText("دریافت کد تایید و ورود")).toBeInTheDocument();
|
|
});
|
|
});
|
|
});
|