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); service = module.get(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"); }); }); });