|
| 1 | +import { Test, TestingModule } from '@nestjs/testing'; |
| 2 | +import { NotFoundException } from '@nestjs/common'; |
| 3 | +import { ProfileController } from './profile.controller'; |
| 4 | +import { ProfileService } from './profile.service'; |
| 5 | +import { UserProfile, DefaultSplitType } from './profile.entity'; |
| 6 | + |
| 7 | +describe('ProfileController', () => { |
| 8 | + let controller: ProfileController; |
| 9 | + let profileService: ProfileService; |
| 10 | + |
| 11 | + const walletAddress = 'GDZST3XVCDTUJ76ZAV2HA72KYQODXXZ5PTMAPZGDHZ6CS7RO7MGG3DBM'; |
| 12 | + const mockProfile: UserProfile = { |
| 13 | + walletAddress, |
| 14 | + displayName: 'Alice', |
| 15 | + avatarUrl: null, |
| 16 | + preferredCurrency: 'USD', |
| 17 | + defaultSplitType: DefaultSplitType.EQUAL, |
| 18 | + emailNotifications: true, |
| 19 | + pushNotifications: true, |
| 20 | + createdAt: new Date(), |
| 21 | + updatedAt: new Date(), |
| 22 | + }; |
| 23 | + |
| 24 | + const mockProfileService = { |
| 25 | + getByWalletAddress: jest.fn().mockResolvedValue(mockProfile), |
| 26 | + update: jest.fn().mockResolvedValue(mockProfile), |
| 27 | + }; |
| 28 | + |
| 29 | + beforeEach(async () => { |
| 30 | + jest.clearAllMocks(); |
| 31 | + mockProfileService.getByWalletAddress.mockResolvedValue(mockProfile); |
| 32 | + mockProfileService.update.mockResolvedValue(mockProfile); |
| 33 | + |
| 34 | + const module: TestingModule = await Test.createTestingModule({ |
| 35 | + controllers: [ProfileController], |
| 36 | + providers: [ |
| 37 | + { |
| 38 | + provide: ProfileService, |
| 39 | + useValue: mockProfileService, |
| 40 | + }, |
| 41 | + ], |
| 42 | + }).compile(); |
| 43 | + |
| 44 | + controller = module.get<ProfileController>(ProfileController); |
| 45 | + profileService = module.get<ProfileService>(ProfileService); |
| 46 | + }); |
| 47 | + |
| 48 | + it('should be defined', () => { |
| 49 | + expect(controller).toBeDefined(); |
| 50 | + }); |
| 51 | + |
| 52 | + describe('getByWalletAddress', () => { |
| 53 | + it('should return profile for wallet address', async () => { |
| 54 | + const result = await controller.getByWalletAddress(walletAddress); |
| 55 | + expect(result).toEqual(mockProfile); |
| 56 | + expect(profileService.getByWalletAddress).toHaveBeenCalledWith(walletAddress); |
| 57 | + }); |
| 58 | + |
| 59 | + it('should throw when service throws NotFoundException', async () => { |
| 60 | + mockProfileService.getByWalletAddress.mockRejectedValue( |
| 61 | + new NotFoundException('Profile for wallet address GUNKNOWN not found'), |
| 62 | + ); |
| 63 | + await expect( |
| 64 | + controller.getByWalletAddress('GUNKNOWN'), |
| 65 | + ).rejects.toThrow(NotFoundException); |
| 66 | + }); |
| 67 | + }); |
| 68 | + |
| 69 | + describe('update', () => { |
| 70 | + it('should update and return profile', async () => { |
| 71 | + const dto = { |
| 72 | + displayName: 'Alice Updated', |
| 73 | + preferredCurrency: 'EUR' as const, |
| 74 | + }; |
| 75 | + const result = await controller.update(walletAddress, dto); |
| 76 | + expect(result).toEqual(mockProfile); |
| 77 | + expect(profileService.update).toHaveBeenCalledWith(walletAddress, dto); |
| 78 | + }); |
| 79 | + }); |
| 80 | +}); |
0 commit comments