debian-mirror-gitlab/spec/frontend/sidebar/components/sidebar_dropdown_widget_spec.js

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

341 lines
11 KiB
JavaScript
Raw Normal View History

2023-01-13 00:05:48 +05:30
import { GlDropdown, GlLink, GlLoadingIcon, GlSearchBoxByType } from '@gitlab/ui';
2021-09-04 01:27:46 +05:30
import * as Sentry from '@sentry/browser';
2022-04-04 11:22:00 +05:30
import { shallowMount, mount } from '@vue/test-utils';
import Vue, { nextTick } from 'vue';
2021-09-04 01:27:46 +05:30
import VueApollo from 'vue-apollo';
import createMockApollo from 'helpers/mock_apollo_helper';
2021-10-27 15:23:28 +05:30
import { createMockDirective, getBinding } from 'helpers/vue_mock_directive';
2021-09-04 01:27:46 +05:30
import { extendedWrapper } from 'helpers/vue_test_utils_helper';
import waitForPromises from 'helpers/wait_for_promises';
2023-05-27 22:25:52 +05:30
import { createAlert } from '~/alert';
2021-09-04 01:27:46 +05:30
import { getIdFromGraphQLId } from '~/graphql_shared/utils';
2023-04-23 21:23:45 +05:30
import { TYPE_ISSUE } from '~/issues/constants';
2021-10-27 15:23:28 +05:30
import { timeFor } from '~/lib/utils/datetime_utility';
2023-01-13 00:05:48 +05:30
import SidebarDropdown from '~/sidebar/components/sidebar_dropdown.vue';
2021-09-04 01:27:46 +05:30
import SidebarDropdownWidget from '~/sidebar/components/sidebar_dropdown_widget.vue';
import SidebarEditableItem from '~/sidebar/components/sidebar_editable_item.vue';
import { IssuableAttributeType } from '~/sidebar/constants';
import projectIssueMilestoneMutation from '~/sidebar/queries/project_issue_milestone.mutation.graphql';
import projectIssueMilestoneQuery from '~/sidebar/queries/project_issue_milestone.query.graphql';
import projectMilestonesQuery from '~/sidebar/queries/project_milestones.query.graphql';
import {
mockIssue,
mockProjectMilestonesResponse,
noCurrentMilestoneResponse,
mockMilestoneMutationResponse,
mockMilestone2,
} from '../mock_data';
2023-05-27 22:25:52 +05:30
jest.mock('~/alert');
2021-09-04 01:27:46 +05:30
describe('SidebarDropdownWidget', () => {
let wrapper;
let mockApollo;
const promiseData = { issuableSetAttribute: { issue: { attribute: { id: '123' } } } };
const firstErrorMsg = 'first error';
const promiseWithErrors = {
...promiseData,
issuableSetAttribute: { ...promiseData.issuableSetAttribute, errors: [firstErrorMsg] },
};
const mutationSuccess = () => jest.fn().mockResolvedValue({ data: promiseData });
const mutationError = () =>
jest.fn().mockRejectedValue('Failed to set milestone on this issue. Please try again.');
const mutationSuccessWithErrors = () => jest.fn().mockResolvedValue({ data: promiseWithErrors });
const findGlLink = () => wrapper.findComponent(GlLink);
2021-10-27 15:23:28 +05:30
const findDateTooltip = () => getBinding(findGlLink().element, 'gl-tooltip');
2023-01-13 00:05:48 +05:30
const findSidebarDropdown = () => wrapper.findComponent(SidebarDropdown);
2021-09-04 01:27:46 +05:30
const findSidebarEditableItem = () => wrapper.findComponent(SidebarEditableItem);
const findEditButton = () => findSidebarEditableItem().find('[data-testid="edit-button"]');
const findEditableLoadingIcon = () => findSidebarEditableItem().findComponent(GlLoadingIcon);
const findSelectedAttribute = () => wrapper.findByTestId('select-milestone');
const waitForDropdown = async () => {
// BDropdown first changes its `visible` property
// in a requestAnimationFrame callback.
// It then emits `shown` event in a watcher for `visible`
// Hence we need both of these:
await waitForPromises();
2022-04-04 11:22:00 +05:30
await nextTick();
2021-09-04 01:27:46 +05:30
};
const waitForApollo = async () => {
jest.runOnlyPendingTimers();
await waitForPromises();
};
// Used with createComponentWithApollo which uses 'mount'
const clickEdit = async () => {
await findEditButton().trigger('click');
await waitForDropdown();
// We should wait for attributes list to be fetched.
await waitForApollo();
};
// Used with createComponent which shallow mounts components
const toggleDropdown = async () => {
wrapper.vm.$refs.editable.expand();
await waitForDropdown();
};
const createComponentWithApollo = async ({
requestHandlers = [],
projectMilestonesSpy = jest.fn().mockResolvedValue(mockProjectMilestonesResponse),
currentMilestoneSpy = jest.fn().mockResolvedValue(noCurrentMilestoneResponse),
} = {}) => {
2022-04-04 11:22:00 +05:30
Vue.use(VueApollo);
2022-08-13 15:12:31 +05:30
2021-09-04 01:27:46 +05:30
mockApollo = createMockApollo([
[projectMilestonesQuery, projectMilestonesSpy],
[projectIssueMilestoneQuery, currentMilestoneSpy],
...requestHandlers,
]);
wrapper = extendedWrapper(
mount(SidebarDropdownWidget, {
provide: { canUpdate: true },
apolloProvider: mockApollo,
propsData: {
workspacePath: mockIssue.projectPath,
attrWorkspacePath: mockIssue.projectPath,
iid: mockIssue.iid,
2023-04-23 21:23:45 +05:30
issuableType: TYPE_ISSUE,
2021-09-04 01:27:46 +05:30
issuableAttribute: IssuableAttributeType.Milestone,
},
attachTo: document.body,
}),
);
await waitForApollo();
};
const createComponent = ({ data = {}, mutationPromise = mutationSuccess, queries = {} } = {}) => {
wrapper = extendedWrapper(
shallowMount(SidebarDropdownWidget, {
provide: { canUpdate: true },
data() {
return data;
},
propsData: {
workspacePath: '',
attrWorkspacePath: '',
iid: '',
2023-04-23 21:23:45 +05:30
issuableType: TYPE_ISSUE,
2021-09-04 01:27:46 +05:30
issuableAttribute: IssuableAttributeType.Milestone,
},
mocks: {
$apollo: {
mutate: mutationPromise(),
queries: {
currentAttribute: { loading: false },
attributesList: { loading: false },
...queries,
},
},
},
2021-10-27 15:23:28 +05:30
directives: {
2023-05-27 22:25:52 +05:30
GlTooltip: createMockDirective('gl-tooltip'),
2021-10-27 15:23:28 +05:30
},
2021-09-04 01:27:46 +05:30
stubs: {
SidebarEditableItem,
GlSearchBoxByType,
GlDropdown,
},
}),
);
2023-01-13 00:05:48 +05:30
wrapper.vm.$refs.dropdown.show = jest.fn();
2021-09-04 01:27:46 +05:30
// We need to mock out `showDropdown` which
// invokes `show` method of BDropdown used inside GlDropdown.
jest.spyOn(wrapper.vm, 'showDropdown').mockImplementation();
};
describe('when not editing', () => {
beforeEach(() => {
createComponent({
data: {
2021-10-27 15:23:28 +05:30
currentAttribute: { id: 'id', title: 'title', webUrl: 'webUrl', dueDate: '2021-09-09' },
2021-09-04 01:27:46 +05:30
},
stubs: {
GlDropdown,
SidebarEditableItem,
},
});
});
it('shows the current attribute', () => {
expect(findSelectedAttribute().text()).toBe('title');
});
it('links to the current attribute', () => {
expect(findGlLink().attributes().href).toBe('webUrl');
});
it('does not show a loading spinner next to the heading', () => {
expect(findEditableLoadingIcon().exists()).toBe(false);
});
it('shows a loading spinner while fetching the current attribute', () => {
createComponent({
queries: {
currentAttribute: { loading: true },
},
});
expect(findEditableLoadingIcon().exists()).toBe(true);
});
it('shows the loading spinner and the title of the selected attribute while updating', () => {
createComponent({
data: {
updating: true,
selectedTitle: 'Some milestone title',
},
queries: {
currentAttribute: { loading: false },
},
});
expect(findEditableLoadingIcon().exists()).toBe(true);
expect(findSelectedAttribute().text()).toBe('Some milestone title');
});
2021-10-27 15:23:28 +05:30
it('displays time for milestone due date in tooltip', () => {
expect(findDateTooltip().value).toBe(timeFor('2021-09-09'));
});
2021-09-04 01:27:46 +05:30
describe('when current attribute does not exist', () => {
it('renders "None" as the selected attribute title', () => {
createComponent();
expect(findSelectedAttribute().text()).toBe('None');
});
});
2022-10-11 01:57:18 +05:30
describe("when user doesn't have permission to view current attribute", () => {
it('renders no permission text', () => {
createComponent({
data: {
hasCurrentAttribute: true,
currentAttribute: null,
},
queries: {
currentAttribute: { loading: false },
},
});
expect(findSelectedAttribute().text()).toBe(
`You don't have permission to view this ${wrapper.props('issuableAttribute')}.`,
);
});
});
2021-09-04 01:27:46 +05:30
});
describe('when a user can edit', () => {
describe('when user is editing', () => {
describe('when rendering the dropdown', () => {
describe('when clicking on dropdown item', () => {
describe('when currentAttribute is not equal to attribute id', () => {
describe('when error', () => {
const bootstrapComponent = (mutationResp) => {
createComponent({
data: {
attributesList: [
{ id: '123', title: '123' },
{ id: 'id', title: 'title' },
],
2023-01-13 00:05:48 +05:30
currentAttribute: { id: '123' },
2021-09-04 01:27:46 +05:30
},
mutationPromise: mutationResp,
});
};
describe.each`
description | mutationResp | expectedMsg
${'top-level error'} | ${mutationError} | ${'Failed to set milestone on this issue. Please try again.'}
${'user-recoverable error'} | ${mutationSuccessWithErrors} | ${firstErrorMsg}
`(`$description`, ({ mutationResp, expectedMsg }) => {
beforeEach(async () => {
bootstrapComponent(mutationResp);
await toggleDropdown();
2023-01-13 00:05:48 +05:30
findSidebarDropdown().vm.$emit('change', { id: 'error' });
2021-09-04 01:27:46 +05:30
});
2022-11-25 23:54:43 +05:30
it(`calls createAlert with "${expectedMsg}"`, async () => {
2022-04-04 11:22:00 +05:30
await nextTick();
2022-11-25 23:54:43 +05:30
expect(createAlert).toHaveBeenCalledWith({
2021-09-04 01:27:46 +05:30
message: expectedMsg,
captureError: true,
error: expectedMsg,
});
});
});
});
});
});
});
});
});
describe('with mock apollo', () => {
let error;
beforeEach(() => {
jest.spyOn(Sentry, 'captureException');
error = new Error('mayday');
});
describe("when issuable type is 'issue'", () => {
describe('when dropdown is expanded and user can edit', () => {
let milestoneMutationSpy;
beforeEach(async () => {
milestoneMutationSpy = jest.fn().mockResolvedValue(mockMilestoneMutationResponse);
await createComponentWithApollo({
requestHandlers: [[projectIssueMilestoneMutation, milestoneMutationSpy]],
});
await clickEdit();
});
describe('when currentAttribute is not equal to attribute id', () => {
describe('when update is successful', () => {
2022-08-13 15:12:31 +05:30
it('calls setIssueAttribute mutation', () => {
2023-01-13 00:05:48 +05:30
findSidebarDropdown().vm.$emit('change', { id: mockMilestone2.id });
2021-09-04 01:27:46 +05:30
expect(milestoneMutationSpy).toHaveBeenCalledWith({
iid: mockIssue.iid,
attributeId: getIdFromGraphQLId(mockMilestone2.id),
fullPath: mockIssue.projectPath,
});
});
});
});
});
describe('currentAttributes', () => {
2022-11-25 23:54:43 +05:30
it('should call createAlert if currentAttributes query fails', async () => {
2021-09-04 01:27:46 +05:30
await createComponentWithApollo({
currentMilestoneSpy: jest.fn().mockRejectedValue(error),
});
2022-11-25 23:54:43 +05:30
expect(createAlert).toHaveBeenCalledWith({
2021-09-04 01:27:46 +05:30
message: wrapper.vm.i18n.currentFetchError,
captureError: true,
error: expect.any(Error),
});
});
});
});
});
});