Files
canina/backend/src/reviews/reviews.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

222 lines
6.6 KiB
TypeScript

import { Test, TestingModule } from "@nestjs/testing";
import { ReviewsController } from "./reviews.controller";
import { ReviewsService } from "./reviews.service";
import { CreateReviewDto } from "./dto/create-review.dto";
import { UpdateReviewDto } from "./dto/update-review.dto";
describe("ReviewsController", () => {
let controller: ReviewsController;
let service: ReviewsService;
const mockReviewsService = {
createReview: jest.fn(),
getProductReviews: jest.fn(),
getAdminReviews: jest.fn(),
updateReviewStatus: jest.fn(),
deleteReview: jest.fn(),
};
beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
controllers: [ReviewsController],
providers: [
{
provide: ReviewsService,
useValue: mockReviewsService,
},
],
}).compile();
controller = module.get<ReviewsController>(ReviewsController);
service = module.get<ReviewsService>(ReviewsService);
});
afterEach(() => {
jest.clearAllMocks();
});
it("should be defined", () => {
expect(controller).toBeDefined();
});
describe("createReview", () => {
it("should submit a review with authenticated user ID from req.user", async () => {
const productId = "prod-101";
const dto: CreateReviewDto = {
rating: 5,
comment: "محصول فوق‌العاده با کیفیت بود",
userName: "رضا محمدی",
userPhone: "09121112233",
};
const req = { user: { id: "user-42" } };
const expectedResponse = {
success: true,
message:
"دیدگاه شما با موفقیت ثبت شد و پس از بررسی و تایید کارشناسان در سایت نمایش داده می‌شود.",
data: {
id: "rev-1",
productId,
userId: "user-42",
rating: 5,
comment: dto.comment,
},
};
mockReviewsService.createReview.mockResolvedValue(expectedResponse);
const result = await controller.createReview(productId, dto, req);
expect(service.createReview).toHaveBeenCalledWith(
productId,
dto,
"user-42",
);
expect(result).toEqual(expectedResponse);
});
it("should submit a review with undefined userId when user is guest", async () => {
const productId = "prod-102";
const dto: CreateReviewDto = {
rating: 4,
comment: "دیدگاه کاربر مهمان بدون لاگین",
};
const req = {};
const expectedResponse = {
success: true,
message: "دیدگاه شما با موفقیت ثبت شد",
data: { id: "rev-2", productId, userId: null, rating: 4 },
};
mockReviewsService.createReview.mockResolvedValue(expectedResponse);
const result = await controller.createReview(productId, dto, req);
expect(service.createReview).toHaveBeenCalledWith(
productId,
dto,
undefined,
);
expect(result).toEqual(expectedResponse);
});
});
describe("getProductReviews", () => {
it("should get product reviews with parsed page and limit", async () => {
const productId = "prod-101";
const expectedResponse = {
success: true,
data: [{ id: "rev-1", rating: 5, comment: "عالی" }],
pagination: { total: 1, page: 2, limit: 5, totalPages: 1 },
stats: { averageRating: 5, totalReviews: 1 },
};
mockReviewsService.getProductReviews.mockResolvedValue(expectedResponse);
const result = await controller.getProductReviews(productId, "2", "5");
expect(service.getProductReviews).toHaveBeenCalledWith(productId, 2, 5);
expect(result).toEqual(expectedResponse);
});
it("should fallback to defaults (page 1, limit 10) when query params omitted", async () => {
const productId = "prod-101";
mockReviewsService.getProductReviews.mockResolvedValue({
success: true,
data: [],
});
await controller.getProductReviews(productId);
expect(service.getProductReviews).toHaveBeenCalledWith(productId, 1, 10);
});
});
describe("getAdminReviews", () => {
it("should pass filters and parsed pagination to admin reviews service", async () => {
const expectedResponse = {
success: true,
data: [],
pagination: { total: 0, page: 1, limit: 20, totalPages: 0 },
};
mockReviewsService.getAdminReviews.mockResolvedValue(expectedResponse);
const result = await controller.getAdminReviews(
"3",
"25",
"PENDING",
"ویتامین",
"prod-101",
);
expect(service.getAdminReviews).toHaveBeenCalledWith({
page: 3,
limit: 25,
status: "PENDING",
search: "ویتامین",
productId: "prod-101",
});
expect(result).toEqual(expectedResponse);
});
it("should use defaults when admin query params omitted", async () => {
mockReviewsService.getAdminReviews.mockResolvedValue({
success: true,
data: [],
});
await controller.getAdminReviews();
expect(service.getAdminReviews).toHaveBeenCalledWith({
page: 1,
limit: 20,
status: undefined,
search: undefined,
productId: undefined,
});
});
});
describe("updateReviewStatus", () => {
it("should call updateReviewStatus with id and dto", async () => {
const reviewId = "rev-99";
const dto: UpdateReviewDto = {
status: "APPROVED",
adminReply: "با تشکر از نظر ارزشمند شما",
};
const expectedResponse = {
success: true,
message: "وضعیت دیدگاه با موفقیت بروزرسانی شد",
data: { id: reviewId, status: "APPROVED", adminReply: dto.adminReply },
};
mockReviewsService.updateReviewStatus.mockResolvedValue(expectedResponse);
const result = await controller.updateReviewStatus(reviewId, dto);
expect(service.updateReviewStatus).toHaveBeenCalledWith(reviewId, dto);
expect(result).toEqual(expectedResponse);
});
});
describe("deleteReview", () => {
it("should call deleteReview with review id", async () => {
const reviewId = "rev-99";
const expectedResponse = {
success: true,
message: "دیدگاه با موفقیت حذف شد",
};
mockReviewsService.deleteReview.mockResolvedValue(expectedResponse);
const result = await controller.deleteReview(reviewId);
expect(service.deleteReview).toHaveBeenCalledWith(reviewId);
expect(result).toEqual(expectedResponse);
});
});
});