-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathdeps.test.ts
More file actions
83 lines (68 loc) · 2.54 KB
/
Copy pathdeps.test.ts
File metadata and controls
83 lines (68 loc) · 2.54 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
import type { Deps } from '../deps';
describe('getDeps()', () => {
beforeEach(() => {
jest.clearAllMocks();
jest.resetModules();
// pino
jest.mock('pino', () => ({
__esModule: true,
default: jest.fn(() => ({
info: jest.fn(),
error: jest.fn(),
warn: jest.fn(),
debug: jest.fn(),
})),
}));
jest.mock('@aws-sdk/client-s3', () => ({
S3Client: jest.fn(),
}));
// Repo client
jest.mock('../../../../../internal/datastore', () => ({
LetterRepository: jest.fn(),
}));
// Env
jest.mock('../env', () => ({
lambdaEnv: {
LETTERS_TABLE_NAME: 'LettersTable',
LETTER_TTL_HOURS: '24',
SUPPLIER_ID_HEADER: 'nhsd-supplier-id',
APIM_CORRELATION_HEADER: 'nhsd-correlation-id',
},
}));
});
test('constructs deps and wires repository config correctly', async () => {
// get current mock instances
const { S3Client } = jest.requireMock('@aws-sdk/client-s3') as { S3Client: jest.Mock };
const pinoMock = jest.requireMock('pino') as { default: jest.Mock };
const { LetterRepository } = jest.requireMock('../../../../../internal/datastore') as { LetterRepository: jest.Mock };
const { getDeps } = require('../deps');
const deps: Deps = getDeps();
expect(S3Client).toHaveBeenCalledTimes(1);
expect(pinoMock.default).toHaveBeenCalledTimes(1);
expect(LetterRepository).toHaveBeenCalledTimes(1);
const repoCtorArgs = (LetterRepository as jest.Mock).mock.calls[0];
expect(repoCtorArgs[2]).toEqual({
lettersTableName: 'LettersTable',
ttlHours: 24
});
expect(deps.env).toEqual({
LETTERS_TABLE_NAME: 'LettersTable',
LETTER_TTL_HOURS: '24',
SUPPLIER_ID_HEADER: 'nhsd-supplier-id',
APIM_CORRELATION_HEADER: 'nhsd-correlation-id',
});
});
test('is a singleton (second call returns the same object; constructors not re-run)', async () => {
// get current mock instances
const { S3Client } = jest.requireMock('@aws-sdk/client-s3') as { S3Client: jest.Mock };
const pinoMock = jest.requireMock('pino') as { default: jest.Mock };
const { LetterRepository } = jest.requireMock('../../../../../internal/datastore') as { LetterRepository: jest.Mock };
const { getDeps } = require('../deps');
const first = getDeps();
const second = getDeps();
expect(first).toBe(second);
expect(S3Client).toHaveBeenCalledTimes(1);
expect(LetterRepository).toHaveBeenCalledTimes(1);
expect(pinoMock.default).toHaveBeenCalledTimes(1);
});
});