2019-10-28 14:06:15 -04:00
|
|
|
import MockAdapter from 'axios-mock-adapter';
|
|
|
|
import testAction from 'helpers/vuex_action_helper';
|
|
|
|
import * as actions from '~/contributors/stores/actions';
|
|
|
|
import * as types from '~/contributors/stores/mutation_types';
|
2022-09-30 02:09:35 -04:00
|
|
|
import { createAlert } from '~/flash';
|
2021-02-14 13:09:20 -05:00
|
|
|
import axios from '~/lib/utils/axios_utils';
|
2019-10-28 14:06:15 -04:00
|
|
|
|
|
|
|
jest.mock('~/flash.js');
|
|
|
|
|
|
|
|
describe('Contributors store actions', () => {
|
|
|
|
describe('fetchChartData', () => {
|
|
|
|
let mock;
|
|
|
|
const endpoint = '/contributors';
|
|
|
|
const chartData = { '2017-11': 0, '2017-12': 2 };
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
mock = new MockAdapter(axios);
|
|
|
|
});
|
|
|
|
|
2022-04-19 05:08:55 -04:00
|
|
|
afterEach(() => {
|
|
|
|
mock.restore();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should commit SET_CHART_DATA with received response', () => {
|
2019-10-28 14:06:15 -04:00
|
|
|
mock.onGet().reply(200, chartData);
|
|
|
|
|
2022-04-19 05:08:55 -04:00
|
|
|
return testAction(
|
2019-10-28 14:06:15 -04:00
|
|
|
actions.fetchChartData,
|
|
|
|
{ endpoint },
|
|
|
|
{},
|
|
|
|
[
|
|
|
|
{ type: types.SET_LOADING_STATE, payload: true },
|
|
|
|
{ type: types.SET_CHART_DATA, payload: chartData },
|
|
|
|
{ type: types.SET_LOADING_STATE, payload: false },
|
|
|
|
],
|
|
|
|
[],
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2022-04-19 05:08:55 -04:00
|
|
|
it('should show flash on API error', async () => {
|
2019-10-28 14:06:15 -04:00
|
|
|
mock.onGet().reply(400, 'Not Found');
|
|
|
|
|
2022-04-19 05:08:55 -04:00
|
|
|
await testAction(
|
2019-10-28 14:06:15 -04:00
|
|
|
actions.fetchChartData,
|
|
|
|
{ endpoint },
|
|
|
|
{},
|
|
|
|
[{ type: types.SET_LOADING_STATE, payload: true }],
|
|
|
|
[],
|
|
|
|
);
|
2022-09-30 02:09:35 -04:00
|
|
|
expect(createAlert).toHaveBeenCalledWith({
|
2022-04-19 05:08:55 -04:00
|
|
|
message: expect.stringMatching('error'),
|
|
|
|
});
|
2019-10-28 14:06:15 -04:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|