2021-03-24 08:09:32 -04:00
|
|
|
import MockAdapter from 'axios-mock-adapter';
|
|
|
|
import axios from '~/lib/utils/axios_utils';
|
|
|
|
|
2022-04-12 08:08:50 -04:00
|
|
|
import { createStore } from '~/mr_notes/stores';
|
2021-03-17 14:09:01 -04:00
|
|
|
|
|
|
|
describe('MR Notes Mutator Actions', () => {
|
2022-04-12 08:08:50 -04:00
|
|
|
let store;
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
store = createStore();
|
|
|
|
});
|
|
|
|
|
2021-03-17 14:09:01 -04:00
|
|
|
describe('setEndpoints', () => {
|
2022-04-12 08:08:50 -04:00
|
|
|
it('sets endpoints', async () => {
|
2021-03-17 14:09:01 -04:00
|
|
|
const endpoints = { endpointA: 'a' };
|
|
|
|
|
2022-04-12 08:08:50 -04:00
|
|
|
await store.dispatch('setEndpoints', endpoints);
|
2021-03-24 08:09:32 -04:00
|
|
|
|
2022-04-12 08:08:50 -04:00
|
|
|
expect(store.state.page.endpoints).toEqual(endpoints);
|
2021-03-24 08:09:32 -04:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('fetchMrMetadata', () => {
|
|
|
|
const mrMetadata = { meta: true, data: 'foo' };
|
2022-04-12 08:08:50 -04:00
|
|
|
const metadata = 'metadata';
|
|
|
|
const endpoints = { metadata };
|
2021-03-24 08:09:32 -04:00
|
|
|
let mock;
|
|
|
|
|
2022-04-12 08:08:50 -04:00
|
|
|
beforeEach(async () => {
|
|
|
|
await store.dispatch('setEndpoints', endpoints);
|
|
|
|
|
2021-03-24 08:09:32 -04:00
|
|
|
mock = new MockAdapter(axios);
|
|
|
|
|
2022-04-12 08:08:50 -04:00
|
|
|
mock.onGet(metadata).reply(200, mrMetadata);
|
2021-03-24 08:09:32 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
mock.restore();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should fetch the data from the API', async () => {
|
2022-04-12 08:08:50 -04:00
|
|
|
await store.dispatch('fetchMrMetadata');
|
2021-03-24 08:09:32 -04:00
|
|
|
|
|
|
|
await axios.waitForAll();
|
|
|
|
|
|
|
|
expect(mock.history.get).toHaveLength(1);
|
2022-04-12 08:08:50 -04:00
|
|
|
expect(mock.history.get[0].url).toBe(metadata);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should set the fetched data into state', async () => {
|
|
|
|
await store.dispatch('fetchMrMetadata');
|
|
|
|
|
|
|
|
expect(store.state.page.mrMetadata).toEqual(mrMetadata);
|
2021-03-24 08:09:32 -04:00
|
|
|
});
|
|
|
|
|
2022-04-12 08:08:50 -04:00
|
|
|
it('should set failedToLoadMetadata flag when request fails', async () => {
|
|
|
|
mock.onGet(metadata).reply(500);
|
|
|
|
|
|
|
|
await store.dispatch('fetchMrMetadata');
|
|
|
|
|
|
|
|
expect(store.state.page.failedToLoadMetadata).toBe(true);
|
2021-03-24 08:09:32 -04:00
|
|
|
});
|
|
|
|
});
|
2021-03-17 14:09:01 -04:00
|
|
|
});
|