1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
|
import { GlModal, GlSprintf, GlAlert } from '@gitlab/ui';
import { nextTick } from 'vue';
import { shallowMountExtended } from 'helpers/vue_test_utils_helper';
import Api from '~/api';
import InviteGroupsModal from '~/invite_members/components/invite_groups_modal.vue';
import InviteModalBase from '~/invite_members/components/invite_modal_base.vue';
import ContentTransition from '~/vue_shared/components/content_transition.vue';
import GroupSelect from '~/invite_members/components/group_select.vue';
import InviteGroupNotification from '~/invite_members/components/invite_group_notification.vue';
import { stubComponent } from 'helpers/stub_component';
import {
displaySuccessfulInvitationAlert,
reloadOnInvitationSuccess,
} from '~/invite_members/utils/trigger_successful_invite_alert';
import {
GROUP_MODAL_TO_GROUP_ALERT_BODY,
GROUP_MODAL_TO_GROUP_ALERT_LINK,
GROUP_MODAL_TO_PROJECT_ALERT_BODY,
GROUP_MODAL_TO_PROJECT_ALERT_LINK,
} from '~/invite_members/constants';
import { propsData, sharedGroup } from '../mock_data/group_modal';
jest.mock('~/invite_members/utils/trigger_successful_invite_alert');
describe('InviteGroupsModal', () => {
let wrapper;
const mockToastShow = jest.fn();
const createComponent = (props = {}) => {
wrapper = shallowMountExtended(InviteGroupsModal, {
propsData: {
...propsData,
...props,
},
stubs: {
InviteModalBase,
ContentTransition,
GlSprintf,
GlModal: stubComponent(GlModal, {
template: '<div><slot></slot><slot name="modal-footer"></slot></div>',
}),
},
mocks: {
$toast: {
show: mockToastShow,
},
},
});
};
afterEach(() => {
mockToastShow.mockClear();
});
const createInviteGroupToProjectWrapper = () => {
createComponent({ isProject: true });
};
const createInviteGroupToGroupWrapper = () => {
createComponent({ isProject: false });
};
const findModal = () => wrapper.findComponent(GlModal);
const findGroupSelect = () => wrapper.findComponent(GroupSelect);
const findInviteGroupAlert = () => wrapper.findComponent(InviteGroupNotification);
const findIntroText = () => wrapper.findByTestId('modal-base-intro-text').text();
const findMembersFormGroup = () => wrapper.findByTestId('members-form-group');
const membersFormGroupInvalidFeedback = () =>
findMembersFormGroup().attributes('invalid-feedback');
const findBase = () => wrapper.findComponent(InviteModalBase);
const triggerGroupSelect = (val) => findGroupSelect().vm.$emit('input', val);
const hideModal = () => findModal().vm.$emit('hidden', { preventDefault: jest.fn() });
const emitClickFromModal = (testId) => () =>
wrapper.findByTestId(testId).vm.$emit('click', { preventDefault: jest.fn() });
const clickInviteButton = emitClickFromModal('invite-modal-submit');
const clickCancelButton = emitClickFromModal('invite-modal-cancel');
describe('passes correct props to InviteModalBase', () => {
it('set accessLevel', () => {
createInviteGroupToProjectWrapper();
expect(findBase().props('accessLevels')).toMatchObject({
validRoles: propsData.accessLevels,
});
});
});
describe('displaying the correct introText and form group description', () => {
describe('when inviting to a project', () => {
it('includes the correct type, and formatted intro text', () => {
createInviteGroupToProjectWrapper();
expect(findIntroText()).toBe("You're inviting a group to the test name project.");
});
});
describe('when inviting to a group', () => {
it('includes the correct type, and formatted intro text', () => {
createInviteGroupToGroupWrapper();
expect(findIntroText()).toBe("You're inviting a group to the test name group.");
});
});
});
describe('rendering the invite group notification', () => {
it('shows the user limit notification alert when free user cap is enabled', () => {
createComponent({ freeUserCapEnabled: true });
expect(findInviteGroupAlert().exists()).toBe(true);
});
it('does not show the user limit notification alert', () => {
createComponent();
expect(findInviteGroupAlert().exists()).toBe(false);
});
it('shows the user limit notification alert with correct link and text for group', () => {
createComponent({ freeUserCapEnabled: true });
expect(findInviteGroupAlert().props()).toMatchObject({
name: propsData.name,
notificationText: GROUP_MODAL_TO_GROUP_ALERT_BODY,
notificationLink: GROUP_MODAL_TO_GROUP_ALERT_LINK,
});
});
it('shows the user limit notification alert with correct link and text for project', () => {
createComponent({ freeUserCapEnabled: true, isProject: true });
expect(findInviteGroupAlert().props()).toMatchObject({
name: propsData.name,
notificationText: GROUP_MODAL_TO_PROJECT_ALERT_BODY,
notificationLink: GROUP_MODAL_TO_PROJECT_ALERT_LINK,
});
});
});
describe('submitting the invite form', () => {
let apiResolve;
let apiReject;
const groupPostData = {
group_id: sharedGroup.id,
group_access: propsData.defaultAccessLevel,
expires_at: undefined,
format: 'json',
};
beforeEach(() => {
createComponent();
triggerGroupSelect(sharedGroup);
jest.spyOn(Api, 'groupShareWithGroup').mockImplementation(
() =>
new Promise((resolve, reject) => {
apiResolve = resolve;
apiReject = reject;
}),
);
clickInviteButton();
});
it('shows loading', () => {
expect(findBase().props('isLoading')).toBe(true);
});
it('calls Api groupShareWithGroup with the correct params', () => {
expect(Api.groupShareWithGroup).toHaveBeenCalledWith(propsData.id, groupPostData);
});
describe('when succeeds', () => {
beforeEach(() => {
apiResolve({ data: groupPostData });
});
it('hides loading', () => {
expect(findBase().props('isLoading')).toBe(false);
});
it('has no error message', () => {
expect(findBase().props('invalidFeedbackMessage')).toBe('');
});
it('displays the successful toastMessage', () => {
expect(mockToastShow).toHaveBeenCalledWith('Members were successfully added', {
onComplete: expect.any(Function),
});
});
it('does not call displaySuccessfulInvitationAlert on mount', () => {
expect(displaySuccessfulInvitationAlert).not.toHaveBeenCalled();
});
it('does not call reloadOnInvitationSuccess', () => {
expect(reloadOnInvitationSuccess).not.toHaveBeenCalled();
});
});
describe('when fails', () => {
beforeEach(() => {
apiReject({ response: { data: { success: false } } });
});
it('does not show the toast message on failure', () => {
expect(mockToastShow).not.toHaveBeenCalled();
});
it('displays the generic error for http server error', () => {
expect(membersFormGroupInvalidFeedback()).toBe('Something went wrong');
});
it.each`
desc | act
${'when the cancel button is clicked'} | ${clickCancelButton}
${'when the modal is hidden'} | ${hideModal}
${'when invite button is clicked'} | ${clickInviteButton}
${'when group input changes'} | ${() => triggerGroupSelect(sharedGroup)}
`('clears the error, $desc', async ({ act }) => {
act();
await nextTick();
expect(membersFormGroupInvalidFeedback()).toBe('');
});
});
});
describe('submitting the invite form with reloadPageOnSubmit set true', () => {
const groupPostData = {
group_id: sharedGroup.id,
group_access: propsData.defaultAccessLevel,
expires_at: undefined,
format: 'json',
};
beforeEach(() => {
createComponent({ reloadPageOnSubmit: true });
triggerGroupSelect(sharedGroup);
jest.spyOn(Api, 'groupShareWithGroup').mockResolvedValue({ data: groupPostData });
clickInviteButton();
});
describe('when succeeds', () => {
it('calls displaySuccessfulInvitationAlert on mount', () => {
expect(displaySuccessfulInvitationAlert).toHaveBeenCalled();
});
it('calls reloadOnInvitationSuccess', () => {
expect(reloadOnInvitationSuccess).toHaveBeenCalled();
});
it('does not show the toast message on failure', () => {
expect(mockToastShow).not.toHaveBeenCalled();
});
});
});
describe('when group select emits an error event', () => {
it('displays error alert', async () => {
createComponent();
findGroupSelect().vm.$emit('error', GroupSelect.i18n.errorMessage);
await nextTick();
expect(wrapper.findComponent(GlAlert).text()).toBe(GroupSelect.i18n.errorMessage);
});
});
});
|