- 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.
118 lines
3.7 KiB
TypeScript
118 lines
3.7 KiB
TypeScript
import { Test, TestingModule } from "@nestjs/testing";
|
|
import { SeoService } from "./seo.service";
|
|
import { PrismaService } from "../prisma/prisma.service";
|
|
|
|
describe("SeoService", () => {
|
|
let service: SeoService;
|
|
|
|
const mockPrisma = {
|
|
product: {
|
|
findMany: jest.fn(),
|
|
findFirst: jest.fn(),
|
|
},
|
|
category: {
|
|
findMany: jest.fn(),
|
|
},
|
|
blog: {
|
|
findMany: jest.fn(),
|
|
},
|
|
};
|
|
|
|
beforeEach(async () => {
|
|
const module: TestingModule = await Test.createTestingModule({
|
|
providers: [SeoService, { provide: PrismaService, useValue: mockPrisma }],
|
|
}).compile();
|
|
|
|
service = module.get<SeoService>(SeoService);
|
|
jest.clearAllMocks();
|
|
});
|
|
|
|
it("should be defined", () => {
|
|
expect(service).toBeDefined();
|
|
});
|
|
|
|
describe("getSitemapUrls", () => {
|
|
it("should aggregate sitemap URLs for products, categories, and published blogs", async () => {
|
|
const now = new Date("2026-10-10T12:00:00Z");
|
|
mockPrisma.product.findMany.mockResolvedValue([
|
|
{ slug: "canivita-tonic", createdAt: now },
|
|
]);
|
|
mockPrisma.category.findMany.mockResolvedValue([
|
|
{ slug: "vitamins", createdAt: now },
|
|
]);
|
|
mockPrisma.blog.findMany.mockResolvedValue([
|
|
{ slug: "dog-nutrition-guide", updatedAt: now },
|
|
]);
|
|
|
|
const sitemap = await service.getSitemapUrls();
|
|
|
|
expect(sitemap.products).toEqual([
|
|
{ url: "/shop/canivita-tonic", lastmod: now },
|
|
]);
|
|
expect(sitemap.categories).toEqual([
|
|
{ url: "/shop?category=vitamins", lastmod: now },
|
|
]);
|
|
expect(sitemap.blogs).toEqual([
|
|
{ url: "/blog/dog-nutrition-guide", lastmod: now },
|
|
]);
|
|
expect(mockPrisma.blog.findMany).toHaveBeenCalledWith({
|
|
where: { isPublished: true },
|
|
select: { slug: true, updatedAt: true },
|
|
});
|
|
});
|
|
});
|
|
|
|
describe("getProductSchema", () => {
|
|
it("should return valid JSON-LD schema by slug", async () => {
|
|
mockPrisma.product.findFirst.mockResolvedValue({
|
|
id: "p-1",
|
|
slug: "petvital-energy",
|
|
nameFa: "انرژی پتویتال",
|
|
nameEn: "Petvital Energy",
|
|
imageUrl: "https://canina.ir/img/energy.jpg",
|
|
description: "مکمل تقویتی سگ و گربه",
|
|
artNo: "ART-1002",
|
|
barcode: "626000000123",
|
|
priceValue: 850000,
|
|
});
|
|
|
|
const schema = await service.getProductSchema("petvital-energy");
|
|
|
|
expect(schema).toBeDefined();
|
|
expect(schema!["@context"]).toBe("https://schema.org/");
|
|
expect(schema!["@type"]).toBe("Product");
|
|
expect(schema!.name).toBe("انرژی پتویتال");
|
|
expect(schema!.sku).toBe("ART-1002");
|
|
expect(schema!.gtin).toBe("626000000123");
|
|
expect(schema!.offers.price).toBe(8500000); // 850,000 Toman * 10 = Rial
|
|
expect(schema!.offers.priceCurrency).toBe("IRR");
|
|
expect(mockPrisma.product.findFirst).toHaveBeenCalledWith({
|
|
where: { slug: "petvital-energy" },
|
|
});
|
|
});
|
|
|
|
it("should query by UUID if idOrSlug matches UUID regex format", async () => {
|
|
const validUuid = "12345678-1234-1234-1234-123456789abc";
|
|
mockPrisma.product.findFirst.mockResolvedValue({
|
|
id: validUuid,
|
|
slug: "petvital-energy",
|
|
nameFa: "انرژی پتویتال",
|
|
priceValue: 500000,
|
|
});
|
|
|
|
await service.getProductSchema(validUuid);
|
|
|
|
expect(mockPrisma.product.findFirst).toHaveBeenCalledWith({
|
|
where: { id: validUuid },
|
|
});
|
|
});
|
|
|
|
it("should return null if product is not found", async () => {
|
|
mockPrisma.product.findFirst.mockResolvedValue(null);
|
|
|
|
const res = await service.getProductSchema("non-existent");
|
|
expect(res).toBeNull();
|
|
});
|
|
});
|
|
});
|