refactor
This commit is contained in:
@@ -5,10 +5,16 @@ import { MediaService } from './MediaService';
|
||||
import type { Response } from 'express';
|
||||
import { RequestAvatarGenerationInputDTO } from './dtos/RequestAvatarGenerationInputDTO';
|
||||
import { UploadMediaInputDTO } from './dtos/UploadMediaInputDTO';
|
||||
import { RequestAvatarGenerationOutputDTO } from './dtos/RequestAvatarGenerationOutputDTO';
|
||||
import { UploadMediaOutputDTO } from './dtos/UploadMediaOutputDTO';
|
||||
import { GetMediaOutputDTO } from './dtos/GetMediaOutputDTO';
|
||||
import { DeleteMediaOutputDTO } from './dtos/DeleteMediaOutputDTO';
|
||||
import { GetAvatarOutputDTO } from './dtos/GetAvatarOutputDTO';
|
||||
import { UpdateAvatarOutputDTO } from './dtos/UpdateAvatarOutputDTO';
|
||||
|
||||
describe('MediaController', () => {
|
||||
let controller: MediaController;
|
||||
let service: ReturnType<typeof vi.mocked<MediaService>>;
|
||||
let service: jest.Mocked<MediaService>;
|
||||
|
||||
beforeEach(async () => {
|
||||
const module: TestingModule = await Test.createTestingModule({
|
||||
@@ -29,153 +35,201 @@ describe('MediaController', () => {
|
||||
}).compile();
|
||||
|
||||
controller = module.get<MediaController>(MediaController);
|
||||
service = vi.mocked(module.get(MediaService));
|
||||
service = module.get(MediaService) as jest.Mocked<MediaService>;
|
||||
});
|
||||
|
||||
const createMockResponse = (): Response => {
|
||||
const res: Partial<Response> = {};
|
||||
res.status = vi.fn().mockReturnValue(res as Response);
|
||||
res.json = vi.fn().mockReturnValue(res as Response);
|
||||
return res as Response;
|
||||
};
|
||||
|
||||
describe('requestAvatarGeneration', () => {
|
||||
it('should request avatar generation and return 201 on success', async () => {
|
||||
const input: RequestAvatarGenerationInputDTO = { driverId: 'driver-123' };
|
||||
const viewModel = { success: true, jobId: 'job-123' } as any;
|
||||
service.requestAvatarGeneration.mockResolvedValue({ viewModel } as any);
|
||||
const input: RequestAvatarGenerationInputDTO = {
|
||||
userId: 'user-123',
|
||||
facePhotoData: 'photo-data',
|
||||
suitColor: 'red',
|
||||
};
|
||||
const dto: RequestAvatarGenerationOutputDTO = {
|
||||
success: true,
|
||||
requestId: 'req-123',
|
||||
avatarUrls: ['https://example.com/avatar.png'],
|
||||
};
|
||||
service.requestAvatarGeneration.mockResolvedValue(dto);
|
||||
|
||||
const mockRes: ReturnType<typeof vi.mocked<Response>> = {
|
||||
status: vi.fn().mockReturnThis(),
|
||||
json: vi.fn(),
|
||||
} as unknown as ReturnType<typeof vi.mocked<Response>>;
|
||||
const res = createMockResponse();
|
||||
|
||||
await controller.requestAvatarGeneration(input, mockRes);
|
||||
await controller.requestAvatarGeneration(input, res);
|
||||
|
||||
expect(service.requestAvatarGeneration).toHaveBeenCalledWith(input);
|
||||
expect(mockRes.status).toHaveBeenCalledWith(201);
|
||||
expect(mockRes.json).toHaveBeenCalledWith(viewModel);
|
||||
expect(res.status).toHaveBeenCalledWith(201);
|
||||
expect(res.json).toHaveBeenCalledWith(dto);
|
||||
});
|
||||
|
||||
it('should return 400 on failure', async () => {
|
||||
const input: RequestAvatarGenerationInputDTO = { driverId: 'driver-123' };
|
||||
const viewModel = { success: false, error: 'Error' } as any;
|
||||
service.requestAvatarGeneration.mockResolvedValue({ viewModel } as any);
|
||||
const input: RequestAvatarGenerationInputDTO = {
|
||||
userId: 'user-123',
|
||||
facePhotoData: 'photo-data',
|
||||
suitColor: 'red',
|
||||
};
|
||||
const dto: RequestAvatarGenerationOutputDTO = {
|
||||
success: false,
|
||||
errorMessage: 'Error',
|
||||
};
|
||||
service.requestAvatarGeneration.mockResolvedValue(dto);
|
||||
|
||||
const mockRes: ReturnType<typeof vi.mocked<Response>> = {
|
||||
status: vi.fn().mockReturnThis(),
|
||||
json: vi.fn(),
|
||||
} as unknown as ReturnType<typeof vi.mocked<Response>>;
|
||||
const res = createMockResponse();
|
||||
|
||||
await controller.requestAvatarGeneration(input, mockRes);
|
||||
await controller.requestAvatarGeneration(input, res);
|
||||
|
||||
expect(mockRes.status).toHaveBeenCalledWith(400);
|
||||
expect(mockRes.json).toHaveBeenCalledWith(viewModel);
|
||||
expect(service.requestAvatarGeneration).toHaveBeenCalledWith(input);
|
||||
expect(res.status).toHaveBeenCalledWith(400);
|
||||
expect(res.json).toHaveBeenCalledWith(dto);
|
||||
});
|
||||
});
|
||||
|
||||
describe('uploadMedia', () => {
|
||||
it('should upload media and return 201 on success', async () => {
|
||||
const file: Express.Multer.File = { filename: 'file.jpg' } as Express.Multer.File;
|
||||
const input: UploadMediaInputDTO = { type: 'image' };
|
||||
const viewModel = { success: true, mediaId: 'media-123' } as any;
|
||||
service.uploadMedia.mockResolvedValue({ viewModel } as any);
|
||||
const input: UploadMediaInputDTO = { type: 'image' } as UploadMediaInputDTO;
|
||||
const dto: UploadMediaOutputDTO = {
|
||||
success: true,
|
||||
mediaId: 'media-123',
|
||||
url: 'https://example.com/file.jpg',
|
||||
};
|
||||
service.uploadMedia.mockResolvedValue(dto);
|
||||
|
||||
const mockRes: ReturnType<typeof vi.mocked<Response>> = {
|
||||
status: vi.fn().mockReturnThis(),
|
||||
json: vi.fn(),
|
||||
} as unknown as ReturnType<typeof vi.mocked<Response>>;
|
||||
const res = createMockResponse();
|
||||
|
||||
await controller.uploadMedia(file, input, mockRes);
|
||||
await controller.uploadMedia(file, input, res);
|
||||
|
||||
expect(service.uploadMedia).toHaveBeenCalledWith({ ...input, file });
|
||||
expect(mockRes.status).toHaveBeenCalledWith(201);
|
||||
expect(mockRes.json).toHaveBeenCalledWith(viewModel);
|
||||
expect(res.status).toHaveBeenCalledWith(201);
|
||||
expect(res.json).toHaveBeenCalledWith(dto);
|
||||
});
|
||||
|
||||
it('should return 400 when upload fails', async () => {
|
||||
const file: Express.Multer.File = { filename: 'file.jpg' } as Express.Multer.File;
|
||||
const input: UploadMediaInputDTO = { type: 'image' } as UploadMediaInputDTO;
|
||||
const dto: UploadMediaOutputDTO = {
|
||||
success: false,
|
||||
error: 'Upload failed',
|
||||
};
|
||||
service.uploadMedia.mockResolvedValue(dto);
|
||||
|
||||
const res = createMockResponse();
|
||||
|
||||
await controller.uploadMedia(file, input, res);
|
||||
|
||||
expect(service.uploadMedia).toHaveBeenCalledWith({ ...input, file });
|
||||
expect(res.status).toHaveBeenCalledWith(400);
|
||||
expect(res.json).toHaveBeenCalledWith(dto);
|
||||
});
|
||||
});
|
||||
|
||||
describe('getMedia', () => {
|
||||
it('should return media if found', async () => {
|
||||
const mediaId = 'media-123';
|
||||
const viewModel = { id: mediaId, url: 'url' } as any;
|
||||
service.getMedia.mockResolvedValue({ viewModel } as any);
|
||||
const dto: GetMediaOutputDTO = {
|
||||
id: mediaId,
|
||||
url: 'https://example.com/file.jpg',
|
||||
type: 'image',
|
||||
category: 'avatar',
|
||||
uploadedAt: new Date(),
|
||||
size: 123,
|
||||
};
|
||||
service.getMedia.mockResolvedValue(dto);
|
||||
|
||||
const mockRes: ReturnType<typeof vi.mocked<Response>> = {
|
||||
status: vi.fn().mockReturnThis(),
|
||||
json: vi.fn(),
|
||||
} as unknown as ReturnType<typeof vi.mocked<Response>>;
|
||||
const res = createMockResponse();
|
||||
|
||||
await controller.getMedia(mediaId, mockRes);
|
||||
await controller.getMedia(mediaId, res);
|
||||
|
||||
expect(service.getMedia).toHaveBeenCalledWith(mediaId);
|
||||
expect(mockRes.status).toHaveBeenCalledWith(200);
|
||||
expect(mockRes.json).toHaveBeenCalledWith(viewModel);
|
||||
expect(res.status).toHaveBeenCalledWith(200);
|
||||
expect(res.json).toHaveBeenCalledWith(dto);
|
||||
});
|
||||
|
||||
it('should return 404 if not found', async () => {
|
||||
const mediaId = 'media-123';
|
||||
service.getMedia.mockResolvedValue({ viewModel: null } as any);
|
||||
service.getMedia.mockResolvedValue(null);
|
||||
|
||||
const mockRes: ReturnType<typeof vi.mocked<Response>> = {
|
||||
status: vi.fn().mockReturnThis(),
|
||||
json: vi.fn(),
|
||||
} as unknown as ReturnType<typeof vi.mocked<Response>>;
|
||||
const res = createMockResponse();
|
||||
|
||||
await controller.getMedia(mediaId, mockRes);
|
||||
await controller.getMedia(mediaId, res);
|
||||
|
||||
expect(mockRes.status).toHaveBeenCalledWith(404);
|
||||
expect(mockRes.json).toHaveBeenCalledWith({ error: 'Media not found' });
|
||||
expect(service.getMedia).toHaveBeenCalledWith(mediaId);
|
||||
expect(res.status).toHaveBeenCalledWith(404);
|
||||
expect(res.json).toHaveBeenCalledWith({ error: 'Media not found' });
|
||||
});
|
||||
});
|
||||
|
||||
describe('deleteMedia', () => {
|
||||
it('should delete media', async () => {
|
||||
it('should delete media and return result', async () => {
|
||||
const mediaId = 'media-123';
|
||||
const viewModel = { success: true } as any;
|
||||
service.deleteMedia.mockResolvedValue({ viewModel } as any);
|
||||
const dto: DeleteMediaOutputDTO = {
|
||||
success: true,
|
||||
};
|
||||
service.deleteMedia.mockResolvedValue(dto);
|
||||
|
||||
const mockRes: ReturnType<typeof vi.mocked<Response>> = {
|
||||
status: vi.fn().mockReturnThis(),
|
||||
json: vi.fn(),
|
||||
} as unknown as ReturnType<typeof vi.mocked<Response>>;
|
||||
const res = createMockResponse();
|
||||
|
||||
await controller.deleteMedia(mediaId, mockRes);
|
||||
await controller.deleteMedia(mediaId, res);
|
||||
|
||||
expect(service.deleteMedia).toHaveBeenCalledWith(mediaId);
|
||||
expect(mockRes.status).toHaveBeenCalledWith(200);
|
||||
expect(mockRes.json).toHaveBeenCalledWith(viewModel);
|
||||
expect(res.status).toHaveBeenCalledWith(200);
|
||||
expect(res.json).toHaveBeenCalledWith(dto);
|
||||
});
|
||||
});
|
||||
|
||||
describe('getAvatar', () => {
|
||||
it('should return avatar if found', async () => {
|
||||
const driverId = 'driver-123';
|
||||
const result = { url: 'avatar.jpg' };
|
||||
service.getAvatar.mockResolvedValue(result);
|
||||
const dto: GetAvatarOutputDTO = {
|
||||
avatarUrl: 'https://example.com/avatar.png',
|
||||
};
|
||||
service.getAvatar.mockResolvedValue(dto);
|
||||
|
||||
const mockRes: ReturnType<typeof vi.mocked<Response>> = {
|
||||
status: vi.fn().mockReturnThis(),
|
||||
json: vi.fn(),
|
||||
} as unknown as ReturnType<typeof vi.mocked<Response>>;
|
||||
const res = createMockResponse();
|
||||
|
||||
await controller.getAvatar(driverId, mockRes);
|
||||
await controller.getAvatar(driverId, res);
|
||||
|
||||
expect(service.getAvatar).toHaveBeenCalledWith(driverId);
|
||||
expect(mockRes.status).toHaveBeenCalledWith(200);
|
||||
expect(mockRes.json).toHaveBeenCalledWith(result);
|
||||
expect(res.status).toHaveBeenCalledWith(200);
|
||||
expect(res.json).toHaveBeenCalledWith(dto);
|
||||
});
|
||||
|
||||
it('should return 404 when avatar not found', async () => {
|
||||
const driverId = 'driver-123';
|
||||
service.getAvatar.mockResolvedValue(null);
|
||||
|
||||
const res = createMockResponse();
|
||||
|
||||
await controller.getAvatar(driverId, res);
|
||||
|
||||
expect(service.getAvatar).toHaveBeenCalledWith(driverId);
|
||||
expect(res.status).toHaveBeenCalledWith(404);
|
||||
expect(res.json).toHaveBeenCalledWith({ error: 'Avatar not found' });
|
||||
});
|
||||
});
|
||||
|
||||
describe('updateAvatar', () => {
|
||||
it('should update avatar', async () => {
|
||||
it('should update avatar and return result', async () => {
|
||||
const driverId = 'driver-123';
|
||||
const input = { url: 'new-avatar.jpg' };
|
||||
const result = { success: true };
|
||||
service.updateAvatar.mockResolvedValue(result);
|
||||
const input = { mediaUrl: 'https://example.com/new-avatar.png' } as UpdateAvatarOutputDTO;
|
||||
const dto: UpdateAvatarOutputDTO = {
|
||||
success: true,
|
||||
};
|
||||
service.updateAvatar.mockResolvedValue(dto);
|
||||
|
||||
const mockRes: ReturnType<typeof vi.mocked<Response>> = {
|
||||
status: vi.fn().mockReturnThis(),
|
||||
json: vi.fn(),
|
||||
} as unknown as ReturnType<typeof vi.mocked<Response>>;
|
||||
const res = createMockResponse();
|
||||
|
||||
await controller.updateAvatar(driverId, input, mockRes);
|
||||
await controller.updateAvatar(driverId, input as any, res);
|
||||
|
||||
expect(service.updateAvatar).toHaveBeenCalledWith(driverId, input);
|
||||
expect(mockRes.status).toHaveBeenCalledWith(200);
|
||||
expect(mockRes.json).toHaveBeenCalledWith(result);
|
||||
expect(service.updateAvatar).toHaveBeenCalledWith(driverId, input as any);
|
||||
expect(res.status).toHaveBeenCalledWith(200);
|
||||
expect(res.json).toHaveBeenCalledWith(dto);
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user