debian-mirror-gitlab/spec/frontend/super_sidebar/components/context_switcher_spec.js

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

310 lines
10 KiB
JavaScript
Raw Normal View History

2023-05-27 22:25:52 +05:30
import Vue, { nextTick } from 'vue';
import VueApollo from 'vue-apollo';
2023-07-09 08:55:56 +05:30
import { GlDisclosureDropdown, GlSearchBoxByType, GlLoadingIcon, GlAlert } from '@gitlab/ui';
2023-05-27 22:25:52 +05:30
import * as Sentry from '@sentry/browser';
import { s__ } from '~/locale';
import { shallowMountExtended } from 'helpers/vue_test_utils_helper';
import ContextSwitcher from '~/super_sidebar/components/context_switcher.vue';
2023-07-09 08:55:56 +05:30
import ContextSwitcherToggle from '~/super_sidebar/components/context_switcher_toggle.vue';
2023-06-20 00:43:36 +05:30
import NavItem from '~/super_sidebar/components/nav_item.vue';
2023-05-27 22:25:52 +05:30
import ProjectsList from '~/super_sidebar/components/projects_list.vue';
import GroupsList from '~/super_sidebar/components/groups_list.vue';
import createMockApollo from 'helpers/mock_apollo_helper';
import searchUserProjectsAndGroupsQuery from '~/super_sidebar/graphql/queries/search_user_groups_and_projects.query.graphql';
import { trackContextAccess, formatContextSwitcherItems } from '~/super_sidebar/utils';
import { DEFAULT_DEBOUNCE_AND_THROTTLE_MS } from '~/lib/utils/constants';
import waitForPromises from 'helpers/wait_for_promises';
import { stubComponent } from 'helpers/stub_component';
import { searchUserProjectsAndGroupsResponseMock } from '../mock_data';
jest.mock('~/super_sidebar/utils', () => ({
getStorageKeyFor: jest.requireActual('~/super_sidebar/utils').getStorageKeyFor,
getTopFrequentItems: jest.requireActual('~/super_sidebar/utils').getTopFrequentItems,
formatContextSwitcherItems: jest.requireActual('~/super_sidebar/utils')
.formatContextSwitcherItems,
trackContextAccess: jest.fn(),
}));
2023-06-20 00:43:36 +05:30
const focusInputMock = jest.fn();
2023-05-27 22:25:52 +05:30
2023-06-20 00:43:36 +05:30
const persistentLinks = [
{ title: 'Explore', link: '/explore', icon: 'compass', link_classes: 'persistent-link-class' },
];
2023-05-27 22:25:52 +05:30
const username = 'root';
const projectsPath = 'projectsPath';
const groupsPath = 'groupsPath';
2023-07-09 08:55:56 +05:30
const contextHeader = { avatar_shape: 'circle' };
2023-05-27 22:25:52 +05:30
Vue.use(VueApollo);
describe('ContextSwitcher component', () => {
let wrapper;
let mockApollo;
2023-07-09 08:55:56 +05:30
const findDisclosureDropdown = () => wrapper.findComponent(GlDisclosureDropdown);
const findContextSwitcherToggle = () => wrapper.findComponent(ContextSwitcherToggle);
2023-06-20 00:43:36 +05:30
const findNavItems = () => wrapper.findAllComponents(NavItem);
2023-05-27 22:25:52 +05:30
const findSearchBox = () => wrapper.findComponent(GlSearchBoxByType);
const findProjectsList = () => wrapper.findComponent(ProjectsList);
const findGroupsList = () => wrapper.findComponent(GroupsList);
2023-06-20 00:43:36 +05:30
const findLoadingIcon = () => wrapper.findComponent(GlLoadingIcon);
const findAlert = () => wrapper.findComponent(GlAlert);
2023-05-27 22:25:52 +05:30
const triggerSearchQuery = async () => {
findSearchBox().vm.$emit('input', 'foo');
await nextTick();
jest.advanceTimersByTime(DEFAULT_DEBOUNCE_AND_THROTTLE_MS);
return waitForPromises();
};
const searchUserProjectsAndGroupsHandlerSuccess = jest
.fn()
.mockResolvedValue(searchUserProjectsAndGroupsResponseMock);
const createWrapper = ({ props = {}, requestHandlers = {} } = {}) => {
mockApollo = createMockApollo([
[
searchUserProjectsAndGroupsQuery,
requestHandlers.searchUserProjectsAndGroupsQueryHandler ??
searchUserProjectsAndGroupsHandlerSuccess,
],
]);
wrapper = shallowMountExtended(ContextSwitcher, {
apolloProvider: mockApollo,
propsData: {
2023-06-20 00:43:36 +05:30
persistentLinks,
2023-05-27 22:25:52 +05:30
username,
projectsPath,
groupsPath,
2023-07-09 08:55:56 +05:30
contextHeader,
2023-05-27 22:25:52 +05:30
...props,
},
stubs: {
2023-07-09 08:55:56 +05:30
GlDisclosureDropdown: stubComponent(GlDisclosureDropdown, {
template: `
<div>
<slot name="toggle" />
<slot />
</div>
`,
}),
2023-05-27 22:25:52 +05:30
GlSearchBoxByType: stubComponent(GlSearchBoxByType, {
props: ['placeholder'],
2023-06-20 00:43:36 +05:30
methods: { focusInput: focusInputMock },
2023-05-27 22:25:52 +05:30
}),
ProjectsList: stubComponent(ProjectsList, {
props: ['username', 'viewAllLink', 'isSearch', 'searchResults'],
}),
GroupsList: stubComponent(GroupsList, {
props: ['username', 'viewAllLink', 'isSearch', 'searchResults'],
}),
},
});
};
describe('default', () => {
beforeEach(() => {
createWrapper();
});
2023-06-20 00:43:36 +05:30
it('renders the persistent links', () => {
const navItems = findNavItems();
const firstNavItem = navItems.at(0);
expect(navItems.length).toBe(persistentLinks.length);
expect(firstNavItem.props('item')).toBe(persistentLinks[0]);
expect(firstNavItem.props('linkClasses')).toEqual({
[persistentLinks[0].link_classes]: persistentLinks[0].link_classes,
});
});
2023-05-27 22:25:52 +05:30
it('passes the placeholder to the search box', () => {
expect(findSearchBox().props('placeholder')).toBe(
2023-06-20 00:43:36 +05:30
s__('Navigation|Search your projects or groups'),
2023-05-27 22:25:52 +05:30
);
});
2023-07-09 08:55:56 +05:30
it('passes the correct props to the frequent projects list', () => {
2023-05-27 22:25:52 +05:30
expect(findProjectsList().props()).toEqual({
username,
viewAllLink: projectsPath,
isSearch: false,
searchResults: [],
});
});
2023-07-09 08:55:56 +05:30
it('passes the correct props to the frequent groups list', () => {
2023-05-27 22:25:52 +05:30
expect(findGroupsList().props()).toEqual({
username,
viewAllLink: groupsPath,
isSearch: false,
searchResults: [],
});
});
it('does not trigger the search query on mount', () => {
expect(searchUserProjectsAndGroupsHandlerSuccess).not.toHaveBeenCalled();
});
2023-06-20 00:43:36 +05:30
it('shows a loading spinner when search query is typed in', async () => {
findSearchBox().vm.$emit('input', 'foo');
await nextTick();
expect(findLoadingIcon().exists()).toBe(true);
});
2023-07-09 08:55:56 +05:30
it('passes the correct props to the toggle', () => {
expect(findContextSwitcherToggle().props('context')).toEqual(contextHeader);
expect(findContextSwitcherToggle().props('expanded')).toEqual(false);
});
it("passes Popper.js' options to the disclosure dropdown", () => {
expect(findDisclosureDropdown().props('popperOptions')).toMatchObject({
modifiers: expect.any(Array),
});
});
it('does not emit the `toggle` event initially', () => {
expect(wrapper.emitted('toggle')).toBe(undefined);
});
});
describe('visibility changes', () => {
beforeEach(() => {
createWrapper();
findDisclosureDropdown().vm.$emit('shown');
});
it('emits the `toggle` event, focuses the search input and puts the toggle in the expanded state when opened', () => {
expect(wrapper.emitted('toggle')).toHaveLength(1);
expect(wrapper.emitted('toggle')[0]).toEqual([true]);
expect(focusInputMock).toHaveBeenCalledTimes(1);
expect(findContextSwitcherToggle().props('expanded')).toBe(true);
});
it("emits the `toggle` event, does not attempt to focus the input, and resets the toggle's `expanded` props to `false` when closed", async () => {
findDisclosureDropdown().vm.$emit('hidden');
await nextTick();
expect(wrapper.emitted('toggle')).toHaveLength(2);
expect(wrapper.emitted('toggle')[1]).toEqual([false]);
expect(focusInputMock).toHaveBeenCalledTimes(1);
expect(findContextSwitcherToggle().props('expanded')).toBe(false);
});
2023-05-27 22:25:52 +05:30
});
describe('item access tracking', () => {
it('does not track anything if not within a trackable context', () => {
createWrapper();
expect(trackContextAccess).not.toHaveBeenCalled();
});
it('tracks item access if within a trackable context', () => {
const currentContext = { namespace: 'groups' };
createWrapper({
props: {
currentContext,
},
});
expect(trackContextAccess).toHaveBeenCalledWith(username, currentContext);
});
});
describe('on search', () => {
beforeEach(() => {
createWrapper();
return triggerSearchQuery();
});
2023-06-20 00:43:36 +05:30
it('hides persistent links', () => {
expect(findNavItems().length).toBe(0);
});
2023-05-27 22:25:52 +05:30
it('triggers the search query on search', () => {
expect(searchUserProjectsAndGroupsHandlerSuccess).toHaveBeenCalled();
});
2023-06-20 00:43:36 +05:30
it('hides the loading spinner', () => {
expect(findLoadingIcon().exists()).toBe(false);
});
2023-05-27 22:25:52 +05:30
it('passes the projects to the frequent projects list', () => {
expect(findProjectsList().props('isSearch')).toBe(true);
expect(findProjectsList().props('searchResults')).toEqual(
formatContextSwitcherItems(searchUserProjectsAndGroupsResponseMock.data.projects.nodes),
);
});
it('passes the groups to the frequent groups list', () => {
expect(findGroupsList().props('isSearch')).toBe(true);
expect(findGroupsList().props('searchResults')).toEqual(
formatContextSwitcherItems(searchUserProjectsAndGroupsResponseMock.data.user.groups.nodes),
);
});
});
describe('when search query does not match any items', () => {
beforeEach(() => {
createWrapper({
requestHandlers: {
searchUserProjectsAndGroupsQueryHandler: jest.fn().mockResolvedValue({
data: {
projects: {
nodes: [],
},
user: {
id: '1',
groups: {
nodes: [],
},
},
},
}),
},
});
return triggerSearchQuery();
});
it('passes empty results to the lists', () => {
expect(findProjectsList().props('isSearch')).toBe(true);
expect(findProjectsList().props('searchResults')).toEqual([]);
expect(findGroupsList().props('isSearch')).toBe(true);
expect(findGroupsList().props('searchResults')).toEqual([]);
});
});
describe('when search query fails', () => {
beforeEach(() => {
jest.spyOn(Sentry, 'captureException');
});
2023-06-20 00:43:36 +05:30
it('captures exception and shows an alert if response is formatted incorrectly', async () => {
2023-05-27 22:25:52 +05:30
createWrapper({
requestHandlers: {
searchUserProjectsAndGroupsQueryHandler: jest.fn().mockResolvedValue({
data: {},
}),
},
});
await triggerSearchQuery();
expect(Sentry.captureException).toHaveBeenCalled();
2023-06-20 00:43:36 +05:30
expect(findAlert().exists()).toBe(true);
2023-05-27 22:25:52 +05:30
});
2023-06-20 00:43:36 +05:30
it('captures exception and shows an alert if query fails', async () => {
2023-05-27 22:25:52 +05:30
createWrapper({
requestHandlers: {
searchUserProjectsAndGroupsQueryHandler: jest.fn().mockRejectedValue(),
},
});
await triggerSearchQuery();
expect(Sentry.captureException).toHaveBeenCalled();
2023-06-20 00:43:36 +05:30
expect(findAlert().exists()).toBe(true);
2023-05-27 22:25:52 +05:30
});
});
});