2020-10-30 14:08:56 -04:00
|
|
|
import MockAdapter from 'axios-mock-adapter';
|
|
|
|
import testAction from 'helpers/vuex_action_helper';
|
2020-12-15 07:10:17 -05:00
|
|
|
import Api from '~/api';
|
2021-02-14 13:09:20 -05:00
|
|
|
import createFlash from '~/flash';
|
|
|
|
import axios from '~/lib/utils/axios_utils';
|
|
|
|
import * as urlUtils from '~/lib/utils/url_utility';
|
2020-10-30 14:08:56 -04:00
|
|
|
import * as actions from '~/search/store/actions';
|
|
|
|
import * as types from '~/search/store/mutation_types';
|
2020-12-15 07:10:17 -05:00
|
|
|
import createState from '~/search/store/state';
|
2021-02-12 04:08:48 -05:00
|
|
|
import { MOCK_QUERY, MOCK_GROUPS, MOCK_PROJECT, MOCK_PROJECTS } from '../mock_data';
|
2020-10-30 14:08:56 -04:00
|
|
|
|
|
|
|
jest.mock('~/flash');
|
2020-11-16 19:09:16 -05:00
|
|
|
jest.mock('~/lib/utils/url_utility', () => ({
|
|
|
|
setUrlParams: jest.fn(),
|
2020-12-15 07:10:17 -05:00
|
|
|
joinPaths: jest.fn().mockReturnValue(''),
|
2020-11-16 19:09:16 -05:00
|
|
|
visitUrl: jest.fn(),
|
|
|
|
}));
|
2020-10-30 14:08:56 -04:00
|
|
|
|
|
|
|
describe('Global Search Store Actions', () => {
|
|
|
|
let mock;
|
2020-12-15 07:10:17 -05:00
|
|
|
let state;
|
2020-10-30 14:08:56 -04:00
|
|
|
|
|
|
|
const noCallback = () => {};
|
|
|
|
const flashCallback = () => {
|
|
|
|
expect(createFlash).toHaveBeenCalledTimes(1);
|
|
|
|
createFlash.mockClear();
|
|
|
|
};
|
|
|
|
|
|
|
|
beforeEach(() => {
|
2020-12-15 07:10:17 -05:00
|
|
|
state = createState({ query: MOCK_QUERY });
|
2020-10-30 14:08:56 -04:00
|
|
|
mock = new MockAdapter(axios);
|
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(() => {
|
2020-12-15 07:10:17 -05:00
|
|
|
state = null;
|
2020-10-30 14:08:56 -04:00
|
|
|
mock.restore();
|
|
|
|
});
|
|
|
|
|
|
|
|
describe.each`
|
2021-02-12 04:08:48 -05:00
|
|
|
action | axiosMock | type | expectedMutations | callback
|
|
|
|
${actions.fetchGroups} | ${{ method: 'onGet', code: 200, res: MOCK_GROUPS }} | ${'success'} | ${[{ type: types.REQUEST_GROUPS }, { type: types.RECEIVE_GROUPS_SUCCESS, payload: MOCK_GROUPS }]} | ${noCallback}
|
|
|
|
${actions.fetchGroups} | ${{ method: 'onGet', code: 500, res: null }} | ${'error'} | ${[{ type: types.REQUEST_GROUPS }, { type: types.RECEIVE_GROUPS_ERROR }]} | ${flashCallback}
|
|
|
|
${actions.fetchProjects} | ${{ method: 'onGet', code: 200, res: MOCK_PROJECTS }} | ${'success'} | ${[{ type: types.REQUEST_PROJECTS }, { type: types.RECEIVE_PROJECTS_SUCCESS, payload: MOCK_PROJECTS }]} | ${noCallback}
|
|
|
|
${actions.fetchProjects} | ${{ method: 'onGet', code: 500, res: null }} | ${'error'} | ${[{ type: types.REQUEST_PROJECTS }, { type: types.RECEIVE_PROJECTS_ERROR }]} | ${flashCallback}
|
|
|
|
`(`axios calls`, ({ action, axiosMock, type, expectedMutations, callback }) => {
|
2020-10-30 14:08:56 -04:00
|
|
|
describe(action.name, () => {
|
|
|
|
describe(`on ${type}`, () => {
|
|
|
|
beforeEach(() => {
|
2021-02-12 04:08:48 -05:00
|
|
|
mock[axiosMock.method]().replyOnce(axiosMock.code, axiosMock.res);
|
2020-10-30 14:08:56 -04:00
|
|
|
});
|
|
|
|
it(`should dispatch the correct mutations`, () => {
|
2021-02-12 04:08:48 -05:00
|
|
|
return testAction({ action, state, expectedMutations }).then(() => callback());
|
2020-10-30 14:08:56 -04:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2020-11-16 19:09:16 -05:00
|
|
|
|
2020-12-15 07:10:17 -05:00
|
|
|
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();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2020-11-16 19:09:16 -05:00
|
|
|
describe('setQuery', () => {
|
|
|
|
const payload = { key: 'key1', value: 'value1' };
|
|
|
|
|
2020-12-15 07:10:17 -05:00
|
|
|
it('calls the SET_QUERY mutation', () => {
|
|
|
|
return testAction({
|
|
|
|
action: actions.setQuery,
|
|
|
|
payload,
|
|
|
|
state,
|
|
|
|
expectedMutations: [{ type: types.SET_QUERY, payload }],
|
|
|
|
});
|
2020-11-16 19:09:16 -05:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('applyQuery', () => {
|
|
|
|
it('calls visitUrl and setParams with the state.query', () => {
|
2020-12-15 07:10:17 -05:00
|
|
|
return testAction(actions.applyQuery, null, state, [], [], () => {
|
|
|
|
expect(urlUtils.setUrlParams).toHaveBeenCalledWith({ ...state.query, page: null });
|
|
|
|
expect(urlUtils.visitUrl).toHaveBeenCalled();
|
2020-11-16 19:09:16 -05:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('resetQuery', () => {
|
|
|
|
it('calls visitUrl and setParams with empty values', () => {
|
2020-12-15 07:10:17 -05:00
|
|
|
return testAction(actions.resetQuery, null, state, [], [], () => {
|
|
|
|
expect(urlUtils.setUrlParams).toHaveBeenCalledWith({
|
2020-11-16 19:09:16 -05:00
|
|
|
...state.query,
|
|
|
|
page: null,
|
|
|
|
state: null,
|
|
|
|
confidential: null,
|
|
|
|
});
|
2020-12-15 07:10:17 -05:00
|
|
|
expect(urlUtils.visitUrl).toHaveBeenCalled();
|
2020-11-16 19:09:16 -05:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2020-10-30 14:08:56 -04:00
|
|
|
});
|