debian-mirror-gitlab/spec/frontend/members/components/modals/leave_modal_spec.js

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

189 lines
5.2 KiB
JavaScript
Raw Normal View History

2021-01-03 14:25:43 +05:30
import { GlModal, GlForm } from '@gitlab/ui';
2021-04-29 21:17:54 +05:30
import { cloneDeep } from 'lodash';
2022-04-04 11:22:00 +05:30
import Vue, { nextTick } from 'vue';
2021-01-03 14:25:43 +05:30
import Vuex from 'vuex';
2023-03-17 16:20:25 +05:30
import { mountExtended, extendedWrapper } from 'helpers/vue_test_utils_helper';
2021-02-22 17:27:13 +05:30
import LeaveModal from '~/members/components/modals/leave_modal.vue';
2023-03-17 16:20:25 +05:30
import {
LEAVE_MODAL_ID,
MEMBER_TYPES,
MEMBER_MODEL_TYPE_PROJECT_MEMBER,
} from '~/members/constants';
2021-11-18 22:05:49 +05:30
import UserDeletionObstaclesList from '~/vue_shared/components/user_deletion_obstacles/user_deletion_obstacles_list.vue';
import { parseUserDeletionObstacles } from '~/vue_shared/components/user_deletion_obstacles/utils';
2021-02-22 17:27:13 +05:30
import { member } from '../../mock_data';
2021-01-03 14:25:43 +05:30
jest.mock('~/lib/utils/csrf', () => ({ token: 'mock-csrf-token' }));
2022-04-04 11:22:00 +05:30
Vue.use(Vuex);
2021-01-03 14:25:43 +05:30
describe('LeaveModal', () => {
let wrapper;
const createStore = (state = {}) => {
return new Vuex.Store({
2021-04-29 21:17:54 +05:30
modules: {
[MEMBER_TYPES.user]: {
namespaced: true,
state: {
memberPath: '/groups/foo-bar/-/group_members/:id',
...state,
},
},
2021-01-03 14:25:43 +05:30
},
});
};
2023-03-17 16:20:25 +05:30
const createComponent = async (propsData = {}, state) => {
wrapper = mountExtended(LeaveModal, {
2021-01-03 14:25:43 +05:30
store: createStore(state),
2021-04-29 21:17:54 +05:30
provide: {
namespace: MEMBER_TYPES.user,
},
2021-01-03 14:25:43 +05:30
propsData: {
member,
2023-03-17 16:20:25 +05:30
permissions: {
canRemove: true,
},
2021-01-03 14:25:43 +05:30
...propsData,
},
attrs: {
static: true,
visible: true,
},
});
2023-03-17 16:20:25 +05:30
await nextTick();
2021-01-03 14:25:43 +05:30
};
2023-03-17 16:20:25 +05:30
const findModal = () => extendedWrapper(wrapper.findComponent(GlModal));
2021-04-29 21:17:54 +05:30
const findForm = () => findModal().findComponent(GlForm);
2021-11-18 22:05:49 +05:30
const findUserDeletionObstaclesList = () => findModal().findComponent(UserDeletionObstaclesList);
2021-01-03 14:25:43 +05:30
2023-03-17 16:20:25 +05:30
it('sets modal ID', async () => {
await createComponent();
2021-01-03 14:25:43 +05:30
expect(findModal().props('modalId')).toBe(LEAVE_MODAL_ID);
});
2023-03-17 16:20:25 +05:30
describe('when leave is allowed', () => {
it('displays modal title', async () => {
await createComponent();
expect(findModal().findByText(`Leave "${member.source.fullName}"`).exists()).toBe(true);
});
it('displays modal body', async () => {
await createComponent();
expect(
findModal()
.findByText(`Are you sure you want to leave "${member.source.fullName}"?`)
.exists(),
).toBe(true);
});
2021-01-03 14:25:43 +05:30
});
2023-03-17 16:20:25 +05:30
describe('when leave is blocked by last owner', () => {
const permissions = {
canRemove: false,
canRemoveBlockedByLastOwner: true,
};
it('does not show primary action button', async () => {
await createComponent({
permissions,
});
expect(findModal().props('actionPrimary')).toBe(null);
});
it('displays modal title', async () => {
await createComponent({
permissions,
});
expect(findModal().findByText(`Cannot leave "${member.source.fullName}"`).exists()).toBe(
true,
);
});
describe('when member model type is `GroupMember`', () => {
it('displays modal body', async () => {
await createComponent({
permissions,
});
expect(
findModal().findByText(LeaveModal.i18n.preventedBodyGroupMemberModelType).exists(),
).toBe(true);
});
});
describe('when member model type is `ProjectMember`', () => {
it('displays modal body', async () => {
await createComponent({
member: {
...member,
type: MEMBER_MODEL_TYPE_PROJECT_MEMBER,
},
permissions,
});
expect(
findModal().findByText(LeaveModal.i18n.preventedBodyProjectMemberModelType).exists(),
).toBe(true);
});
});
2021-01-03 14:25:43 +05:30
});
2023-03-17 16:20:25 +05:30
it('displays form with correct action and inputs', async () => {
await createComponent();
2021-01-03 14:25:43 +05:30
const form = findForm();
expect(form.attributes('action')).toBe('/groups/foo-bar/-/group_members/leave');
expect(form.find('input[name="_method"]').attributes('value')).toBe('delete');
expect(form.find('input[name="authenticity_token"]').attributes('value')).toBe(
'mock-csrf-token',
);
});
2021-11-18 22:05:49 +05:30
describe('User deletion obstacles list', () => {
2023-03-17 16:20:25 +05:30
it("displays obstacles list when member's user is part of on-call management", async () => {
await createComponent();
2021-11-18 22:05:49 +05:30
const obstaclesList = findUserDeletionObstaclesList();
expect(obstaclesList.exists()).toBe(true);
expect(obstaclesList.props()).toMatchObject({
2021-04-29 21:17:54 +05:30
isCurrentUser: true,
2021-11-18 22:05:49 +05:30
obstacles: parseUserDeletionObstacles(member.user),
2021-04-29 21:17:54 +05:30
});
});
2021-11-18 22:05:49 +05:30
it("does NOT display obstacles list when member's user is NOT a part of on-call management", async () => {
2021-11-11 11:23:49 +05:30
wrapper.destroy();
2021-11-18 22:05:49 +05:30
const memberWithoutOncall = cloneDeep(member);
delete memberWithoutOncall.user.oncallSchedules;
delete memberWithoutOncall.user.escalationPolicies;
2023-03-17 16:20:25 +05:30
await createComponent({ member: memberWithoutOncall });
2021-11-11 11:23:49 +05:30
2021-11-18 22:05:49 +05:30
expect(findUserDeletionObstaclesList().exists()).toBe(false);
2021-04-29 21:17:54 +05:30
});
});
2023-03-17 16:20:25 +05:30
it('submits the form when "Leave" button is clicked', async () => {
await createComponent();
2021-01-03 14:25:43 +05:30
const submitSpy = jest.spyOn(findForm().element, 'submit');
2023-03-17 16:20:25 +05:30
findModal().findByText('Leave').trigger('click');
2021-01-03 14:25:43 +05:30
expect(submitSpy).toHaveBeenCalled();
submitSpy.mockRestore();
});
});