Files
canina/backend/src/seo/seo.controller.spec.ts
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

134 lines
3.9 KiB
TypeScript

import { Test, TestingModule } from "@nestjs/testing";
import { SeoController } from "./seo.controller";
import { SeoService } from "./seo.service";
import { NotFoundException } from "@nestjs/common";
describe("SeoController", () => {
let controller: SeoController;
let service: SeoService;
const mockSeoService = {
getSitemapUrls: jest.fn(),
getProductSchema: jest.fn(),
};
beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
controllers: [SeoController],
providers: [
{
provide: SeoService,
useValue: mockSeoService,
},
],
}).compile();
controller = module.get<SeoController>(SeoController);
service = module.get<SeoService>(SeoService);
});
afterEach(() => {
jest.clearAllMocks();
});
it("should be defined", () => {
expect(controller).toBeDefined();
expect(service).toBeDefined();
});
describe("getSitemap", () => {
it("should return sitemap URL list successfully", async () => {
const mockUrls = [
{
loc: "https://canina.ir/",
lastmod: "2026-10-10",
changefreq: "daily",
priority: 1.0,
},
{
loc: "https://canina.ir/products",
lastmod: "2026-10-11",
changefreq: "daily",
priority: 0.9,
},
{
loc: "https://canina.ir/products/canhydrox-gag",
lastmod: "2026-10-12",
changefreq: "weekly",
priority: 0.8,
},
];
mockSeoService.getSitemapUrls.mockResolvedValue(mockUrls);
const result = await controller.getSitemap();
expect(service.getSitemapUrls).toHaveBeenCalledTimes(1);
expect(result).toEqual(mockUrls);
expect(result).toHaveLength(3);
});
it("should propagate errors from seoService.getSitemapUrls", async () => {
mockSeoService.getSitemapUrls.mockRejectedValue(
new Error("Redis connection dropped"),
);
await expect(controller.getSitemap()).rejects.toThrow(
"Redis connection dropped",
);
expect(service.getSitemapUrls).toHaveBeenCalledTimes(1);
});
});
describe("getProductSchema", () => {
it("should return product JSON-LD schema when found by id or slug", async () => {
const mockSchema = {
"@context": "https://schema.org",
"@type": "Product",
name: "کانین هیدروکس گگ",
image: "https://canina.ir/images/canhydrox.jpg",
description: "مکمل غذایی تخصصی مفاصل و استخوان سگ",
sku: "CAN-HYDROX-100",
offers: {
"@type": "Offer",
priceCurrency: "IRR",
price: "25000000",
availability: "https://schema.org/InStock",
},
};
mockSeoService.getProductSchema.mockResolvedValue(mockSchema);
const result = await controller.getProductSchema("canina-canhydrox-gag");
expect(service.getProductSchema).toHaveBeenCalledWith(
"canina-canhydrox-gag",
);
expect(result).toEqual(mockSchema);
expect(result["@type"]).toBe("Product");
});
it("should throw NotFoundException when product schema is null or not found", async () => {
mockSeoService.getProductSchema.mockResolvedValue(null);
await expect(
controller.getProductSchema("non-existent-product-slug"),
).rejects.toThrow(NotFoundException);
expect(service.getProductSchema).toHaveBeenCalledWith(
"non-existent-product-slug",
);
});
it("should throw NotFoundException when product schema is undefined", async () => {
mockSeoService.getProductSchema.mockResolvedValue(undefined);
await expect(controller.getProductSchema("unknown-id")).rejects.toThrow(
new NotFoundException("Product for schema not found"),
);
expect(service.getProductSchema).toHaveBeenCalledWith("unknown-id");
});
});
});