forked from StellarSplit/StellarSplit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathemail.controller.spec.ts
More file actions
68 lines (57 loc) · 1.88 KB
/
Copy pathemail.controller.spec.ts
File metadata and controls
68 lines (57 loc) · 1.88 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
import { Test, TestingModule } from "@nestjs/testing";
import { EmailController } from "./email.controller";
import { EmailService } from "./email.service";
import { NotFoundException } from "@nestjs/common";
describe("EmailController", () => {
let controller: EmailController;
let service: any;
beforeEach(async () => {
service = {
getUser: jest.fn(),
updatePreferences: jest.fn(),
};
const module: TestingModule = await Test.createTestingModule({
controllers: [EmailController],
providers: [
{
provide: EmailService,
useValue: service,
},
],
}).compile();
controller = module.get<EmailController>(EmailController);
});
it("should be defined", () => {
expect(controller).toBeDefined();
});
it("should get preferences for a user", async () => {
const mockUser = {
id: "u1",
emailPreferences: { invitations: true, reminders: false },
};
service.getUser.mockResolvedValue(mockUser);
const result = await controller.getPreferences("u1");
expect(result).toEqual(mockUser.emailPreferences);
});
it("should throw NotFoundException if user not found while getting preferences", async () => {
service.getUser.mockResolvedValue(null);
await expect(controller.getPreferences("u1")).rejects.toThrow(
NotFoundException,
);
});
it("should update preferences for a user", async () => {
const mockUser = {
id: "u1",
emailPreferences: { invitations: true, reminders: true },
};
service.getUser.mockResolvedValue(mockUser);
const dto = { invitations: false };
const result = await controller.updatePreferences("u1", dto);
expect(result.invitations).toBe(false);
expect(result.reminders).toBe(true);
expect(service.updatePreferences).toHaveBeenCalledWith("u1", {
invitations: false,
reminders: true,
});
});
});