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

125 lines
4.5 KiB
JavaScript
Raw Normal View History

2021-01-29 00:20:46 +05:30
import MockAdapter from 'axios-mock-adapter';
import testAction from 'helpers/vuex_action_helper';
2021-02-22 17:27:13 +05:30
import Api from '~/api';
2021-03-11 19:13:27 +05:30
import createFlash from '~/flash';
import axios from '~/lib/utils/axios_utils';
import * as urlUtils from '~/lib/utils/url_utility';
2021-01-29 00:20:46 +05:30
import * as actions from '~/search/store/actions';
import * as types from '~/search/store/mutation_types';
2021-02-22 17:27:13 +05:30
import createState from '~/search/store/state';
import { MOCK_QUERY, MOCK_GROUPS, MOCK_PROJECT, MOCK_PROJECTS } from '../mock_data';
2021-01-29 00:20:46 +05:30
jest.mock('~/flash');
jest.mock('~/lib/utils/url_utility', () => ({
setUrlParams: jest.fn(),
2021-02-22 17:27:13 +05:30
joinPaths: jest.fn().mockReturnValue(''),
2021-01-29 00:20:46 +05:30
visitUrl: jest.fn(),
}));
describe('Global Search Store Actions', () => {
let mock;
2021-02-22 17:27:13 +05:30
let state;
2021-01-29 00:20:46 +05:30
2021-09-04 01:27:46 +05:30
const flashCallback = (callCount) => {
expect(createFlash).toHaveBeenCalledTimes(callCount);
2021-01-29 00:20:46 +05:30
createFlash.mockClear();
};
beforeEach(() => {
2021-02-22 17:27:13 +05:30
state = createState({ query: MOCK_QUERY });
2021-01-29 00:20:46 +05:30
mock = new MockAdapter(axios);
});
afterEach(() => {
2021-02-22 17:27:13 +05:30
state = null;
2021-01-29 00:20:46 +05:30
mock.restore();
});
describe.each`
2021-09-04 01:27:46 +05:30
action | axiosMock | type | expectedMutations | flashCallCount
${actions.fetchGroups} | ${{ method: 'onGet', code: 200, res: MOCK_GROUPS }} | ${'success'} | ${[{ type: types.REQUEST_GROUPS }, { type: types.RECEIVE_GROUPS_SUCCESS, payload: MOCK_GROUPS }]} | ${0}
${actions.fetchGroups} | ${{ method: 'onGet', code: 500, res: null }} | ${'error'} | ${[{ type: types.REQUEST_GROUPS }, { type: types.RECEIVE_GROUPS_ERROR }]} | ${1}
${actions.fetchProjects} | ${{ method: 'onGet', code: 200, res: MOCK_PROJECTS }} | ${'success'} | ${[{ type: types.REQUEST_PROJECTS }, { type: types.RECEIVE_PROJECTS_SUCCESS, payload: MOCK_PROJECTS }]} | ${0}
${actions.fetchProjects} | ${{ method: 'onGet', code: 500, res: null }} | ${'error'} | ${[{ type: types.REQUEST_PROJECTS }, { type: types.RECEIVE_PROJECTS_ERROR }]} | ${2}
`(`axios calls`, ({ action, axiosMock, type, expectedMutations, flashCallCount }) => {
2021-01-29 00:20:46 +05:30
describe(action.name, () => {
describe(`on ${type}`, () => {
beforeEach(() => {
mock[axiosMock.method]().replyOnce(axiosMock.code, axiosMock.res);
});
it(`should dispatch the correct mutations`, () => {
2021-09-04 01:27:46 +05:30
return testAction({ action, state, expectedMutations }).then(() =>
flashCallback(flashCallCount),
);
2021-01-29 00:20:46 +05:30
});
});
});
});
2021-02-22 17:27:13 +05:30
describe('getProjectsData', () => {
const mockCommit = () => {};
beforeEach(() => {
jest.spyOn(Api, 'groupProjects').mockResolvedValue(MOCK_PROJECTS);
jest.spyOn(Api, 'projects').mockResolvedValue(MOCK_PROJECT);
});
describe('when groupId is set', () => {
it('calls Api.groupProjects', () => {
actions.fetchProjects({ commit: mockCommit, state });
expect(Api.groupProjects).toHaveBeenCalled();
expect(Api.projects).not.toHaveBeenCalled();
});
});
describe('when groupId is not set', () => {
beforeEach(() => {
state = createState({ query: { group_id: null } });
});
it('calls Api.projects', () => {
actions.fetchProjects({ commit: mockCommit, state });
expect(Api.groupProjects).not.toHaveBeenCalled();
expect(Api.projects).toHaveBeenCalled();
});
});
});
2021-01-29 00:20:46 +05:30
describe('setQuery', () => {
const payload = { key: 'key1', value: 'value1' };
2021-02-22 17:27:13 +05:30
it('calls the SET_QUERY mutation', () => {
return testAction({
action: actions.setQuery,
payload,
state,
expectedMutations: [{ type: types.SET_QUERY, payload }],
});
2021-01-29 00:20:46 +05:30
});
});
describe('applyQuery', () => {
it('calls visitUrl and setParams with the state.query', () => {
2021-02-22 17:27:13 +05:30
return testAction(actions.applyQuery, null, state, [], [], () => {
expect(urlUtils.setUrlParams).toHaveBeenCalledWith({ ...state.query, page: null });
expect(urlUtils.visitUrl).toHaveBeenCalled();
2021-01-29 00:20:46 +05:30
});
});
});
describe('resetQuery', () => {
it('calls visitUrl and setParams with empty values', () => {
2021-02-22 17:27:13 +05:30
return testAction(actions.resetQuery, null, state, [], [], () => {
expect(urlUtils.setUrlParams).toHaveBeenCalledWith({
2021-01-29 00:20:46 +05:30
...state.query,
page: null,
state: null,
confidential: null,
});
2021-02-22 17:27:13 +05:30
expect(urlUtils.visitUrl).toHaveBeenCalled();
2021-01-29 00:20:46 +05:30
});
});
});
});