2018-11-08 19:23:39 +05:30
|
|
|
import MockAdapter from 'axios-mock-adapter';
|
2021-03-11 19:13:27 +05:30
|
|
|
import testAction from 'helpers/vuex_action_helper';
|
2018-11-08 19:23:39 +05:30
|
|
|
import * as actions from '~/frequent_items/store/actions';
|
|
|
|
import * as types from '~/frequent_items/store/mutation_types';
|
|
|
|
import state from '~/frequent_items/store/state';
|
2021-03-11 19:13:27 +05:30
|
|
|
import AccessorUtilities from '~/lib/utils/accessor';
|
|
|
|
import axios from '~/lib/utils/axios_utils';
|
2018-11-08 19:23:39 +05:30
|
|
|
import {
|
|
|
|
mockNamespace,
|
|
|
|
mockStorageKey,
|
|
|
|
mockFrequentProjects,
|
|
|
|
mockSearchedProjects,
|
|
|
|
} from '../mock_data';
|
|
|
|
|
|
|
|
describe('Frequent Items Dropdown Store Actions', () => {
|
|
|
|
let mockedState;
|
|
|
|
let mock;
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
mockedState = state();
|
|
|
|
mock = new MockAdapter(axios);
|
|
|
|
|
|
|
|
mockedState.namespace = mockNamespace;
|
|
|
|
mockedState.storageKey = mockStorageKey;
|
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
mock.restore();
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('setNamespace', () => {
|
2022-06-21 17:19:12 +05:30
|
|
|
it('should set namespace', () => {
|
|
|
|
return testAction(
|
2018-11-08 19:23:39 +05:30
|
|
|
actions.setNamespace,
|
|
|
|
mockNamespace,
|
|
|
|
mockedState,
|
|
|
|
[{ type: types.SET_NAMESPACE, payload: mockNamespace }],
|
|
|
|
[],
|
|
|
|
);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('setStorageKey', () => {
|
2022-06-21 17:19:12 +05:30
|
|
|
it('should set storage key', () => {
|
|
|
|
return testAction(
|
2018-11-08 19:23:39 +05:30
|
|
|
actions.setStorageKey,
|
|
|
|
mockStorageKey,
|
|
|
|
mockedState,
|
|
|
|
[{ type: types.SET_STORAGE_KEY, payload: mockStorageKey }],
|
|
|
|
[],
|
|
|
|
);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('requestFrequentItems', () => {
|
2022-06-21 17:19:12 +05:30
|
|
|
it('should request frequent items', () => {
|
|
|
|
return testAction(
|
2018-11-08 19:23:39 +05:30
|
|
|
actions.requestFrequentItems,
|
|
|
|
null,
|
|
|
|
mockedState,
|
|
|
|
[{ type: types.REQUEST_FREQUENT_ITEMS }],
|
|
|
|
[],
|
|
|
|
);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('receiveFrequentItemsSuccess', () => {
|
2022-06-21 17:19:12 +05:30
|
|
|
it('should set frequent items', () => {
|
|
|
|
return testAction(
|
2018-11-08 19:23:39 +05:30
|
|
|
actions.receiveFrequentItemsSuccess,
|
|
|
|
mockFrequentProjects,
|
|
|
|
mockedState,
|
|
|
|
[{ type: types.RECEIVE_FREQUENT_ITEMS_SUCCESS, payload: mockFrequentProjects }],
|
|
|
|
[],
|
|
|
|
);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('receiveFrequentItemsError', () => {
|
2022-06-21 17:19:12 +05:30
|
|
|
it('should set frequent items error state', () => {
|
|
|
|
return testAction(
|
2018-11-08 19:23:39 +05:30
|
|
|
actions.receiveFrequentItemsError,
|
|
|
|
null,
|
|
|
|
mockedState,
|
|
|
|
[{ type: types.RECEIVE_FREQUENT_ITEMS_ERROR }],
|
|
|
|
[],
|
|
|
|
);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('fetchFrequentItems', () => {
|
2022-06-21 17:19:12 +05:30
|
|
|
it('should dispatch `receiveFrequentItemsSuccess`', () => {
|
2018-11-08 19:23:39 +05:30
|
|
|
mockedState.namespace = mockNamespace;
|
|
|
|
mockedState.storageKey = mockStorageKey;
|
|
|
|
|
2022-06-21 17:19:12 +05:30
|
|
|
return testAction(
|
2018-11-08 19:23:39 +05:30
|
|
|
actions.fetchFrequentItems,
|
|
|
|
null,
|
|
|
|
mockedState,
|
|
|
|
[],
|
|
|
|
[{ type: 'requestFrequentItems' }, { type: 'receiveFrequentItemsSuccess', payload: [] }],
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2022-06-21 17:19:12 +05:30
|
|
|
it('should dispatch `receiveFrequentItemsError`', () => {
|
2021-11-11 11:23:49 +05:30
|
|
|
jest.spyOn(AccessorUtilities, 'canUseLocalStorage').mockReturnValue(false);
|
2018-11-08 19:23:39 +05:30
|
|
|
mockedState.namespace = mockNamespace;
|
|
|
|
mockedState.storageKey = mockStorageKey;
|
|
|
|
|
2022-06-21 17:19:12 +05:30
|
|
|
return testAction(
|
2018-11-08 19:23:39 +05:30
|
|
|
actions.fetchFrequentItems,
|
|
|
|
null,
|
|
|
|
mockedState,
|
|
|
|
[],
|
|
|
|
[{ type: 'requestFrequentItems' }, { type: 'receiveFrequentItemsError' }],
|
|
|
|
);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('requestSearchedItems', () => {
|
2022-06-21 17:19:12 +05:30
|
|
|
it('should request searched items', () => {
|
|
|
|
return testAction(
|
2018-11-08 19:23:39 +05:30
|
|
|
actions.requestSearchedItems,
|
|
|
|
null,
|
|
|
|
mockedState,
|
|
|
|
[{ type: types.REQUEST_SEARCHED_ITEMS }],
|
|
|
|
[],
|
|
|
|
);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('receiveSearchedItemsSuccess', () => {
|
2022-06-21 17:19:12 +05:30
|
|
|
it('should set searched items', () => {
|
|
|
|
return testAction(
|
2018-11-08 19:23:39 +05:30
|
|
|
actions.receiveSearchedItemsSuccess,
|
|
|
|
mockSearchedProjects,
|
|
|
|
mockedState,
|
|
|
|
[{ type: types.RECEIVE_SEARCHED_ITEMS_SUCCESS, payload: mockSearchedProjects }],
|
|
|
|
[],
|
|
|
|
);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('receiveSearchedItemsError', () => {
|
2022-06-21 17:19:12 +05:30
|
|
|
it('should set searched items error state', () => {
|
|
|
|
return testAction(
|
2018-11-08 19:23:39 +05:30
|
|
|
actions.receiveSearchedItemsError,
|
|
|
|
null,
|
|
|
|
mockedState,
|
|
|
|
[{ type: types.RECEIVE_SEARCHED_ITEMS_ERROR }],
|
|
|
|
[],
|
|
|
|
);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('fetchSearchedItems', () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
gon.api_version = 'v4';
|
|
|
|
});
|
|
|
|
|
2022-06-21 17:19:12 +05:30
|
|
|
it('should dispatch `receiveSearchedItemsSuccess`', () => {
|
2019-12-26 22:10:19 +05:30
|
|
|
mock.onGet(/\/api\/v4\/projects.json(.*)$/).replyOnce(200, mockSearchedProjects, {});
|
2018-11-08 19:23:39 +05:30
|
|
|
|
2022-06-21 17:19:12 +05:30
|
|
|
return testAction(
|
2018-11-08 19:23:39 +05:30
|
|
|
actions.fetchSearchedItems,
|
|
|
|
null,
|
|
|
|
mockedState,
|
|
|
|
[],
|
|
|
|
[
|
|
|
|
{ type: 'requestSearchedItems' },
|
2019-12-26 22:10:19 +05:30
|
|
|
{
|
|
|
|
type: 'receiveSearchedItemsSuccess',
|
|
|
|
payload: { data: mockSearchedProjects, headers: {} },
|
|
|
|
},
|
2018-11-08 19:23:39 +05:30
|
|
|
],
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2022-06-21 17:19:12 +05:30
|
|
|
it('should dispatch `receiveSearchedItemsError`', () => {
|
2018-11-08 19:23:39 +05:30
|
|
|
gon.api_version = 'v4';
|
|
|
|
mock.onGet(/\/api\/v4\/projects.json(.*)$/).replyOnce(500);
|
|
|
|
|
2022-06-21 17:19:12 +05:30
|
|
|
return testAction(
|
2018-11-08 19:23:39 +05:30
|
|
|
actions.fetchSearchedItems,
|
|
|
|
null,
|
|
|
|
mockedState,
|
|
|
|
[],
|
|
|
|
[{ type: 'requestSearchedItems' }, { type: 'receiveSearchedItemsError' }],
|
|
|
|
);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('setSearchQuery', () => {
|
2022-06-21 17:19:12 +05:30
|
|
|
it('should commit query and dispatch `fetchSearchedItems` when query is present', () => {
|
|
|
|
return testAction(
|
2018-11-08 19:23:39 +05:30
|
|
|
actions.setSearchQuery,
|
|
|
|
{ query: 'test' },
|
|
|
|
mockedState,
|
2018-11-18 11:00:15 +05:30
|
|
|
[{ type: types.SET_SEARCH_QUERY, payload: { query: 'test' } }],
|
2018-11-08 19:23:39 +05:30
|
|
|
[{ type: 'fetchSearchedItems', payload: { query: 'test' } }],
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2022-06-21 17:19:12 +05:30
|
|
|
it('should commit query and dispatch `fetchFrequentItems` when query is empty', () => {
|
|
|
|
return testAction(
|
2018-11-08 19:23:39 +05:30
|
|
|
actions.setSearchQuery,
|
|
|
|
null,
|
|
|
|
mockedState,
|
2018-11-18 11:00:15 +05:30
|
|
|
[{ type: types.SET_SEARCH_QUERY, payload: null }],
|
2018-11-08 19:23:39 +05:30
|
|
|
[{ type: 'fetchFrequentItems' }],
|
|
|
|
);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|