-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathindex.test.js
77 lines (60 loc) · 1.86 KB
/
index.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
const axios = require('axios');
const Forecast = require('./index');
jest.mock('axios');
jest.mock('./toDateString', () => () => 'mock__dateString');
const MOCK_CREDENTIALS = {
accountId: 'mock__accountId',
token: 'mock__token',
};
describe('forecast-promise', () => {
it('crashes with no input', () => {
expect(() => new Forecast()).toThrow(
'Forecast module requires accountId and token to be configured.'
);
});
it('crashes with only one input', () => {
expect(
() => new Forecast({ accountId: MOCK_CREDENTIALS.accountId })
).toThrow('Forecast module requires accountId and token to be configured.');
expect(() => new Forecast({ token: MOCK_CREDENTIALS.token })).toThrow(
'Forecast module requires accountId and token to be configured.'
);
});
it('instantiates axios with valid input', () => {
axios.create.mockImplementation(args => args);
const f = new Forecast(MOCK_CREDENTIALS);
expect(f.instance).toEqual({
baseURL: 'https://api.forecastapp.com/',
headers: {
Authorization: `Bearer ${MOCK_CREDENTIALS.token}`,
'Forecast-Account-Id': MOCK_CREDENTIALS.accountId,
'User-Agent': 'https://www.npmjs.com/package/forecast-promise',
},
});
});
it('calls requests on axios', async () => {
const get = jest.fn().mockImplementation(async url => {
if (url === '/whoami') {
return {
data: { current_user: 'mock__user' },
};
}
return {
data: {
[url.substring(1)]: url,
},
};
});
const axiosMock = { get };
const f = new Forecast(MOCK_CREDENTIALS, axiosMock);
const whoAmI = await f.whoAmI();
expect(whoAmI).toEqual('mock__user');
const projects = await f.projects({
startDate: 'mock__input',
endDate: 'mock__input',
});
// Not mocking API response, just want to know the query went out
expect(projects).toEqual('/projects');
expect(get).toBeCalledTimes(2);
});
});