Commit b6687dbd authored by Mark Florian's avatar Mark Florian

Merge branch...

Merge branch '18140-add-to-next-and-previous-commit-buttons-when-viewing-commits-in-merge-request-3' into 'master'

Add a Vuex action that changes what `commit` the Diffs app is currently viewing

See merge request gitlab-org/gitlab!29831
parents 5e907ec1 2e43bff4
......@@ -642,5 +642,28 @@ export const setSuggestPopoverDismissed = ({ commit, state }) =>
createFlash(s__('MergeRequest|Error dismissing suggestion popover. Please try again.'));
});
export function changeCurrentCommit({ dispatch, commit, state }, { commitId }) {
/* eslint-disable @gitlab/require-i18n-strings */
if (!commitId) {
return Promise.reject(new Error('`commitId` is a required argument'));
} else if (!state.commit) {
return Promise.reject(new Error('`state` must already contain a valid `commit`'));
}
/* eslint-enable @gitlab/require-i18n-strings */
// this is less than ideal, see: https://gitlab.com/gitlab-org/gitlab/-/issues/215421
const commitRE = new RegExp(state.commit.id, 'g');
commit(types.SET_DIFF_FILES, []);
commit(types.SET_BASE_CONFIG, {
...state,
endpoint: state.endpoint.replace(commitRE, commitId),
endpointBatch: state.endpointBatch.replace(commitRE, commitId),
endpointMetadata: state.endpointMetadata.replace(commitRE, commitId),
});
return dispatch('fetchDiffFilesMeta');
}
// prevent babel-plugin-rewire from generating an invalid default during karma tests
export default () => {};
......@@ -43,6 +43,7 @@ import {
setFileCollapsed,
setExpandedDiffLines,
setSuggestPopoverDismissed,
changeCurrentCommit,
} from '~/diffs/store/actions';
import eventHub from '~/notes/event_hub';
import * as types from '~/diffs/store/mutation_types';
......@@ -1347,4 +1348,62 @@ describe('DiffsStoreActions', () => {
);
});
});
describe('changeCurrentCommit', () => {
it('commits the new commit information and re-requests the diff metadata for the commit', () => {
return testAction(
changeCurrentCommit,
{ commitId: 'NEW' },
{
commit: {
id: 'OLD',
},
endpoint: 'URL/OLD',
endpointBatch: 'URL/OLD',
endpointMetadata: 'URL/OLD',
},
[
{ type: types.SET_DIFF_FILES, payload: [] },
{
type: types.SET_BASE_CONFIG,
payload: {
commit: {
id: 'OLD', // Not a typo: the action fired next will overwrite all of the `commit` in state
},
endpoint: 'URL/NEW',
endpointBatch: 'URL/NEW',
endpointMetadata: 'URL/NEW',
},
},
],
[{ type: 'fetchDiffFilesMeta' }],
);
});
it.each`
commitId | commit | msg
${undefined} | ${{ id: 'OLD' }} | ${'`commitId` is a required argument'}
${'NEW'} | ${null} | ${'`state` must already contain a valid `commit`'}
${undefined} | ${null} | ${'`commitId` is a required argument'}
`(
'returns a rejected promise with the error message $msg given `{ "commitId": $commitId, "state.commit": $commit }`',
({ commitId, commit, msg }) => {
const err = new Error(msg);
const actionReturn = testAction(
changeCurrentCommit,
{ commitId },
{
endpoint: 'URL/OLD',
endpointBatch: 'URL/OLD',
endpointMetadata: 'URL/OLD',
commit,
},
[],
[],
);
return expect(actionReturn).rejects.toStrictEqual(err);
},
);
});
});
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