import { Test, TestingModule } from "@nestjs/testing"; import { BlogsService } from "./blogs.service"; import { PrismaService } from "../prisma/prisma.service"; import { NotFoundException } from "@nestjs/common"; describe("BlogsService", () => { let service: BlogsService; const mockPrisma = { blog: { findMany: jest.fn(), count: jest.fn(), findFirst: jest.fn(), update: jest.fn(), }, product: { findMany: jest.fn().mockResolvedValue([]), }, }; beforeEach(async () => { const module: TestingModule = await Test.createTestingModule({ providers: [ BlogsService, { provide: PrismaService, useValue: mockPrisma, }, ], }).compile(); service = module.get(BlogsService); jest.clearAllMocks(); }); it("should be defined", () => { expect(service).toBeDefined(); }); describe("findAll", () => { it("should query published blogs with pagination", async () => { const blogs = [ { id: "1", title: "Blog 1", isPublished: true, status: "PUBLISHED", author: null }, ]; mockPrisma.blog.findMany.mockResolvedValue(blogs); mockPrisma.blog.count.mockResolvedValue(1); const result = await service.findAll({ page: 1, limit: 10 }); expect(mockPrisma.blog.findMany).toHaveBeenCalled(); expect(result.data).toEqual(blogs); expect(result.meta.total).toBe(1); const query = mockPrisma.blog.findMany.mock.calls[0][0]; expect(query.include.author).toEqual({ select: { id: true, firstName: true, lastName: true } }); expect(query.orderBy).toEqual([{ publishedAt: "desc" }, { id: "asc" }]); }); it("should filter by categorySlug and search term", async () => { mockPrisma.blog.findMany.mockResolvedValue([]); mockPrisma.blog.count.mockResolvedValue(0); await service.findAll({ categorySlug: "nutrition", search: "dog" }); expect(mockPrisma.blog.findMany).toHaveBeenCalledWith( expect.objectContaining({ where: expect.objectContaining({ category: { slug: "nutrition" }, AND: expect.any(Array), }), }), ); }); }); it("selects the minimal public author identity for list results", async () => { mockPrisma.blog.findMany.mockResolvedValue([]); mockPrisma.blog.count.mockResolvedValue(0); await service.findAll({}); expect(mockPrisma.blog.findMany.mock.calls[0][0].include.author).toEqual({ select: { id: true, firstName: true, lastName: true } }); }); describe("findHomepage", () => { it("selects the minimal public author identity for homepage results", async () => { mockPrisma.blog.findMany.mockResolvedValueOnce([]).mockResolvedValueOnce([]); await service.findHomepage(); for (const call of mockPrisma.blog.findMany.mock.calls.slice(0, 2)) expect(call[0].include.author).toEqual({ select: { id: true, firstName: true, lastName: true } }); }); it("should return selected blogs if showOnHome posts exist", async () => { const selected = [{ id: "b1", title: "Hero Blog", showOnHome: true }]; mockPrisma.blog.findMany.mockResolvedValueOnce(selected); const result = await service.findHomepage(); expect(result.mode).toBe("selected"); expect(result.blogs).toEqual(selected); }); it("should fallback to newest blogs if no showOnHome posts exist", async () => { const newest = [{ id: "b2", title: "Newest Blog" }]; mockPrisma.blog.findMany .mockResolvedValueOnce([]) .mockResolvedValueOnce(newest); const result = await service.findHomepage(); expect(result.mode).toBe("newest"); expect(result.blogs).toEqual(newest); }); }); describe("findOneBySlug", () => { it("selects the minimal public author identity for detail results", async () => { mockPrisma.blog.findFirst.mockResolvedValue(null); await expect(service.findOneBySlug("missing")).rejects.toThrow(NotFoundException); expect(mockPrisma.blog.findFirst.mock.calls[0][0].include.author).toEqual({ select: { id: true, firstName: true, lastName: true } }); }); it("should throw NotFoundException if slug is empty or blog not found", async () => { mockPrisma.blog.findFirst.mockResolvedValue(null); await expect(service.findOneBySlug("unknown-slug")).rejects.toThrow( NotFoundException, ); }); it("should throw NotFoundException if blog is not published or draft", async () => { mockPrisma.blog.findFirst.mockResolvedValue({ id: "1", slug: "draft-post", isPublished: false, status: "DRAFT", }); await expect(service.findOneBySlug("draft-post")).rejects.toThrow( NotFoundException, ); }); it("should return published blog and increment view count asynchronously", async () => { const blog = { id: "1", slug: "dog-vitamins", isPublished: true, status: "PUBLISHED", publishedAt: new Date(Date.now() - 10000), relatedProductIds: [], }; mockPrisma.blog.findFirst.mockResolvedValue(blog); mockPrisma.blog.update.mockResolvedValue({}); const result = await service.findOneBySlug("dog-vitamins"); expect(result.id).toBe("1"); expect(mockPrisma.blog.update).toHaveBeenCalledWith({ where: { id: "1" }, data: { viewCount: { increment: 1 } }, }); }); }); });