1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
|
import MockAdapter from 'axios-mock-adapter';
import testAction from 'helpers/vuex_action_helper';
import Api from '~/api';
import { createAlert } from '~/alert';
import * as logger from '~/lib/logger';
import axios from '~/lib/utils/axios_utils';
import { HTTP_STATUS_INTERNAL_SERVER_ERROR, HTTP_STATUS_OK } from '~/lib/utils/http_status';
import * as urlUtils from '~/lib/utils/url_utility';
import * as actions from '~/search/store/actions';
import {
GROUPS_LOCAL_STORAGE_KEY,
PROJECTS_LOCAL_STORAGE_KEY,
SIDEBAR_PARAMS,
} from '~/search/store/constants';
import * as types from '~/search/store/mutation_types';
import createState from '~/search/store/state';
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,
PRELOAD_EXPECTED_MUTATIONS,
PROMISE_ALL_EXPECTED_MUTATIONS,
MOCK_NAVIGATION_DATA,
MOCK_NAVIGATION_ACTION_MUTATION,
MOCK_ENDPOINT_RESPONSE,
MOCK_RECEIVE_AGGREGATIONS_SUCCESS_MUTATION,
MOCK_RECEIVE_AGGREGATIONS_ERROR_MUTATION,
MOCK_AGGREGATIONS,
MOCK_LABEL_AGGREGATIONS,
} from '../mock_data';
jest.mock('~/alert');
jest.mock('~/lib/utils/url_utility', () => ({
setUrlParams: jest.fn(),
joinPaths: jest.fn().mockReturnValue(''),
visitUrl: jest.fn(),
}));
jest.mock('~/lib/logger', () => ({
logError: jest.fn(),
}));
describe('Global Search Store Actions', () => {
let mock;
let state;
const alertCallback = (callCount) => {
expect(createAlert).toHaveBeenCalledTimes(callCount);
createAlert.mockClear();
};
beforeEach(() => {
state = createState({ query: MOCK_QUERY });
mock = new MockAdapter(axios);
});
afterEach(() => {
state = null;
mock.restore();
});
describe.each`
action | axiosMock | type | expectedMutations | alertCallCount
${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}
`(`axios calls`, ({ action, axiosMock, type, expectedMutations, alertCallCount }) => {
describe(action.name, () => {
describe(`on ${type}`, () => {
beforeEach(() => {
mock[axiosMock.method]().replyOnce(axiosMock.code, axiosMock.res);
});
it(`should dispatch the correct mutations`, () => {
return testAction({ action, state, expectedMutations }).then(() =>
alertCallback(alertCallCount),
);
});
});
});
});
describe.each`
action | axiosMock | type | expectedMutations | alertCallCount
${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}
`('Promise.all calls', ({ action, axiosMock, type, expectedMutations, alertCallCount }) => {
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);
});
it(`should dispatch the correct mutations`, () => {
return testAction({ action, state, expectedMutations }).then(() => {
alertCallback(alertCallCount);
});
});
});
});
});
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' });
});
});
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 with expected parameters', () => {
actions.fetchProjects({ commit: mockCommit, state }, MOCK_QUERY.search);
expect(Api.groupProjects).toHaveBeenCalledWith(state.query.group_id, state.query.search, {
order_by: 'similarity',
include_subgroups: true,
with_shared: false,
});
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).toHaveBeenCalledWith(state.query.search, {
order_by: 'similarity',
});
});
});
});
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);
});
it(`should dispatch the correct mutations`, () => {
return testAction({ action: actions.setQuery, payload, state, expectedMutations });
});
});
});
describe('applyQuery', () => {
it('calls visitUrl and setParams with the state.query', () => {
return testAction(actions.applyQuery, null, state, [], [], () => {
expect(urlUtils.setUrlParams).toHaveBeenCalledWith({ ...state.query, page: null });
expect(urlUtils.visitUrl).toHaveBeenCalled();
});
});
});
describe('resetQuery', () => {
it('calls visitUrl and setParams with empty values', () => {
return testAction(actions.resetQuery, null, state, [], [], () => {
expect(urlUtils.setUrlParams).toHaveBeenCalledWith({
...state.query,
page: null,
state: null,
confidential: null,
});
expect(urlUtils.visitUrl).toHaveBeenCalled();
});
});
});
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);
});
});
describe('setFrequentGroup', () => {
beforeEach(() => {
storeUtils.setFrequentItemToLS = jest.fn().mockReturnValue(FRESH_STORED_DATA);
});
it(`calls setFrequentItemToLS with ${GROUPS_LOCAL_STORAGE_KEY} and item data then commits LOAD_FREQUENT_ITEMS`, async () => {
await testAction({
action: actions.setFrequentGroup,
expectedMutations: [
{
type: types.LOAD_FREQUENT_ITEMS,
payload: { key: GROUPS_LOCAL_STORAGE_KEY, data: FRESH_STORED_DATA },
},
],
payload: MOCK_GROUP,
state,
});
expect(storeUtils.setFrequentItemToLS).toHaveBeenCalledWith(
GROUPS_LOCAL_STORAGE_KEY,
state.frequentItems,
MOCK_GROUP,
);
});
});
describe('setFrequentProject', () => {
beforeEach(() => {
storeUtils.setFrequentItemToLS = jest.fn().mockReturnValue(FRESH_STORED_DATA);
});
it(`calls setFrequentItemToLS with ${PROJECTS_LOCAL_STORAGE_KEY} and item data`, async () => {
await testAction({
action: actions.setFrequentProject,
expectedMutations: [
{
type: types.LOAD_FREQUENT_ITEMS,
payload: { key: PROJECTS_LOCAL_STORAGE_KEY, data: FRESH_STORED_DATA },
},
],
payload: MOCK_PROJECT,
state,
});
expect(storeUtils.setFrequentItemToLS).toHaveBeenCalledWith(
PROJECTS_LOCAL_STORAGE_KEY,
state.frequentItems,
MOCK_PROJECT,
);
});
});
describe.each`
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 }} | ${'error'} | ${'projects'} | ${[]} | ${1}
${actions.fetchSidebarCount} | ${{ method: 'onGet', code: HTTP_STATUS_INTERNAL_SERVER_ERROR }} | ${'error'} | ${'issues'} | ${[]} | ${1}
`('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);
});
});
});
});
describe.each`
action | axiosMock | type | expectedMutations | errorLogs
${actions.fetchAllAggregation} | ${{ method: 'onGet', code: HTTP_STATUS_OK }} | ${'success'} | ${MOCK_RECEIVE_AGGREGATIONS_SUCCESS_MUTATION} | ${0}
${actions.fetchAllAggregation} | ${{ method: 'onPut', code: 0 }} | ${'error'} | ${MOCK_RECEIVE_AGGREGATIONS_ERROR_MUTATION} | ${1}
${actions.fetchAllAggregation} | ${{ method: 'onGet', code: HTTP_STATUS_INTERNAL_SERVER_ERROR }} | ${'error'} | ${MOCK_RECEIVE_AGGREGATIONS_ERROR_MUTATION} | ${1}
`('fetchAllAggregation', ({ 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);
});
});
});
});
describe('resetLanguageQueryWithRedirect', () => {
it('calls visitUrl and setParams with the state.query', () => {
return testAction(actions.resetLanguageQueryWithRedirect, null, state, [], [], () => {
expect(urlUtils.setUrlParams).toHaveBeenCalledWith({ ...state.query, page: null });
expect(urlUtils.visitUrl).toHaveBeenCalled();
});
});
});
describe('resetLanguageQuery', () => {
it('calls commit SET_QUERY with value []', () => {
state = { ...state, query: { ...state.query, language: ['YAML', 'Text', 'Markdown'] } };
return testAction(
actions.resetLanguageQuery,
null,
state,
[{ type: types.SET_QUERY, payload: { key: 'language', value: [] } }],
[],
);
});
});
describe('closeLabel', () => {
beforeEach(() => {
state = createState({
query: MOCK_QUERY,
aggregations: MOCK_LABEL_AGGREGATIONS,
});
});
it('removes correct labels from query and sets sidebar dirty', () => {
const expectedResult = [
{
payload: {
key: 'labels',
value: ['37'],
},
type: 'SET_QUERY',
},
{
payload: true,
type: 'SET_SIDEBAR_DIRTY',
},
];
return testAction(actions.closeLabel, { key: '60' }, state, expectedResult, []);
});
});
describe('setLabelFilterSearch', () => {
beforeEach(() => {
state = createState({
query: MOCK_QUERY,
aggregations: MOCK_LABEL_AGGREGATIONS,
});
});
it('sets search string', () => {
const expectedResult = [
{
payload: 'test',
type: 'SET_LABEL_SEARCH_STRING',
},
];
return testAction(actions.setLabelFilterSearch, { value: 'test' }, state, expectedResult, []);
});
});
});
|