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
|
import { GlCollapsibleListbox, GlListboxItem } from '@gitlab/ui';
import { GlBreakpointInstance as bp } from '@gitlab/ui/dist/utils';
import * as Sentry from '@sentry/browser';
import { mount } from '@vue/test-utils';
import Vue, { nextTick } from 'vue';
import Vuex from 'vuex';
import waitForPromises from 'helpers/wait_for_promises';
import RoleDropdown from '~/members/components/table/role_dropdown.vue';
import { MEMBER_TYPES } from '~/members/constants';
import { guestOverageConfirmAction } from 'ee_else_ce/members/guest_overage_confirm_action';
import { member } from '../../mock_data';
Vue.use(Vuex);
jest.mock('ee_else_ce/members/guest_overage_confirm_action');
jest.mock('@sentry/browser');
describe('RoleDropdown', () => {
let wrapper;
let actions;
const $toast = {
show: jest.fn(),
};
const createStore = ({ updateMemberRoleReturn = Promise.resolve() } = {}) => {
actions = {
updateMemberRole: jest.fn(() => updateMemberRoleReturn),
};
return new Vuex.Store({
modules: {
[MEMBER_TYPES.user]: { namespaced: true, actions },
},
});
};
const createComponent = (propsData = {}, store = createStore()) => {
wrapper = mount(RoleDropdown, {
provide: {
namespace: MEMBER_TYPES.user,
group: {
name: 'groupname',
path: '/grouppath/',
},
},
propsData: {
member,
permissions: {},
...propsData,
},
store,
mocks: {
$toast,
},
});
};
const findListbox = () => wrapper.findComponent(GlCollapsibleListbox);
const findListboxItems = () => wrapper.findAllComponents(GlListboxItem);
const findListboxItemByText = (text) =>
findListboxItems().wrappers.find((item) => item.text() === text);
beforeEach(() => {
gon.features = { showOverageOnRolePromotion: true };
});
it('has correct header text props', () => {
createComponent();
expect(findListbox().props('headerText')).toBe('Change role');
});
it('has items prop with all valid roles', () => {
createComponent();
const roles = findListbox()
.props('items')
.map((item) => item.text);
expect(roles).toEqual(Object.keys(member.validRoles));
});
describe('when listbox is open', () => {
beforeEach(async () => {
guestOverageConfirmAction.mockReturnValue(true);
createComponent();
await findListbox().vm.$emit('click');
});
it('sets dropdown toggle and checks selected role', () => {
expect(findListbox().props('toggleText')).toBe('Owner');
expect(findListbox().find('[aria-selected=true]').text()).toBe('Owner');
});
describe('when dropdown item is selected', () => {
it('does nothing if the item selected was already selected', async () => {
await findListboxItemByText('Owner').trigger('click');
expect(actions.updateMemberRole).not.toHaveBeenCalled();
});
it('calls `updateMemberRole` Vuex action', async () => {
await findListboxItemByText('Developer').trigger('click');
expect(actions.updateMemberRole).toHaveBeenCalledWith(expect.any(Object), {
memberId: member.id,
accessLevel: { integerValue: 30, stringValue: 'Developer' },
});
});
describe('when updateMemberRole is successful', () => {
it('displays toast', async () => {
await findListboxItemByText('Developer').trigger('click');
await nextTick();
expect($toast.show).toHaveBeenCalledWith('Role updated successfully.');
});
it('puts dropdown in loading state while waiting for `updateMemberRole` to resolve', async () => {
await findListboxItemByText('Developer').trigger('click');
expect(findListbox().props('loading')).toBe(true);
});
it('enables dropdown after `updateMemberRole` resolves', async () => {
await findListboxItemByText('Developer').trigger('click');
await waitForPromises();
expect(findListbox().props('disabled')).toBe(false);
});
it('does not log error to Sentry', async () => {
await findListboxItemByText('Developer').trigger('click');
await waitForPromises();
expect(Sentry.captureException).not.toHaveBeenCalled();
});
});
describe('when updateMemberRole is not successful', () => {
const reason = 'Rejected ☹️';
beforeEach(() => {
createComponent({}, createStore({ updateMemberRoleReturn: Promise.reject(reason) }));
});
it('does not display toast', async () => {
await findListboxItemByText('Developer').trigger('click');
await nextTick();
expect($toast.show).not.toHaveBeenCalled();
});
it('puts dropdown in loading state while waiting for `updateMemberRole` to resolve', async () => {
await findListboxItemByText('Developer').trigger('click');
expect(findListbox().props('loading')).toBe(true);
});
it('enables dropdown after `updateMemberRole` resolves', async () => {
await findListboxItemByText('Developer').trigger('click');
await waitForPromises();
expect(findListbox().props('disabled')).toBe(false);
});
it('logs error to Sentry', async () => {
await findListboxItemByText('Developer').trigger('click');
await waitForPromises();
expect(Sentry.captureException).toHaveBeenCalledWith(reason);
});
});
});
});
it("sets initial dropdown toggle value to member's role", () => {
createComponent();
expect(findListbox().props('toggleText')).toBe('Owner');
});
it('sets the dropdown alignment to right on mobile', async () => {
jest.spyOn(bp, 'isDesktop').mockReturnValue(false);
createComponent();
await nextTick();
expect(findListbox().props('placement')).toBe('right');
});
it('sets the dropdown alignment to left on desktop', async () => {
jest.spyOn(bp, 'isDesktop').mockReturnValue(true);
createComponent();
await nextTick();
expect(findListbox().props('placement')).toBe('left');
});
describe('guestOverageConfirmAction', () => {
const mockConfirmAction = ({ confirmed }) => {
guestOverageConfirmAction.mockResolvedValueOnce(confirmed);
};
beforeEach(() => {
createComponent();
findListbox().vm.$emit('click');
});
afterEach(() => {
guestOverageConfirmAction.mockReset();
});
describe('when guestOverageConfirmAction returns true', () => {
beforeEach(() => {
mockConfirmAction({ confirmed: true });
findListboxItemByText('Reporter').trigger('click');
});
it('calls updateMemberRole', () => {
expect(actions.updateMemberRole).toHaveBeenCalled();
});
});
describe('when guestOverageConfirmAction returns false', () => {
beforeEach(() => {
mockConfirmAction({ confirmed: false });
findListboxItemByText('Reporter').trigger('click');
});
it('does not call updateMemberRole', () => {
expect(actions.updateMemberRole).not.toHaveBeenCalled();
});
});
});
});
|