|
| 1 | +import { Test, TestingModule } from '@nestjs/testing'; |
| 2 | +import { BadRequestException, ConflictException, NotFoundException } from '@nestjs/common'; |
| 3 | +import { ProfileService } from './profile.service'; |
| 4 | +import { PrismaClient } from '@prisma/client'; |
| 5 | + |
| 6 | +jest.mock('@prisma/client', () => { |
| 7 | + const mPrismaClient = { |
| 8 | + user: { |
| 9 | + findUnique: jest.fn(), |
| 10 | + update: jest.fn(), |
| 11 | + }, |
| 12 | + watchlist: { |
| 13 | + count: jest.fn(), |
| 14 | + }, |
| 15 | + alert: { |
| 16 | + count: jest.fn(), |
| 17 | + }, |
| 18 | + }; |
| 19 | + return { PrismaClient: jest.fn(() => mPrismaClient) }; |
| 20 | +}); |
| 21 | + |
| 22 | +describe('ProfileService', () => { |
| 23 | + let service: ProfileService; |
| 24 | + let prisma: PrismaClient; |
| 25 | + |
| 26 | + const mockUser = { |
| 27 | + id: 'user-1', |
| 28 | + email: 'user@example.com', |
| 29 | + createdAt: new Date('2026-06-15T10:00:00.000Z'), |
| 30 | + notificationPreferences: [ |
| 31 | + { |
| 32 | + discordEnabled: true, |
| 33 | + telegramEnabled: false, |
| 34 | + emailEnabled: true, |
| 35 | + alertTypes: ['critical'], |
| 36 | + }, |
| 37 | + ], |
| 38 | + }; |
| 39 | + |
| 40 | + beforeEach(async () => { |
| 41 | + const module: TestingModule = await Test.createTestingModule({ |
| 42 | + providers: [ProfileService], |
| 43 | + }).compile(); |
| 44 | + |
| 45 | + service = module.get<ProfileService>(ProfileService); |
| 46 | + prisma = new PrismaClient(); |
| 47 | + }); |
| 48 | + |
| 49 | + afterEach(() => { |
| 50 | + jest.clearAllMocks(); |
| 51 | + }); |
| 52 | + |
| 53 | + it('should be defined', () => { |
| 54 | + expect(service).toBeDefined(); |
| 55 | + }); |
| 56 | + |
| 57 | + describe('getProfile', () => { |
| 58 | + it('returns profile with metadata', async () => { |
| 59 | + (prisma.user.findUnique as jest.Mock).mockResolvedValue(mockUser); |
| 60 | + (prisma.watchlist.count as jest.Mock).mockResolvedValue(3); |
| 61 | + (prisma.alert.count as jest.Mock).mockResolvedValue(2); |
| 62 | + |
| 63 | + const result = await service.getProfile('user-1'); |
| 64 | + |
| 65 | + expect(result).toEqual({ |
| 66 | + id: 'user-1', |
| 67 | + email: 'user@example.com', |
| 68 | + createdAt: '2026-06-15T10:00:00.000Z', |
| 69 | + metadata: { |
| 70 | + watchlistCount: 3, |
| 71 | + openAlertsCount: 2, |
| 72 | + notificationPreferences: { |
| 73 | + discordEnabled: true, |
| 74 | + telegramEnabled: false, |
| 75 | + emailEnabled: true, |
| 76 | + alertTypes: ['critical'], |
| 77 | + }, |
| 78 | + }, |
| 79 | + }); |
| 80 | + }); |
| 81 | + |
| 82 | + it('throws NotFoundException when user does not exist', async () => { |
| 83 | + (prisma.user.findUnique as jest.Mock).mockResolvedValue(null); |
| 84 | + |
| 85 | + await expect(service.getProfile('missing')).rejects.toThrow(NotFoundException); |
| 86 | + }); |
| 87 | + }); |
| 88 | + |
| 89 | + describe('updateProfile', () => { |
| 90 | + it('updates email and returns profile', async () => { |
| 91 | + (prisma.user.findUnique as jest.Mock).mockResolvedValue(mockUser); |
| 92 | + (prisma.user.update as jest.Mock).mockResolvedValue({ |
| 93 | + ...mockUser, |
| 94 | + email: 'new@example.com', |
| 95 | + }); |
| 96 | + (prisma.watchlist.count as jest.Mock).mockResolvedValue(1); |
| 97 | + (prisma.alert.count as jest.Mock).mockResolvedValue(0); |
| 98 | + |
| 99 | + const result = await service.updateProfile('user-1', { email: 'new@example.com' }); |
| 100 | + |
| 101 | + expect(prisma.user.update).toHaveBeenCalledWith({ |
| 102 | + where: { id: 'user-1' }, |
| 103 | + data: { email: 'new@example.com' }, |
| 104 | + include: { |
| 105 | + notificationPreferences: { |
| 106 | + take: 1, |
| 107 | + orderBy: { createdAt: 'desc' }, |
| 108 | + }, |
| 109 | + }, |
| 110 | + }); |
| 111 | + expect(result.email).toBe('new@example.com'); |
| 112 | + }); |
| 113 | + |
| 114 | + it('throws BadRequestException for invalid email', async () => { |
| 115 | + await expect(service.updateProfile('user-1', { email: 'not-an-email' })).rejects.toThrow( |
| 116 | + BadRequestException, |
| 117 | + ); |
| 118 | + }); |
| 119 | + |
| 120 | + it('throws BadRequestException when email is missing', async () => { |
| 121 | + await expect(service.updateProfile('user-1', {})).rejects.toThrow(BadRequestException); |
| 122 | + }); |
| 123 | + |
| 124 | + it('throws NotFoundException when user does not exist', async () => { |
| 125 | + (prisma.user.findUnique as jest.Mock).mockResolvedValue(null); |
| 126 | + |
| 127 | + await expect(service.updateProfile('missing', { email: 'a@b.com' })).rejects.toThrow( |
| 128 | + NotFoundException, |
| 129 | + ); |
| 130 | + }); |
| 131 | + |
| 132 | + it('throws ConflictException on duplicate email', async () => { |
| 133 | + (prisma.user.findUnique as jest.Mock).mockResolvedValue(mockUser); |
| 134 | + (prisma.user.update as jest.Mock).mockRejectedValue({ code: 'P2002' }); |
| 135 | + |
| 136 | + await expect(service.updateProfile('user-1', { email: 'taken@example.com' })).rejects.toThrow( |
| 137 | + ConflictException, |
| 138 | + ); |
| 139 | + }); |
| 140 | + }); |
| 141 | +}); |
0 commit comments