- 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.
181 lines
5.6 KiB
TypeScript
181 lines
5.6 KiB
TypeScript
import { Test, TestingModule } from "@nestjs/testing";
|
|
import { MediaController } from "./media.controller";
|
|
import { MediaService } from "./media.service";
|
|
import { BadRequestException } from "@nestjs/common";
|
|
|
|
describe("MediaController (Admin)", () => {
|
|
let controller: MediaController;
|
|
let service: MediaService;
|
|
|
|
const mockMediaService = {
|
|
getAllMedia: jest.fn(),
|
|
uploadFile: jest.fn(),
|
|
deleteMedia: jest.fn(),
|
|
deleteManyMedia: jest.fn(),
|
|
updateMedia: jest.fn(),
|
|
};
|
|
|
|
beforeEach(async () => {
|
|
const module: TestingModule = await Test.createTestingModule({
|
|
controllers: [MediaController],
|
|
providers: [
|
|
{
|
|
provide: MediaService,
|
|
useValue: mockMediaService,
|
|
},
|
|
],
|
|
}).compile();
|
|
|
|
controller = module.get<MediaController>(MediaController);
|
|
service = module.get<MediaService>(MediaService);
|
|
});
|
|
|
|
afterEach(() => {
|
|
jest.clearAllMocks();
|
|
});
|
|
|
|
it("should be defined", () => {
|
|
expect(controller).toBeDefined();
|
|
});
|
|
|
|
describe("getAllMedia", () => {
|
|
it("should return list of media wrapped with success: true", async () => {
|
|
const mockMediaList = [
|
|
{
|
|
id: "med-1",
|
|
url: "/uploads/canivita.jpg",
|
|
filename: "canivita.jpg",
|
|
mimetype: "image/jpeg",
|
|
size: 10240,
|
|
altText: "مکمل کانیویتا",
|
|
title: "Canivita Multivitamin",
|
|
},
|
|
{
|
|
id: "med-2",
|
|
url: "/uploads/petvital.webp",
|
|
filename: "petvital.webp",
|
|
mimetype: "image/webp",
|
|
size: 20480,
|
|
altText: "پودر پتویتال",
|
|
title: "Petvital Mineral",
|
|
},
|
|
];
|
|
|
|
mockMediaService.getAllMedia.mockResolvedValue(mockMediaList);
|
|
|
|
const result = await controller.getAllMedia();
|
|
|
|
expect(service.getAllMedia).toHaveBeenCalledTimes(1);
|
|
expect(result).toEqual({ success: true, data: mockMediaList });
|
|
});
|
|
});
|
|
|
|
describe("uploadFile", () => {
|
|
it("should throw BadRequestException if file is missing/undefined", async () => {
|
|
await expect(controller.uploadFile(undefined as any)).rejects.toThrow(
|
|
BadRequestException,
|
|
);
|
|
await expect(controller.uploadFile(undefined as any)).rejects.toThrow(
|
|
"File is missing",
|
|
);
|
|
expect(service.uploadFile).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("should successfully upload file and return success: true with media data", async () => {
|
|
const mockFile = {
|
|
fieldname: "file",
|
|
originalname: "joint-supplement.png",
|
|
encoding: "7bit",
|
|
mimetype: "image/png",
|
|
buffer: Buffer.from("fake-image-bytes"),
|
|
size: 5120,
|
|
} as Express.Multer.File;
|
|
|
|
const createdMedia = {
|
|
id: "med-3",
|
|
url: "/uploads/1700000000-123456.png",
|
|
filename: "joint-supplement.png",
|
|
mimetype: "image/png",
|
|
size: 5120,
|
|
};
|
|
|
|
mockMediaService.uploadFile.mockResolvedValue(createdMedia);
|
|
|
|
const result = await controller.uploadFile(mockFile);
|
|
|
|
expect(service.uploadFile).toHaveBeenCalledWith(mockFile);
|
|
expect(result).toEqual({ success: true, data: createdMedia });
|
|
});
|
|
});
|
|
|
|
describe("deleteMedia", () => {
|
|
it("should delete single media item by id and return success: true with result", async () => {
|
|
const mediaId = "med-1";
|
|
mockMediaService.deleteMedia.mockResolvedValue({ success: true });
|
|
|
|
const result = await controller.deleteMedia(mediaId);
|
|
|
|
expect(service.deleteMedia).toHaveBeenCalledWith(mediaId);
|
|
expect(result).toEqual({ success: true, data: { success: true } });
|
|
});
|
|
});
|
|
|
|
describe("deleteManyMedia (bulk delete)", () => {
|
|
it("should throw BadRequestException if ids is undefined, empty, or not an array", async () => {
|
|
await expect(
|
|
controller.deleteManyMedia(undefined as any),
|
|
).rejects.toThrow(BadRequestException);
|
|
await expect(controller.deleteManyMedia([] as any)).rejects.toThrow(
|
|
"لیست شناسههای رسانه الزامی است",
|
|
);
|
|
await expect(
|
|
controller.deleteManyMedia("invalid-id" as any),
|
|
).rejects.toThrow(BadRequestException);
|
|
expect(service.deleteManyMedia).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("should delete multiple media items and return count", async () => {
|
|
const ids = ["med-1", "med-2"];
|
|
mockMediaService.deleteManyMedia.mockResolvedValue({
|
|
success: true,
|
|
count: 2,
|
|
});
|
|
|
|
const result = await controller.deleteManyMedia(ids);
|
|
|
|
expect(service.deleteManyMedia).toHaveBeenCalledWith(ids);
|
|
expect(result).toEqual({
|
|
success: true,
|
|
data: { success: true, count: 2 },
|
|
});
|
|
});
|
|
});
|
|
|
|
describe("updateMedia", () => {
|
|
it("should update media metadata (altText, title, description, caption)", async () => {
|
|
const mediaId = "med-1";
|
|
const body = {
|
|
altText: "تصویر مکمل مفاصل کانیویتا",
|
|
title: "کانیویتا سگ و گربه",
|
|
description: "مولتی ویتامین مایع حاوی ۱۱ ویتامین ضروری",
|
|
caption: "بستهبندی ۱۰۰ میلیلیتری",
|
|
};
|
|
|
|
const updatedMedia = {
|
|
id: mediaId,
|
|
url: "/uploads/canivita.jpg",
|
|
filename: "canivita.jpg",
|
|
mimetype: "image/jpeg",
|
|
...body,
|
|
};
|
|
|
|
mockMediaService.updateMedia.mockResolvedValue(updatedMedia);
|
|
|
|
const result = await controller.updateMedia(mediaId, body);
|
|
|
|
expect(service.updateMedia).toHaveBeenCalledWith(mediaId, body);
|
|
expect(result).toEqual({ success: true, data: updatedMedia });
|
|
});
|
|
});
|
|
});
|