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';
|
2021-06-30 02:07:17 -04:00
|
|
|
import { GROUPS_LOCAL_STORAGE_KEY, PROJECTS_LOCAL_STORAGE_KEY } from '~/search/store/constants';
|
2020-10-30 14:08:56 -04:00
|
|
|
import * as types from '~/search/store/mutation_types';
|
2020-12-15 07:10:17 -05:00
|
|
|
import createState from '~/search/store/state';
|
2021-06-30 02:07:17 -04:00
|
|
|
import * as storeUtils from '~/search/store/utils';
|
2021-07-11 23:09:27 -04:00
|
|
|
import {
|
|
|
|
MOCK_QUERY,
|
|
|
|
MOCK_GROUPS,
|
|
|
|
MOCK_PROJECT,
|
|
|
|
MOCK_PROJECTS,
|
|
|
|
MOCK_GROUP,
|
|
|
|
FRESH_STORED_DATA,
|
|
|
|
MOCK_FRESH_DATA_RES,
|
2021-08-05 02:09:25 -04:00
|
|
|
PRELOAD_EXPECTED_MUTATIONS,
|
2021-07-11 23:09:27 -04:00
|
|
|
PROMISE_ALL_EXPECTED_MUTATIONS,
|
|
|
|
} 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
|
|
|
|
2021-06-07 05:10:26 -04:00
|
|
|
const flashCallback = (callCount) => {
|
|
|
|
expect(createFlash).toHaveBeenCalledTimes(callCount);
|
2020-10-30 14:08:56 -04:00
|
|
|
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-06-07 05:10:26 -04:00
|
|
|
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 }) => {
|
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-06-07 05:10:26 -04:00
|
|
|
return testAction({ action, state, expectedMutations }).then(() =>
|
|
|
|
flashCallback(flashCallCount),
|
|
|
|
);
|
2020-10-30 14:08:56 -04:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2020-11-16 19:09:16 -05:00
|
|
|
|
2021-07-11 23:09:27 -04:00
|
|
|
describe.each`
|
2021-08-05 02:09:25 -04:00
|
|
|
action | axiosMock | type | expectedMutations | flashCallCount
|
|
|
|
${actions.loadFrequentGroups} | ${{ method: 'onGet', code: 200 }} | ${'success'} | ${[PROMISE_ALL_EXPECTED_MUTATIONS.resGroups]} | ${0}
|
|
|
|
${actions.loadFrequentGroups} | ${{ method: 'onGet', code: 500 }} | ${'error'} | ${[]} | ${1}
|
|
|
|
${actions.loadFrequentProjects} | ${{ method: 'onGet', code: 200 }} | ${'success'} | ${[PROMISE_ALL_EXPECTED_MUTATIONS.resProjects]} | ${0}
|
|
|
|
${actions.loadFrequentProjects} | ${{ method: 'onGet', code: 500 }} | ${'error'} | ${[]} | ${1}
|
|
|
|
`('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-07-11 23:09:27 -04:00
|
|
|
|
2021-08-05 02:09:25 -04:00
|
|
|
it(`should dispatch the correct mutations`, () => {
|
|
|
|
return testAction({ action, state, expectedMutations }).then(() => {
|
|
|
|
flashCallback(flashCallCount);
|
2021-07-11 23:09:27 -04:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2021-08-05 02:09:25 -04:00
|
|
|
});
|
|
|
|
});
|
2021-07-11 23:09:27 -04:00
|
|
|
|
2021-06-23 20:07:35 -04:00
|
|
|
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' });
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
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', () => {
|
2021-07-14 14:08:31 -04:00
|
|
|
it('calls Api.groupProjects with expected parameters', () => {
|
2020-12-15 07:10:17 -05:00
|
|
|
actions.fetchProjects({ commit: mockCommit, state });
|
|
|
|
|
2021-06-18 08:10:03 -04:00
|
|
|
expect(Api.groupProjects).toHaveBeenCalledWith(
|
|
|
|
state.query.group_id,
|
|
|
|
state.query.search,
|
|
|
|
{
|
|
|
|
order_by: 'similarity',
|
2021-07-14 14:08:31 -04:00
|
|
|
include_subgroups: true,
|
|
|
|
with_shared: false,
|
2021-06-18 08:10:03 -04:00
|
|
|
},
|
|
|
|
expect.any(Function),
|
|
|
|
);
|
2020-12-15 07:10:17 -05:00
|
|
|
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
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2021-06-30 02:07:17 -04:00
|
|
|
|
2021-08-05 02:09:25 -04:00
|
|
|
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-06-30 02:07:17 -04:00
|
|
|
describe('setFrequentGroup', () => {
|
|
|
|
beforeEach(() => {
|
2021-08-05 02:09:25 -04:00
|
|
|
storeUtils.setFrequentItemToLS = jest.fn().mockReturnValue(FRESH_STORED_DATA);
|
2021-06-30 02:07:17 -04:00
|
|
|
});
|
|
|
|
|
2021-08-05 02:09:25 -04:00
|
|
|
it(`calls setFrequentItemToLS with ${GROUPS_LOCAL_STORAGE_KEY} and item data then commits LOAD_FREQUENT_ITEMS`, async () => {
|
2021-06-30 02:07:17 -04:00
|
|
|
await testAction({
|
|
|
|
action: actions.setFrequentGroup,
|
2021-08-05 02:09:25 -04:00
|
|
|
expectedMutations: [
|
|
|
|
{
|
|
|
|
type: types.LOAD_FREQUENT_ITEMS,
|
|
|
|
payload: { key: GROUPS_LOCAL_STORAGE_KEY, data: FRESH_STORED_DATA },
|
|
|
|
},
|
|
|
|
],
|
2021-06-30 02:07:17 -04:00
|
|
|
payload: MOCK_GROUP,
|
|
|
|
state,
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(storeUtils.setFrequentItemToLS).toHaveBeenCalledWith(
|
|
|
|
GROUPS_LOCAL_STORAGE_KEY,
|
|
|
|
state.frequentItems,
|
|
|
|
MOCK_GROUP,
|
|
|
|
);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('setFrequentProject', () => {
|
|
|
|
beforeEach(() => {
|
2021-08-05 02:09:25 -04:00
|
|
|
storeUtils.setFrequentItemToLS = jest.fn().mockReturnValue(FRESH_STORED_DATA);
|
2021-06-30 02:07:17 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
it(`calls setFrequentItemToLS with ${PROJECTS_LOCAL_STORAGE_KEY} and item data`, async () => {
|
|
|
|
await testAction({
|
|
|
|
action: actions.setFrequentProject,
|
2021-08-05 02:09:25 -04:00
|
|
|
expectedMutations: [
|
|
|
|
{
|
|
|
|
type: types.LOAD_FREQUENT_ITEMS,
|
|
|
|
payload: { key: PROJECTS_LOCAL_STORAGE_KEY, data: FRESH_STORED_DATA },
|
|
|
|
},
|
|
|
|
],
|
2021-06-30 02:07:17 -04:00
|
|
|
payload: MOCK_PROJECT,
|
|
|
|
state,
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(storeUtils.setFrequentItemToLS).toHaveBeenCalledWith(
|
|
|
|
PROJECTS_LOCAL_STORAGE_KEY,
|
|
|
|
state.frequentItems,
|
|
|
|
MOCK_PROJECT,
|
|
|
|
);
|
|
|
|
});
|
|
|
|
});
|
2020-10-30 14:08:56 -04:00
|
|
|
});
|