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
|
import {
GlDropdown,
GlDropdownItem,
GlSearchBoxByType,
GlSkeletonLoader,
GlFormGroup,
GlDropdownText,
} from '@gitlab/ui';
import Vue, { nextTick } from 'vue';
import VueApollo from 'vue-apollo';
import WorkItemMilestone from '~/work_items/components/work_item_milestone.vue';
import createMockApollo from 'helpers/mock_apollo_helper';
import { mockTracking } from 'helpers/tracking_helper';
import { shallowMountExtended } from 'helpers/vue_test_utils_helper';
import waitForPromises from 'helpers/wait_for_promises';
import { TRACKING_CATEGORY_SHOW } from '~/work_items/constants';
import updateWorkItemMutation from '~/work_items/graphql/update_work_item.mutation.graphql';
import projectMilestonesQuery from '~/sidebar/queries/project_milestones.query.graphql';
import {
projectMilestonesResponse,
projectMilestonesResponseWithNoMilestones,
mockMilestoneWidgetResponse,
updateWorkItemMutationErrorResponse,
updateWorkItemMutationResponse,
} from '../mock_data';
describe('WorkItemMilestone component', () => {
Vue.use(VueApollo);
let wrapper;
const workItemId = 'gid://gitlab/WorkItem/1';
const workItemType = 'Task';
const findDropdown = () => wrapper.findComponent(GlDropdown);
const findSearchBox = () => wrapper.findComponent(GlSearchBoxByType);
const findSkeletonLoader = () => wrapper.findComponent(GlSkeletonLoader);
const findNoMilestoneDropdownItem = () => wrapper.findByTestId('no-milestone');
const findDropdownItems = () => wrapper.findAllComponents(GlDropdownItem);
const findFirstDropdownItem = () => findDropdownItems().at(0);
const findDropdownTexts = () => wrapper.findAllComponents(GlDropdownText);
const findDropdownItemAtIndex = (index) => findDropdownItems().at(index);
const findDisabledTextSpan = () => wrapper.findByTestId('disabled-text');
const findDropdownTextAtIndex = (index) => findDropdownTexts().at(index);
const findInputGroup = () => wrapper.findComponent(GlFormGroup);
const successSearchQueryHandler = jest.fn().mockResolvedValue(projectMilestonesResponse);
const successSearchWithNoMatchingMilestones = jest
.fn()
.mockResolvedValue(projectMilestonesResponseWithNoMilestones);
const successUpdateWorkItemMutationHandler = jest
.fn()
.mockResolvedValue(updateWorkItemMutationResponse);
const showDropdown = () => findDropdown().vm.$emit('shown');
const hideDropdown = () => findDropdown().vm.$emit('hide');
const createComponent = ({
canUpdate = true,
milestone = mockMilestoneWidgetResponse,
searchQueryHandler = successSearchQueryHandler,
mutationHandler = successUpdateWorkItemMutationHandler,
} = {}) => {
wrapper = shallowMountExtended(WorkItemMilestone, {
apolloProvider: createMockApollo([
[projectMilestonesQuery, searchQueryHandler],
[updateWorkItemMutation, mutationHandler],
]),
provide: {
fullPath: 'full-path',
},
propsData: {
canUpdate,
workItemMilestone: milestone,
workItemId,
workItemType,
},
stubs: {
GlDropdown,
GlSearchBoxByType,
},
});
};
it('has "Milestone" label', () => {
createComponent();
expect(findInputGroup().exists()).toBe(true);
expect(findInputGroup().attributes('label')).toBe(WorkItemMilestone.i18n.MILESTONE);
});
describe('Default text with canUpdate false and milestone value', () => {
describe.each`
description | milestone | value
${'when no milestone'} | ${null} | ${WorkItemMilestone.i18n.NONE}
${'when milestone set'} | ${mockMilestoneWidgetResponse} | ${mockMilestoneWidgetResponse.title}
`('$description', ({ milestone, value }) => {
it(`has a value of "${value}"`, () => {
createComponent({ canUpdate: false, milestone });
expect(findDisabledTextSpan().text()).toBe(value);
expect(findDropdown().exists()).toBe(false);
});
});
});
describe('Default text value when canUpdate true and no milestone set', () => {
it(`has a value of "Add to milestone"`, () => {
createComponent({ canUpdate: true, milestone: null });
expect(findDropdown().props('text')).toBe(WorkItemMilestone.i18n.MILESTONE_PLACEHOLDER);
});
});
describe('Dropdown search', () => {
it('has the search box', () => {
createComponent();
expect(findSearchBox().exists()).toBe(true);
});
it('shows no matching results when no items', () => {
createComponent({
searchQueryHandler: successSearchWithNoMatchingMilestones,
});
expect(findDropdownTextAtIndex(0).text()).toBe(WorkItemMilestone.i18n.NO_MATCHING_RESULTS);
expect(findDropdownItems()).toHaveLength(1);
expect(findDropdownTexts()).toHaveLength(1);
});
});
describe('Dropdown options', () => {
beforeEach(() => {
createComponent({ canUpdate: true });
});
it('calls successSearchQueryHandler with variables when dropdown is opened', async () => {
showDropdown();
await nextTick();
expect(successSearchQueryHandler).toHaveBeenCalledWith({
first: 20,
fullPath: 'full-path',
state: 'active',
title: '',
});
});
it('shows the skeleton loader when the items are being fetched on click', async () => {
showDropdown();
await nextTick();
expect(findSkeletonLoader().exists()).toBe(true);
});
it('shows the milestones in dropdown when the items have finished fetching', async () => {
showDropdown();
await waitForPromises();
expect(findSkeletonLoader().exists()).toBe(false);
expect(findNoMilestoneDropdownItem().exists()).toBe(true);
expect(findDropdownItems()).toHaveLength(
projectMilestonesResponse.data.workspace.attributes.nodes.length + 1,
);
});
it('changes the milestone to null when clicked on no milestone', async () => {
showDropdown();
findFirstDropdownItem().vm.$emit('click');
hideDropdown();
await nextTick();
expect(findDropdown().props('loading')).toBe(true);
await waitForPromises();
expect(findDropdown().props('loading')).toBe(false);
expect(findDropdown().props('text')).toBe(WorkItemMilestone.i18n.MILESTONE_PLACEHOLDER);
});
it('changes the milestone to the selected milestone', async () => {
const milestoneIndex = 1;
/** the index is -1 since no matching results is also a dropdown item */
const milestoneAtIndex =
projectMilestonesResponse.data.workspace.attributes.nodes[milestoneIndex - 1];
showDropdown();
await waitForPromises();
findDropdownItemAtIndex(milestoneIndex).vm.$emit('click');
hideDropdown();
await waitForPromises();
expect(findDropdown().props('text')).toBe(milestoneAtIndex.title);
});
});
describe('Error handlers', () => {
it.each`
errorType | expectedErrorMessage | mockValue | resolveFunction
${'graphql error'} | ${'Something went wrong while updating the task. Please try again.'} | ${updateWorkItemMutationErrorResponse} | ${'mockResolvedValue'}
${'network error'} | ${'Something went wrong while updating the task. Please try again.'} | ${new Error()} | ${'mockRejectedValue'}
`(
'emits an error when there is a $errorType',
async ({ mockValue, expectedErrorMessage, resolveFunction }) => {
createComponent({
mutationHandler: jest.fn()[resolveFunction](mockValue),
canUpdate: true,
});
showDropdown();
findFirstDropdownItem().vm.$emit('click');
hideDropdown();
await waitForPromises();
expect(wrapper.emitted('error')).toEqual([[expectedErrorMessage]]);
},
);
});
describe('Tracking event', () => {
it('tracks updating the milestone', async () => {
const trackingSpy = mockTracking(undefined, wrapper.element, jest.spyOn);
createComponent({ canUpdate: true });
showDropdown();
findFirstDropdownItem().vm.$emit('click');
hideDropdown();
await waitForPromises();
expect(trackingSpy).toHaveBeenCalledWith(TRACKING_CATEGORY_SHOW, 'updated_milestone', {
category: TRACKING_CATEGORY_SHOW,
label: 'item_milestone',
property: 'type_Task',
});
});
});
});
|