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';
|
2023-03-17 16:20:25 +05:30
|
|
|
import { useLocalStorageSpy } from 'helpers/local_storage_helper';
|
2018-11-08 19:23:39 +05:30
|
|
|
import {
|
|
|
|
mockNamespace,
|
|
|
|
mockStorageKey,
|
|
|
|
mockFrequentProjects,
|
|
|
|
mockSearchedProjects,
|
|
|
|
} from '../mock_data';
|
|
|
|
|
|
|
|
describe('Frequent Items Dropdown Store Actions', () => {
|
2023-03-17 16:20:25 +05:30
|
|
|
useLocalStorageSpy();
|
2018-11-08 19:23:39 +05:30
|
|
|
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 }],
|
|
|
|
[],
|
|
|
|
);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2023-03-17 16:20:25 +05:30
|
|
|
describe('toggleItemsListEditablity', () => {
|
|
|
|
it('should toggle items list editablity', () => {
|
|
|
|
return testAction(
|
|
|
|
actions.toggleItemsListEditablity,
|
|
|
|
null,
|
|
|
|
mockedState,
|
|
|
|
[{ type: types.TOGGLE_ITEMS_LIST_EDITABILITY }],
|
|
|
|
[],
|
|
|
|
);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2018-11-08 19:23:39 +05:30
|
|
|
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' }],
|
|
|
|
);
|
|
|
|
});
|
|
|
|
});
|
2023-03-17 16:20:25 +05:30
|
|
|
|
|
|
|
describe('removeFrequentItemSuccess', () => {
|
|
|
|
it('should remove frequent item on success', () => {
|
|
|
|
return testAction(
|
|
|
|
actions.removeFrequentItemSuccess,
|
|
|
|
{ itemId: 1 },
|
|
|
|
mockedState,
|
|
|
|
[
|
|
|
|
{
|
|
|
|
type: types.RECEIVE_REMOVE_FREQUENT_ITEM_SUCCESS,
|
|
|
|
payload: { itemId: 1 },
|
|
|
|
},
|
|
|
|
],
|
|
|
|
[],
|
|
|
|
);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('removeFrequentItemError', () => {
|
|
|
|
it('should should not remove frequent item on failure', () => {
|
|
|
|
return testAction(
|
|
|
|
actions.removeFrequentItemError,
|
|
|
|
null,
|
|
|
|
mockedState,
|
|
|
|
[{ type: types.RECEIVE_REMOVE_FREQUENT_ITEM_ERROR }],
|
|
|
|
[],
|
|
|
|
);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('removeFrequentItem', () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
mockedState.items = [...mockFrequentProjects];
|
|
|
|
window.localStorage.setItem(mockStorageKey, JSON.stringify(mockFrequentProjects));
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should remove provided itemId from localStorage', () => {
|
|
|
|
jest.spyOn(AccessorUtilities, 'canUseLocalStorage').mockReturnValue(true);
|
|
|
|
|
|
|
|
actions.removeFrequentItem(
|
|
|
|
{ commit: jest.fn(), dispatch: jest.fn(), state: mockedState },
|
|
|
|
mockFrequentProjects[0].id,
|
|
|
|
);
|
|
|
|
|
|
|
|
expect(window.localStorage.getItem(mockStorageKey)).toBe(
|
|
|
|
JSON.stringify(mockFrequentProjects.slice(1)), // First item was removed
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should dispatch `removeFrequentItemSuccess` on localStorage update success', () => {
|
|
|
|
jest.spyOn(AccessorUtilities, 'canUseLocalStorage').mockReturnValue(true);
|
|
|
|
|
|
|
|
return testAction(
|
|
|
|
actions.removeFrequentItem,
|
|
|
|
mockFrequentProjects[0].id,
|
|
|
|
mockedState,
|
|
|
|
[],
|
|
|
|
[{ type: 'removeFrequentItemSuccess', payload: mockFrequentProjects[0].id }],
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should dispatch `removeFrequentItemError` on localStorage update failure', () => {
|
|
|
|
jest.spyOn(AccessorUtilities, 'canUseLocalStorage').mockReturnValue(false);
|
|
|
|
|
|
|
|
return testAction(
|
|
|
|
actions.removeFrequentItem,
|
|
|
|
mockFrequentProjects[0].id,
|
|
|
|
mockedState,
|
|
|
|
[],
|
|
|
|
[{ type: 'removeFrequentItemError' }],
|
|
|
|
);
|
|
|
|
});
|
|
|
|
});
|
2018-11-08 19:23:39 +05:30
|
|
|
});
|