133 lines
4.0 KiB
TypeScript
133 lines
4.0 KiB
TypeScript
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>(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" },
|
|
];
|
|
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);
|
|
});
|
|
|
|
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),
|
|
}),
|
|
}),
|
|
);
|
|
});
|
|
});
|
|
|
|
describe("findHomepage", () => {
|
|
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("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 } },
|
|
});
|
|
});
|
|
});
|
|
});
|