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'; ...@@ -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 setProjectId = ({ commit }, projectId) => commit(types.SET_PROJECT_ID, projectId);
export const fetchMergeRequests = ({ dispatch, state, rootState }) => { export const fetchMergeRequests = ({ commit, state, rootState }) => {
dispatch('requestMergeRequests'); commit(types.REQUEST_MERGE_REQUESTS);
const { projectId, pageInfo } = state; const { projectId, pageInfo } = state;
...@@ -24,24 +24,17 @@ export const fetchMergeRequests = ({ dispatch, state, rootState }) => { ...@@ -24,24 +24,17 @@ export const fetchMergeRequests = ({ dispatch, state, rootState }) => {
return API.codeReviewAnalytics(params) return API.codeReviewAnalytics(params)
.then(response => { .then(response => {
const { headers, data } = 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)); .catch(({ response }) => {
}; const { status } = response;
commit(types.RECEIVE_MERGE_REQUESTS_ERROR, status);
export const requestMergeRequests = ({ commit }) => commit(types.REQUEST_MERGE_REQUESTS); createFlash(__('An error occurred while loading 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.'));
}; };
export const setPage = ({ commit }, page) => commit(types.SET_PAGE, page); export const setPage = ({ commit }, page) => commit(types.SET_PAGE, page);
...@@ -74,11 +74,14 @@ describe('Code review analytics mergeRequests actions', () => { ...@@ -74,11 +74,14 @@ describe('Code review analytics mergeRequests actions', () => {
actions.fetchMergeRequests, actions.fetchMergeRequests,
null, null,
state, state,
[],
[ [
{ type: 'requestMergeRequests' }, { type: types.REQUEST_MERGE_REQUESTS },
{ type: 'receiveMergeRequestsSuccess', payload: { headers, data: mockMergeRequests } }, {
type: types.RECEIVE_MERGE_REQUESTS_SUCCESS,
payload: { pageInfo, mergeRequests: mockMergeRequests },
},
], ],
[],
); );
}); });
}); });
...@@ -88,66 +91,28 @@ describe('Code review analytics mergeRequests actions', () => { ...@@ -88,66 +91,28 @@ describe('Code review analytics mergeRequests actions', () => {
mock.onGet(/api\/(.*)\/analytics\/code_review/).replyOnce(500); mock.onGet(/api\/(.*)\/analytics\/code_review/).replyOnce(500);
}); });
it('dispatches error', () => { it('dispatches error', done => {
testAction( testAction(
actions.fetchMergeRequests, actions.fetchMergeRequests,
null, null,
state, state,
[],
[ [
{ type: 'requestMergeRequests' }, { type: types.REQUEST_MERGE_REQUESTS },
{ {
type: 'receiveMergeRequestsError', type: types.RECEIVE_MERGE_REQUESTS_ERROR,
payload: new Error('Request failed with status code 500'), 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', () => { describe('setPage', () => {
it('commits SET_PAGE mutation', () => { it('commits SET_PAGE mutation', () => {
testAction(actions.setPage, 2, state, [{ type: types.SET_PAGE, payload: 2 }], []); 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