-
Notifications
You must be signed in to change notification settings - Fork 0
/
get-post.ecs-task.test.ts
57 lines (49 loc) · 1.7 KB
/
get-post.ecs-task.test.ts
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
import { DynamoDBClient, GetItemCommand } from '@aws-sdk/client-dynamodb';
import { marshall } from '@aws-sdk/util-dynamodb';
import { mockClient } from 'aws-sdk-client-mock';
import request from 'supertest';
import app from '../../src/posts/get-post.ecs-task';
import { testCases } from '../components/test-cases';
const TABLE_NAME = 'test-table';
const ddbMock = mockClient(DynamoDBClient);
beforeEach(() => {
process.env.POWERTOOLS_SERVICE_NAME = 'dev';
process.env.POWERTOOLS_METRICS_NAMESPACE = 'dev';
process.env.TABLE_NAME = TABLE_NAME;
jest.spyOn(console, 'info').mockImplementation(() => {});
jest.spyOn(console, 'error').mockImplementation(() => {});
jest.spyOn(console, 'warn').mockImplementation(() => {});
jest.spyOn(console, 'log').mockImplementation(() => {});
});
afterEach(() => {
jest.clearAllMocks();
ddbMock.reset();
});
afterAll(() => {
process.env.TABLE_NAME = undefined;
process.env.POWERTOOLS_SERVICE_NAME = undefined;
process.env.POWERTOOLS_METRICS_NAMESPACE = undefined;
jest.restoreAllMocks();
});
describe('get post', () => {
test.each([testCases])('successful', async (testCase) => {
// GIVEN
ddbMock.on(GetItemCommand).resolves({
Item: marshall(testCase),
});
// WHEN
const res = await request(app).get(`/posts/${testCase.pk}`);
// THEN
expect(res.statusCode).toEqual(200);
expect(res.body).toEqual(testCase);
});
test.each([testCases])('failure', async (testCase) => {
// GIVEN
ddbMock.on(GetItemCommand).rejects({});
// WHEN
const res = await request(app).get(`/posts/${testCase.pk}`);
// THEN
expect(res.statusCode).toEqual(404);
expect(res.body).toEqual({ message: 'Post not found' });
});
});