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

120 lines
3.5 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';
2021-03-11 19:13:27 +05:30
import { mount, createLocalVue, createWrapper } from '@vue/test-utils';
2021-04-29 21:17:54 +05:30
import { cloneDeep } from 'lodash';
2021-03-11 19:13:27 +05:30
import { 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';
import OncallSchedulesList from '~/vue_shared/components/oncall_schedules_list.vue';
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' }));
const localVue = createLocalVue();
localVue.use(Vuex);
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, {
localVue,
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);
const findOncallSchedulesList = () => findModal().findComponent(OncallSchedulesList);
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-04-29 21:17:54 +05:30
describe('On-call schedules list', () => {
it("displays oncall schedules list when member's user is part of on-call schedules ", () => {
const schedulesList = findOncallSchedulesList();
expect(schedulesList.exists()).toBe(true);
expect(schedulesList.props()).toMatchObject({
isCurrentUser: true,
schedules: member.user.oncallSchedules,
});
});
it("does NOT display oncall schedules list when member's user is NOT a part of on-call schedules ", () => {
const memberWithoutOncallSchedules = cloneDeep(member);
delete (memberWithoutOncallSchedules, 'user.oncallSchedules');
createComponent({ member: memberWithoutOncallSchedules });
expect(findOncallSchedulesList().exists()).toBe(false);
});
});
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();
});
});