-
Notifications
You must be signed in to change notification settings - Fork 0
/
handler.spec.ts
72 lines (61 loc) · 2.03 KB
/
handler.spec.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import {getVideo} from './handler';
import * as AWSMock from "aws-sdk-mock";
import * as AWS from "aws-sdk";
import { GetItemInput } from "aws-sdk/clients/dynamodb";
describe('Video handler', () => {
beforeEach(() => {
AWSMock.setSDKInstance(AWS);
})
afterEach(() => {
AWSMock.restore('DynamoDB.DocumentClient')
})
it('should get 200', (done) => {
const expectedId = '1'
const event = buildEvent(expectedId)
mockInstanceWithValue({id: expectedId})
getVideo(event, null, (error, response) => {
expect(response.statusCode).toBe(200);
done()
})
});
it('should get 404', (done) => {
const event = buildEvent('nonexistent id');
mockInstanceWithValue(null);
getVideo(event, null, (error, response) => {
expect(response.statusCode).toBe(404);
done()
})
});
it('should get 501', (done) => {
const event = buildEvent();
mockInstanceWithError('This is an irrelevant error')
getVideo(event, null, (error, response) => {
expect(response.statusCode).toBe(501);
done()
})
});
it('should get custom status', (done) => {
const event = buildEvent();
const expectedStatus = 505;
mockInstanceWithError({statusCode: expectedStatus})
getVideo(event, null, (error, response) => {
expect(response.statusCode).toBe(expectedStatus);
done()
})
});
const buildEvent = (videoId: string = 'irrelevant') => ({
"pathParameters": {
"id": videoId
}
});
const mockInstanceWithValue = (response) => {
AWSMock.mock('DynamoDB.DocumentClient', 'get', (params: GetItemInput, callback: Function) => {
callback(null, {Item: response});
})
}
const mockInstanceWithError = (error) => {
AWSMock.mock('DynamoDB.DocumentClient', 'get', (params: GetItemInput, callback: Function) => {
throw error;
})
}
});