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
|
import Vue from 'vue';
import VueApollo from 'vue-apollo';
import { shallowMountExtended } from 'helpers/vue_test_utils_helper';
import createMockApollo from 'helpers/mock_apollo_helper';
import { mockTracking } from 'helpers/tracking_helper';
import waitForPromises from 'helpers/wait_for_promises';
import { clearDraft } from '~/lib/utils/autosave';
import WorkItemAddNote from '~/work_items/components/notes/work_item_add_note.vue';
import WorkItemCommentLocked from '~/work_items/components/notes/work_item_comment_locked.vue';
import WorkItemCommentForm from '~/work_items/components/notes/work_item_comment_form.vue';
import createNoteMutation from '~/work_items/graphql/notes/create_work_item_note.mutation.graphql';
import { TRACKING_CATEGORY_SHOW } from '~/work_items/constants';
import workItemByIidQuery from '~/work_items/graphql/work_item_by_iid.query.graphql';
import {
createWorkItemNoteResponse,
workItemByIidResponseFactory,
workItemQueryResponse,
} from '../../mock_data';
jest.mock('~/lib/utils/confirm_via_gl_modal/confirm_via_gl_modal');
jest.mock('~/lib/utils/autosave');
const workItemId = workItemQueryResponse.data.workItem.id;
describe('Work item add note', () => {
let wrapper;
Vue.use(VueApollo);
const mutationSuccessHandler = jest.fn().mockResolvedValue(createWorkItemNoteResponse);
let workItemResponseHandler;
const findCommentForm = () => wrapper.findComponent(WorkItemCommentForm);
const findTextarea = () => wrapper.findByTestId('note-reply-textarea');
const findWorkItemLockedComponent = () => wrapper.findComponent(WorkItemCommentLocked);
const createComponent = async ({
mutationHandler = mutationSuccessHandler,
canUpdate = true,
canCreateNote = true,
workItemIid = '1',
workItemResponse = workItemByIidResponseFactory({ canUpdate, canCreateNote }),
signedIn = true,
isEditing = true,
workItemType = 'Task',
isInternalThread = false,
} = {}) => {
workItemResponseHandler = jest.fn().mockResolvedValue(workItemResponse);
if (signedIn) {
window.gon.current_user_id = '1';
window.gon.current_user_avatar_url = 'avatar.png';
}
const apolloProvider = createMockApollo([
[workItemByIidQuery, workItemResponseHandler],
[createNoteMutation, mutationHandler],
]);
const { id } = workItemQueryResponse.data.workItem;
wrapper = shallowMountExtended(WorkItemAddNote, {
apolloProvider,
provide: {
fullPath: 'test-project-path',
},
propsData: {
workItemId: id,
workItemIid,
workItemType,
markdownPreviewPath: '/group/project/preview_markdown?target_type=WorkItem',
autocompleteDataSources: {},
isInternalThread,
},
stubs: {
WorkItemCommentLocked,
},
});
await waitForPromises();
if (isEditing) {
findTextarea().trigger('click');
}
};
describe('adding a comment', () => {
describe.each`
isInternalComment
${false}
${true}
`('when internal comment is $isInternalComment', ({ isInternalComment }) => {
it('calls update widgets mutation', async () => {
const noteText = 'updated desc';
await createComponent({
isEditing: true,
signedIn: true,
});
findCommentForm().vm.$emit('submitForm', {
commentText: noteText,
isNoteInternal: isInternalComment,
});
await waitForPromises();
expect(mutationSuccessHandler).toHaveBeenCalledWith({
input: {
noteableId: workItemId,
body: noteText,
discussionId: null,
internal: isInternalComment,
},
});
});
it('tracks adding comment', async () => {
await createComponent();
const trackingSpy = mockTracking(undefined, wrapper.element, jest.spyOn);
findCommentForm().vm.$emit('submitForm', {
commentText: 'test',
isNoteInternal: isInternalComment,
});
await waitForPromises();
expect(trackingSpy).toHaveBeenCalledWith(TRACKING_CATEGORY_SHOW, 'add_work_item_comment', {
category: TRACKING_CATEGORY_SHOW,
label: 'item_comment',
property: 'type_Task',
});
});
it('emits `replied` event and hides form after successful mutation', async () => {
await createComponent({ isEditing: true, signedIn: true });
findCommentForm().vm.$emit('submitForm', {
commentText: 'some text',
isNoteInternal: isInternalComment,
});
await waitForPromises();
expect(wrapper.emitted('replied')).toEqual([[]]);
});
it('clears a draft after successful mutation', async () => {
await createComponent({
isEditing: true,
signedIn: true,
});
findCommentForm().vm.$emit('submitForm', {
commentText: 'some text',
isNoteInternal: isInternalComment,
});
await waitForPromises();
expect(clearDraft).toHaveBeenCalledWith('gid://gitlab/WorkItem/1-comment');
});
it('emits error when mutation returns error', async () => {
const error = 'eror';
await createComponent({
isEditing: true,
mutationHandler: jest.fn().mockResolvedValue({
data: {
createNote: {
note: {
id: 'gid://gitlab/Discussion/c872ba2d7d3eb780d2255138d67ca8b04f65b122',
discussion: {
id: 'gid://gitlab/Discussion/c872ba2d7d3eb780d2255138d67ca8b04f65b122',
notes: {
nodes: [],
__typename: 'NoteConnection',
},
__typename: 'Discussion',
},
__typename: 'Note',
},
__typename: 'CreateNotePayload',
errors: [error],
},
},
}),
});
findCommentForm().vm.$emit('submitForm', {
commentText: 'updated desc',
isNoteInternal: isInternalComment,
});
await waitForPromises();
expect(wrapper.emitted('error')).toEqual([[error]]);
});
it('emits error when mutation fails', async () => {
const error = 'eror';
await createComponent({
isEditing: true,
mutationHandler: jest.fn().mockRejectedValue(new Error(error)),
});
findCommentForm().vm.$emit('submitForm', {
commentText: 'updated desc',
isNoteInternal: isInternalComment,
});
await waitForPromises();
expect(wrapper.emitted('error')).toEqual([[error]]);
});
it('ignores errors when mutation returns additional information as errors for quick actions', async () => {
await createComponent({
isEditing: true,
mutationHandler: jest.fn().mockResolvedValue({
data: {
createNote: {
note: {
id: 'gid://gitlab/Discussion/c872ba2d7d3eb780d2255138d67ca8b04f65b122',
discussion: {
id: 'gid://gitlab/Discussion/c872ba2d7d3eb780d2255138d67ca8b04f65b122',
notes: {
nodes: [],
__typename: 'NoteConnection',
},
__typename: 'Discussion',
},
__typename: 'Note',
},
__typename: 'CreateNotePayload',
errors: ['Commands only Removed assignee @foobar.', 'Command names ["unassign"]'],
},
},
}),
});
findCommentForm().vm.$emit('submitForm', {
commentText: 'updated desc',
isNoteInternal: isInternalComment,
});
await waitForPromises();
expect(clearDraft).toHaveBeenCalledWith('gid://gitlab/WorkItem/1-comment');
});
});
});
it('calls the work item query', async () => {
await createComponent();
expect(workItemResponseHandler).toHaveBeenCalled();
});
it('skips calling the work item query when missing workItemIid', async () => {
await createComponent({ workItemIid: '', isEditing: false });
expect(workItemResponseHandler).not.toHaveBeenCalled();
});
it('wrapper adds `internal-note` class when internal thread', async () => {
await createComponent({ isInternalThread: true });
expect(wrapper.attributes('class')).toContain('internal-note');
});
describe('when work item`createNote` permission false', () => {
it('cannot add comment', async () => {
await createComponent({ isEditing: false, canCreateNote: false });
expect(findWorkItemLockedComponent().exists()).toBe(true);
expect(findCommentForm().exists()).toBe(false);
});
});
});
|