Commit e5eb8437 authored by Scott Hampton's avatar Scott Hampton

Handle when there is no endpoint

When there is no base/head endpoint, we don't
have any reports to compare with. We now
handle that condition with an error and message.
parent aab2067b
import axios from '~/lib/utils/axios_utils';
import * as types from './mutation_types';
import { parseAccessibilityReport, compareAccessibilityReports } from './utils';
import { s__ } from '~/locale';
export const setBaseEndpoint = ({ commit }, endpoint) => commit(types.SET_BASE_ENDPOINT, endpoint);
export const setHeadEndpoint = ({ commit }, endpoint) => commit(types.SET_HEAD_ENDPOINT, endpoint);
......@@ -8,6 +9,15 @@ export const setHeadEndpoint = ({ commit }, endpoint) => commit(types.SET_HEAD_E
export const fetchReport = ({ state, dispatch, commit }) => {
commit(types.REQUEST_REPORT);
// If we don't have both endpoints, throw an error.
if (!state.baseEndpoint || !state.headEndpoint) {
commit(
types.RECEIVE_REPORT_ERROR,
s__('AccessibilityReport|Accessibility report artifact not found'),
);
return;
}
Promise.all([
axios.get(state.baseEndpoint).then(response => ({
...response.data,
......@@ -19,7 +29,12 @@ export const fetchReport = ({ state, dispatch, commit }) => {
})),
])
.then(responses => dispatch('receiveReportSuccess', responses))
.catch(() => commit(types.RECEIVE_REPORT_ERROR));
.catch(() =>
commit(
types.RECEIVE_REPORT_ERROR,
s__('AccessibilityReport|Failed to retrieve accessibility report'),
),
);
};
export const receiveReportSuccess = ({ commit }, responses) => {
......
......@@ -15,9 +15,10 @@ export default {
state.isLoading = false;
state.report = report;
},
[types.RECEIVE_REPORT_ERROR](state) {
[types.RECEIVE_REPORT_ERROR](state, message) {
state.isLoading = false;
state.hasError = true;
state.errorMessage = message;
state.report = {};
},
};
......@@ -1049,6 +1049,12 @@ msgstr ""
msgid "AccessTokens|reset it"
msgstr ""
msgid "AccessibilityReport|Accessibility report artifact not found"
msgstr ""
msgid "AccessibilityReport|Failed to retrieve accessibility report"
msgstr ""
msgid "AccessibilityReport|Learn More"
msgstr ""
......
......@@ -55,6 +55,30 @@ describe('Accessibility Reports actions', () => {
mock.restore();
});
describe('when no endpoints are given', () => {
beforeEach(() => {
localState.baseEndpoint = null;
localState.headEndpoint = null;
});
it('should commit REQUEST_REPORT and RECEIVE_REPORT_ERROR mutations', done => {
testAction(
actions.fetchReport,
null,
localState,
[
{ type: types.REQUEST_REPORT },
{
type: types.RECEIVE_REPORT_ERROR,
payload: 'Accessibility report artifact not found',
},
],
[],
done,
);
});
});
describe('success', () => {
it('should commit REQUEST_REPORT mutation and dispatch receiveReportSuccess', done => {
const data = { report: { summary: {} } };
......@@ -84,7 +108,13 @@ describe('Accessibility Reports actions', () => {
actions.fetchReport,
null,
localState,
[{ type: types.REQUEST_REPORT }, { type: types.RECEIVE_REPORT_ERROR }],
[
{ type: types.REQUEST_REPORT },
{
type: types.RECEIVE_REPORT_ERROR,
payload: 'Failed to retrieve accessibility report',
},
],
[],
done,
);
......
......@@ -69,5 +69,11 @@ describe('Accessibility Reports mutations', () => {
expect(localState.hasError).toEqual(true);
});
it('sets errorMessage to given message', () => {
mutations.RECEIVE_REPORT_ERROR(localState, 'message');
expect(localState.errorMessage).toEqual('message');
});
});
});
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