Commit fefbc89b authored by ap4y's avatar ap4y

Improve NOT_FOUND response handling on ThreatMonitoring page

NOT_FOUND responses use the same code path as successful responses but
provide unexpected payload. This patch normalises payloads between 2
flows and improves this logic by letting mutator decide which code
path to use.
parent b9997292
......@@ -3,7 +3,6 @@ import pollUntilComplete from '~/lib/utils/poll_until_complete';
import httpStatusCodes from '~/lib/utils/http_status';
import createFlash from '~/flash';
import * as types from './mutation_types';
import createState from './state';
import { getTimeWindowParams } from '../../utils';
export const requestStatistics = ({ commit }, timeWindowParams) => {
......@@ -40,7 +39,7 @@ export const fetchStatistics = ({ state, dispatch, rootState }) => {
// no data, but we can't distinguish between them, yet. So, just render
// no data.
if (error.response.status === httpStatusCodes.NOT_FOUND) {
dispatch('receiveStatisticsSuccess', createState().statistics);
dispatch('receiveStatisticsSuccess', null);
} else {
dispatch('receiveStatisticsError');
}
......
import * as types from './mutation_types';
import createState from './state';
export default transformFunc => ({
[types.SET_ENDPOINT](state, endpoint) {
......@@ -10,7 +11,8 @@ export default transformFunc => ({
state.timeRange = timeRange;
},
[types.RECEIVE_STATISTICS_SUCCESS](state, payload) {
state.statistics = transformFunc(payload);
const stats = payload ? transformFunc(payload) : createState().statistics;
state.statistics = stats;
state.isLoadingStatistics = false;
state.errorLoadingStatistics = false;
},
......
......@@ -140,17 +140,7 @@ describe('threatMonitoringStatistics actions', () => {
[],
[
{ type: 'requestStatistics', payload: expect.any(Object) },
{
type: 'receiveStatisticsSuccess',
payload: expect.objectContaining({
total: 0,
anomalous: 0,
history: {
nominal: [],
anomalous: [],
},
}),
},
{ type: 'receiveStatisticsSuccess', payload: null },
],
));
});
......
......@@ -53,6 +53,31 @@ describe('threatMonitoringStatistics mutations', () => {
it('sets errorLoadingStatistics to false', () => {
expect(state.errorLoadingStatistics).toBe(false);
});
describe('with null payload', () => {
beforeEach(() => {
mutations[types.RECEIVE_STATISTICS_SUCCESS](state, null);
});
it('sets statistics to the empty state', () => {
expect(state.statistics).toEqual({
total: 0,
anomalous: 0,
history: {
nominal: [],
anomalous: [],
},
});
});
it('sets isLoadingStatistics to false', () => {
expect(state.isLoadingStatistics).toBe(false);
});
it('sets errorLoadingStatistics to false', () => {
expect(state.errorLoadingStatistics).toBe(false);
});
});
});
describe(types.RECEIVE_STATISTICS_ERROR, () => {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment