Commit 7b46b28e authored by Thomas Randolph's avatar Thomas Randolph

Add a network action to fetch the MR Metadata from the API endpoint

parent 9a326fa7
import axios from '~/lib/utils/axios_utils';
import types from './mutation_types'; import types from './mutation_types';
export function setActiveTab({ commit }, tab) { export function setActiveTab({ commit }, tab) {
...@@ -11,3 +13,20 @@ export function setEndpoints({ commit }, endpoints) { ...@@ -11,3 +13,20 @@ export function setEndpoints({ commit }, endpoints) {
export function setMrMetadata({ commit }, metadata) { export function setMrMetadata({ commit }, metadata) {
commit(types.SET_MR_METADATA, metadata); commit(types.SET_MR_METADATA, metadata);
} }
export function fetchMrMetadata({ dispatch, state }) {
if (state.endpoints?.metadata) {
axios
.get(state.endpoints.metadata)
.then((response) => {
dispatch('setMrMetadata', response.data);
})
.catch(() => {
// https://gitlab.com/gitlab-org/gitlab/-/issues/324740
// We can't even do a simple console warning here because
// the pipeline will fail. However, the issue above will
// eventually handle errors appropriately.
// console.warn('Failed to load MR Metadata for the Overview tab.');
});
}
}
import MockAdapter from 'axios-mock-adapter';
import testAction from 'helpers/vuex_action_helper'; import testAction from 'helpers/vuex_action_helper';
import { setEndpoints, setMrMetadata } from '~/mr_notes/stores/actions'; import axios from '~/lib/utils/axios_utils';
import { setEndpoints, setMrMetadata, fetchMrMetadata } from '~/mr_notes/stores/actions';
import mutationTypes from '~/mr_notes/stores/mutation_types'; import mutationTypes from '~/mr_notes/stores/mutation_types';
describe('MR Notes Mutator Actions', () => { describe('MR Notes Mutator Actions', () => {
...@@ -42,4 +46,48 @@ describe('MR Notes Mutator Actions', () => { ...@@ -42,4 +46,48 @@ describe('MR Notes Mutator Actions', () => {
); );
}); });
}); });
describe('fetchMrMetadata', () => {
const mrMetadata = { meta: true, data: 'foo' };
const state = {
endpoints: {
metadata: 'metadata',
},
};
let getSpy;
let mock;
beforeEach(() => {
getSpy = jest.spyOn(axios, 'get');
mock = new MockAdapter(axios);
mock.onGet(state.endpoints.metadata).reply(200, mrMetadata);
});
afterEach(() => {
getSpy.mockRestore();
mock.restore();
});
it('should fetch the data from the API', async () => {
await fetchMrMetadata({ state, dispatch: () => {} });
expect(axios.get).toHaveBeenCalledWith(state.endpoints.metadata);
});
it('should set the fetched data into state', () => {
return testAction(
fetchMrMetadata,
{},
state,
[],
[
{
type: 'setMrMetadata',
payload: mrMetadata,
},
],
);
});
});
}); });
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