Commit 2e43bff4 authored by Thomas Randolph's avatar Thomas Randolph

Add an action that changes the Diffs app active commit

parent b8c11c51
...@@ -642,5 +642,28 @@ export const setSuggestPopoverDismissed = ({ commit, state }) => ...@@ -642,5 +642,28 @@ export const setSuggestPopoverDismissed = ({ commit, state }) =>
createFlash(s__('MergeRequest|Error dismissing suggestion popover. Please try again.')); 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 // prevent babel-plugin-rewire from generating an invalid default during karma tests
export default () => {}; export default () => {};
...@@ -43,6 +43,7 @@ import { ...@@ -43,6 +43,7 @@ import {
setFileCollapsed, setFileCollapsed,
setExpandedDiffLines, setExpandedDiffLines,
setSuggestPopoverDismissed, setSuggestPopoverDismissed,
changeCurrentCommit,
} from '~/diffs/store/actions'; } from '~/diffs/store/actions';
import eventHub from '~/notes/event_hub'; import eventHub from '~/notes/event_hub';
import * as types from '~/diffs/store/mutation_types'; import * as types from '~/diffs/store/mutation_types';
...@@ -1347,4 +1348,62 @@ describe('DiffsStoreActions', () => { ...@@ -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