forked from quantica-io/gimme-time
-
Notifications
You must be signed in to change notification settings - Fork 0
/
handler.test.js
61 lines (46 loc) · 1.64 KB
/
handler.test.js
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
jest.mock('moment-timezone');
const { tz } = require('moment-timezone');
const handler = require('./handler');
const EXPECTED_DATE = '2018-10-01 00:00:00';
const TIMEZONE = 'Europe/Berlin';
const DEFAULT_TIMEZONE = 'Europe/Rome';
describe('When call handler.gimmetime', () => {
it('Should return the expected date if the provided timezone exists', async () => {
const event = {
queryStringParameters: {
tz: TIMEZONE
}
};
tz.names = () => { return [TIMEZONE]; };
tz.mockImplementation(() => {
return {
format: () => { return EXPECTED_DATE; }
};
});
const response = await handler.gimmetime(event);
expect(response.statusCode).toMatch(/200/);
expect(response.body).toMatch(`The time in ${TIMEZONE} is: ${EXPECTED_DATE}`);
});
it('Should return the date for the default timezone if none has been specified', async () => {
tz.names = () => { return [TIMEZONE]; };
tz.mockImplementation(() => {
return {
format: () => { return EXPECTED_DATE; }
};
});
const response = await handler.gimmetime({ queryStringParameters: {} });
expect(response.statusCode).toMatch(/200/);
expect(response.body).toMatch(`The time in ${DEFAULT_TIMEZONE} is: ${EXPECTED_DATE}`);
});
it('Should return an error if the provided timezone does not exists', async () => {
const event = {
queryStringParameters: {
tz: TIMEZONE
}
};
tz.names = () => { return []; };
const response = await handler.gimmetime(event);
expect(response.statusCode).toMatch(/400/);
expect(response.body).toMatch(`Unknown timezone ${TIMEZONE}`);
});
});