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
|
import { GlButton } from '@gitlab/ui';
import { shallowMount } from '@vue/test-utils';
import Vue from 'vue';
import VueApollo from 'vue-apollo';
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 { config } from '~/graphql_shared/issuable_client';
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 workItemQuery from '~/work_items/graphql/work_item.query.graphql';
import workItemNotesQuery from '~/work_items/graphql/notes/work_item_notes.query.graphql';
import workItemByIidQuery from '~/work_items/graphql/work_item_by_iid.query.graphql';
import {
workItemResponseFactory,
workItemQueryResponse,
projectWorkItemResponse,
createWorkItemNoteResponse,
mockWorkItemNotesResponse,
} 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('WorkItemCommentForm', () => {
let wrapper;
Vue.use(VueApollo);
const mutationSuccessHandler = jest.fn().mockResolvedValue(createWorkItemNoteResponse);
const workItemByIidResponseHandler = jest.fn().mockResolvedValue(projectWorkItemResponse);
let workItemResponseHandler;
const findCommentForm = () => wrapper.findComponent(WorkItemCommentForm);
const createComponent = async ({
mutationHandler = mutationSuccessHandler,
canUpdate = true,
workItemResponse = workItemResponseFactory({ canUpdate }),
queryVariables = { id: workItemId },
fetchByIid = false,
signedIn = true,
isEditing = true,
workItemType = 'Task',
} = {}) => {
workItemResponseHandler = jest.fn().mockResolvedValue(workItemResponse);
if (signedIn) {
window.gon.current_user_id = '1';
window.gon.current_user_avatar_url = 'avatar.png';
}
const apolloProvider = createMockApollo(
[
[workItemQuery, workItemResponseHandler],
[createNoteMutation, mutationHandler],
[workItemByIidQuery, workItemByIidResponseHandler],
],
{},
{ ...config.cacheConfig },
);
apolloProvider.clients.defaultClient.writeQuery({
query: workItemNotesQuery,
variables: {
id: workItemId,
pageSize: 100,
},
data: mockWorkItemNotesResponse.data,
});
const { id } = workItemQueryResponse.data.workItem;
wrapper = shallowMount(WorkItemAddNote, {
apolloProvider,
propsData: {
workItemId: id,
fullPath: 'test-project-path',
queryVariables,
fetchByIid,
workItemType,
},
stubs: {
WorkItemCommentLocked,
},
});
await waitForPromises();
if (isEditing) {
wrapper.findComponent(GlButton).vm.$emit('click');
}
};
describe('adding a comment', () => {
it('calls update widgets mutation', async () => {
const noteText = 'updated desc';
await createComponent({
isEditing: true,
signedIn: true,
});
findCommentForm().vm.$emit('submitForm', noteText);
await waitForPromises();
expect(mutationSuccessHandler).toHaveBeenCalledWith({
input: {
noteableId: workItemId,
body: noteText,
discussionId: null,
},
});
});
it('tracks adding comment', async () => {
await createComponent();
const trackingSpy = mockTracking(undefined, wrapper.element, jest.spyOn);
findCommentForm().vm.$emit('submitForm', 'test');
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,
queryVariables: {
id: mockWorkItemNotesResponse.data.workItem.id,
},
});
findCommentForm().vm.$emit('submitForm', 'some text');
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', 'some text');
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', 'updated desc');
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', 'updated desc');
await waitForPromises();
expect(wrapper.emitted('error')).toEqual([[error]]);
});
});
it('calls the global ID work item query when `fetchByIid` prop is false', async () => {
createComponent({ fetchByIid: false });
await waitForPromises();
expect(workItemResponseHandler).toHaveBeenCalled();
expect(workItemByIidResponseHandler).not.toHaveBeenCalled();
});
it('calls the IID work item query when when `fetchByIid` prop is true', async () => {
await createComponent({ fetchByIid: true, isEditing: false });
expect(workItemResponseHandler).not.toHaveBeenCalled();
expect(workItemByIidResponseHandler).toHaveBeenCalled();
});
it('skips calling the handlers when missing the needed queryVariables', async () => {
await createComponent({ queryVariables: {}, fetchByIid: false, isEditing: false });
expect(workItemResponseHandler).not.toHaveBeenCalled();
});
});
|