-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathreceipts.service.spec.ts
More file actions
92 lines (84 loc) · 3.17 KB
/
Copy pathreceipts.service.spec.ts
File metadata and controls
92 lines (84 loc) · 3.17 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
import { NotFoundException } from '@nestjs/common';
import { ReceiptsService } from './receipts.service';
describe('ReceiptsService', () => {
it('maps jpg uploads to image/jpeg', async () => {
const prisma: any = {
expense: {
findUnique: jest.fn().mockResolvedValue({ id: 'expense-1', groupId: 'group-1' }),
},
receipt: {
upsert: jest.fn().mockResolvedValue({ id: 'receipt-1' }),
},
};
const s3: any = {
getPresignedUploadUrl: jest.fn().mockResolvedValue('https://signed.example/upload'),
};
const jobsQueue: any = {
enqueueReceiptProcessing: jest.fn().mockResolvedValue(undefined),
};
const service = new ReceiptsService(prisma, jobsQueue, s3);
await service.createUploadUrl('expense-1', { extension: 'jpg' });
expect(s3.getPresignedUploadUrl).toHaveBeenCalledWith(
expect.stringMatching(/^receipts\/group-1\/expense-1\/.+\.jpg$/),
'image/jpeg',
);
});
it('creates a receipt record and returns a presigned upload URL', async () => {
const prisma: any = {
expense: {
findUnique: jest.fn().mockResolvedValue({ id: 'expense-1', groupId: 'group-1' }),
},
receipt: {
upsert: jest.fn().mockResolvedValue({ id: 'receipt-1' }),
},
};
const s3: any = {
getPresignedUploadUrl: jest.fn().mockResolvedValue('https://signed.example/upload'),
};
const jobsQueue: any = {
enqueueReceiptProcessing: jest.fn().mockResolvedValue(undefined),
};
const service = new ReceiptsService(prisma, jobsQueue, s3);
const result = await service.createUploadUrl('expense-1', { extension: 'png' });
expect(prisma.expense.findUnique).toHaveBeenCalledWith({ where: { id: 'expense-1' }, select: { groupId: true } });
expect(prisma.receipt.upsert).toHaveBeenCalledWith({
where: { expenseId: 'expense-1' },
update: {
fileKey: expect.stringMatching(/^receipts\/group-1\/expense-1\/.+\.png$/),
},
create: {
expenseId: 'expense-1',
fileKey: expect.stringMatching(/^receipts\/group-1\/expense-1\/.+\.png$/),
},
});
expect(s3.getPresignedUploadUrl).toHaveBeenCalledWith(
expect.stringMatching(/^receipts\/group-1\/expense-1\/.+\.png$/),
'image/png',
);
expect(jobsQueue.enqueueReceiptProcessing).toHaveBeenCalledWith({
receiptId: 'receipt-1',
expenseId: 'expense-1',
});
expect(result).toEqual({
uploadUrl: 'https://signed.example/upload',
fileKey: expect.stringMatching(/^receipts\/group-1\/expense-1\/.+\.png$/),
});
});
it('throws when the expense does not exist', async () => {
const prisma: any = {
expense: {
findUnique: jest.fn().mockResolvedValue(null),
},
};
const s3: any = {
getPresignedUploadUrl: jest.fn(),
};
const jobsQueue: any = {
enqueueReceiptProcessing: jest.fn(),
};
const service = new ReceiptsService(prisma, jobsQueue, s3);
await expect(service.createUploadUrl('missing-expense', {})).rejects.toBeInstanceOf(NotFoundException);
expect(s3.getPresignedUploadUrl).not.toHaveBeenCalled();
expect(jobsQueue.enqueueReceiptProcessing).not.toHaveBeenCalled();
});
});