717 lines
25 KiB
TypeScript
717 lines
25 KiB
TypeScript
import { Test, TestingModule } from "@nestjs/testing";
|
|
import { BlogsService } from "./blogs.service";
|
|
import { PrismaService } from "../prisma/prisma.service";
|
|
import { RevalidationService } from "../common/revalidation/revalidation.service";
|
|
import { NotFoundException } from "@nestjs/common";
|
|
|
|
describe("Admin BlogsService", () => {
|
|
let service: BlogsService;
|
|
|
|
const mockPrisma = {
|
|
blog: {
|
|
findMany: jest.fn(),
|
|
count: jest.fn(),
|
|
findUnique: jest.fn(),
|
|
create: jest.fn(),
|
|
update: jest.fn(),
|
|
delete: jest.fn(),
|
|
},
|
|
blogCategory: {
|
|
upsert: jest.fn(),
|
|
findMany: jest.fn(),
|
|
create: jest.fn(),
|
|
update: jest.fn(),
|
|
delete: jest.fn(),
|
|
},
|
|
blogTag: {
|
|
findMany: jest.fn(),
|
|
create: jest.fn(),
|
|
delete: jest.fn(),
|
|
},
|
|
blogComment: {
|
|
findMany: jest.fn(),
|
|
count: jest.fn(),
|
|
findUnique: jest.fn(),
|
|
update: jest.fn(),
|
|
delete: jest.fn(),
|
|
},
|
|
user: {
|
|
findMany: jest.fn(),
|
|
},
|
|
};
|
|
|
|
const mockRevalidationService = {
|
|
revalidateBlog: jest.fn().mockResolvedValue(true),
|
|
};
|
|
|
|
beforeEach(async () => {
|
|
const module: TestingModule = await Test.createTestingModule({
|
|
providers: [
|
|
BlogsService,
|
|
{ provide: PrismaService, useValue: mockPrisma },
|
|
{ provide: RevalidationService, useValue: mockRevalidationService },
|
|
],
|
|
}).compile();
|
|
|
|
service = module.get<BlogsService>(BlogsService);
|
|
mockRevalidationService.revalidateBlog.mockReset();
|
|
mockRevalidationService.revalidateBlog.mockResolvedValue(true);
|
|
jest.clearAllMocks();
|
|
});
|
|
|
|
afterEach(() => {
|
|
service.onModuleDestroy();
|
|
});
|
|
|
|
it("should be defined", () => {
|
|
expect(service).toBeDefined();
|
|
});
|
|
|
|
describe("onModuleInit and seedDefaultCategories", () => {
|
|
it("should seed default categories and set interval timer", async () => {
|
|
mockPrisma.blogCategory.upsert.mockResolvedValue({});
|
|
mockPrisma.blog.findMany.mockResolvedValue([]);
|
|
|
|
await service.onModuleInit();
|
|
|
|
expect(mockPrisma.blogCategory.upsert).toHaveBeenCalledTimes(5);
|
|
expect(mockPrisma.blog.findMany).toHaveBeenCalledWith({
|
|
where: {
|
|
status: "SCHEDULED",
|
|
scheduledAt: { lte: expect.any(Date) },
|
|
},
|
|
select: {
|
|
id: true,
|
|
slug: true,
|
|
title: true,
|
|
scheduledAt: true,
|
|
},
|
|
});
|
|
});
|
|
});
|
|
|
|
describe("getBlogs", () => {
|
|
it("should return paginated blogs without filters", async () => {
|
|
const mockBlogs = [{ id: "b1", title: "Post 1" }];
|
|
mockPrisma.blog.findMany.mockResolvedValue(mockBlogs);
|
|
mockPrisma.blog.count.mockResolvedValue(1);
|
|
|
|
const result = await service.getBlogs({});
|
|
|
|
expect(mockPrisma.blog.findMany).toHaveBeenCalledWith(
|
|
expect.objectContaining({
|
|
skip: 0,
|
|
take: 10,
|
|
where: {},
|
|
}),
|
|
);
|
|
expect(result).toEqual({
|
|
data: mockBlogs,
|
|
meta: { total: 1, page: 1, limit: 10, lastPage: 1 },
|
|
});
|
|
});
|
|
|
|
it("should apply search, status, and categoryId filters", async () => {
|
|
mockPrisma.blog.findMany.mockResolvedValue([]);
|
|
mockPrisma.blog.count.mockResolvedValue(0);
|
|
|
|
await service.getBlogs({
|
|
page: 2,
|
|
limit: 5,
|
|
search: "canine",
|
|
status: "PUBLISHED",
|
|
categoryId: "cat-1",
|
|
});
|
|
|
|
expect(mockPrisma.blog.findMany).toHaveBeenCalledWith(
|
|
expect.objectContaining({
|
|
skip: 5,
|
|
take: 5,
|
|
where: {
|
|
OR: [
|
|
{ title: { contains: "canine", mode: "insensitive" } },
|
|
{ excerpt: { contains: "canine", mode: "insensitive" } },
|
|
{ slug: { contains: "canine", mode: "insensitive" } },
|
|
],
|
|
status: "PUBLISHED",
|
|
categoryId: "cat-1",
|
|
},
|
|
}),
|
|
);
|
|
});
|
|
});
|
|
|
|
describe("getBlogById", () => {
|
|
it("should throw NotFoundException if blog not found", async () => {
|
|
mockPrisma.blog.findUnique.mockResolvedValue(null);
|
|
|
|
await expect(service.getBlogById("unknown-id")).rejects.toThrow(
|
|
NotFoundException,
|
|
);
|
|
});
|
|
|
|
it("should return blog if found", async () => {
|
|
const mockBlog = { id: "b1", title: "Test Blog" };
|
|
mockPrisma.blog.findUnique.mockResolvedValue(mockBlog);
|
|
|
|
const result = await service.getBlogById("b1");
|
|
expect(result).toEqual(mockBlog);
|
|
});
|
|
});
|
|
|
|
describe("createBlog", () => {
|
|
it("should create an immediately published blog with tags and trigger revalidation", async () => {
|
|
const blogData = {
|
|
title: "New Health Post",
|
|
slug: "new-health-post",
|
|
content: "Content here",
|
|
tagNames: ["تغذیه سالم", "Dog Food"],
|
|
categoryId: "cat-1",
|
|
readingTime: 5,
|
|
};
|
|
|
|
const createdBlog = { id: "blog-1", ...blogData };
|
|
mockPrisma.blog.create.mockResolvedValue(createdBlog);
|
|
|
|
const result = await service.createBlog(blogData, "user-admin-1");
|
|
|
|
expect(mockPrisma.blog.create).toHaveBeenCalledWith(
|
|
expect.objectContaining({
|
|
data: expect.objectContaining({
|
|
title: "New Health Post",
|
|
status: "PUBLISHED",
|
|
isPublished: true,
|
|
author: { connect: { id: "user-admin-1" } },
|
|
category: { connect: { id: "cat-1" } },
|
|
readingTime: 5,
|
|
tags: {
|
|
connectOrCreate: [
|
|
{
|
|
where: { name: "تغذیه سالم" },
|
|
create: { name: "تغذیه سالم", slug: "تغذیه-سالم" },
|
|
},
|
|
{
|
|
where: { name: "Dog Food" },
|
|
create: { name: "Dog Food", slug: "dog-food" },
|
|
},
|
|
],
|
|
},
|
|
}),
|
|
}),
|
|
);
|
|
expect(mockRevalidationService.revalidateBlog).toHaveBeenCalledWith(
|
|
"new-health-post",
|
|
);
|
|
expect(result).toEqual({
|
|
...createdBlog,
|
|
cacheRevalidation: "success",
|
|
});
|
|
});
|
|
|
|
it("allowlists create fields and blocks server-owned and unknown fields", async () => {
|
|
mockPrisma.blog.create.mockResolvedValue({ id: "created", slug: "safe-post" });
|
|
|
|
await service.createBlog(
|
|
{
|
|
title: "Safe post",
|
|
slug: "safe-post",
|
|
content: "Safe content",
|
|
id: "client-id",
|
|
createdAt: "2000-01-01T00:00:00.000Z",
|
|
updatedAt: "2000-01-01T00:00:00.000Z",
|
|
viewCount: 999999,
|
|
serverOwned: "must-not-pass",
|
|
},
|
|
"user-admin-1",
|
|
);
|
|
|
|
const payload = mockPrisma.blog.create.mock.calls[0][0].data;
|
|
expect(payload).toEqual(expect.objectContaining({
|
|
title: "Safe post",
|
|
slug: "safe-post",
|
|
content: "Safe content",
|
|
}));
|
|
expect(payload).not.toHaveProperty("id");
|
|
expect(payload).not.toHaveProperty("createdAt");
|
|
expect(payload).not.toHaveProperty("updatedAt");
|
|
expect(payload).not.toHaveProperty("viewCount");
|
|
expect(payload).not.toHaveProperty("serverOwned");
|
|
});
|
|
|
|
it.each([
|
|
["contentUpdatedAt", { contentUpdatedAt: "not-a-date" }, "Invalid contentUpdatedAt"],
|
|
["publishedAt", { publishedAt: "not-a-date" }, "Invalid publishedAt"],
|
|
["scheduledAt", { scheduledAt: "not-a-date" }, "Invalid scheduledAt"],
|
|
])("rejects invalid explicit %s", async (_field, timestamps, message) => {
|
|
await expect(
|
|
service.createBlog(
|
|
{ title: "Bad date", slug: "bad-date", content: "Content", ...timestamps },
|
|
"user-admin-1",
|
|
),
|
|
).rejects.toThrow(message);
|
|
expect(mockPrisma.blog.create).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("should create a SCHEDULED blog if scheduledAt is in the future", async () => {
|
|
const futureDate = new Date(Date.now() + 86400000).toISOString();
|
|
const blogData = {
|
|
title: "Future Post",
|
|
slug: "future-post",
|
|
scheduledAt: futureDate,
|
|
};
|
|
|
|
mockPrisma.blog.create.mockResolvedValue({
|
|
id: "b-fut",
|
|
slug: "future-post",
|
|
});
|
|
|
|
await service.createBlog(blogData, "user-admin-1");
|
|
|
|
expect(mockPrisma.blog.create).toHaveBeenCalledWith(
|
|
expect.objectContaining({
|
|
data: expect.objectContaining({
|
|
status: "SCHEDULED",
|
|
isPublished: false,
|
|
publishedAt: null,
|
|
}),
|
|
}),
|
|
);
|
|
});
|
|
|
|
it("should expose cacheRevalidation status on create result without failing persistence", async () => {
|
|
const blogData = {
|
|
title: "Health Post",
|
|
slug: "health-post",
|
|
content: "Content",
|
|
};
|
|
const createdBlog = { id: "blog-1", ...blogData };
|
|
mockPrisma.blog.create.mockResolvedValue(createdBlog);
|
|
mockRevalidationService.revalidateBlog.mockResolvedValue(true);
|
|
|
|
const result = await service.createBlog(blogData, "user-admin-1");
|
|
|
|
expect(mockPrisma.blog.create).toHaveBeenCalled();
|
|
expect(mockRevalidationService.revalidateBlog).toHaveBeenCalledWith("health-post");
|
|
expect(result).toEqual({
|
|
...createdBlog,
|
|
cacheRevalidation: "success",
|
|
});
|
|
});
|
|
|
|
it("should return cacheRevalidation 'failed' when revalidation fails without failing persistence", async () => {
|
|
const blogData = {
|
|
title: "Health Post",
|
|
slug: "health-post",
|
|
content: "Content",
|
|
};
|
|
const createdBlog = { id: "blog-1", ...blogData };
|
|
mockPrisma.blog.create.mockResolvedValue(createdBlog);
|
|
mockRevalidationService.revalidateBlog.mockResolvedValue(false);
|
|
|
|
const result = await service.createBlog(blogData, "user-admin-1");
|
|
|
|
expect(mockPrisma.blog.create).toHaveBeenCalled();
|
|
expect(mockRevalidationService.revalidateBlog).toHaveBeenCalledWith("health-post");
|
|
expect(result).toEqual({
|
|
...createdBlog,
|
|
cacheRevalidation: "failed",
|
|
});
|
|
});
|
|
|
|
it("should catch unexpected revalidation exceptions on create and return cacheRevalidation 'failed'", async () => {
|
|
const blogData = {
|
|
title: "Health Post",
|
|
slug: "health-post",
|
|
content: "Content",
|
|
};
|
|
const createdBlog = { id: "blog-1", ...blogData };
|
|
mockPrisma.blog.create.mockResolvedValue(createdBlog);
|
|
mockRevalidationService.revalidateBlog.mockRejectedValue(new Error("Frontend offline"));
|
|
|
|
const result = await service.createBlog(blogData, "user-admin-1");
|
|
|
|
expect(mockPrisma.blog.create).toHaveBeenCalled();
|
|
expect(mockRevalidationService.revalidateBlog).toHaveBeenCalledWith("health-post");
|
|
expect(result).toEqual({
|
|
...createdBlog,
|
|
cacheRevalidation: "failed",
|
|
});
|
|
});
|
|
});
|
|
|
|
describe("updateBlog", () => {
|
|
it("should throw NotFoundException if blog does not exist", async () => {
|
|
mockPrisma.blog.findUnique.mockResolvedValue(null);
|
|
|
|
await expect(
|
|
service.updateBlog("b-unknown", { title: "Updated" }),
|
|
).rejects.toThrow(NotFoundException);
|
|
});
|
|
|
|
it("should update blog, handle future scheduledAt, and revalidate", async () => {
|
|
const existing = { id: "b1", slug: "my-post", title: "Old Title" };
|
|
mockPrisma.blog.findUnique.mockResolvedValue(existing);
|
|
|
|
const futureDate = new Date(Date.now() + 60000).toISOString();
|
|
const updated = { ...existing, title: "New Title", status: "SCHEDULED" };
|
|
mockPrisma.blog.update.mockResolvedValue(updated);
|
|
|
|
const result = await service.updateBlog("b1", {
|
|
title: "New Title",
|
|
scheduledAt: futureDate,
|
|
tagNames: ["تگ جدید"],
|
|
});
|
|
|
|
expect(mockPrisma.blog.update).toHaveBeenCalledWith(
|
|
expect.objectContaining({
|
|
where: { id: "b1" },
|
|
data: expect.objectContaining({
|
|
title: "New Title",
|
|
status: "SCHEDULED",
|
|
isPublished: false,
|
|
tags: expect.objectContaining({
|
|
set: [],
|
|
connectOrCreate: expect.any(Array),
|
|
}),
|
|
}),
|
|
}),
|
|
);
|
|
expect(mockRevalidationService.revalidateBlog).toHaveBeenCalledWith(
|
|
"my-post",
|
|
);
|
|
expect(result).toEqual({
|
|
...updated,
|
|
cacheRevalidation: "success",
|
|
});
|
|
});
|
|
|
|
it("should expose cacheRevalidation status on update result without failing persistence", async () => {
|
|
const existing = { id: "b1", slug: "my-post", title: "Old Title" };
|
|
mockPrisma.blog.findUnique.mockResolvedValue(existing);
|
|
const updated = { ...existing, title: "New Title" };
|
|
mockPrisma.blog.update.mockResolvedValue(updated);
|
|
mockRevalidationService.revalidateBlog.mockResolvedValue(true);
|
|
|
|
const result = await service.updateBlog("b1", { title: "New Title" });
|
|
|
|
expect(mockPrisma.blog.update).toHaveBeenCalled();
|
|
expect(mockRevalidationService.revalidateBlog).toHaveBeenCalledWith("my-post");
|
|
expect(result).toEqual({
|
|
...updated,
|
|
cacheRevalidation: "success",
|
|
});
|
|
});
|
|
|
|
it("should return cacheRevalidation 'failed' when revalidation fails on update without failing persistence", async () => {
|
|
const existing = { id: "b1", slug: "my-post", title: "Old Title" };
|
|
mockPrisma.blog.findUnique.mockResolvedValue(existing);
|
|
const updated = { ...existing, title: "New Title" };
|
|
mockPrisma.blog.update.mockResolvedValue(updated);
|
|
mockRevalidationService.revalidateBlog.mockResolvedValue(false);
|
|
|
|
const result = await service.updateBlog("b1", { title: "New Title" });
|
|
|
|
expect(mockPrisma.blog.update).toHaveBeenCalled();
|
|
expect(mockRevalidationService.revalidateBlog).toHaveBeenCalledWith("my-post");
|
|
expect(result).toEqual({
|
|
...updated,
|
|
cacheRevalidation: "failed",
|
|
});
|
|
});
|
|
|
|
it("should catch unexpected revalidation exceptions on update and return cacheRevalidation 'failed'", async () => {
|
|
const existing = { id: "b1", slug: "my-post", title: "Old Title" };
|
|
mockPrisma.blog.findUnique.mockResolvedValue(existing);
|
|
const updated = { ...existing, title: "New Title" };
|
|
mockPrisma.blog.update.mockResolvedValue(updated);
|
|
mockRevalidationService.revalidateBlog.mockRejectedValue(new Error("Revalidation timeout"));
|
|
|
|
const result = await service.updateBlog("b1", { title: "New Title" });
|
|
|
|
expect(mockPrisma.blog.update).toHaveBeenCalled();
|
|
expect(mockRevalidationService.revalidateBlog).toHaveBeenCalledWith("my-post");
|
|
expect(result).toEqual({
|
|
...updated,
|
|
cacheRevalidation: "failed",
|
|
});
|
|
});
|
|
});
|
|
|
|
describe("material freshness and scheduled publishing", () => {
|
|
it("sets contentUpdatedAt intentionally and never rewrites createdAt", async () => {
|
|
mockPrisma.blog.findUnique.mockResolvedValue({ id: "b1", slug: "post", createdAt: new Date("2025-01-01") });
|
|
mockPrisma.blog.update.mockResolvedValue({ id: "b1", slug: "post" });
|
|
await service.updateBlog("b1", { title: "Material edit", markContentUpdated: true });
|
|
const payload = mockPrisma.blog.update.mock.calls[0][0].data;
|
|
expect(payload.contentUpdatedAt).toEqual(expect.any(Date));
|
|
expect(payload).not.toHaveProperty("markContentUpdated");
|
|
expect(payload).not.toHaveProperty("createdAt");
|
|
});
|
|
it("automatically marks freshness when material article content changes", async () => {
|
|
mockPrisma.blog.findUnique.mockResolvedValue({ id: "b1", slug: "post", content: "Old content" });
|
|
mockPrisma.blog.update.mockResolvedValue({ id: "b1", slug: "post" });
|
|
await service.updateBlog("b1", { content: "Meaningfully revised content" });
|
|
expect(mockPrisma.blog.update.mock.calls[0][0].data.contentUpdatedAt).toEqual(expect.any(Date));
|
|
});
|
|
it("does not mark freshness for attribution-only changes", async () => {
|
|
mockPrisma.blog.findUnique.mockResolvedValue({ id: "b1", slug: "post", content: "Same content" });
|
|
mockPrisma.blog.update.mockResolvedValue({ id: "b1", slug: "post" });
|
|
await service.updateBlog("b1", { authorTeam: "Clinical team", authorRole: "Review" });
|
|
expect(mockPrisma.blog.update.mock.calls[0][0].data).not.toHaveProperty("contentUpdatedAt");
|
|
});
|
|
it("allows explicitly clearing contentUpdatedAt", async () => {
|
|
mockPrisma.blog.findUnique.mockResolvedValue({ id: "b1", slug: "post" });
|
|
mockPrisma.blog.update.mockResolvedValue({ id: "b1", slug: "post" });
|
|
await service.updateBlog("b1", { contentUpdatedAt: null });
|
|
expect(mockPrisma.blog.update.mock.calls[0][0].data.contentUpdatedAt).toBeNull();
|
|
});
|
|
it("publishes due posts without rewriting createdAt", async () => {
|
|
const scheduledAt = new Date("2025-02-01T10:00:00Z");
|
|
mockPrisma.blog.findMany.mockResolvedValue([{ id: "b1", slug: "post", title: "Post", scheduledAt }]);
|
|
mockPrisma.blog.update.mockResolvedValue({});
|
|
await service.publishDueScheduledBlogs();
|
|
expect(mockPrisma.blog.update).toHaveBeenCalledWith({ where: { id: "b1" }, data: { status: "PUBLISHED", isPublished: true, publishedAt: scheduledAt } });
|
|
});
|
|
});
|
|
|
|
describe("updateBlog security and freshness validation", () => {
|
|
beforeEach(() => {
|
|
mockPrisma.blog.findUnique.mockResolvedValue({ id: "blog-1", slug: "safe" });
|
|
mockPrisma.blog.update.mockResolvedValue({ id: "blog-1", slug: "safe" });
|
|
});
|
|
|
|
it("allowlists mutable fields and ignores createdAt and unrecognized fields", async () => {
|
|
await service.updateBlog("blog-1", {
|
|
title: "Allowed title",
|
|
createdAt: "2000-01-01T00:00:00.000Z",
|
|
serverOwned: "must-not-pass",
|
|
});
|
|
|
|
const payload = mockPrisma.blog.update.mock.calls[0][0].data;
|
|
expect(payload.title).toBe("Allowed title");
|
|
expect(payload).not.toHaveProperty("createdAt");
|
|
expect(payload).not.toHaveProperty("serverOwned");
|
|
});
|
|
|
|
it("rejects an invalid explicit contentUpdatedAt", async () => {
|
|
await expect(
|
|
service.updateBlog("blog-1", { contentUpdatedAt: "not-a-date" }),
|
|
).rejects.toThrow("Invalid contentUpdatedAt");
|
|
expect(mockPrisma.blog.update).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("preserves explicit null clearing and markContentUpdated precedence", async () => {
|
|
await service.updateBlog("blog-1", { contentUpdatedAt: null });
|
|
expect(mockPrisma.blog.update.mock.calls[0][0].data.contentUpdatedAt).toBeNull();
|
|
|
|
await service.updateBlog("blog-1", {
|
|
contentUpdatedAt: "not-a-date",
|
|
markContentUpdated: true,
|
|
});
|
|
expect(mockPrisma.blog.update.mock.calls[1][0].data.contentUpdatedAt).toEqual(expect.any(Date));
|
|
});
|
|
});
|
|
|
|
describe("deleteBlog", () => {
|
|
it("should throw NotFoundException if blog not found", async () => {
|
|
mockPrisma.blog.findUnique.mockResolvedValue(null);
|
|
|
|
await expect(service.deleteBlog("b-none")).rejects.toThrow(
|
|
NotFoundException,
|
|
);
|
|
});
|
|
|
|
it("should delete blog and trigger revalidation", async () => {
|
|
const mockBlog = { id: "b1", slug: "del-slug" };
|
|
mockPrisma.blog.findUnique.mockResolvedValue(mockBlog);
|
|
mockPrisma.blog.delete.mockResolvedValue(mockBlog);
|
|
|
|
const result = await service.deleteBlog("b1");
|
|
|
|
expect(mockPrisma.blog.delete).toHaveBeenCalledWith({
|
|
where: { id: "b1" },
|
|
});
|
|
expect(mockRevalidationService.revalidateBlog).toHaveBeenCalledWith(
|
|
"del-slug",
|
|
);
|
|
expect(result).toEqual(mockBlog);
|
|
});
|
|
});
|
|
|
|
describe("Authors", () => {
|
|
it("should fetch authors list", async () => {
|
|
const users = [{ id: "u1", firstName: "Admin", role: "ADMIN" }];
|
|
mockPrisma.user.findMany.mockResolvedValue(users);
|
|
|
|
const result = await service.getAuthors();
|
|
expect(result).toEqual(users);
|
|
expect(mockPrisma.user.findMany).toHaveBeenCalled();
|
|
});
|
|
});
|
|
|
|
describe("Blog Categories Management", () => {
|
|
it("should get categories", async () => {
|
|
mockPrisma.blogCategory.findMany.mockResolvedValue([]);
|
|
const res = await service.getBlogCategories();
|
|
expect(res).toEqual([]);
|
|
expect(mockPrisma.blogCategory.findMany).toHaveBeenCalled();
|
|
});
|
|
|
|
it("should create blog category", async () => {
|
|
const cat = { name: "Cat 1", slug: "cat-1" };
|
|
mockPrisma.blogCategory.create.mockResolvedValue(cat);
|
|
|
|
const res = await service.createBlogCategory(cat);
|
|
expect(res).toEqual(cat);
|
|
expect(mockPrisma.blogCategory.create).toHaveBeenCalledWith({
|
|
data: cat,
|
|
});
|
|
});
|
|
|
|
it("should update blog category", async () => {
|
|
mockPrisma.blogCategory.update.mockResolvedValue({
|
|
id: "c1",
|
|
name: "Updated",
|
|
});
|
|
const res = await service.updateBlogCategory("c1", { name: "Updated" });
|
|
expect(res.name).toBe("Updated");
|
|
});
|
|
|
|
it("should delete blog category", async () => {
|
|
mockPrisma.blogCategory.delete.mockResolvedValue({ id: "c1" });
|
|
const res = await service.deleteBlogCategory("c1");
|
|
expect(res.id).toBe("c1");
|
|
});
|
|
});
|
|
|
|
describe("Blog Tags Management", () => {
|
|
it("should get tags", async () => {
|
|
mockPrisma.blogTag.findMany.mockResolvedValue([]);
|
|
const res = await service.getBlogTags();
|
|
expect(res).toEqual([]);
|
|
});
|
|
|
|
it("should create blog tag with custom or generated slug", async () => {
|
|
mockPrisma.blogTag.create.mockImplementation(({ data }) => data);
|
|
|
|
const res = await service.createBlogTag({ name: "سلامت سگ" });
|
|
expect(res.slug).toBe("سلامت-سگ");
|
|
expect(res.name).toBe("سلامت سگ");
|
|
});
|
|
|
|
it("should delete blog tag", async () => {
|
|
mockPrisma.blogTag.delete.mockResolvedValue({ id: "t1" });
|
|
const res = await service.deleteBlogTag("t1");
|
|
expect(res.id).toBe("t1");
|
|
});
|
|
});
|
|
|
|
describe("Blog Comments Management", () => {
|
|
it("should get comments with pagination and filters", async () => {
|
|
mockPrisma.blogComment.findMany.mockResolvedValue([]);
|
|
mockPrisma.blogComment.count.mockResolvedValue(0);
|
|
|
|
const res = await service.getComments({
|
|
page: 1,
|
|
limit: 10,
|
|
status: "PENDING",
|
|
blogId: "b1",
|
|
});
|
|
expect(res.meta.total).toBe(0);
|
|
expect(mockPrisma.blogComment.findMany).toHaveBeenCalledWith(
|
|
expect.objectContaining({
|
|
where: { status: "PENDING", blogId: "b1" },
|
|
skip: 0,
|
|
take: 10,
|
|
}),
|
|
);
|
|
});
|
|
|
|
it("should update comment status and trigger revalidation", async () => {
|
|
mockPrisma.blogComment.findUnique.mockResolvedValue({
|
|
id: "comm-1",
|
|
blog: { slug: "target-blog" },
|
|
});
|
|
mockPrisma.blogComment.update.mockResolvedValue({
|
|
id: "comm-1",
|
|
status: "APPROVED",
|
|
});
|
|
|
|
const res = await service.updateCommentStatus("comm-1", "APPROVED");
|
|
expect(res.status).toBe("APPROVED");
|
|
expect(mockRevalidationService.revalidateBlog).toHaveBeenCalledWith(
|
|
"target-blog",
|
|
);
|
|
});
|
|
|
|
it("should throw NotFoundException if comment to update is not found", async () => {
|
|
mockPrisma.blogComment.findUnique.mockResolvedValue(null);
|
|
|
|
await expect(
|
|
service.updateCommentStatus("c-none", "REJECTED"),
|
|
).rejects.toThrow(NotFoundException);
|
|
});
|
|
|
|
it("should delete comment and trigger revalidation", async () => {
|
|
mockPrisma.blogComment.findUnique.mockResolvedValue({
|
|
id: "comm-2",
|
|
blog: { slug: "target-blog" },
|
|
});
|
|
mockPrisma.blogComment.delete.mockResolvedValue({ id: "comm-2" });
|
|
|
|
const res = await service.deleteComment("comm-2");
|
|
expect(res.id).toBe("comm-2");
|
|
expect(mockRevalidationService.revalidateBlog).toHaveBeenCalledWith(
|
|
"target-blog",
|
|
);
|
|
});
|
|
|
|
it("should throw NotFoundException if comment to delete is not found", async () => {
|
|
mockPrisma.blogComment.findUnique.mockResolvedValue(null);
|
|
|
|
await expect(service.deleteComment("c-none")).rejects.toThrow(
|
|
NotFoundException,
|
|
);
|
|
});
|
|
});
|
|
|
|
describe("publishDueScheduledBlogs", () => {
|
|
it("should return 0 when no due scheduled blogs found", async () => {
|
|
mockPrisma.blog.findMany.mockResolvedValue([]);
|
|
|
|
const count = await service.publishDueScheduledBlogs();
|
|
expect(count).toBe(0);
|
|
expect(mockPrisma.blog.update).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("should publish due scheduled blogs and revalidate", async () => {
|
|
const scheduledDate = new Date(Date.now() - 3600000);
|
|
const due = [
|
|
{
|
|
id: "b1",
|
|
slug: "due-blog-1",
|
|
title: "Due Blog 1",
|
|
scheduledAt: scheduledDate,
|
|
},
|
|
];
|
|
mockPrisma.blog.findMany.mockResolvedValue(due);
|
|
mockPrisma.blog.update.mockResolvedValue({});
|
|
|
|
const count = await service.publishDueScheduledBlogs();
|
|
|
|
expect(count).toBe(1);
|
|
expect(mockPrisma.blog.update).toHaveBeenCalledWith({
|
|
where: { id: "b1" },
|
|
data: {
|
|
status: "PUBLISHED",
|
|
isPublished: true,
|
|
publishedAt: scheduledDate,
|
|
},
|
|
});
|
|
expect(mockRevalidationService.revalidateBlog).toHaveBeenCalledWith(
|
|
"due-blog-1",
|
|
);
|
|
});
|
|
});
|
|
});
|