debian-mirror-gitlab/spec/frontend/notes/components/note_actions_spec.js

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

425 lines
13 KiB
JavaScript
Raw Normal View History

2023-07-09 08:55:56 +05:30
import { GlDisclosureDropdown, GlDisclosureDropdownItem } from '@gitlab/ui';
import { mount } from '@vue/test-utils';
2020-10-24 23:57:45 +05:30
import AxiosMockAdapter from 'axios-mock-adapter';
2022-04-04 11:22:00 +05:30
import { nextTick } from 'vue';
2023-07-09 08:55:56 +05:30
import { stubComponent } from 'helpers/stub_component';
2021-03-11 19:13:27 +05:30
import { TEST_HOST } from 'spec/test_constants';
import axios from '~/lib/utils/axios_utils';
2018-03-17 18:26:18 +05:30
import noteActions from '~/notes/components/note_actions.vue';
2023-01-13 00:05:48 +05:30
import { NOTEABLE_TYPE_MAPPING } from '~/notes/constants';
import TimelineEventButton from '~/notes/components/note_actions/timeline_event_button.vue';
2023-04-23 21:23:45 +05:30
import AbuseCategorySelector from '~/abuse_reports/components/abuse_category_selector.vue';
2021-03-11 19:13:27 +05:30
import createStore from '~/notes/stores';
2021-04-17 20:07:23 +05:30
import UserAccessRoleBadge from '~/vue_shared/components/user_access_role_badge.vue';
2018-03-17 18:26:18 +05:30
import { userDataMock } from '../mock_data';
2019-03-02 22:35:43 +05:30
describe('noteActions', () => {
let wrapper;
2018-11-08 19:23:39 +05:30
let store;
2019-03-02 22:35:43 +05:30
let props;
2020-06-23 00:09:42 +05:30
let actions;
let axiosMock;
2019-03-02 22:35:43 +05:30
2023-07-09 08:55:56 +05:30
const mockCloseDropdown = jest.fn();
2022-08-27 11:52:29 +05:30
const findUserAccessRoleBadge = (idx) => wrapper.findAllComponents(UserAccessRoleBadge).at(idx);
2021-04-17 20:07:23 +05:30
const findUserAccessRoleBadgeText = (idx) => findUserAccessRoleBadge(idx).text().trim();
2023-01-13 00:05:48 +05:30
const findTimelineButton = () => wrapper.findComponent(TimelineEventButton);
2023-04-23 21:23:45 +05:30
const findReportAbuseButton = () => wrapper.find(`[data-testid="report-abuse-button"]`);
2023-01-13 00:05:48 +05:30
const setupStoreForIncidentTimelineEvents = ({
userCanAdd,
noteableType,
isPromotionInProgress = true,
}) => {
store.dispatch('setUserData', {
...userDataMock,
can_add_timeline_events: userCanAdd,
});
store.state.noteableData = {
...store.state.noteableData,
type: noteableType,
};
store.state.isPromoteCommentToTimelineEventInProgress = isPromotionInProgress;
};
2021-04-17 20:07:23 +05:30
2021-01-29 00:20:46 +05:30
const mountNoteActions = (propsData, computed) => {
2022-04-04 11:22:00 +05:30
return mount(noteActions, {
2019-03-02 22:35:43 +05:30
store,
propsData,
2020-06-23 00:09:42 +05:30
computed,
2023-07-09 08:55:56 +05:30
stubs: {
GlDisclosureDropdown: stubComponent(GlDisclosureDropdown, {
methods: {
close: mockCloseDropdown,
},
}),
GlDisclosureDropdownItem,
},
2019-03-02 22:35:43 +05:30
});
};
2018-03-17 18:26:18 +05:30
beforeEach(() => {
2018-11-08 19:23:39 +05:30
store = createStore();
2020-06-23 00:09:42 +05:30
2019-03-02 22:35:43 +05:30
props = {
accessLevel: 'Maintainer',
2020-06-23 00:09:42 +05:30
authorId: 1,
author: userDataMock,
2019-03-02 22:35:43 +05:30
canDelete: true,
canEdit: true,
canAwardEmoji: true,
canReportAsAbuse: true,
2020-11-24 15:15:51 +05:30
isAuthor: true,
isContributor: false,
noteableType: 'MergeRequest',
2019-03-02 22:35:43 +05:30
noteId: '539',
2020-03-13 15:44:24 +05:30
noteUrl: `${TEST_HOST}/group/project/-/merge_requests/1#note_1`,
2020-11-24 15:15:51 +05:30
projectName: 'project',
2019-03-02 22:35:43 +05:30
showReply: false,
2021-04-17 20:07:23 +05:30
awardPath: `${TEST_HOST}/award_emoji`,
2019-03-02 22:35:43 +05:30
};
2020-06-23 00:09:42 +05:30
actions = {
updateAssignees: jest.fn(),
};
axiosMock = new AxiosMockAdapter(axios);
2018-03-17 18:26:18 +05:30
});
afterEach(() => {
2020-06-23 00:09:42 +05:30
axiosMock.restore();
2018-03-17 18:26:18 +05:30
});
describe('user is logged in', () => {
beforeEach(() => {
store.dispatch('setUserData', userDataMock);
2021-01-29 00:20:46 +05:30
wrapper = mountNoteActions(props);
2018-03-17 18:26:18 +05:30
});
2020-11-24 15:15:51 +05:30
it('should render noteable author badge', () => {
2021-04-17 20:07:23 +05:30
expect(findUserAccessRoleBadgeText(0)).toBe('Author');
2020-11-24 15:15:51 +05:30
});
2018-03-17 18:26:18 +05:30
it('should render access level badge', () => {
2021-04-17 20:07:23 +05:30
expect(findUserAccessRoleBadgeText(1)).toBe(props.accessLevel);
2018-03-17 18:26:18 +05:30
});
2022-04-04 11:22:00 +05:30
it('should render contributor badge', async () => {
2020-11-24 15:15:51 +05:30
wrapper.setProps({
accessLevel: null,
isContributor: true,
});
2022-04-04 11:22:00 +05:30
await nextTick();
expect(findUserAccessRoleBadgeText(1)).toBe('Contributor');
2020-11-24 15:15:51 +05:30
});
2018-03-17 18:26:18 +05:30
it('should render emoji link', () => {
2022-06-21 17:19:12 +05:30
expect(wrapper.find('[data-testid="note-emoji-button"]').exists()).toBe(true);
2018-03-17 18:26:18 +05:30
});
describe('actions dropdown', () => {
it('should be possible to edit the comment', () => {
2019-03-02 22:35:43 +05:30
expect(wrapper.find('.js-note-edit').exists()).toBe(true);
2018-03-17 18:26:18 +05:30
});
2019-09-04 21:01:54 +05:30
it('should be possible to report abuse to admin', () => {
2023-04-23 21:23:45 +05:30
expect(findReportAbuseButton().exists()).toBe(true);
2018-03-17 18:26:18 +05:30
});
2018-11-20 20:47:30 +05:30
it('should be possible to copy link to a note', () => {
2019-03-02 22:35:43 +05:30
expect(wrapper.find('.js-btn-copy-note-link').exists()).toBe(true);
2018-11-20 20:47:30 +05:30
});
2022-04-04 11:22:00 +05:30
it('should not show copy link action when `noteUrl` prop is empty', async () => {
2019-03-02 22:35:43 +05:30
wrapper.setProps({
...props,
2020-06-23 00:09:42 +05:30
author: {
avatar_url: 'mock_path',
id: 26,
name: 'Example Maintainer',
path: '/ExampleMaintainer',
state: 'active',
username: 'ExampleMaintainer',
},
2019-03-02 22:35:43 +05:30
noteUrl: '',
});
2022-04-04 11:22:00 +05:30
await nextTick();
expect(wrapper.find('.js-btn-copy-note-link').exists()).toBe(false);
2018-11-20 20:47:30 +05:30
});
2018-03-17 18:26:18 +05:30
it('should be possible to delete comment', () => {
2019-03-02 22:35:43 +05:30
expect(wrapper.find('.js-note-delete').exists()).toBe(true);
2018-03-17 18:26:18 +05:30
});
2019-07-07 11:18:12 +05:30
2020-07-28 23:09:34 +05:30
it('should not be possible to assign or unassign the comment author in a merge request', () => {
2020-06-23 00:09:42 +05:30
const assignUserButton = wrapper.find('[data-testid="assign-user"]');
2020-07-28 23:09:34 +05:30
expect(assignUserButton.exists()).toBe(false);
});
2021-04-29 21:17:54 +05:30
it('should render the correct (unescaped) name in the Resolved By tooltip', () => {
const complexUnescapedName = 'This is a Ǝ\'𝞓\'E "cat"?';
wrapper = mountNoteActions({
...props,
canResolve: true,
isResolving: false,
isResolved: true,
resolvedBy: {
name: complexUnescapedName,
},
});
const { resolveButton } = wrapper.vm.$refs;
expect(resolveButton.$el.getAttribute('title')).toBe(`Resolved by ${complexUnescapedName}`);
});
2023-07-09 08:55:56 +05:30
it('closes the dropdown', () => {
findReportAbuseButton().vm.$emit('action');
expect(mockCloseDropdown).toHaveBeenCalled();
});
2020-07-28 23:09:34 +05:30
});
});
2020-06-23 00:09:42 +05:30
2022-08-27 11:52:29 +05:30
describe('when a user can set metadata of an issue', () => {
2020-07-28 23:09:34 +05:30
const testButtonClickTriggersAction = () => {
axiosMock.onPut(`${TEST_HOST}/api/v4/projects/group/project/issues/1`).reply(() => {
expect(actions.updateAssignees).toHaveBeenCalled();
2020-06-23 00:09:42 +05:30
});
2020-07-28 23:09:34 +05:30
const assignUserButton = wrapper.find('[data-testid="assign-user"]');
expect(assignUserButton.exists()).toBe(true);
assignUserButton.trigger('click');
};
beforeEach(() => {
2021-01-29 00:20:46 +05:30
wrapper = mountNoteActions(props, {
2020-07-28 23:09:34 +05:30
targetType: () => 'issue',
2020-06-23 00:09:42 +05:30
});
2020-07-28 23:09:34 +05:30
store.state.noteableData = {
current_user: {
2022-08-27 11:52:29 +05:30
can_set_issue_metadata: true,
2020-07-28 23:09:34 +05:30
},
};
store.state.userData = userDataMock;
2018-03-17 18:26:18 +05:30
});
2020-07-28 23:09:34 +05:30
afterEach(() => {
axiosMock.restore();
});
it('should be possible to assign the comment author', testButtonClickTriggersAction);
it('should be possible to unassign the comment author', testButtonClickTriggersAction);
});
2022-08-27 11:52:29 +05:30
describe('when a user can update but not set metadata of an issue', () => {
beforeEach(() => {
wrapper = mountNoteActions(props, {
targetType: () => 'issue',
});
store.state.noteableData = {
current_user: {
can_update: true,
can_set_issue_metadata: false,
},
};
store.state.userData = userDataMock;
});
afterEach(() => {
axiosMock.restore();
});
it('should not be possible to assign or unassign the comment author', () => {
const assignUserButton = wrapper.find('[data-testid="assign-user"]');
expect(assignUserButton.exists()).toBe(false);
});
});
2020-07-28 23:09:34 +05:30
describe('when a user does not have access to edit an issue', () => {
const testButtonDoesNotRender = () => {
const assignUserButton = wrapper.find('[data-testid="assign-user"]');
expect(assignUserButton.exists()).toBe(false);
};
beforeEach(() => {
2021-01-29 00:20:46 +05:30
wrapper = mountNoteActions(props, {
2020-07-28 23:09:34 +05:30
targetType: () => 'issue',
});
});
it('should not be possible to assign the comment author', testButtonDoesNotRender);
it('should not be possible to unassign the comment author', testButtonDoesNotRender);
2018-03-17 18:26:18 +05:30
});
describe('user is not logged in', () => {
beforeEach(() => {
2023-01-13 00:05:48 +05:30
// userData can be null https://gitlab.com/gitlab-org/gitlab/-/issues/379375
store.dispatch('setUserData', null);
2021-01-29 00:20:46 +05:30
wrapper = mountNoteActions({
2019-03-02 22:35:43 +05:30
...props,
2018-03-17 18:26:18 +05:30
canDelete: false,
canEdit: false,
2018-05-09 12:01:36 +05:30
canAwardEmoji: false,
2018-03-17 18:26:18 +05:30
canReportAsAbuse: false,
2019-03-02 22:35:43 +05:30
});
2018-03-17 18:26:18 +05:30
});
it('should not render emoji link', () => {
2019-03-02 22:35:43 +05:30
expect(wrapper.find('.js-add-award').exists()).toBe(false);
2018-03-17 18:26:18 +05:30
});
it('should not render actions dropdown', () => {
2019-03-02 22:35:43 +05:30
expect(wrapper.find('.more-actions').exists()).toBe(false);
});
});
2019-07-07 11:18:12 +05:30
describe('for showReply = true', () => {
2019-03-02 22:35:43 +05:30
beforeEach(() => {
2021-01-29 00:20:46 +05:30
wrapper = mountNoteActions({
2019-07-07 11:18:12 +05:30
...props,
showReply: true,
2019-03-02 22:35:43 +05:30
});
});
2019-07-07 11:18:12 +05:30
it('shows a reply button', () => {
2022-08-27 11:52:29 +05:30
const replyButton = wrapper.findComponent({ ref: 'replyButton' });
2019-03-02 22:35:43 +05:30
2019-07-07 11:18:12 +05:30
expect(replyButton.exists()).toBe(true);
2019-03-02 22:35:43 +05:30
});
});
2019-07-07 11:18:12 +05:30
describe('for showReply = false', () => {
2019-03-02 22:35:43 +05:30
beforeEach(() => {
2021-01-29 00:20:46 +05:30
wrapper = mountNoteActions({
2019-07-07 11:18:12 +05:30
...props,
showReply: false,
2019-03-02 22:35:43 +05:30
});
});
2019-07-07 11:18:12 +05:30
it('does not show a reply button', () => {
2022-08-27 11:52:29 +05:30
const replyButton = wrapper.findComponent({ ref: 'replyButton' });
2019-03-02 22:35:43 +05:30
2019-07-07 11:18:12 +05:30
expect(replyButton.exists()).toBe(false);
2018-03-17 18:26:18 +05:30
});
});
2020-06-23 00:09:42 +05:30
describe('Draft notes', () => {
beforeEach(() => {
store.dispatch('setUserData', userDataMock);
2021-01-29 00:20:46 +05:30
wrapper = mountNoteActions({ ...props, canResolve: true, isDraft: true });
2020-06-23 00:09:42 +05:30
});
it('should render the right resolve button title', () => {
2022-08-27 11:52:29 +05:30
const resolveButton = wrapper.findComponent({ ref: 'resolveButton' });
2020-06-23 00:09:42 +05:30
expect(resolveButton.exists()).toBe(true);
expect(resolveButton.attributes('title')).toBe('Thread stays unresolved');
});
});
2023-01-13 00:05:48 +05:30
describe('timeline event button', () => {
// why: We are working with an integrated store, so let's imply the getter is used
describe.each`
desc | userCanAdd | noteableType | exists
${'default'} | ${true} | ${NOTEABLE_TYPE_MAPPING.Incident} | ${true}
${'when cannot add incident timeline event'} | ${false} | ${NOTEABLE_TYPE_MAPPING.Incident} | ${false}
${'when is not incident'} | ${true} | ${NOTEABLE_TYPE_MAPPING.MergeRequest} | ${false}
`('$desc', ({ userCanAdd, noteableType, exists }) => {
beforeEach(() => {
setupStoreForIncidentTimelineEvents({
userCanAdd,
noteableType,
});
wrapper = mountNoteActions({ ...props });
});
it(`handles rendering of timeline button (exists=${exists})`, () => {
expect(findTimelineButton().exists()).toBe(exists);
});
});
describe('default', () => {
beforeEach(() => {
setupStoreForIncidentTimelineEvents({
userCanAdd: true,
noteableType: NOTEABLE_TYPE_MAPPING.Incident,
});
wrapper = mountNoteActions({ ...props });
});
it('should render timeline-event-button', () => {
expect(findTimelineButton().props()).toEqual({
noteId: props.noteId,
isPromotionInProgress: true,
});
});
it('when timeline-event-button emits click-promote-comment-to-event, dispatches action', () => {
jest.spyOn(store, 'dispatch').mockImplementation();
expect(store.dispatch).not.toHaveBeenCalled();
findTimelineButton().vm.$emit('click-promote-comment-to-event');
expect(store.dispatch).toHaveBeenCalledTimes(1);
expect(store.dispatch).toHaveBeenCalledWith('promoteCommentToTimelineEvent');
});
});
});
2023-04-23 21:23:45 +05:30
describe('report abuse button', () => {
const findAbuseCategorySelector = () => wrapper.findComponent(AbuseCategorySelector);
describe('when user is not allowed to report abuse', () => {
beforeEach(() => {
store.dispatch('setUserData', userDataMock);
wrapper = mountNoteActions({ ...props, canReportAsAbuse: false });
});
it('does not render the report abuse', () => {
expect(findReportAbuseButton().exists()).toBe(false);
});
it('does not render the abuse category drawer', () => {
expect(findAbuseCategorySelector().exists()).toBe(false);
});
});
describe('when user is allowed to report abuse', () => {
beforeEach(() => {
store.dispatch('setUserData', userDataMock);
wrapper = mountNoteActions({ ...props, canReportAsAbuse: true });
});
it('renders report abuse button', () => {
expect(findReportAbuseButton().exists()).toBe(true);
});
it('does not render the abuse category drawer immediately', () => {
expect(findAbuseCategorySelector().exists()).toBe(false);
});
it('opens the drawer when report abuse button is clicked', async () => {
2023-07-09 08:55:56 +05:30
await findReportAbuseButton().vm.$emit('action');
2023-04-23 21:23:45 +05:30
expect(findAbuseCategorySelector().props('showDrawer')).toEqual(true);
});
it('closes the drawer', async () => {
2023-07-09 08:55:56 +05:30
await findReportAbuseButton().vm.$emit('action');
2023-04-23 21:23:45 +05:30
findAbuseCategorySelector().vm.$emit('close-drawer');
await nextTick();
expect(findAbuseCategorySelector().exists()).toEqual(false);
});
});
});
2018-03-17 18:26:18 +05:30
});