Skip to content

Commit

Permalink
fix(rhsmActions): issues/185 combine actions, promise.all (#201)
Browse files Browse the repository at this point in the history
* graphCard, switch over to using getGraphReportsCapacity
* rhsmActions, getGraphReportsCapacity promise.all action
* reduxHelpers, expand to work with Promise.all array responses
* graphReducer, apply combined report capacity type
* graphCardSelectors, combined response clean up
* rhsmTypes, add combined report capacity type
  • Loading branch information
cdcabrera committed Feb 12, 2020
1 parent 5396223 commit d478ed3
Show file tree
Hide file tree
Showing 15 changed files with 502 additions and 471 deletions.
14 changes: 5 additions & 9 deletions src/components/graphCard/graphCard.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,15 @@ class GraphCard extends React.Component {
}

onUpdateGraphData = () => {
const { getGraphCapacity, getGraphReports, graphGranularity, startDate, endDate, productId } = this.props;
const { getGraphReportsCapacity, graphGranularity, startDate, endDate, productId } = this.props;
const query = {
[rhsmApiTypes.RHSM_API_QUERY_GRANULARITY]: graphGranularity,
[rhsmApiTypes.RHSM_API_QUERY_START_DATE]: startDate.toISOString(),
[rhsmApiTypes.RHSM_API_QUERY_END_DATE]: endDate.toISOString()
};

if (productId) {
getGraphCapacity(productId, query);
getGraphReports(productId, query);
getGraphReportsCapacity(productId, query);
}
};

Expand Down Expand Up @@ -164,8 +163,7 @@ GraphCard.propTypes = {
stroke: PropTypes.string
})
),
getGraphCapacity: PropTypes.func,
getGraphReports: PropTypes.func,
getGraphReportsCapacity: PropTypes.func,
graphData: PropTypes.object,
graphGranularity: PropTypes.oneOf([
GRANULARITY_TYPES.DAILY,
Expand All @@ -186,8 +184,7 @@ GraphCard.defaultProps = {
cardTitle: null,
error: false,
filterGraphData: [],
getGraphCapacity: helpers.noop,
getGraphReports: helpers.noop,
getGraphReportsCapacity: helpers.noop,
graphData: {},
graphGranularity: GRANULARITY_TYPES.DAILY,
pending: false,
Expand All @@ -208,8 +205,7 @@ const makeMapStateToProps = () => {
};

const mapDispatchToProps = dispatch => ({
getGraphCapacity: (id, query) => dispatch(reduxActions.rhsm.getGraphCapacity(id, query)),
getGraphReports: (id, query) => dispatch(reduxActions.rhsm.getGraphReports(id, query))
getGraphReportsCapacity: (id, query) => dispatch(reduxActions.rhsm.getGraphReportsCapacity(id, query))
});

const ConnectedGraphCard = connectTranslate(makeMapStateToProps, mapDispatchToProps)(GraphCard);
Expand Down
4 changes: 2 additions & 2 deletions src/components/i18n/__tests__/__snapshots__/i18n.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ msgstr \\"\\"
msgid \\"curiosity-graph.dropdownMonthly\\"
msgstr \\"\\"
#: src/components/graphCard/graphCard.js:131
#: src/components/graphCard/graphCard.js:135
#: src/components/graphCard/graphCard.js:130
#: src/components/graphCard/graphCard.js:134
msgid \\"curiosity-graph.dropdownPlaceholder\\"
msgstr \\"\\"
Expand Down
28 changes: 19 additions & 9 deletions src/redux/actions/__tests__/rhsmActions.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,22 +19,32 @@ describe('RhsmActions', () => {
beforeEach(() => {
moxios.install();

moxios.wait(() => {
const request = moxios.requests.mostRecent();
request.respondWith({
status: 200,
response: {
test: 'success',
[rhsmApiTypes.RHSM_API_RESPONSE_PRODUCTS_DATA]: ['success']
}
});
moxios.stubRequest(/\/(tally|capacity|version).*?/, {
status: 200,
responseText: 'success',
timeout: 1,
response: {
test: 'success',
[rhsmApiTypes.RHSM_API_RESPONSE_PRODUCTS_DATA]: ['success']
}
});
});

afterEach(() => {
moxios.uninstall();
});

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

dispatcher(store.dispatch).then(() => {
const response = store.getState().graph;
expect(response.reportCapacity.fulfilled).toBe(true);
done();
});
});

it('Should return response content for getGraphReports method', done => {
const store = generateStore();
const dispatcher = rhsmActions.getGraphReports();
Expand Down
21 changes: 19 additions & 2 deletions src/redux/actions/rhsmActions.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,23 @@
import { rhsmTypes } from '../types';
import { rhsmServices } from '../../services/rhsmServices';

const getGraphReportsCapacity = (id = null, query = {}) => dispatch =>
dispatch({
type: rhsmTypes.GET_GRAPH_REPORT_CAPACITY_RHSM,
payload: Promise.all([rhsmServices.getGraphReports(id, query), rhsmServices.getGraphCapacity(id, query)]),
meta: {
data: { id },
query,
notifications: {
rejected: {
variant: 'info',
title: 'Reporting and capacity connection has failed',
description: `Product ID: ${id}`
}
}
}
});

const getGraphReports = (id = null, query = {}) => dispatch =>
dispatch({
type: rhsmTypes.GET_GRAPH_REPORT_RHSM,
Expand Down Expand Up @@ -35,6 +52,6 @@ const getGraphCapacity = (id = null, query = {}) => dispatch =>
}
});

const rhsmActions = { getGraphCapacity, getGraphReports };
const rhsmActions = { getGraphReportsCapacity, getGraphCapacity, getGraphReports };

export { rhsmActions as default, rhsmActions, getGraphCapacity, getGraphReports };
export { rhsmActions as default, rhsmActions, getGraphReportsCapacity, getGraphCapacity, getGraphReports };
17 changes: 17 additions & 0 deletions src/redux/common/__tests__/__snapshots__/reduxHelpers.test.js.snap
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`ReduxHelpers should get a date from a service call response: ARRAY date 1`] = `"Fri, 07 Feb 2020 10:46:53 GMT"`;

exports[`ReduxHelpers should get a date from a service call response: date 1`] = `"Fri, 07 Feb 2020 10:46:53 GMT"`;

exports[`ReduxHelpers should get a http status from a service call response: ARRAY mismatched status 1`] = `400`;

exports[`ReduxHelpers should get a http status from a service call response: ARRAY status 1`] = `200`;

exports[`ReduxHelpers should get a http status from a service call response: fallback status 1`] = `200`;

exports[`ReduxHelpers should get a http status from a service call response: missing status 1`] = `0`;
Expand All @@ -12,6 +20,12 @@ exports[`ReduxHelpers should get a message from a service call response: 4XX fal

exports[`ReduxHelpers should get a message from a service call response: 5XX fallback message 1`] = `"Internal Server Error"`;

exports[`ReduxHelpers should get a message from a service call response: ARRAY mismatched response messages 1`] = `"Request failed with status code 400"`;

exports[`ReduxHelpers should get a message from a service call response: ARRAY response for 2XX 1`] = `"OK"`;

exports[`ReduxHelpers should get a message from a service call response: ARRAY response for 4XX 1`] = `"Request failed with status code 400"`;

exports[`ReduxHelpers should get a message from a service call response: response for 2XX 1`] = `"OK"`;

exports[`ReduxHelpers should get a message from a service call response: response for 4XX 1`] = `"Request failed with status code 400"`;
Expand All @@ -23,9 +37,12 @@ Object {
"PENDING_ACTION": [Function],
"REJECTED_ACTION": [Function],
"generatedPromiseActionReducer": [Function],
"getDateFromResults": [Function],
"getMessageFromResults": [Function],
"getSingleResponseFromResultArray": [Function],
"getStatusFromResults": [Function],
"setStateProp": [Function],
"singlePromiseDataResponseFromArray": [Function],
}
`;

Expand Down
97 changes: 97 additions & 0 deletions src/redux/common/__tests__/reduxHelpers.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,21 @@ describe('ReduxHelpers', () => {
})
).toMatchSnapshot('response for 2XX');

expect(
reduxHelpers.getMessageFromResults([
{
status: 200,
statusText: 'OK',
data: { lorem: 'ipsum', dolor: 'sit' }
},
{
status: 200,
statusText: 'OK',
data: { hello: 'world' }
}
])
).toMatchSnapshot('ARRAY response for 2XX');

expect(
reduxHelpers.getMessageFromResults({
response: {
Expand All @@ -34,6 +49,23 @@ describe('ReduxHelpers', () => {
})
).toMatchSnapshot('response for 4XX');

expect(
reduxHelpers.getMessageFromResults([
{
status: 400,
statusText: 'Bad Request',
data: { lorem: 'ipsum', dolor: 'sit' },
message: 'Request failed with status code 400'
},
{
status: 400,
statusText: 'Bad Request',
data: { hello: 'world' },
message: 'Request failed with status code 400'
}
])
).toMatchSnapshot('ARRAY response for 4XX');

expect(
reduxHelpers.getMessageFromResults({
response: {
Expand All @@ -60,6 +92,22 @@ describe('ReduxHelpers', () => {
}
})
).toMatchSnapshot('5XX fallback message');

expect(
reduxHelpers.getMessageFromResults([
{
status: 200,
statusText: 'OK',
data: { lorem: 'ipsum', dolor: 'sit' }
},
{
status: 400,
statusText: 'Bad Request',
data: { hello: 'world' },
message: 'Request failed with status code 400'
}
])
).toMatchSnapshot('ARRAY mismatched response messages');
});

it('should get a http status from a service call response', () => {
Expand All @@ -71,13 +119,62 @@ describe('ReduxHelpers', () => {
})
).toMatchSnapshot('status');

expect(
reduxHelpers.getStatusFromResults([
{
status: 200
},
{
status: 200
}
])
).toMatchSnapshot('ARRAY status');

expect(
reduxHelpers.getStatusFromResults({
status: 200
})
).toMatchSnapshot('fallback status');

expect(reduxHelpers.getStatusFromResults({})).toMatchSnapshot('missing status');

expect(
reduxHelpers.getStatusFromResults([
{
status: 200
},
{
status: 400
}
])
).toMatchSnapshot('ARRAY mismatched status');
});

it('should get a date from a service call response', () => {
expect(
reduxHelpers.getDateFromResults({
headers: {
date: 'Fri, 07 Feb 2020 10:46:53 GMT'
}
})
).toMatchSnapshot('date');

expect(
reduxHelpers.getDateFromResults([
{
headers: {
date: 'Fri, 07 Feb 2020 10:46:53 GMT'
},
status: 200
},
{
headers: {
date: 'Fri, 07 Feb 2020 10:46:53 GMT'
},
status: 200
}
])
).toMatchSnapshot('ARRAY date');
});

it('should update a state object', () => {
Expand Down
Loading

0 comments on commit d478ed3

Please sign in to comment.