forked from MergeFi/backend
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathidempotency-cleanup.service.spec.ts
More file actions
49 lines (39 loc) · 1.5 KB
/
Copy pathidempotency-cleanup.service.spec.ts
File metadata and controls
49 lines (39 loc) · 1.5 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
import { Test, TestingModule } from '@nestjs/testing';
import { getRepositoryToken } from '@nestjs/typeorm';
import { LessThan } from 'typeorm';
import { IdempotencyKey } from '../entities/idempotency-key.entity';
import { IdempotencyCleanupService } from './idempotency-cleanup.service';
interface DeleteCriteria {
expiresAt: ReturnType<typeof LessThan>;
}
describe('IdempotencyCleanupService', () => {
let service: IdempotencyCleanupService;
let repo: {
delete: jest.Mock<Promise<{ affected: number }>, [DeleteCriteria]>;
};
beforeEach(async () => {
repo = {
delete: jest
.fn<Promise<{ affected: number }>, [DeleteCriteria]>()
.mockResolvedValue({ affected: 0 }),
};
const module: TestingModule = await Test.createTestingModule({
providers: [
IdempotencyCleanupService,
{ provide: getRepositoryToken(IdempotencyKey), useValue: repo },
],
}).compile();
service = module.get(IdempotencyCleanupService);
});
it('deletes rows whose expiresAt has already passed', async () => {
repo.delete.mockResolvedValue({ affected: 3 });
await service.removeExpired();
expect(repo.delete).toHaveBeenCalledTimes(1);
const criteria = repo.delete.mock.calls[0][0];
expect(criteria.expiresAt).toEqual(LessThan(expect.any(Date) as Date));
});
it('does not throw when nothing is expired', async () => {
repo.delete.mockResolvedValue({ affected: 0 });
await expect(service.removeExpired()).resolves.toBeUndefined();
});
});