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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
|
import { GlTokenSelector, GlSkeletonLoader } from '@gitlab/ui';
import Vue, { nextTick } from 'vue';
import VueApollo from 'vue-apollo';
import createMockApollo from 'helpers/mock_apollo_helper';
import waitForPromises from 'helpers/wait_for_promises';
import { mountExtended } from 'helpers/vue_test_utils_helper';
import { DEFAULT_DEBOUNCE_AND_THROTTLE_MS } from '~/lib/utils/constants';
import labelSearchQuery from '~/sidebar/components/labels/labels_select_widget/graphql/project_labels.query.graphql';
import workItemQuery from '~/work_items/graphql/work_item.query.graphql';
import workItemLabelsSubscription from 'ee_else_ce/work_items/graphql/work_item_labels.subscription.graphql';
import updateWorkItemMutation from '~/work_items/graphql/update_work_item.mutation.graphql';
import workItemByIidQuery from '~/work_items/graphql/work_item_by_iid.query.graphql';
import WorkItemLabels from '~/work_items/components/work_item_labels.vue';
import { i18n, I18N_WORK_ITEM_ERROR_FETCHING_LABELS } from '~/work_items/constants';
import {
projectLabelsResponse,
mockLabels,
workItemQueryResponse,
workItemResponseFactory,
updateWorkItemMutationResponse,
workItemLabelsSubscriptionResponse,
projectWorkItemResponse,
} from '../mock_data';
Vue.use(VueApollo);
const workItemId = 'gid://gitlab/WorkItem/1';
describe('WorkItemLabels component', () => {
let wrapper;
const findTokenSelector = () => wrapper.findComponent(GlTokenSelector);
const findSkeletonLoader = () => wrapper.findComponent(GlSkeletonLoader);
const findEmptyState = () => wrapper.findByTestId('empty-state');
const findLabelsTitle = () => wrapper.findByTestId('labels-title');
const workItemQuerySuccess = jest.fn().mockResolvedValue(workItemQueryResponse);
const workItemByIidResponseHandler = jest.fn().mockResolvedValue(projectWorkItemResponse);
const successSearchQueryHandler = jest.fn().mockResolvedValue(projectLabelsResponse);
const successUpdateWorkItemMutationHandler = jest
.fn()
.mockResolvedValue(updateWorkItemMutationResponse);
const subscriptionHandler = jest.fn().mockResolvedValue(workItemLabelsSubscriptionResponse);
const errorHandler = jest.fn().mockRejectedValue('Houston, we have a problem');
const createComponent = ({
canUpdate = true,
workItemQueryHandler = workItemQuerySuccess,
searchQueryHandler = successSearchQueryHandler,
updateWorkItemMutationHandler = successUpdateWorkItemMutationHandler,
fetchByIid = false,
queryVariables = { id: workItemId },
} = {}) => {
const apolloProvider = createMockApollo([
[workItemQuery, workItemQueryHandler],
[labelSearchQuery, searchQueryHandler],
[updateWorkItemMutation, updateWorkItemMutationHandler],
[workItemLabelsSubscription, subscriptionHandler],
[workItemByIidQuery, workItemByIidResponseHandler],
]);
wrapper = mountExtended(WorkItemLabels, {
propsData: {
workItemId,
canUpdate,
fullPath: 'test-project-path',
queryVariables,
fetchByIid,
},
attachTo: document.body,
apolloProvider,
});
};
afterEach(() => {
wrapper.destroy();
});
it('has a label', () => {
createComponent();
expect(findTokenSelector().props('ariaLabelledby')).toEqual(findLabelsTitle().attributes('id'));
});
it('focuses token selector on token selector input event', async () => {
createComponent();
findTokenSelector().vm.$emit('input', [mockLabels[0]]);
await waitForPromises();
expect(findEmptyState().exists()).toBe(false);
expect(findTokenSelector().element.contains(document.activeElement)).toBe(true);
});
it('does not start search by default', () => {
createComponent();
expect(findTokenSelector().props('loading')).toBe(false);
expect(findTokenSelector().props('dropdownItems')).toEqual([]);
});
it('starts search on hovering for more than 250ms', async () => {
createComponent();
findTokenSelector().trigger('mouseover');
jest.advanceTimersByTime(DEFAULT_DEBOUNCE_AND_THROTTLE_MS);
await nextTick();
expect(findTokenSelector().props('loading')).toBe(true);
});
it('starts search on focusing token selector', async () => {
createComponent();
findTokenSelector().vm.$emit('focus');
await nextTick();
expect(findTokenSelector().props('loading')).toBe(true);
});
it('does not start searching if token-selector was hovered for less than 250ms', async () => {
createComponent();
findTokenSelector().trigger('mouseover');
jest.advanceTimersByTime(100);
await nextTick();
expect(findTokenSelector().props('loading')).toBe(false);
});
it('does not start searching if cursor was moved out from token selector before 250ms passed', async () => {
createComponent();
findTokenSelector().trigger('mouseover');
jest.advanceTimersByTime(100);
findTokenSelector().trigger('mouseout');
jest.advanceTimersByTime(DEFAULT_DEBOUNCE_AND_THROTTLE_MS);
await nextTick();
expect(findTokenSelector().props('loading')).toBe(false);
});
it('shows skeleton loader on dropdown when loading', async () => {
createComponent();
findTokenSelector().vm.$emit('focus');
await nextTick();
expect(findSkeletonLoader().exists()).toBe(true);
});
it('shows list in dropdown when loaded', async () => {
createComponent();
findTokenSelector().vm.$emit('focus');
await nextTick();
expect(findSkeletonLoader().exists()).toBe(true);
await waitForPromises();
expect(findSkeletonLoader().exists()).toBe(false);
expect(findTokenSelector().props('dropdownItems')).toHaveLength(2);
});
it.each([true, false])(
'passes canUpdate=%s prop to view-only of token-selector',
async (canUpdate) => {
createComponent({ canUpdate });
await waitForPromises();
expect(findTokenSelector().props('viewOnly')).toBe(!canUpdate);
},
);
it('emits error event if search query fails', async () => {
createComponent({ searchQueryHandler: errorHandler });
findTokenSelector().vm.$emit('focus');
await waitForPromises();
expect(wrapper.emitted('error')).toEqual([[I18N_WORK_ITEM_ERROR_FETCHING_LABELS]]);
});
it('should search for with correct key after text input', async () => {
const searchKey = 'Hello';
createComponent();
findTokenSelector().vm.$emit('focus');
findTokenSelector().vm.$emit('text-input', searchKey);
await waitForPromises();
expect(successSearchQueryHandler).toHaveBeenCalledWith(
expect.objectContaining({ searchTerm: searchKey }),
);
});
it('adds new labels to the end', async () => {
const response = workItemResponseFactory({ labels: [mockLabels[1]] });
const workItemQueryHandler = jest.fn().mockResolvedValue(response);
createComponent({
workItemQueryHandler,
updateWorkItemMutationHandler: successUpdateWorkItemMutationHandler,
});
await waitForPromises();
findTokenSelector().vm.$emit('input', [mockLabels[0]]);
await waitForPromises();
const labels = findTokenSelector().props('selectedTokens');
expect(labels[0]).toMatchObject(mockLabels[1]);
expect(labels[1]).toMatchObject(mockLabels[0]);
});
describe('when clicking outside the token selector', () => {
it('calls a mutation with correct variables', () => {
createComponent();
findTokenSelector().vm.$emit('input', [mockLabels[0]]);
findTokenSelector().vm.$emit('blur', new FocusEvent({ relatedTarget: null }));
expect(successUpdateWorkItemMutationHandler).toHaveBeenCalledWith({
input: {
labelsWidget: { addLabelIds: [mockLabels[0].id], removeLabelIds: [] },
id: 'gid://gitlab/WorkItem/1',
},
});
});
it('emits an error and resets labels if mutation was rejected', async () => {
createComponent({ updateWorkItemMutationHandler: errorHandler });
await waitForPromises();
const initialLabels = findTokenSelector().props('selectedTokens');
findTokenSelector().vm.$emit('input', [mockLabels[0]]);
findTokenSelector().vm.$emit('blur', new FocusEvent({ relatedTarget: null }));
await waitForPromises();
const updatedLabels = findTokenSelector().props('selectedTokens');
expect(wrapper.emitted('error')).toEqual([[i18n.updateError]]);
expect(updatedLabels).toEqual(initialLabels);
});
it('does not make server request if no labels added or removed', async () => {
const updateWorkItemMutationHandler = jest
.fn()
.mockResolvedValue(updateWorkItemMutationResponse);
createComponent({ updateWorkItemMutationHandler });
await waitForPromises();
findTokenSelector().vm.$emit('input', []);
findTokenSelector().vm.$emit('blur', new FocusEvent({ relatedTarget: null }));
await waitForPromises();
expect(updateWorkItemMutationHandler).not.toHaveBeenCalled();
});
it('has a subscription', async () => {
createComponent();
await waitForPromises();
expect(subscriptionHandler).toHaveBeenCalledWith({
issuableId: workItemId,
});
});
});
it('calls the global ID work item query when `fetchByIid` prop is false', async () => {
createComponent({ fetchByIid: false });
await waitForPromises();
expect(workItemQuerySuccess).toHaveBeenCalled();
expect(workItemByIidResponseHandler).not.toHaveBeenCalled();
});
it('calls the IID work item query when when `fetchByIid` prop is true', async () => {
createComponent({ fetchByIid: true });
await waitForPromises();
expect(workItemQuerySuccess).not.toHaveBeenCalled();
expect(workItemByIidResponseHandler).toHaveBeenCalled();
});
it('skips calling the handlers when missing the needed queryVariables', async () => {
createComponent({ queryVariables: {}, fetchByIid: false });
await waitForPromises();
expect(workItemQuerySuccess).not.toHaveBeenCalled();
});
});
|