Skip to content

Commit

Permalink
add unit tests to localStorage helper and API.js -> api.js
Browse files Browse the repository at this point in the history
  • Loading branch information
Kyubinhan committed Sep 25, 2018
1 parent dd2482f commit eced189
Show file tree
Hide file tree
Showing 17 changed files with 1,302 additions and 646 deletions.
1,860 changes: 1,232 additions & 628 deletions package-lock.json

Large diffs are not rendered by default.

4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,8 @@
"axios-mock-adapter": "^1.15.0",
"enzyme": "^3.3.0",
"enzyme-adapter-react-16": "^1.1.1",
"jest": "20.0.4",
"jest": "^21.2.1",
"jest-localstorage-mock": "^2.2.0",
"puppeteer": "^1.5.0"
},
"scripts": {
Expand All @@ -102,6 +103,7 @@
"!src/registerServiceWorker.js"
],
"setupFiles": [
"jest-localstorage-mock",
"<rootDir>/config/polyfills.js",
"<rootDir>/config/setupTests.js"
],
Expand Down
2 changes: 1 addition & 1 deletion src/actionCreators/commonActionCreator.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { normalize } from 'normalizr';
import { axios, createConfigWithHeader, saveReferencesInLocalStorage } from '../utils';
import * as API from '../constants/API';
import * as API from '../constants/api';
import { storeUsers, storeReferences, storeZones } from '../actions';
import * as schema from './schema';

Expand Down
2 changes: 1 addition & 1 deletion src/actionCreators/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import { normalize } from 'normalizr';
import * as schema from './schema';
import * as actions from '../actions';
import * as reducerTypes from '../constants/reducerTypes';
import * as API from '../constants/API';
import * as API from '../constants/api';
import { getIsFetchingAgreements } from '../reducers/rootReducer';
import { axios, saveUserProfileInLocal, createConfigWithHeader } from '../utils';
import { toastSuccessMessage, toastErrorMessage } from './toastActionCreator';
Expand Down
2 changes: 1 addition & 1 deletion src/actionCreators/planActionCreator.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { success, request, error, storePlan } from '../actions';
import { UPDATE_PLAN_STATUS_SUCCESS, UPDATE_AGREEMENT_ZONE_SUCCESS } from '../constants/strings';
import { toastSuccessMessage, toastErrorMessage } from './toastActionCreator';
import * as reducerTypes from '../constants/reducerTypes';
import * as API from '../constants/API';
import * as API from '../constants/api';
import * as schema from './schema';
import { getPasturesMap, getGrazingSchedulesMap, getMinisterIssuesMap, getReferences } from '../reducers/rootReducer';
import { REFERENCE_KEY, PLAN_STATUS, AMENDMENT_TYPE } from '../constants/variables';
Expand Down
2 changes: 1 addition & 1 deletion src/components/Login.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { connect } from 'react-redux';
import PropTypes from 'prop-types';
import { Button, Segment } from 'semantic-ui-react';
import { Loading } from './common';
import { SSO_LOGIN_ENDPOINT, SSO_IDIR_LOGIN_ENDPOINT, SSO_BCEID_LOGIN_ENDPOINT } from '../constants/API';
import { SSO_LOGIN_ENDPOINT, SSO_IDIR_LOGIN_ENDPOINT, SSO_BCEID_LOGIN_ENDPOINT } from '../constants/api';
import { ELEMENT_ID, IMAGE_SRC } from '../constants/variables';
import { storeAuthData } from '../actions';
import { fetchUser } from '../actionCreators';
Expand Down
2 changes: 1 addition & 1 deletion src/components/Navbar.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { Dropdown } from 'semantic-ui-react';
import { Avatar } from './common';
import * as Routes from '../constants/routes';
import { IMAGE_SRC, ELEMENT_ID } from '../constants/variables';
import { SITEMINDER_LOGOUT_ENDPOINT } from '../constants/API';
import { SITEMINDER_LOGOUT_ENDPOINT } from '../constants/api';
import { getUser } from '../reducers/rootReducer';
import { isUserAdmin, isUserActive } from '../utils';
import { signOut } from '../actionCreators';
Expand Down
2 changes: 1 addition & 1 deletion src/components/ReturnPage.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import { parseQuery, getTokenFromSSO, saveAuthDataInLocal } from '../utils';
import { SSO_LOGOUT_ENDPOINT } from '../constants/API';
import { SSO_LOGOUT_ENDPOINT } from '../constants/api';
import { REDIRECTING } from '../constants/strings';

const propTypes = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ import MinorTabsForMultiple from './MinorTabsForMultiple';
import MandatoryTabsForSingle from './MandatoryTabsForSingle';
import { isSingleClient, isMinorAmendment, isMandatoryAmendment } from '../../../utils';

