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.

125 lines
3.7 KiB
JavaScript
Raw Normal View History

2021-01-03 14:25:43 +05:30
import { GlModal, GlForm } from '@gitlab/ui';
import { within } from '@testing-library/dom';
2022-04-04 11:22:00 +05:30
import { mount, createWrapper } from '@vue/test-utils';
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';
2021-02-22 17:27:13 +05:30
import LeaveModal from '~/members/components/modals/leave_modal.vue';
2021-04-29 21:17:54 +05:30
import { LEAVE_MODAL_ID, MEMBER_TYPES } 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
},
});
};
const createComponent = (propsData = {}, state) => {
wrapper = mount(LeaveModal, {
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,
...propsData,
},
attrs: {
static: true,
visible: true,
},
});
};
2021-04-29 21:17:54 +05:30
const findModal = () => wrapper.findComponent(GlModal);
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
const getByText = (text, options) =>
createWrapper(within(findModal().element).getByText(text, options));
beforeEach(async () => {
createComponent();
await nextTick();
});
afterEach(() => {
wrapper.destroy();
});
it('sets modal ID', () => {
expect(findModal().props('modalId')).toBe(LEAVE_MODAL_ID);
});
it('displays modal title', () => {
2021-03-08 18:12:59 +05:30
expect(getByText(`Leave "${member.source.fullName}"`).exists()).toBe(true);
2021-01-03 14:25:43 +05:30
});
it('displays modal body', () => {
2021-03-08 18:12:59 +05:30
expect(getByText(`Are you sure you want to leave "${member.source.fullName}"?`).exists()).toBe(
2021-01-03 14:25:43 +05:30
true,
);
});
it('displays form with correct action and inputs', () => {
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', () => {
it("displays obstacles list when member's user is part of on-call management", () => {
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;
createComponent({ member: memberWithoutOncall });
2021-11-11 11:23:49 +05:30
await nextTick();
2021-11-18 22:05:49 +05:30
expect(findUserDeletionObstaclesList().exists()).toBe(false);
2021-04-29 21:17:54 +05:30
});
});
2021-01-03 14:25:43 +05:30
it('submits the form when "Leave" button is clicked', () => {
const submitSpy = jest.spyOn(findForm().element, 'submit');
getByText('Leave').trigger('click');
expect(submitSpy).toHaveBeenCalled();
submitSpy.mockRestore();
});
});