forked from StellarSplit/StellarSplit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprofile.controller.spec.ts
More file actions
93 lines (82 loc) · 3.05 KB
/
Copy pathprofile.controller.spec.ts
File metadata and controls
93 lines (82 loc) · 3.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import { Test, TestingModule } from '@nestjs/testing';
import { CanActivate, NotFoundException } from '@nestjs/common';
import { ProfileController } from './profile.controller';
import { ProfileService } from './profile.service';
import { UserProfile, DefaultSplitType } from './profile.entity';
import { JwtAuthGuard } from '../auth/guards/jwt-auth.guard';
import { ProfilePolicyGuard } from './profile-policy.guard';
const allowAllGuard: CanActivate = { canActivate: () => true };
describe('ProfileController', () => {
let controller: ProfileController;
let profileService: ProfileService;
const walletAddress = 'GDZST3XVCDTUJ76ZAV2HA72KYQODXXZ5PTMAPZGDHZ6CS7RO7MGG3DBM';
const mockProfile: UserProfile = {
walletAddress,
displayName: 'Alice',
email: null,
avatarUrl: null,
preferredCurrency: 'USD',
defaultSplitType: DefaultSplitType.EQUAL,
emailNotifications: true,
pushNotifications: true,
createdAt: new Date(),
updatedAt: new Date(),
};
const mockProfileService = {
getByWalletAddress: jest.fn().mockResolvedValue(mockProfile),
update: jest.fn().mockResolvedValue(mockProfile),
};
beforeEach(async () => {
jest.clearAllMocks();
mockProfileService.getByWalletAddress.mockResolvedValue(mockProfile);
mockProfileService.update.mockResolvedValue(mockProfile);
const module: TestingModule = await Test.createTestingModule({
controllers: [ProfileController],
providers: [
{
provide: ProfileService,
useValue: mockProfileService,
},
],
})
.overrideGuard(JwtAuthGuard)
.useValue(allowAllGuard)
.overrideGuard(ProfilePolicyGuard)
.useValue(allowAllGuard)
.compile();
controller = module.get<ProfileController>(ProfileController);
profileService = module.get<ProfileService>(ProfileService);
});
it('should be defined', () => {
expect(controller).toBeDefined();
});
describe('getByWalletAddress', () => {
it('should return profile for wallet address', async () => {
const result = await controller.getByWalletAddress(walletAddress);
expect(result).toEqual(mockProfile);
expect(profileService.getByWalletAddress).toHaveBeenCalledWith(walletAddress);
});
it('should throw when service throws NotFoundException', async () => {
mockProfileService.getByWalletAddress.mockRejectedValue(
new NotFoundException('Profile for wallet address GUNKNOWN not found'),
);
await expect(
controller.getByWalletAddress('GUNKNOWN'),
).rejects.toThrow(NotFoundException);
});
});
describe('update', () => {
it('should update and return profile', async () => {
const dto = {
displayName: 'Alice Updated',
preferredCurrency: 'EUR' as const,
};
const req = {
user: { walletAddress, id: 'user-id' },
};
const result = await controller.update(walletAddress, dto, req as any);
expect(result).toEqual(mockProfile);
expect(profileService.update).toHaveBeenCalledWith(walletAddress, dto);
});
});
});