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
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
|
import { GlSorting, GlSortingItem, GlTab } from '@gitlab/ui';
import Vue, { nextTick } from 'vue';
import AxiosMockAdapter from 'axios-mock-adapter';
import { mountExtended } from 'helpers/vue_test_utils_helper';
import OverviewTabs from '~/groups/components/overview_tabs.vue';
import GroupsApp from '~/groups/components/app.vue';
import GroupFolderComponent from '~/groups/components/group_folder.vue';
import SubgroupsAndProjectsEmptyState from '~/groups/components/empty_states/subgroups_and_projects_empty_state.vue';
import SharedProjectsEmptyState from '~/groups/components/empty_states/shared_projects_empty_state.vue';
import ArchivedProjectsEmptyState from '~/groups/components/empty_states/archived_projects_empty_state.vue';
import GroupsStore from '~/groups/store/groups_store';
import GroupsService from '~/groups/service/groups_service';
import ArchivedProjectsService from '~/groups/service/archived_projects_service';
import { createRouter } from '~/groups/init_overview_tabs';
import eventHub from '~/groups/event_hub';
import {
ACTIVE_TAB_SUBGROUPS_AND_PROJECTS,
ACTIVE_TAB_SHARED,
ACTIVE_TAB_ARCHIVED,
SORTING_ITEM_NAME,
SORTING_ITEM_UPDATED,
SORTING_ITEM_STARS,
} from '~/groups/constants';
import axios from '~/lib/utils/axios_utils';
import waitForPromises from 'helpers/wait_for_promises';
Vue.component('GroupFolder', GroupFolderComponent);
const router = createRouter();
describe('OverviewTabs', () => {
let wrapper;
let axiosMock;
const defaultProvide = {
groupId: '1',
endpoints: {
subgroups_and_projects: '/groups/foobar/-/children.json',
shared: '/groups/foobar/-/shared_projects.json',
archived: '/groups/foobar/-/children.json?archived=only',
},
newSubgroupPath: '/groups/new',
newProjectPath: 'projects/new',
newSubgroupIllustration: '',
newProjectIllustration: '',
emptyProjectsIllustration: '',
emptySubgroupIllustration: '',
canCreateSubgroups: false,
canCreateProjects: false,
initialSort: 'name_asc',
};
const routerMock = {
push: jest.fn(),
};
const createComponent = async ({
route = { name: ACTIVE_TAB_SUBGROUPS_AND_PROJECTS, params: { group: 'foo/bar/baz' } },
provide = {},
} = {}) => {
wrapper = mountExtended(OverviewTabs, {
router,
provide: {
...defaultProvide,
...provide,
},
mocks: { $route: route, $router: routerMock },
});
await nextTick();
};
const findTabPanels = () => wrapper.findAllComponents(GlTab);
const findTab = (name) => wrapper.findByRole('tab', { name });
const findSelectedTab = () => wrapper.findByRole('tab', { selected: true });
const findSearchInput = () => wrapper.findByPlaceholderText(OverviewTabs.i18n.searchPlaceholder);
beforeEach(() => {
axiosMock = new AxiosMockAdapter(axios);
axiosMock.onGet({ data: [] });
});
afterEach(() => {
axiosMock.restore();
});
it('renders `Subgroups and projects` tab with `GroupsApp` component with correct empty state', async () => {
await createComponent();
const tabPanel = findTabPanels().at(0);
expect(tabPanel.vm.$attrs).toMatchObject({
title: OverviewTabs.i18n[ACTIVE_TAB_SUBGROUPS_AND_PROJECTS],
lazy: false,
});
expect(tabPanel.findComponent(GroupsApp).props()).toMatchObject({
action: ACTIVE_TAB_SUBGROUPS_AND_PROJECTS,
store: new GroupsStore({ showSchemaMarkup: true }),
service: new GroupsService(
defaultProvide.endpoints[ACTIVE_TAB_SUBGROUPS_AND_PROJECTS],
defaultProvide.initialSort,
),
});
await waitForPromises();
expect(wrapper.findComponent(SubgroupsAndProjectsEmptyState).exists()).toBe(true);
});
it('renders `Shared projects` tab and renders `GroupsApp` component with correct empty state after clicking tab', async () => {
await createComponent();
const tabPanel = findTabPanels().at(1);
expect(tabPanel.vm.$attrs).toMatchObject({
title: OverviewTabs.i18n[ACTIVE_TAB_SHARED],
lazy: true,
});
await findTab(OverviewTabs.i18n[ACTIVE_TAB_SHARED]).trigger('click');
expect(tabPanel.findComponent(GroupsApp).props()).toMatchObject({
action: ACTIVE_TAB_SHARED,
store: new GroupsStore(),
service: new GroupsService(
defaultProvide.endpoints[ACTIVE_TAB_SHARED],
defaultProvide.initialSort,
),
});
expect(tabPanel.vm.$attrs.lazy).toBe(false);
await waitForPromises();
expect(wrapper.findComponent(SharedProjectsEmptyState).exists()).toBe(true);
});
it('renders `Archived projects` tab and renders `GroupsApp` component with correct empty state after clicking tab', async () => {
await createComponent();
const tabPanel = findTabPanels().at(2);
expect(tabPanel.vm.$attrs).toMatchObject({
title: OverviewTabs.i18n[ACTIVE_TAB_ARCHIVED],
lazy: true,
});
await findTab(OverviewTabs.i18n[ACTIVE_TAB_ARCHIVED]).trigger('click');
expect(tabPanel.findComponent(GroupsApp).props()).toMatchObject({
action: ACTIVE_TAB_ARCHIVED,
store: new GroupsStore(),
service: new ArchivedProjectsService(defaultProvide.groupId, defaultProvide.initialSort),
});
expect(tabPanel.vm.$attrs.lazy).toBe(false);
await waitForPromises();
expect(wrapper.findComponent(ArchivedProjectsEmptyState).exists()).toBe(true);
});
it('sets `lazy` prop to `false` for initially active tab and `true` for all other tabs', async () => {
await createComponent({ route: { name: ACTIVE_TAB_SHARED, params: { group: 'foo/bar' } } });
expect(findTabPanels().at(0).vm.$attrs.lazy).toBe(true);
expect(findTabPanels().at(1).vm.$attrs.lazy).toBe(false);
expect(findTabPanels().at(2).vm.$attrs.lazy).toBe(true);
});
describe.each([
[
{ name: ACTIVE_TAB_SUBGROUPS_AND_PROJECTS, params: { group: 'foo/bar/baz' } },
OverviewTabs.i18n[ACTIVE_TAB_SHARED],
{
name: ACTIVE_TAB_SHARED,
params: { group: ['foo', 'bar', 'baz'] },
},
],
[
{ name: ACTIVE_TAB_SUBGROUPS_AND_PROJECTS, params: { group: ['foo', 'bar', 'baz'] } },
OverviewTabs.i18n[ACTIVE_TAB_SHARED],
{
name: ACTIVE_TAB_SHARED,
params: { group: ['foo', 'bar', 'baz'] },
},
],
[
{ name: ACTIVE_TAB_SUBGROUPS_AND_PROJECTS, params: { group: 'foo' } },
OverviewTabs.i18n[ACTIVE_TAB_SHARED],
{
name: ACTIVE_TAB_SHARED,
params: { group: ['foo'] },
},
],
[
{ name: ACTIVE_TAB_SHARED, params: { group: 'foo/bar' } },
OverviewTabs.i18n[ACTIVE_TAB_ARCHIVED],
{
name: ACTIVE_TAB_ARCHIVED,
params: { group: ['foo', 'bar'] },
},
],
[
{ name: ACTIVE_TAB_SHARED, params: { group: 'foo/bar' } },
OverviewTabs.i18n[ACTIVE_TAB_SUBGROUPS_AND_PROJECTS],
{
name: ACTIVE_TAB_SUBGROUPS_AND_PROJECTS,
params: { group: ['foo', 'bar'] },
},
],
[
{ name: ACTIVE_TAB_ARCHIVED, params: { group: ['foo'] } },
OverviewTabs.i18n[ACTIVE_TAB_SHARED],
{
name: ACTIVE_TAB_SHARED,
params: { group: ['foo'] },
},
],
])('when current route is %j', (currentRoute, tabToClick, expectedRoute) => {
beforeEach(async () => {
await createComponent({ route: currentRoute });
});
it(`sets ${OverviewTabs.i18n[currentRoute.name]} as active tab`, () => {
expect(findSelectedTab().text()).toBe(OverviewTabs.i18n[currentRoute.name]);
});
it(`pushes expected route when ${tabToClick} tab is clicked`, async () => {
await findTab(tabToClick).trigger('click');
expect(routerMock.push).toHaveBeenCalledWith(expect.objectContaining(expectedRoute));
});
});
describe('searching and sorting', () => {
const setup = async () => {
jest.spyOn(eventHub, '$emit');
await createComponent();
// Click through tabs so they are all loaded
await findTab(OverviewTabs.i18n[ACTIVE_TAB_SHARED]).trigger('click');
await findTab(OverviewTabs.i18n[ACTIVE_TAB_ARCHIVED]).trigger('click');
await findTab(OverviewTabs.i18n[ACTIVE_TAB_SUBGROUPS_AND_PROJECTS]).trigger('click');
};
const sharedAssertions = ({ search, sort }) => {
it('sets `lazy` prop to `true` for all of the non-active tabs so they are reloaded after sort or search is applied', () => {
expect(findTabPanels().at(0).vm.$attrs.lazy).toBe(false);
expect(findTabPanels().at(1).vm.$attrs.lazy).toBe(true);
expect(findTabPanels().at(2).vm.$attrs.lazy).toBe(true);
});
it('emits `fetchFilteredAndSortedGroups` event from `eventHub`', () => {
expect(eventHub.$emit).toHaveBeenCalledWith(
`${ACTIVE_TAB_SUBGROUPS_AND_PROJECTS}fetchFilteredAndSortedGroups`,
{
filterGroupsBy: search,
sortBy: sort,
},
);
});
};
describe('when search is typed in', () => {
describe('when search is greater than or equal to 3 characters', () => {
const search = 'Foo bar';
beforeEach(async () => {
await setup();
await findSearchInput().setValue(search);
});
it('updates query string with `filter` key', () => {
expect(routerMock.push).toHaveBeenCalledWith({ query: { filter: search } });
});
sharedAssertions({ search, sort: defaultProvide.initialSort });
});
describe('when search is less than 3 characters', () => {
const search = 'Fo';
beforeEach(async () => {
await setup();
await findSearchInput().setValue(search);
});
it('does not emit `fetchFilteredAndSortedGroups` event from `eventHub`', () => {
expect(eventHub.$emit).not.toHaveBeenCalledWith(
`${ACTIVE_TAB_SUBGROUPS_AND_PROJECTS}fetchFilteredAndSortedGroups`,
{
filterGroupsBy: search,
sortBy: defaultProvide.initialSort,
},
);
});
});
});
describe('when sort is changed', () => {
beforeEach(async () => {
await setup();
wrapper.findAllComponents(GlSortingItem).at(2).vm.$emit('click');
await nextTick();
});
it('updates query string with `sort` key', () => {
expect(routerMock.push).toHaveBeenCalledWith({
query: { sort: SORTING_ITEM_UPDATED.asc },
});
});
sharedAssertions({ search: '', sort: SORTING_ITEM_UPDATED.asc });
});
describe('when tab is changed', () => {
describe('when selected sort is supported', () => {
beforeEach(async () => {
await createComponent({
route: {
name: ACTIVE_TAB_SUBGROUPS_AND_PROJECTS,
params: { group: 'foo/bar/baz' },
query: { sort: SORTING_ITEM_NAME.asc },
},
});
});
it('adds sort query string', async () => {
await findTab(OverviewTabs.i18n[ACTIVE_TAB_ARCHIVED]).trigger('click');
expect(routerMock.push).toHaveBeenCalledWith(
expect.objectContaining({
query: { sort: SORTING_ITEM_NAME.asc },
}),
);
});
});
describe('when selected sort is not supported', () => {
beforeEach(async () => {
await createComponent({
route: {
name: ACTIVE_TAB_SUBGROUPS_AND_PROJECTS,
params: { group: 'foo/bar/baz' },
query: { sort: SORTING_ITEM_STARS.asc },
},
});
});
it('defaults to sorting by name', async () => {
await findTab(OverviewTabs.i18n[ACTIVE_TAB_ARCHIVED]).trigger('click');
expect(routerMock.push).toHaveBeenCalledWith(
expect.objectContaining({
query: { sort: SORTING_ITEM_NAME.asc },
}),
);
});
});
});
describe('when sort direction is changed', () => {
beforeEach(async () => {
await setup();
await wrapper
.findByRole('button', { name: 'Sorting Direction: Ascending' })
.trigger('click');
});
it('updates query string with `sort` key', () => {
expect(routerMock.push).toHaveBeenCalledWith({
query: { sort: SORTING_ITEM_NAME.desc },
});
});
sharedAssertions({ search: '', sort: SORTING_ITEM_NAME.desc });
});
describe('when `filter` and `sort` query strings are set', () => {
beforeEach(async () => {
await createComponent({
route: {
name: ACTIVE_TAB_SUBGROUPS_AND_PROJECTS,
params: { group: 'foo/bar/baz' },
query: { filter: 'Foo bar', sort: SORTING_ITEM_UPDATED.desc },
},
});
});
it('sets value of search input', () => {
expect(
wrapper.findByPlaceholderText(OverviewTabs.i18n.searchPlaceholder).element.value,
).toBe('Foo bar');
});
describe('when search is cleared', () => {
it('removes `filter` key from query string', async () => {
await findSearchInput().setValue('');
expect(routerMock.push).toHaveBeenCalledWith({
query: { sort: SORTING_ITEM_UPDATED.desc },
});
});
});
it('sets sort dropdown', () => {
expect(wrapper.findComponent(GlSorting).props()).toMatchObject({
text: SORTING_ITEM_UPDATED.label,
isAscending: false,
});
expect(wrapper.findAllComponents(GlSortingItem).at(2).vm.$attrs.active).toBe(true);
});
});
});
});
|