debian-mirror-gitlab/spec/frontend/related_merge_requests/store/actions_spec.js

112 lines
2.8 KiB
JavaScript
Raw Normal View History

2019-07-07 11:18:12 +05:30
import MockAdapter from 'axios-mock-adapter';
2020-05-24 23:13:21 +05:30
import testAction from 'helpers/vuex_action_helper';
2020-10-24 23:57:45 +05:30
import { deprecatedCreateFlash as createFlash } from '~/flash';
2019-07-07 11:18:12 +05:30
import axios from '~/lib/utils/axios_utils';
2020-05-24 23:13:21 +05:30
import * as actions from '~/related_merge_requests/store/actions';
2021-03-11 19:13:27 +05:30
import * as types from '~/related_merge_requests/store/mutation_types';
2020-05-24 23:13:21 +05:30
jest.mock('~/flash');
2019-07-07 11:18:12 +05:30
describe('RelatedMergeRequest store actions', () => {
let state;
let mock;
beforeEach(() => {
state = {
apiEndpoint: '/api/related_merge_requests',
};
mock = new MockAdapter(axios);
});
afterEach(() => {
mock.restore();
});
describe('setInitialState', () => {
2021-03-08 18:12:59 +05:30
it('commits types.SET_INITIAL_STATE with given props', (done) => {
2019-07-07 11:18:12 +05:30
const props = { a: 1, b: 2 };
testAction(
actions.setInitialState,
props,
{},
[{ type: types.SET_INITIAL_STATE, payload: props }],
[],
done,
);
});
});
describe('requestData', () => {
2021-03-08 18:12:59 +05:30
it('commits types.REQUEST_DATA', (done) => {
2019-07-07 11:18:12 +05:30
testAction(actions.requestData, null, {}, [{ type: types.REQUEST_DATA }], [], done);
});
});
describe('receiveDataSuccess', () => {
2021-03-08 18:12:59 +05:30
it('commits types.RECEIVE_DATA_SUCCESS with data', (done) => {
2019-07-07 11:18:12 +05:30
const data = { a: 1, b: 2 };
testAction(
actions.receiveDataSuccess,
data,
{},
[{ type: types.RECEIVE_DATA_SUCCESS, payload: data }],
[],
done,
);
});
});
describe('receiveDataError', () => {
2021-03-08 18:12:59 +05:30
it('commits types.RECEIVE_DATA_ERROR', (done) => {
2019-07-07 11:18:12 +05:30
testAction(
actions.receiveDataError,
null,
{},
[{ type: types.RECEIVE_DATA_ERROR }],
[],
done,
);
});
});
describe('fetchMergeRequests', () => {
describe('for a successful request', () => {
2021-03-08 18:12:59 +05:30
it('should dispatch success action', (done) => {
2019-07-07 11:18:12 +05:30
const data = { a: 1 };
mock.onGet(`${state.apiEndpoint}?per_page=100`).replyOnce(200, data, { 'x-total': 2 });
testAction(
actions.fetchMergeRequests,
null,
state,
[],
[{ type: 'requestData' }, { type: 'receiveDataSuccess', payload: { data, total: 2 } }],
done,
);
});
});
describe('for a failing request', () => {
2021-03-08 18:12:59 +05:30
it('should dispatch error action', (done) => {
2019-07-07 11:18:12 +05:30
mock.onGet(`${state.apiEndpoint}?per_page=100`).replyOnce(400);
testAction(
actions.fetchMergeRequests,
null,
state,
[],
[{ type: 'requestData' }, { type: 'receiveDataError' }],
() => {
2020-05-24 23:13:21 +05:30
expect(createFlash).toHaveBeenCalledTimes(1);
expect(createFlash).toHaveBeenCalledWith(expect.stringMatching('Something went wrong'));
2019-07-07 11:18:12 +05:30
done();
},
);
});
});
});
});