Skip to content

Commit

Permalink
refactor(services): move config to serviceConfig
Browse files Browse the repository at this point in the history
* authentication, flip to platform redux action
* platform, userActions, move authorizeUser
* platform, userTypes, move authorize user type
* userReducer, flip to platform redux types
* userSelectors, passthrough for transformed responses
* services, move config to serviceConfig
* serviceConfig, expand cancel ability to url funcs
* platformServices, schemas, transformers, mock calls
* rhsmServices, testing naming for consistency
* userServices, remove deprecated authorizeUser func
  • Loading branch information
cdcabrera committed Mar 16, 2022
1 parent 1064a6f commit d952387
Show file tree
Hide file tree
Showing 40 changed files with 1,569 additions and 1,280 deletions.
2 changes: 1 addition & 1 deletion src/components/authentication/authentication.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ Authentication.propTypes = {
*/
Authentication.defaultProps = {
appName: routerHelpers.appName,
authorizeUser: reduxActions.user.authorizeUser,
authorizeUser: reduxActions.platform.authorizeUser,
hideGlobalFilter: reduxActions.platform.hideGlobalFilter,
initializeChrome: reduxActions.platform.initializeChrome,
isDisabled: helpers.UI_DISABLED,
Expand Down
24 changes: 24 additions & 0 deletions src/redux/actions/__tests__/platformActions.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,30 @@
import promiseMiddleware from 'redux-promise-middleware';
import { applyMiddleware, combineReducers, createStore } from 'redux';
import { platformActions } from '../platformActions';
import { userReducer } from '../../reducers';

describe('PlatformActions', () => {
const middleware = [promiseMiddleware];
const generateStore = () =>
createStore(
combineReducers({
user: userReducer
}),
applyMiddleware(...middleware)
);

it('Should return response content for authorizeUser method', done => {
const store = generateStore();
const dispatcher = platformActions.authorizeUser();

dispatcher(store.dispatch).then(() => {
const response = store.getState().user;

expect(response.session.fulfilled).toBe(true);
done();
});
});

it('Should return a dispatch object for the hideGlobalFilter method', () => {
expect(platformActions.hideGlobalFilter()).toMatchSnapshot('dispatch object');
});
Expand Down
12 changes: 0 additions & 12 deletions src/redux/actions/__tests__/userActions.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,6 @@ describe('UserActions', () => {
moxios.uninstall();
});

it('Should return response content for authorizeUser method', done => {
const store = generateStore();
const dispatcher = userActions.authorizeUser();

dispatcher(store.dispatch).then(() => {
const response = store.getState().user;

expect(response.session.fulfilled).toBe(true);
done();
});
});

it('Should return response content for deleteAccountOptIn method', done => {
const store = generateStore();
const dispatcher = userActions.deleteAccountOptIn();
Expand Down
14 changes: 14 additions & 0 deletions src/redux/actions/platformActions.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,18 @@ const removeNotification = id => RcsRemoveNotification(id);
*/
const clearNotifications = () => RcsClearNotifications();

/**
* Get an emulated and combined API response from the platforms "getUser" and "getUserPermissions" global methods.
*
* @param {string|Array} appName
* @returns {Function}
*/
const authorizeUser = appName => dispatch =>
dispatch({
type: platformTypes.PLATFORM_USER_AUTH,
payload: Promise.all([platformServices.getUser(), platformServices.getUserPermissions(appName)])
});

/**
* Hide platform global filter.
*
Expand Down Expand Up @@ -103,6 +115,7 @@ const platformActions = {
addNotification,
removeNotification,
clearNotifications,
authorizeUser,
hideGlobalFilter,
initializeChrome,
onNavigation,
Expand All @@ -116,6 +129,7 @@ export {
addNotification,
removeNotification,
clearNotifications,
authorizeUser,
hideGlobalFilter,
initializeChrome,
onNavigation,
Expand Down
23 changes: 2 additions & 21 deletions src/redux/actions/userActions.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,6 @@ import { userServices } from '../../services/user/userServices';
import { helpers } from '../../common/helpers';
import { translate } from '../../components/i18n/i18n';

/**
* Get an emulated API response from the platforms "getUser" method.
*
* @returns {Function}
*/
const authorizeUser = () => dispatch =>
dispatch({
type: userTypes.USER_AUTH,
payload: userServices.authorizeUser()
});

/**
* Get a user's locale.
*
Expand Down Expand Up @@ -84,14 +73,6 @@ const updateAccountOptIn =
}
});

const userActions = { authorizeUser, getLocale, deleteAccountOptIn, getAccountOptIn, updateAccountOptIn };
const userActions = { getLocale, deleteAccountOptIn, getAccountOptIn, updateAccountOptIn };

export {
userActions as default,
userActions,
authorizeUser,
getLocale,
deleteAccountOptIn,
getAccountOptIn,
updateAccountOptIn
};
export { userActions as default, userActions, getLocale, deleteAccountOptIn, getAccountOptIn, updateAccountOptIn };
78 changes: 39 additions & 39 deletions src/redux/reducers/__tests__/__snapshots__/userReducer.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -40,42 +40,42 @@ Object {
}
`;

exports[`UserReducer should handle all defined error types: rejected types UPDATE_USER_OPTIN 1`] = `
exports[`UserReducer should handle all defined error types: rejected types PLATFORM_USER_AUTH 1`] = `
Object {
"result": Object {
"optin": Object {
"optin": Object {},
"session": Object {
"error": true,
"errorCodes": Array [],
"errorMessage": "MESSAGE",
"fulfilled": false,
"locale": null,
"meta": Object {},
"pending": false,
"status": 0,
},
"session": Object {
"errorCodes": Array [],
"locale": null,
},
},
"type": "UPDATE_USER_OPTIN_REJECTED",
"type": "PLATFORM_USER_AUTH_REJECTED",
}
`;

exports[`UserReducer should handle all defined error types: rejected types USER_AUTH 1`] = `
exports[`UserReducer should handle all defined error types: rejected types UPDATE_USER_OPTIN 1`] = `
Object {
"result": Object {
"optin": Object {},
"session": Object {
"optin": Object {
"error": true,
"errorCodes": Array [],
"errorMessage": "MESSAGE",
"fulfilled": false,
"locale": null,
"meta": Object {},
"pending": false,
"status": 0,
},
"session": Object {
"errorCodes": Array [],
"locale": null,
},
},
"type": "USER_AUTH_REJECTED",
"type": "UPDATE_USER_OPTIN_REJECTED",
}
`;

Expand Down Expand Up @@ -143,10 +143,11 @@ Object {
}
`;

exports[`UserReducer should handle all defined fulfilled types: fulfilled types UPDATE_USER_OPTIN 1`] = `
exports[`UserReducer should handle all defined fulfilled types: fulfilled types PLATFORM_USER_AUTH 1`] = `
Object {
"result": Object {
"optin": Object {
"optin": Object {},
"session": Object {
"data": Object {
"permissions": Array [],
"test": "success",
Expand All @@ -160,26 +161,23 @@ Object {
},
"date": null,
"error": false,
"errorCodes": Array [],
"errorMessage": "",
"fulfilled": true,
"locale": null,
"meta": Object {},
"pending": false,
"status": 0,
},
"session": Object {
"errorCodes": Array [],
"locale": null,
},
},
"type": "UPDATE_USER_OPTIN_FULFILLED",
"type": "PLATFORM_USER_AUTH_FULFILLED",
}
`;

exports[`UserReducer should handle all defined fulfilled types: fulfilled types USER_AUTH 1`] = `
exports[`UserReducer should handle all defined fulfilled types: fulfilled types UPDATE_USER_OPTIN 1`] = `
Object {
"result": Object {
"optin": Object {},
"session": Object {
"optin": Object {
"data": Object {
"permissions": Array [],
"test": "success",
Expand All @@ -193,16 +191,18 @@ Object {
},
"date": null,
"error": false,
"errorCodes": Array [],
"errorMessage": "",
"fulfilled": true,
"locale": null,
"meta": Object {},
"pending": false,
"status": 0,
},
"session": Object {
"errorCodes": Array [],
"locale": null,
},
},
"type": "USER_AUTH_FULFILLED",
"type": "UPDATE_USER_OPTIN_FULFILLED",
}
`;

Expand Down Expand Up @@ -267,40 +267,40 @@ Object {
}
`;

exports[`UserReducer should handle all defined pending types: pending types UPDATE_USER_OPTIN 1`] = `
exports[`UserReducer should handle all defined pending types: pending types PLATFORM_USER_AUTH 1`] = `
Object {
"result": Object {
"optin": Object {
"optin": Object {},
"session": Object {
"error": false,
"errorCodes": Array [],
"errorMessage": "",
"fulfilled": false,
"locale": null,
"meta": Object {},
"pending": true,
},
"session": Object {
"errorCodes": Array [],
"locale": null,
},
},
"type": "UPDATE_USER_OPTIN_PENDING",
"type": "PLATFORM_USER_AUTH_PENDING",
}
`;

exports[`UserReducer should handle all defined pending types: pending types USER_AUTH 1`] = `
exports[`UserReducer should handle all defined pending types: pending types UPDATE_USER_OPTIN 1`] = `
Object {
"result": Object {
"optin": Object {},
"session": Object {
"optin": Object {
"error": false,
"errorCodes": Array [],
"errorMessage": "",
"fulfilled": false,
"locale": null,
"meta": Object {},
"pending": true,
},
"session": Object {
"errorCodes": Array [],
"locale": null,
},
},
"type": "USER_AUTH_PENDING",
"type": "UPDATE_USER_OPTIN_PENDING",
}
`;

Expand Down
18 changes: 14 additions & 4 deletions src/redux/reducers/__tests__/userReducer.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import userReducer from '../userReducer';
import { rhsmApiTypes } from '../../../types';
import { platformConstants as platformApiTypes } from '../../../services/platform/platformConstants';
import { appTypes, userTypes as types } from '../../types';
import { appTypes, platformTypes, userTypes as types } from '../../types';
import { reduxHelpers } from '../../common/reduxHelpers';

describe('UserReducer', () => {
Expand Down Expand Up @@ -44,7 +44,12 @@ describe('UserReducer', () => {
});

it('should handle all defined error types', () => {
const specificTypes = [types.USER_AUTH, types.DELETE_USER_OPTIN, types.GET_USER_OPTIN, types.UPDATE_USER_OPTIN];
const specificTypes = [
platformTypes.PLATFORM_USER_AUTH,
types.DELETE_USER_OPTIN,
types.GET_USER_OPTIN,
types.UPDATE_USER_OPTIN
];

specificTypes.forEach(value => {
const dispatched = {
Expand All @@ -71,7 +76,12 @@ describe('UserReducer', () => {
});

it('should handle all defined pending types', () => {
const specificTypes = [types.USER_AUTH, types.DELETE_USER_OPTIN, types.GET_USER_OPTIN, types.UPDATE_USER_OPTIN];
const specificTypes = [
platformTypes.PLATFORM_USER_AUTH,
types.DELETE_USER_OPTIN,
types.GET_USER_OPTIN,
types.UPDATE_USER_OPTIN
];

specificTypes.forEach(value => {
const dispatched = {
Expand All @@ -88,7 +98,7 @@ describe('UserReducer', () => {

it('should handle all defined fulfilled types', () => {
const specificTypes = [
types.USER_AUTH,
platformTypes.PLATFORM_USER_AUTH,
types.USER_LOCALE,
types.DELETE_USER_OPTIN,
types.GET_USER_OPTIN,
Expand Down
4 changes: 2 additions & 2 deletions src/redux/reducers/userReducer.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import _get from 'lodash/get';
import { appTypes, userTypes } from '../types';
import { appTypes, platformTypes, userTypes } from '../types';
import { rhsmApiTypes } from '../../types/rhsmApiTypes';
import { reduxHelpers } from '../common/reduxHelpers';

Expand Down Expand Up @@ -72,7 +72,7 @@ const userReducer = (state = initialState, action) => {
{ ref: 'optin', type: userTypes.DELETE_USER_OPTIN },
{ ref: 'optin', type: userTypes.GET_USER_OPTIN },
{ ref: 'optin', type: userTypes.UPDATE_USER_OPTIN },
{ ref: 'session', type: userTypes.USER_AUTH }
{ ref: 'session', type: platformTypes.PLATFORM_USER_AUTH }
],
state,
action
Expand Down
Loading

0 comments on commit d952387

Please sign in to comment.