/* eslint-disable jsx-a11y/label-has-for */
/* eslint-disable jsx-a11y/label-has-associated-control */
/* eslint-disable jsx-a11y/label-has-for, jsx-a11y/label-has-associated-control */
const propTypes = {
user: PropTypes.shape({}).isRequired,
open: PropTypes.bool.isRequired,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ import React, { Component } from 'react';
// import { Button, Radio, Form, Icon, Checkbox } from 'semantic-ui-react';
// import { PLAN_STATUS } from '../../../constants/variables';

/* eslint-disable jsx-a11y/label-has-for */
/* eslint-disable jsx-a11y/label-has-associated-control */
/* eslint-disable jsx-a11y/label-has-for, jsx-a11y/label-has-associated-control */
const propTypes = {
// clients: PropTypes.arrayOf(PropTypes.object),
// activeTab: PropTypes.number.isRequired,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ import PropTypes from 'prop-types';
import { Button, Radio, Form, Icon, Checkbox } from 'semantic-ui-react';
import { PLAN_STATUS } from '../../../constants/variables';

/* eslint-disable jsx-a11y/label-has-for */
/* eslint-disable jsx-a11y/label-has-associated-control */
/* eslint-disable jsx-a11y/label-has-for, jsx-a11y/label-has-associated-control */
const propTypes = {
activeTab: PropTypes.number.isRequired,
isAgreed: PropTypes.bool.isRequired,
Expand Down
1 change: 1 addition & 0 deletions src/constants/API.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// export const SSO_BASE_URL = 'https://dev-sso.pathfinder.gov.bc.ca';
// export const SSO_REALM_NAME = 'mobile';
// export const SSO_CLIENT_ID = 'range-test';

export const SSO_BASE_URL = 'https://sso-dev.pathfinder.gov.bc.ca';
export const SSO_REALM_NAME = 'range';
export const SSO_CLIENT_ID = 'myrangebc';
Expand Down
2 changes: 1 addition & 1 deletion src/tests/intergration/Home.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { getAgreements } from '../../reducers/rootReducer';
import { configureMockStore, flushAllPromises } from '../helpers/utils';
import { ELEMENT_ID } from '../../constants/variables';
import { mockRequestHeader, mockAgreements, mockAgreement, mockReference } from './mockData';
import * as API from '../../constants/API';
import * as API from '../../constants/api';

jest.mock('lodash.debounce');

Expand Down
14 changes: 14 additions & 0 deletions src/tests/reducers/planReducer.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const initialState = {
pastures: {},
grazingSchedules: {},
ministerIssues: {},
confirmations: {},
};

const mockState = {
Expand All @@ -22,6 +23,7 @@ const mockState = {
pastures: ['pasture_id'],
grazingSchedules: ['grazing_schedule_id'],
ministerIssues: ['minister_issue_id'],
confirmations: ['confirmation_id'],
},
},
allIds: ['plan_id'],
Expand Down Expand Up @@ -50,6 +52,12 @@ const mockState = {
detail: 'detail',
},
},
confirmations: {
'confirmation_id': {
id: 'confirmation_id',
clientId: '00000000',
},
},
};
const mockPlanData = {
id: 'plan_id',
Expand Down Expand Up @@ -78,6 +86,12 @@ const mockPlanData = {
detail: 'detail',
},
],
confirmations: [
{
id: 'confirmation_id',
clientId: '00000000',
},
],
};
describe('Plan reducer', () => {
it('Initialized with the initial state', () => {
Expand Down
36 changes: 36 additions & 0 deletions src/tests/unit/localStorage.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/* eslint-disable no-underscore-dangle, dot-notation */

import {
saveDataInLocalStorage,
getDataFromLocalStorage,
deleteDataFromLocalStorage,
} from '../../utils';
// Please view https://www.npmjs.com/package/jest-localstorage-mock for usage of the local storage mock
beforeEach(() => {
localStorage.clear();
saveDataInLocalStorage('validKey', { a: 123 });
saveDataInLocalStorage('anotherKey', 'a random string');
});

describe('Local Storage Helper Methods', () => {
test('saveDataInLocalStorage stores data that is passed in', () => {
expect(localStorage.__STORE__['validKey']).toBe(JSON.stringify({ a: 123 }));
expect(localStorage.__STORE__['anotherKey']).toBe('a random string');
});

test('getDataInLocalStorage gets data when passed a valid key', () => {
expect(getDataFromLocalStorage('validKey')).toEqual({ a: 123 });
expect(getDataFromLocalStorage('anotherKey')).toBe('a random string');
});

test('getDataFromLocalStorage returns undefined when an invalid key is passed in', () => {
expect(getDataFromLocalStorage('asdlkjf;asd')).toBe(undefined);
expect(getDataFromLocalStorage(123123)).toBe(undefined);
expect(getDataFromLocalStorage({ a: true })).toBe(undefined);
});

test('deleteDataFromLocalStorage deletes data from storage when key is passed in', () => {
deleteDataFromLocalStorage('validKey');
expect(getDataFromLocalStorage('validKey')).toBe(undefined);
});
});
8 changes: 5 additions & 3 deletions src/utils/authentication.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import {
SSO_CLIENT_ID,
GET_TOKEN_FROM_SSO,
REFRESH_TOKEN_FROM_SSO,
} from '../constants/API';
} from '../constants/api';
import { saveDataInLocalStorage, getDataFromLocalStorage } from './localStorage';
import { stringifyQuery } from './index';
import { LOCAL_STORAGE_KEY } from '../constants/variables';
Expand Down Expand Up @@ -160,8 +160,9 @@ export const registerAxiosInterceptors = (logout) => {
axios.interceptors.request.use((c) => {
const config = { ...c };
if (isTokenExpired() && !config.isRetry && isRangeAPIs()) {
const refreshToken = getRefreshTokenFromLocal();
if (process.env.NODE_ENV !== 'production') console.log('Access token is expired. Trying to refresh it');

const refreshToken = getRefreshTokenFromLocal();
return refreshAccessToken(refreshToken).then(
(response) => {
saveAuthDataInLocal(response);
Expand All @@ -173,8 +174,9 @@ export const registerAxiosInterceptors = (logout) => {
return config;
},
(err) => {
logout();
if (process.env.NODE_ENV !== 'production') console.log('Refresh token is also expired. Signing out.');

logout();
return err;
},
);
Expand Down
2 changes: 1 addition & 1 deletion src/utils/axios.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import axios from 'axios';
import { API_BASE_URL } from '../constants/API';
import { API_BASE_URL } from '../constants/api';

const instance = axios.create({
baseURL: API_BASE_URL,
Expand Down

0 comments on commit eced189

Please sign in to comment.