forked from ZyntariHQ/Invoisio
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathusers.service.spec.ts
More file actions
58 lines (49 loc) · 1.88 KB
/
Copy pathusers.service.spec.ts
File metadata and controls
58 lines (49 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
import { NotFoundException } from "@nestjs/common";
import { PrismaService } from "../prisma/prisma.service";
import { UsersService } from "./users.service";
describe("UsersService notification preferences", () => {
const preferences = new Map<string, boolean>([
["merchant-a-user", true],
["merchant-b-user", true],
]);
const prisma = {
user: {
update: jest.fn(async ({ where, data }) => {
if (!preferences.has(where.id)) throw new Error("Record not found");
preferences.set(where.id, data.pushNotificationsEnabled);
return { id: where.id, ...data };
}),
findUnique: jest.fn(async ({ where }) => {
const value = preferences.get(where.id);
return value === undefined ? null : { pushNotificationsEnabled: value };
}),
},
};
const service = new UsersService(prisma as unknown as PrismaService);
beforeEach(() => {
preferences.set("merchant-a-user", true);
preferences.set("merchant-b-user", true);
jest.clearAllMocks();
});
it("reads the persisted value after an update", async () => {
await service.updatePreferences("merchant-a-user", false);
await expect(service.getPreferences("merchant-a-user")).resolves.toEqual({
pushNotificationsEnabled: false,
});
});
it("scopes reads and updates to the authenticated merchant user", async () => {
await service.updatePreferences("merchant-a-user", false);
await expect(service.getPreferences("merchant-b-user")).resolves.toEqual({
pushNotificationsEnabled: true,
});
expect(prisma.user.findUnique).toHaveBeenLastCalledWith({
where: { id: "merchant-b-user" },
select: { pushNotificationsEnabled: true },
});
});
it("rejects preference reads for a missing user", async () => {
await expect(service.getPreferences("missing-user")).rejects.toBeInstanceOf(
NotFoundException,
);
});
});