Skip to content

Commit

Permalink
Test Forecast module construction
Browse files Browse the repository at this point in the history
  • Loading branch information
Dan Hughes committed Aug 2, 2018
1 parent 0630e55 commit 263f957
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const axios = require('axios');
const stringifyDates = require('./stringifyDates');

class Forecast {
constructor({ accountId, token }) {
constructor({ accountId, token } = {}) {
if (!accountId || !token) {
throw new Error(
'Forecast module requires accountId and token to be configured.'
Expand Down
40 changes: 38 additions & 2 deletions src/index.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,41 @@
const axios = require('axios');
const Forecast = require('./index');

jest.mock('axios');

const MOCK_CREDENTIALS = {
accountId: 'mock__accountId',
token: 'mock__token',
};

describe('forecast-promise', () => {
it('passes dummy test', () => {
expect(true).toBe(true);
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',
},
});
});
});

0 comments on commit 263f957

Please sign in to comment.