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
|
import { GlTokenSelector } from '@gitlab/ui';
import { shallowMount } from '@vue/test-utils';
import { nextTick } from 'vue';
import { stubComponent } from 'helpers/stub_component';
import waitForPromises from 'helpers/wait_for_promises';
import * as UserApi from '~/api/user_api';
import MembersTokenSelect from '~/invite_members/components/members_token_select.vue';
import { VALID_TOKEN_BACKGROUND, INVALID_TOKEN_BACKGROUND } from '~/invite_members/constants';
const label = 'testgroup';
const placeholder = 'Search for a member';
const user1 = { id: 1, name: 'John Smith', username: 'one_1', avatar_url: '' };
const user2 = { id: 2, name: 'Jane Doe', username: 'two_2', avatar_url: '' };
const allUsers = [user1, user2];
const createComponent = (props) => {
return shallowMount(MembersTokenSelect, {
propsData: {
ariaLabelledby: label,
invalidMembers: {},
placeholder,
...props,
},
stubs: {
GlTokenSelector: stubComponent(GlTokenSelector),
},
});
};
describe('MembersTokenSelect', () => {
let wrapper;
const findTokenSelector = () => wrapper.findComponent(GlTokenSelector);
describe('rendering the token-selector component', () => {
it('renders with the correct props', () => {
wrapper = createComponent();
const expectedProps = {
ariaLabelledby: label,
placeholder,
};
expect(findTokenSelector().props()).toEqual(expect.objectContaining(expectedProps));
});
});
describe('when there are invalidMembers', () => {
it('adds in the correct class values for the tokens', async () => {
const badToken = { ...user1, class: INVALID_TOKEN_BACKGROUND };
const goodToken = { ...user2, class: VALID_TOKEN_BACKGROUND };
wrapper = createComponent();
findTokenSelector().vm.$emit('input', [user1, user2]);
await waitForPromises();
expect(findTokenSelector().props('selectedTokens')).toEqual([user1, user2]);
await wrapper.setProps({ invalidMembers: { one_1: 'bad stuff' } });
expect(findTokenSelector().props('selectedTokens')).toEqual([badToken, goodToken]);
});
it('does not change class when invalid members are cleared', async () => {
// arrange - invalidMembers is non-empty and then tokens are added
wrapper = createComponent();
await wrapper.setProps({ invalidMembers: { one_1: 'bad stuff' } });
findTokenSelector().vm.$emit('input', [user1, user2]);
await waitForPromises();
// act - invalidMembers clears out
await wrapper.setProps({ invalidMembers: {} });
// assert - we didn't try to update the tokens
expect(findTokenSelector().props('selectedTokens')).toEqual([user1, user2]);
});
});
describe('users', () => {
beforeEach(() => {
jest.spyOn(UserApi, 'getUsers').mockResolvedValue({ data: allUsers });
wrapper = createComponent();
});
describe('when input is focused for the first time (modal auto-focus)', () => {
it('does not call the API', async () => {
findTokenSelector().vm.$emit('focus');
await waitForPromises();
expect(UserApi.getUsers).not.toHaveBeenCalled();
});
});
describe('when input is manually focused', () => {
it('calls the API and sets dropdown items as request result', async () => {
const tokenSelector = findTokenSelector();
tokenSelector.vm.$emit('focus');
tokenSelector.vm.$emit('blur');
tokenSelector.vm.$emit('focus');
await waitForPromises();
expect(tokenSelector.props('dropdownItems')).toMatchObject(allUsers);
expect(tokenSelector.props('hideDropdownWithNoItems')).toBe(false);
});
});
describe('when text input is typed in', () => {
let tokenSelector;
beforeEach(() => {
tokenSelector = findTokenSelector();
});
it('calls the API with search parameter', async () => {
const searchParam = 'One';
tokenSelector.vm.$emit('text-input', searchParam);
await waitForPromises();
expect(UserApi.getUsers).toHaveBeenCalledWith(searchParam, {
active: true,
without_project_bots: true,
});
expect(tokenSelector.props('hideDropdownWithNoItems')).toBe(false);
});
it('calls the API with search parameter with whitespaces and is trimmed', async () => {
tokenSelector.vm.$emit('text-input', ' foo@bar.com ');
await waitForPromises();
expect(UserApi.getUsers).toHaveBeenCalledWith('foo@bar.com', {
active: true,
without_project_bots: true,
});
expect(tokenSelector.props('hideDropdownWithNoItems')).toBe(false);
});
describe('when input text is an email', () => {
it.each`
email | result
${'foo@bar.com'} | ${true}
${'foo@bar.com '} | ${false}
${' foo@bar.com'} | ${false}
${'foo@ba r.com'} | ${false}
${'fo o@bar.com'} | ${false}
`(`with token creation validation on $email`, async ({ email, result }) => {
tokenSelector.vm.$emit('text-input', email);
await nextTick();
expect(tokenSelector.props('allowUserDefinedTokens')).toBe(result);
});
});
});
describe('when user is selected', () => {
it('emits `input` event with selected users', () => {
findTokenSelector().vm.$emit('input', [user1, user2]);
expect(wrapper.emitted().input[0][0]).toEqual([user1, user2]);
});
});
describe('when user is removed', () => {
it('emits `clear` event', () => {
findTokenSelector().vm.$emit('token-remove', [user1]);
expect(wrapper.emitted('clear')).toEqual([[]]);
expect(wrapper.emitted('token-remove')).toBeUndefined();
});
it('emits `token-remove` event with the token when there are still tokens selected', () => {
findTokenSelector().vm.$emit('input', [user1, user2]);
findTokenSelector().vm.$emit('token-remove', [user1]);
expect(wrapper.emitted('token-remove')).toEqual([[[user1]]]);
expect(wrapper.emitted('clear')).toBeUndefined();
});
});
});
describe('when text input is blurred', () => {
it('clears text input', async () => {
wrapper = createComponent();
const tokenSelector = findTokenSelector();
tokenSelector.vm.$emit('blur');
await nextTick();
expect(tokenSelector.props('hideDropdownWithNoItems')).toBe(false);
});
});
describe('when component is mounted for a group using a saml provider', () => {
const searchParam = 'name';
const samlProviderId = 123;
let resolveApiRequest;
beforeEach(() => {
jest.spyOn(UserApi, 'getUsers').mockImplementation(
() =>
new Promise((resolve) => {
resolveApiRequest = resolve;
}),
);
wrapper = createComponent({ filterId: samlProviderId, usersFilter: 'saml_provider_id' });
findTokenSelector().vm.$emit('text-input', searchParam);
});
it('calls the API with the saml provider ID param', () => {
resolveApiRequest({ data: allUsers });
expect(UserApi.getUsers).toHaveBeenCalledWith(searchParam, {
active: true,
without_project_bots: true,
saml_provider_id: samlProviderId,
});
});
});
});
|