debian-mirror-gitlab/spec/frontend/groups/components/overview_tabs_spec.js

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

365 lines
12 KiB
JavaScript
Raw Normal View History

2022-11-25 23:54:43 +05:30
import { GlSorting, GlSortingItem, GlTab } from '@gitlab/ui';
2023-03-04 22:38:38 +05:30
import Vue, { nextTick } from 'vue';
2022-10-11 01:57:18 +05:30
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';
2022-11-25 23:54:43 +05:30
import GroupFolderComponent from '~/groups/components/group_folder.vue';
2023-03-04 22:38:38 +05:30
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';
2022-10-11 01:57:18 +05:30
import GroupsStore from '~/groups/store/groups_store';
import GroupsService from '~/groups/service/groups_service';
import { createRouter } from '~/groups/init_overview_tabs';
2022-11-25 23:54:43 +05:30
import eventHub from '~/groups/event_hub';
2022-10-11 01:57:18 +05:30
import {
ACTIVE_TAB_SUBGROUPS_AND_PROJECTS,
ACTIVE_TAB_SHARED,
ACTIVE_TAB_ARCHIVED,
2022-11-25 23:54:43 +05:30
OVERVIEW_TABS_SORTING_ITEMS,
2022-10-11 01:57:18 +05:30
} from '~/groups/constants';
import axios from '~/lib/utils/axios_utils';
2023-03-04 22:38:38 +05:30
import waitForPromises from 'helpers/wait_for_promises';
2022-10-11 01:57:18 +05:30
2023-03-04 22:38:38 +05:30
Vue.component('GroupFolder', GroupFolderComponent);
2022-10-11 01:57:18 +05:30
const router = createRouter();
2022-11-25 23:54:43 +05:30
const [SORTING_ITEM_NAME, , SORTING_ITEM_UPDATED] = OVERVIEW_TABS_SORTING_ITEMS;
2022-10-11 01:57:18 +05:30
describe('OverviewTabs', () => {
let wrapper;
2022-11-25 23:54:43 +05:30
let axiosMock;
2022-10-11 01:57:18 +05:30
2022-11-25 23:54:43 +05:30
const defaultProvide = {
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: '',
2023-06-20 00:43:36 +05:30
emptyProjectsIllustration: '',
2022-11-25 23:54:43 +05:30
emptySubgroupIllustration: '',
canCreateSubgroups: false,
canCreateProjects: false,
initialSort: 'name_asc',
2022-10-11 01:57:18 +05:30
};
const routerMock = {
push: jest.fn(),
};
const createComponent = async ({
route = { name: ACTIVE_TAB_SUBGROUPS_AND_PROJECTS, params: { group: 'foo/bar/baz' } },
2022-11-25 23:54:43 +05:30
provide = {},
2022-10-11 01:57:18 +05:30
} = {}) => {
wrapper = mountExtended(OverviewTabs, {
router,
provide: {
2022-11-25 23:54:43 +05:30
...defaultProvide,
...provide,
2022-10-11 01:57:18 +05:30
},
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 });
2023-01-13 00:05:48 +05:30
const findSearchInput = () => wrapper.findByPlaceholderText(OverviewTabs.i18n.searchPlaceholder);
2022-10-11 01:57:18 +05:30
2022-11-25 23:54:43 +05:30
beforeEach(() => {
axiosMock = new AxiosMockAdapter(axios);
2023-03-04 22:38:38 +05:30
axiosMock.onGet({ data: [] });
2022-10-11 01:57:18 +05:30
});
2022-11-25 23:54:43 +05:30
afterEach(() => {
axiosMock.restore();
2022-10-11 01:57:18 +05:30
});
2023-03-04 22:38:38 +05:30
it('renders `Subgroups and projects` tab with `GroupsApp` component with correct empty state', async () => {
2022-10-11 01:57:18 +05:30
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 }),
2022-11-25 23:54:43 +05:30
service: new GroupsService(defaultProvide.endpoints[ACTIVE_TAB_SUBGROUPS_AND_PROJECTS]),
2022-10-11 01:57:18 +05:30
hideProjects: false,
});
2023-03-04 22:38:38 +05:30
await waitForPromises();
expect(wrapper.findComponent(SubgroupsAndProjectsEmptyState).exists()).toBe(true);
2022-10-11 01:57:18 +05:30
});
2023-03-04 22:38:38 +05:30
it('renders `Shared projects` tab and renders `GroupsApp` component with correct empty state after clicking tab', async () => {
2022-10-11 01:57:18 +05:30
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(),
2022-11-25 23:54:43 +05:30
service: new GroupsService(defaultProvide.endpoints[ACTIVE_TAB_SHARED]),
2022-10-11 01:57:18 +05:30
hideProjects: false,
});
expect(tabPanel.vm.$attrs.lazy).toBe(false);
2023-03-04 22:38:38 +05:30
await waitForPromises();
expect(wrapper.findComponent(SharedProjectsEmptyState).exists()).toBe(true);
2022-10-11 01:57:18 +05:30
});
2023-03-04 22:38:38 +05:30
it('renders `Archived projects` tab and renders `GroupsApp` component with correct empty state after clicking tab', async () => {
2022-10-11 01:57:18 +05:30
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(),
2022-11-25 23:54:43 +05:30
service: new GroupsService(defaultProvide.endpoints[ACTIVE_TAB_ARCHIVED]),
2022-10-11 01:57:18 +05:30
hideProjects: false,
});
expect(tabPanel.vm.$attrs.lazy).toBe(false);
2023-03-04 22:38:38 +05:30
await waitForPromises();
expect(wrapper.findComponent(ArchivedProjectsEmptyState).exists()).toBe(true);
2022-10-11 01:57:18 +05:30
});
2022-11-25 23:54:43 +05:30
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);
});
2022-10-11 01:57:18 +05:30
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(expectedRoute);
});
});
2022-11-25 23:54:43 +05:30
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', () => {
2023-01-13 00:05:48 +05:30
describe('when search is greater than or equal to 3 characters', () => {
const search = 'Foo bar';
2022-11-25 23:54:43 +05:30
2023-01-13 00:05:48 +05:30
beforeEach(async () => {
await setup();
await findSearchInput().setValue(search);
});
2022-11-25 23:54:43 +05:30
2023-01-13 00:05:48 +05:30
it('updates query string with `filter` key', () => {
expect(routerMock.push).toHaveBeenCalledWith({ query: { filter: search } });
});
sharedAssertions({ search, sort: defaultProvide.initialSort });
2022-11-25 23:54:43 +05:30
});
2023-01-13 00:05:48 +05:30
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,
},
);
});
});
2022-11-25 23:54:43 +05:30
});
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 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');
});
2023-01-13 00:05:48 +05:30
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 },
});
});
});
2022-11-25 23:54:43 +05:30
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);
});
});
});
2022-10-11 01:57:18 +05:30
});