- 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.
137 lines
4.3 KiB
TypeScript
137 lines
4.3 KiB
TypeScript
"use client";
|
|
import { describe, it, expect, vi, beforeEach } from "vitest";
|
|
import { render, screen, waitFor } from "@testing-library/react";
|
|
import React from "react";
|
|
import AddressModal, { validateAddressForm, normalizeDigits } from "../AddressModal";
|
|
|
|
describe("AddressModal & Address Validation", () => {
|
|
beforeEach(() => {
|
|
vi.clearAllMocks();
|
|
});
|
|
|
|
describe("normalizeDigits", () => {
|
|
it("should correctly convert Persian and Arabic digits to English digits", () => {
|
|
expect(normalizeDigits("۰۹۱۲۳۴۵۶۷۸۹")).toBe("09123456789");
|
|
expect(normalizeDigits("٠١٢٣٤٥٦٧٨٩")).toBe("0123456789");
|
|
expect(normalizeDigits("12345")).toBe("12345");
|
|
expect(normalizeDigits("")).toBe("");
|
|
});
|
|
});
|
|
|
|
describe("validateAddressForm", () => {
|
|
it("should return errors for missing required fields", () => {
|
|
const emptyForm = {
|
|
title: "",
|
|
receptorName: "",
|
|
phone: "",
|
|
province: "",
|
|
city: "",
|
|
detail: "",
|
|
zipCode: "",
|
|
isDefault: false,
|
|
};
|
|
|
|
const errors = validateAddressForm(emptyForm);
|
|
expect(errors.title).toBeDefined();
|
|
expect(errors.receptorName).toBeDefined();
|
|
expect(errors.phone).toBeDefined();
|
|
expect(errors.province).toBeDefined();
|
|
expect(errors.city).toBeDefined();
|
|
expect(errors.detail).toBeDefined();
|
|
expect(errors.zipCode).toBeDefined();
|
|
});
|
|
|
|
it("should validate phone format strictly (09xxxxxxxxx)", () => {
|
|
const invalidPhoneForm = {
|
|
title: "خانه",
|
|
receptorName: "علی رضایی",
|
|
phone: "02112345678",
|
|
province: "تهران",
|
|
city: "تهران",
|
|
detail: "خیابان آزادی",
|
|
zipCode: "1234567890",
|
|
isDefault: false,
|
|
};
|
|
|
|
const errors = validateAddressForm(invalidPhoneForm);
|
|
expect(errors.phone).toContain("معتبر نیست");
|
|
});
|
|
|
|
it("should validate zip code format strictly (10 digits)", () => {
|
|
const invalidZipForm = {
|
|
title: "خانه",
|
|
receptorName: "علی رضایی",
|
|
phone: "09123456789",
|
|
province: "تهران",
|
|
city: "تهران",
|
|
detail: "خیابان آزادی",
|
|
zipCode: "12345", // not 10 digits
|
|
isDefault: false,
|
|
};
|
|
|
|
const errors = validateAddressForm(invalidZipForm);
|
|
expect(errors.zipCode).toContain("۱۰ رقم");
|
|
});
|
|
|
|
it("should pass validation with valid data including Persian digits", () => {
|
|
const validForm = {
|
|
title: "دفتر",
|
|
receptorName: "سارا امینی",
|
|
phone: "۰۹۱۲۳۴۵۶۷۸۹",
|
|
province: "تهران",
|
|
city: "تهران",
|
|
detail: "خیابان انقلاب پلاک ۱۰",
|
|
zipCode: "۱۲۳۴۵۶۷۸۹۰",
|
|
isDefault: true,
|
|
};
|
|
|
|
const errors = validateAddressForm(validForm);
|
|
expect(Object.keys(errors).length).toBe(0);
|
|
});
|
|
});
|
|
|
|
describe("AddressModal Component Rendering & Form Submission", () => {
|
|
it("should render modal form when isOpen is true", () => {
|
|
render(
|
|
<AddressModal
|
|
isOpen={true}
|
|
onClose={vi.fn()}
|
|
onSave={vi.fn()}
|
|
/>
|
|
);
|
|
|
|
expect(screen.getByText("افزودن آدرس جدید")).toBeInTheDocument();
|
|
expect(screen.getByPlaceholderText("مثال: منزل اصلی")).toBeInTheDocument();
|
|
});
|
|
|
|
it("should populate fields when editing an existing address", async () => {
|
|
const existingAddress = {
|
|
id: "addr-1",
|
|
title: "منزل پدری",
|
|
receptorName: "رضا محمدی",
|
|
phone: "09129876543",
|
|
province: "تهران",
|
|
city: "تهران",
|
|
detail: "خیابان بهار کوچه دوم",
|
|
zipCode: "9876543210",
|
|
isDefault: true,
|
|
};
|
|
|
|
render(
|
|
<AddressModal
|
|
isOpen={true}
|
|
onClose={vi.fn()}
|
|
onSave={vi.fn()}
|
|
editingAddress={existingAddress}
|
|
/>
|
|
);
|
|
|
|
expect(screen.getByText("ویرایش آدرس ارسال")).toBeInTheDocument();
|
|
await waitFor(() => {
|
|
expect(screen.getByDisplayValue("منزل پدری")).toBeInTheDocument();
|
|
expect(screen.getByDisplayValue("رضا محمدی")).toBeInTheDocument();
|
|
});
|
|
});
|
|
});
|
|
});
|