Files
canina/frontend/application/components/__tests__/LoginModal.test.tsx
T
devops ddbf1ca026
Deploy Canina / deploy (push) Successful in 4m1s
E2E Playwright Tests / Run Full E2E & Security Suites (push) Failing after 7s
test: comprehensive test suites and quality gates pass across backend, storefront and admin
- 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.
2026-10-13 16:01:00 +00:00

230 lines
7.4 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 LoginModal from "../LoginModal";
import { authService } from "../../lib/services/authService";
import { useUserStore } from "../../lib/store/userStore";
import { toast } from "sonner";
vi.mock("../../lib/services/authService", () => ({
authService: {
sendOtp: vi.fn(),
verifyOtp: vi.fn(),
},
}));
vi.mock("../../lib/store/userStore", () => ({
useUserStore: vi.fn(),
}));
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("LoginModal Component", () => {
const mockOnClose = vi.fn();
const mockOnLogin = 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(
<LoginModal isOpen={false} onClose={mockOnClose} onLogin={mockOnLogin} />
);
expect(container.firstChild).toBeNull();
});
it("renders phone input step initially", () => {
render(
<LoginModal isOpen={true} onClose={mockOnClose} onLogin={mockOnLogin} />
);
expect(screen.getByText("ورود به کنینا")).toBeInTheDocument();
expect(screen.getByPlaceholderText("مثال: ۰۹۱۲۳۴۵۶۷۸۹")).toBeInTheDocument();
expect(screen.getByRole("button", { name: /ارسال کد تایید/ })).toBeDisabled();
});
it("validates mobile number prefix and length (09xxxxxxxxx)", async () => {
render(
<LoginModal isOpen={true} onClose={mockOnClose} onLogin={mockOnLogin} />
);
const input = screen.getByPlaceholderText("مثال: ۰۹۱۲۳۴۵۶۷۸۹");
fireEvent.change(input, { target: { value: "02112345678" } });
const submitBtn = screen.getByRole("button", { name: /ارسال کد تایید/ });
fireEvent.click(submitBtn);
expect(toast.error).toHaveBeenCalledWith(
"شماره موبایل باید با ۰۹ شروع شده و ۱۱ رقم باشد"
);
expect(authService.sendOtp).not.toHaveBeenCalled();
});
it("sends OTP and transitions to OTP verification step", async () => {
vi.mocked(authService.sendOtp).mockResolvedValueOnce({
success: true,
message: "کد ارسال شد",
} as any);
render(
<LoginModal isOpen={true} onClose={mockOnClose} onLogin={mockOnLogin} />
);
const input = screen.getByPlaceholderText("مثال: ۰۹۱۲۳۴۵۶۷۸۹");
fireEvent.change(input, { target: { value: "09129998877" } });
const submitBtn = screen.getByRole("button", { name: /ارسال کد تایید/ });
expect(submitBtn).not.toBeDisabled();
fireEvent.click(submitBtn);
await waitFor(() => {
expect(authService.sendOtp).toHaveBeenCalledWith("09129998877");
expect(toast.success).toHaveBeenCalledWith("کد تایید پیامک شد");
expect(screen.getByTestId("otp-input-container")).toBeInTheDocument();
expect(screen.getByText("کد تایید پیامکی")).toBeInTheDocument();
});
});
it("allows switching back to phone step via 'ویرایش شماره'", async () => {
vi.mocked(authService.sendOtp).mockResolvedValueOnce({ success: true } as any);
render(
<LoginModal isOpen={true} onClose={mockOnClose} onLogin={mockOnLogin} />
);
const input = screen.getByPlaceholderText("مثال: ۰۹۱۲۳۴۵۶۷۸۹");
fireEvent.change(input, { target: { value: "09129998877" } });
fireEvent.click(screen.getByRole("button", { name: /ارسال کد تایید/ }));
await waitFor(() => {
expect(screen.getByText("ویرایش شماره")).toBeInTheDocument();
});
fireEvent.click(screen.getByText("ویرایش شماره"));
await waitFor(() => {
expect(screen.getByPlaceholderText("مثال: ۰۹۱۲۳۴۵۶۷۸۹")).toBeInTheDocument();
});
});
it("completes OTP verification and invokes onLogin and onClose on success", async () => {
vi.mocked(authService.sendOtp).mockResolvedValueOnce({ success: true } as any);
vi.mocked(authService.verifyOtp).mockResolvedValueOnce({
success: true,
token: "jwt-token-123",
} as any);
render(
<LoginModal isOpen={true} onClose={mockOnClose} onLogin={mockOnLogin} />
);
const input = screen.getByPlaceholderText("مثال: ۰۹۱۲۳۴۵۶۷۸۹");
fireEvent.change(input, { target: { value: "09129998877" } });
fireEvent.click(screen.getByRole("button", { name: /ارسال کد تایید/ }));
await waitFor(() => {
expect(screen.getByTestId("mock-otp-input")).toBeInTheDocument();
});
const otpInput = screen.getByTestId("mock-otp-input");
fireEvent.change(otpInput, { target: { value: "12345" } });
await waitFor(() => {
expect(authService.verifyOtp).toHaveBeenCalledWith("09129998877", "12345");
expect(mockFetchProfile).toHaveBeenCalled();
expect(toast.success).toHaveBeenCalledWith("ورود با موفقیت انجام شد");
expect(mockOnLogin).toHaveBeenCalled();
expect(mockOnClose).toHaveBeenCalled();
});
});
it("handles OTP verification error gracefully", async () => {
vi.mocked(authService.sendOtp).mockResolvedValueOnce({ success: true } as any);
vi.mocked(authService.verifyOtp).mockRejectedValueOnce({
message: "کد وارد شده منقضی شده است",
});
render(
<LoginModal isOpen={true} onClose={mockOnClose} onLogin={mockOnLogin} />
);
const input = screen.getByPlaceholderText("مثال: ۰۹۱۲۳۴۵۶۷۸۹");
fireEvent.change(input, { target: { value: "09129998877" } });
fireEvent.click(screen.getByRole("button", { name: /ارسال کد تایید/ }));
await waitFor(() => {
expect(screen.getByTestId("mock-otp-input")).toBeInTheDocument();
});
const otpInput = screen.getByTestId("mock-otp-input");
fireEvent.change(otpInput, { target: { value: "99999" } });
await waitFor(() => {
expect(authService.verifyOtp).toHaveBeenCalledWith("09129998877", "99999");
expect(toast.error).toHaveBeenCalledWith("کد وارد شده منقضی شده است");
expect(mockOnLogin).not.toHaveBeenCalled();
});
});
it("renders pet contextual message when isAdvisorContext is true", () => {
render(
<LoginModal
isOpen={true}
onClose={mockOnClose}
onLogin={mockOnLogin}
isAdvisorContext={true}
petName="ژوپیتر"
/>
);
expect(screen.getByText("ژوپیتر")).toBeInTheDocument();
expect(screen.getByText(/رژیم مکمل پیشنهادی/)).toBeInTheDocument();
});
});