Commit c752700b authored by Phil Hughes's avatar Phil Hughes

Merge branch 'mw-cr-refactor-mr-actions' into 'master'

Code Review Analytics: Refactor merge requests actions

See merge request gitlab-org/gitlab!31063
parents 55432f0f f7a391c7
......@@ -6,8 +6,8 @@ import { normalizeHeaders, parseIntPagination } from '~/lib/utils/common_utils';
export const setProjectId = ({ commit }, projectId) => commit(types.SET_PROJECT_ID, projectId);
export const fetchMergeRequests = ({ dispatch, state, rootState }) => {
dispatch('requestMergeRequests');
export const fetchMergeRequests = ({ commit, state, rootState }) => {
commit(types.REQUEST_MERGE_REQUESTS);
const { projectId, pageInfo } = state;
......@@ -24,24 +24,17 @@ export const fetchMergeRequests = ({ dispatch, state, rootState }) => {
return API.codeReviewAnalytics(params)
.then(response => {
const { headers, data } = response;
dispatch('receiveMergeRequestsSuccess', { headers, data });
const normalizedHeaders = normalizeHeaders(headers);
commit(types.RECEIVE_MERGE_REQUESTS_SUCCESS, {
pageInfo: parseIntPagination(normalizedHeaders),
mergeRequests: data,
});
})
.catch(err => dispatch('receiveMergeRequestsError', err));
};
export const requestMergeRequests = ({ commit }) => commit(types.REQUEST_MERGE_REQUESTS);
export const receiveMergeRequestsSuccess = ({ commit }, { headers, data: mergeRequests }) => {
const normalizedHeaders = normalizeHeaders(headers);
const pageInfo = parseIntPagination(normalizedHeaders);
commit(types.RECEIVE_MERGE_REQUESTS_SUCCESS, { pageInfo, mergeRequests });
};
export const receiveMergeRequestsError = ({ commit }, { response }) => {
const { status } = response;
commit(types.RECEIVE_MERGE_REQUESTS_ERROR, status);
createFlash(__('An error occurred while loading merge requests.'));
.catch(({ response }) => {
const { status } = response;
commit(types.RECEIVE_MERGE_REQUESTS_ERROR, status);
createFlash(__('An error occurred while loading merge requests.'));
});
};
export const setPage = ({ commit }, page) => commit(types.SET_PAGE, page);
......@@ -74,11 +74,14 @@ describe('Code review analytics mergeRequests actions', () => {
actions.fetchMergeRequests,
null,
state,
[],
[
{ type: 'requestMergeRequests' },
{ type: 'receiveMergeRequestsSuccess', payload: { headers, data: mockMergeRequests } },
{ type: types.REQUEST_MERGE_REQUESTS },
{
type: types.RECEIVE_MERGE_REQUESTS_SUCCESS,
payload: { pageInfo, mergeRequests: mockMergeRequests },
},
],
[],
);
});
});
......@@ -88,66 +91,28 @@ describe('Code review analytics mergeRequests actions', () => {
mock.onGet(/api\/(.*)\/analytics\/code_review/).replyOnce(500);
});
it('dispatches error', () => {
it('dispatches error', done => {
testAction(
actions.fetchMergeRequests,
null,
state,
[],
[
{ type: 'requestMergeRequests' },
{ type: types.REQUEST_MERGE_REQUESTS },
{
type: 'receiveMergeRequestsError',
payload: new Error('Request failed with status code 500'),
type: types.RECEIVE_MERGE_REQUESTS_ERROR,
payload: 500,
},
],
[],
() => {
expect(createFlash).toHaveBeenCalled();
done();
},
);
});
});
});
describe('requestMergeRequests', () => {
it('commits REQUEST_MERGE_REQUESTS mutation', () => {
testAction(
actions.requestMergeRequests,
null,
state,
[{ type: types.REQUEST_MERGE_REQUESTS }],
[],
);
});
});
describe('receiveMergeRequestsSuccess', () => {
it('commits RECEIVE_MERGE_REQUESTS_SUCCESS mutation', () => {
testAction(
actions.receiveMergeRequestsSuccess,
{ headers, data: mockMergeRequests },
state,
[
{
type: types.RECEIVE_MERGE_REQUESTS_SUCCESS,
payload: { pageInfo, mergeRequests: mockMergeRequests },
},
],
[],
);
});
});
describe('receiveMergeRequestsError', () => {
it('commits SET_MERGE_REQUEST_ERROR mutation', () =>
testAction(
actions.receiveMergeRequestsError,
{ response: { status: 500 } },
state,
[{ type: types.RECEIVE_MERGE_REQUESTS_ERROR, payload: 500 }],
[],
).then(() => {
expect(createFlash).toHaveBeenCalled();
}));
});
describe('setPage', () => {
it('commits SET_PAGE mutation', () => {
testAction(actions.setPage, 2, state, [{ type: types.SET_PAGE, payload: 2 }], []);
......
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