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';
|
2022-11-25 23:54:43 +05:30
|
|
|
import { createAlert } from '~/flash';
|
2023-01-13 00:05:48 +05:30
|
|
|
import * as logger from '~/lib/logger';
|
2021-03-11 19:13:27 +05:30
|
|
|
import axios from '~/lib/utils/axios_utils';
|
2023-04-23 21:23:45 +05:30
|
|
|
import { HTTP_STATUS_INTERNAL_SERVER_ERROR, HTTP_STATUS_OK } from '~/lib/utils/http_status';
|
2021-03-11 19:13:27 +05:30
|
|
|
import * as urlUtils from '~/lib/utils/url_utility';
|
2021-01-29 00:20:46 +05:30
|
|
|
import * as actions from '~/search/store/actions';
|
2021-12-11 22:18:48 +05:30
|
|
|
import {
|
|
|
|
GROUPS_LOCAL_STORAGE_KEY,
|
|
|
|
PROJECTS_LOCAL_STORAGE_KEY,
|
|
|
|
SIDEBAR_PARAMS,
|
|
|
|
} from '~/search/store/constants';
|
2021-01-29 00:20:46 +05:30
|
|
|
import * as types from '~/search/store/mutation_types';
|
2021-02-22 17:27:13 +05:30
|
|
|
import createState from '~/search/store/state';
|
2021-09-30 23:02:18 +05:30
|
|
|
import * as storeUtils from '~/search/store/utils';
|
|
|
|
import {
|
|
|
|
MOCK_QUERY,
|
|
|
|
MOCK_GROUPS,
|
|
|
|
MOCK_PROJECT,
|
|
|
|
MOCK_PROJECTS,
|
|
|
|
MOCK_GROUP,
|
|
|
|
FRESH_STORED_DATA,
|
|
|
|
MOCK_FRESH_DATA_RES,
|
2021-10-27 15:23:28 +05:30
|
|
|
PRELOAD_EXPECTED_MUTATIONS,
|
2021-09-30 23:02:18 +05:30
|
|
|
PROMISE_ALL_EXPECTED_MUTATIONS,
|
2023-01-13 00:05:48 +05:30
|
|
|
MOCK_NAVIGATION_DATA,
|
|
|
|
MOCK_NAVIGATION_ACTION_MUTATION,
|
|
|
|
MOCK_ENDPOINT_RESPONSE,
|
2023-04-23 21:23:45 +05:30
|
|
|
MOCK_RECEIVE_AGGREGATIONS_SUCCESS_MUTATION,
|
|
|
|
MOCK_RECEIVE_AGGREGATIONS_ERROR_MUTATION,
|
|
|
|
MOCK_AGGREGATIONS,
|
2021-09-30 23:02:18 +05:30
|
|
|
} 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(),
|
|
|
|
}));
|
2023-01-13 00:05:48 +05:30
|
|
|
jest.mock('~/lib/logger', () => ({
|
|
|
|
logError: jest.fn(),
|
|
|
|
}));
|
2021-01-29 00:20:46 +05:30
|
|
|
|
|
|
|
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) => {
|
2022-11-25 23:54:43 +05:30
|
|
|
expect(createAlert).toHaveBeenCalledTimes(callCount);
|
|
|
|
createAlert.mockClear();
|
2021-01-29 00:20:46 +05:30
|
|
|
};
|
|
|
|
|
|
|
|
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`
|
2023-04-23 21:23:45 +05:30
|
|
|
action | axiosMock | type | expectedMutations | flashCallCount
|
|
|
|
${actions.fetchGroups} | ${{ method: 'onGet', code: HTTP_STATUS_OK, res: MOCK_GROUPS }} | ${'success'} | ${[{ type: types.REQUEST_GROUPS }, { type: types.RECEIVE_GROUPS_SUCCESS, payload: MOCK_GROUPS }]} | ${0}
|
|
|
|
${actions.fetchGroups} | ${{ method: 'onGet', code: HTTP_STATUS_INTERNAL_SERVER_ERROR, res: null }} | ${'error'} | ${[{ type: types.REQUEST_GROUPS }, { type: types.RECEIVE_GROUPS_ERROR }]} | ${1}
|
|
|
|
${actions.fetchProjects} | ${{ method: 'onGet', code: HTTP_STATUS_OK, res: MOCK_PROJECTS }} | ${'success'} | ${[{ type: types.REQUEST_PROJECTS }, { type: types.RECEIVE_PROJECTS_SUCCESS, payload: MOCK_PROJECTS }]} | ${0}
|
|
|
|
${actions.fetchProjects} | ${{ method: 'onGet', code: HTTP_STATUS_INTERNAL_SERVER_ERROR, res: null }} | ${'error'} | ${[{ type: types.REQUEST_PROJECTS }, { type: types.RECEIVE_PROJECTS_ERROR }]} | ${1}
|
2021-09-04 01:27:46 +05:30
|
|
|
`(`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-09-30 23:02:18 +05:30
|
|
|
describe.each`
|
2023-04-23 21:23:45 +05:30
|
|
|
action | axiosMock | type | expectedMutations | flashCallCount
|
|
|
|
${actions.loadFrequentGroups} | ${{ method: 'onGet', code: HTTP_STATUS_OK }} | ${'success'} | ${[PROMISE_ALL_EXPECTED_MUTATIONS.resGroups]} | ${0}
|
|
|
|
${actions.loadFrequentGroups} | ${{ method: 'onGet', code: HTTP_STATUS_INTERNAL_SERVER_ERROR }} | ${'error'} | ${[]} | ${1}
|
|
|
|
${actions.loadFrequentProjects} | ${{ method: 'onGet', code: HTTP_STATUS_OK }} | ${'success'} | ${[PROMISE_ALL_EXPECTED_MUTATIONS.resProjects]} | ${0}
|
|
|
|
${actions.loadFrequentProjects} | ${{ method: 'onGet', code: HTTP_STATUS_INTERNAL_SERVER_ERROR }} | ${'error'} | ${[]} | ${1}
|
2021-10-27 15:23:28 +05:30
|
|
|
`('Promise.all calls', ({ action, axiosMock, type, expectedMutations, flashCallCount }) => {
|
|
|
|
describe(action.name, () => {
|
|
|
|
describe(`on ${type}`, () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
state.frequentItems = {
|
|
|
|
[GROUPS_LOCAL_STORAGE_KEY]: FRESH_STORED_DATA,
|
|
|
|
[PROJECTS_LOCAL_STORAGE_KEY]: FRESH_STORED_DATA,
|
|
|
|
};
|
|
|
|
|
|
|
|
mock[axiosMock.method]().reply(axiosMock.code, MOCK_FRESH_DATA_RES);
|
|
|
|
});
|
2021-09-30 23:02:18 +05:30
|
|
|
|
2021-10-27 15:23:28 +05:30
|
|
|
it(`should dispatch the correct mutations`, () => {
|
|
|
|
return testAction({ action, state, expectedMutations }).then(() => {
|
|
|
|
flashCallback(flashCallCount);
|
2021-09-30 23:02:18 +05:30
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2021-10-27 15:23:28 +05:30
|
|
|
});
|
|
|
|
});
|
2021-09-30 23:02:18 +05:30
|
|
|
|
|
|
|
describe('getGroupsData', () => {
|
|
|
|
const mockCommit = () => {};
|
|
|
|
beforeEach(() => {
|
|
|
|
jest.spyOn(Api, 'groups').mockResolvedValue(MOCK_GROUPS);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('calls Api.groups with order_by set to similarity', () => {
|
|
|
|
actions.fetchGroups({ commit: mockCommit }, 'test');
|
|
|
|
|
|
|
|
expect(Api.groups).toHaveBeenCalledWith('test', { order_by: 'similarity' });
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
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', () => {
|
2021-09-30 23:02:18 +05:30
|
|
|
it('calls Api.groupProjects with expected parameters', () => {
|
2022-07-23 23:45:48 +05:30
|
|
|
actions.fetchProjects({ commit: mockCommit, state }, undefined);
|
|
|
|
expect(Api.groupProjects).toHaveBeenCalledWith(state.query.group_id, state.query.search, {
|
|
|
|
order_by: 'similarity',
|
|
|
|
include_subgroups: true,
|
|
|
|
with_shared: false,
|
|
|
|
});
|
2021-02-22 17:27:13 +05:30
|
|
|
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();
|
2022-06-21 17:19:12 +05:30
|
|
|
expect(Api.projects).toHaveBeenCalledWith(state.query.search, {
|
|
|
|
order_by: 'similarity',
|
|
|
|
});
|
2021-02-22 17:27:13 +05:30
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2021-12-11 22:18:48 +05:30
|
|
|
describe.each`
|
|
|
|
payload | isDirty | isDirtyMutation
|
|
|
|
${{ key: SIDEBAR_PARAMS[0], value: 'test' }} | ${false} | ${[{ type: types.SET_SIDEBAR_DIRTY, payload: false }]}
|
|
|
|
${{ key: SIDEBAR_PARAMS[0], value: 'test' }} | ${true} | ${[{ type: types.SET_SIDEBAR_DIRTY, payload: true }]}
|
|
|
|
${{ key: SIDEBAR_PARAMS[1], value: 'test' }} | ${false} | ${[{ type: types.SET_SIDEBAR_DIRTY, payload: false }]}
|
|
|
|
${{ key: SIDEBAR_PARAMS[1], value: 'test' }} | ${true} | ${[{ type: types.SET_SIDEBAR_DIRTY, payload: true }]}
|
|
|
|
${{ key: 'non-sidebar', value: 'test' }} | ${false} | ${[]}
|
|
|
|
${{ key: 'non-sidebar', value: 'test' }} | ${true} | ${[]}
|
|
|
|
`('setQuery', ({ payload, isDirty, isDirtyMutation }) => {
|
|
|
|
describe(`when filter param is ${payload.key} and utils.isSidebarDirty returns ${isDirty}`, () => {
|
|
|
|
const expectedMutations = [{ type: types.SET_QUERY, payload }].concat(isDirtyMutation);
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
storeUtils.isSidebarDirty = jest.fn().mockReturnValue(isDirty);
|
|
|
|
});
|
2021-01-29 00:20:46 +05:30
|
|
|
|
2021-12-11 22:18:48 +05:30
|
|
|
it(`should dispatch the correct mutations`, () => {
|
|
|
|
return testAction({ action: actions.setQuery, payload, state, expectedMutations });
|
2021-02-22 17:27:13 +05:30
|
|
|
});
|
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
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2021-09-30 23:02:18 +05:30
|
|
|
|
2021-10-27 15:23:28 +05:30
|
|
|
describe('preloadStoredFrequentItems', () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
storeUtils.loadDataFromLS = jest.fn().mockReturnValue(FRESH_STORED_DATA);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('calls preloadStoredFrequentItems for both groups and projects and commits LOAD_FREQUENT_ITEMS', async () => {
|
|
|
|
await testAction({
|
|
|
|
action: actions.preloadStoredFrequentItems,
|
|
|
|
state,
|
|
|
|
expectedMutations: PRELOAD_EXPECTED_MUTATIONS,
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(storeUtils.loadDataFromLS).toHaveBeenCalledTimes(2);
|
|
|
|
expect(storeUtils.loadDataFromLS).toHaveBeenCalledWith(GROUPS_LOCAL_STORAGE_KEY);
|
|
|
|
expect(storeUtils.loadDataFromLS).toHaveBeenCalledWith(PROJECTS_LOCAL_STORAGE_KEY);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2021-09-30 23:02:18 +05:30
|
|
|
describe('setFrequentGroup', () => {
|
|
|
|
beforeEach(() => {
|
2021-10-27 15:23:28 +05:30
|
|
|
storeUtils.setFrequentItemToLS = jest.fn().mockReturnValue(FRESH_STORED_DATA);
|
2021-09-30 23:02:18 +05:30
|
|
|
});
|
|
|
|
|
2021-10-27 15:23:28 +05:30
|
|
|
it(`calls setFrequentItemToLS with ${GROUPS_LOCAL_STORAGE_KEY} and item data then commits LOAD_FREQUENT_ITEMS`, async () => {
|
2021-09-30 23:02:18 +05:30
|
|
|
await testAction({
|
|
|
|
action: actions.setFrequentGroup,
|
2021-10-27 15:23:28 +05:30
|
|
|
expectedMutations: [
|
|
|
|
{
|
|
|
|
type: types.LOAD_FREQUENT_ITEMS,
|
|
|
|
payload: { key: GROUPS_LOCAL_STORAGE_KEY, data: FRESH_STORED_DATA },
|
|
|
|
},
|
|
|
|
],
|
2021-09-30 23:02:18 +05:30
|
|
|
payload: MOCK_GROUP,
|
|
|
|
state,
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(storeUtils.setFrequentItemToLS).toHaveBeenCalledWith(
|
|
|
|
GROUPS_LOCAL_STORAGE_KEY,
|
|
|
|
state.frequentItems,
|
|
|
|
MOCK_GROUP,
|
|
|
|
);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('setFrequentProject', () => {
|
|
|
|
beforeEach(() => {
|
2021-10-27 15:23:28 +05:30
|
|
|
storeUtils.setFrequentItemToLS = jest.fn().mockReturnValue(FRESH_STORED_DATA);
|
2021-09-30 23:02:18 +05:30
|
|
|
});
|
|
|
|
|
|
|
|
it(`calls setFrequentItemToLS with ${PROJECTS_LOCAL_STORAGE_KEY} and item data`, async () => {
|
|
|
|
await testAction({
|
|
|
|
action: actions.setFrequentProject,
|
2021-10-27 15:23:28 +05:30
|
|
|
expectedMutations: [
|
|
|
|
{
|
|
|
|
type: types.LOAD_FREQUENT_ITEMS,
|
|
|
|
payload: { key: PROJECTS_LOCAL_STORAGE_KEY, data: FRESH_STORED_DATA },
|
|
|
|
},
|
|
|
|
],
|
2021-09-30 23:02:18 +05:30
|
|
|
payload: MOCK_PROJECT,
|
|
|
|
state,
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(storeUtils.setFrequentItemToLS).toHaveBeenCalledWith(
|
|
|
|
PROJECTS_LOCAL_STORAGE_KEY,
|
|
|
|
state.frequentItems,
|
|
|
|
MOCK_PROJECT,
|
|
|
|
);
|
|
|
|
});
|
|
|
|
});
|
2023-01-13 00:05:48 +05:30
|
|
|
|
|
|
|
describe.each`
|
2023-04-23 21:23:45 +05:30
|
|
|
action | axiosMock | type | scope | expectedMutations | errorLogs
|
|
|
|
${actions.fetchSidebarCount} | ${{ method: 'onGet', code: HTTP_STATUS_OK }} | ${'success'} | ${'issues'} | ${[MOCK_NAVIGATION_ACTION_MUTATION]} | ${0}
|
|
|
|
${actions.fetchSidebarCount} | ${{ method: null, code: 0 }} | ${'success'} | ${'projects'} | ${[]} | ${0}
|
|
|
|
${actions.fetchSidebarCount} | ${{ method: 'onGet', code: HTTP_STATUS_INTERNAL_SERVER_ERROR }} | ${'error'} | ${'issues'} | ${[]} | ${1}
|
2023-01-13 00:05:48 +05:30
|
|
|
`('fetchSidebarCount', ({ action, axiosMock, type, expectedMutations, scope, errorLogs }) => {
|
|
|
|
describe(`on ${type}`, () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
state.navigation = MOCK_NAVIGATION_DATA;
|
|
|
|
state.urlQuery = {
|
|
|
|
scope,
|
|
|
|
};
|
|
|
|
|
|
|
|
if (axiosMock.method) {
|
|
|
|
mock[axiosMock.method]().reply(axiosMock.code, MOCK_ENDPOINT_RESPONSE);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
it(`should ${expectedMutations.length === 0 ? 'NOT ' : ''}dispatch ${
|
|
|
|
expectedMutations.length === 0 ? '' : 'the correct '
|
|
|
|
}mutations for ${scope}`, () => {
|
|
|
|
return testAction({ action, state, expectedMutations }).then(() => {
|
|
|
|
expect(logger.logError).toHaveBeenCalledTimes(errorLogs);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2023-04-23 21:23:45 +05:30
|
|
|
|
|
|
|
describe.each`
|
|
|
|
action | axiosMock | type | expectedMutations | errorLogs
|
|
|
|
${actions.fetchLanguageAggregation} | ${{ method: 'onGet', code: HTTP_STATUS_OK }} | ${'success'} | ${MOCK_RECEIVE_AGGREGATIONS_SUCCESS_MUTATION} | ${0}
|
|
|
|
${actions.fetchLanguageAggregation} | ${{ method: 'onPut', code: 0 }} | ${'error'} | ${MOCK_RECEIVE_AGGREGATIONS_ERROR_MUTATION} | ${1}
|
|
|
|
${actions.fetchLanguageAggregation} | ${{ method: 'onGet', code: HTTP_STATUS_INTERNAL_SERVER_ERROR }} | ${'error'} | ${MOCK_RECEIVE_AGGREGATIONS_ERROR_MUTATION} | ${1}
|
|
|
|
`('fetchLanguageAggregation', ({ action, axiosMock, type, expectedMutations, errorLogs }) => {
|
|
|
|
describe(`on ${type}`, () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
if (axiosMock.method) {
|
|
|
|
mock[axiosMock.method]().reply(
|
|
|
|
axiosMock.code,
|
|
|
|
axiosMock.code === HTTP_STATUS_OK ? MOCK_AGGREGATIONS : [],
|
|
|
|
);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
it(`should ${type === 'error' ? 'NOT ' : ''}dispatch ${
|
|
|
|
type === 'error' ? '' : 'the correct '
|
|
|
|
}mutations`, () => {
|
|
|
|
return testAction({ action, state, expectedMutations }).then(() => {
|
|
|
|
expect(logger.logError).toHaveBeenCalledTimes(errorLogs);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2021-01-29 00:20:46 +05:30
|
|
|
});
|