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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
|
import Vue, { nextTick } from 'vue';
import Vuex from 'vuex';
import { GlAvatarLink, GlAvatar } from '@gitlab/ui';
import { clone } from 'lodash';
import { mountExtended } from 'helpers/vue_test_utils_helper';
import waitForPromises from 'helpers/wait_for_promises';
import DiffsModule from '~/diffs/store/modules';
import NoteActions from '~/notes/components/note_actions.vue';
import NoteBody from '~/notes/components/note_body.vue';
import NoteHeader from '~/notes/components/note_header.vue';
import issueNote from '~/notes/components/noteable_note.vue';
import NotesModule from '~/notes/stores/modules';
import { NOTEABLE_TYPE_MAPPING } from '~/notes/constants';
import { createAlert } from '~/alert';
import { UPDATE_COMMENT_FORM } from '~/notes/i18n';
import { sprintf } from '~/locale';
import { noteableDataMock, notesDataMock, note } from '../mock_data';
Vue.use(Vuex);
jest.mock('~/alert');
const singleLineNotePosition = {
line_range: {
start: {
line_code: 'abc_1_1',
type: null,
old_line: '1',
new_line: '1',
},
end: {
line_code: 'abc_1_1',
type: null,
old_line: '1',
new_line: '1',
},
},
};
describe('issue_note', () => {
let store;
let wrapper;
const REPORT_ABUSE_PATH = '/abuse_reports/add_category';
const findNoteBody = () => wrapper.findComponent(NoteBody);
const findMultilineComment = () => wrapper.findByTestId('multiline-comment');
const createWrapper = (props = {}, storeUpdater = (s) => s) => {
store = new Vuex.Store(
storeUpdater({
modules: {
notes: NotesModule(),
diffs: DiffsModule(),
},
}),
);
store.dispatch('setNoteableData', noteableDataMock);
store.dispatch('setNotesData', notesDataMock);
// the component overwrites the `note` prop with every action, hence create a copy
const noteCopy = clone(props.note || note);
wrapper = mountExtended(issueNote, {
store,
propsData: {
note: noteCopy,
...props,
},
stubs: [
'note-header',
'user-avatar-link',
'note-actions',
'note-body',
'multiline-comment-form',
],
provide: {
reportAbusePath: REPORT_ABUSE_PATH,
},
});
};
describe('mutiline comments', () => {
beforeEach(() => {
createWrapper();
});
it('should render if has multiline comment', async () => {
const position = {
line_range: {
start: {
line_code: 'abc_1_1',
type: null,
old_line: '1',
new_line: '1',
},
end: {
line_code: 'abc_2_2',
type: null,
old_line: '2',
new_line: '2',
},
},
};
const line = {
line_code: 'abc_1_1',
type: null,
old_line: '1',
new_line: '1',
};
wrapper.setProps({
note: { ...note, position },
discussionRoot: true,
line,
});
await nextTick();
expect(findMultilineComment().text()).toBe('Comment on lines 1 to 2');
});
it('should only render if it has everything it needs', async () => {
const position = {
line_range: {
start: {
line_code: 'abc_1_1',
type: null,
old_line: '',
new_line: '',
},
end: {
line_code: 'abc_2_2',
type: null,
old_line: '2',
new_line: '2',
},
},
};
const line = {
line_code: 'abc_1_1',
type: null,
old_line: '1',
new_line: '1',
};
wrapper.setProps({
note: { ...note, position },
discussionRoot: true,
line,
});
await nextTick();
expect(findMultilineComment().exists()).toBe(false);
});
it('should not render if has single line comment', async () => {
const position = {
line_range: {
start: {
line_code: 'abc_1_1',
type: null,
old_line: '1',
new_line: '1',
},
end: {
line_code: 'abc_1_1',
type: null,
old_line: '1',
new_line: '1',
},
},
};
const line = {
line_code: 'abc_1_1',
type: null,
old_line: '1',
new_line: '1',
};
wrapper.setProps({
note: { ...note, position },
discussionRoot: true,
line,
});
await nextTick();
expect(findMultilineComment().exists()).toBe(false);
});
it('should not render if `line_range` is unavailable', () => {
expect(findMultilineComment().exists()).toBe(false);
});
});
describe('rendering', () => {
beforeEach(() => {
createWrapper();
});
describe('avatar sizes in diffs', () => {
const line = {
line_code: 'abc_1_1',
type: null,
old_line: '1',
new_line: '1',
};
it('should render 24px avatars', async () => {
wrapper.setProps({
note: { ...note },
discussionRoot: true,
line,
});
await nextTick();
const avatar = wrapper.findComponent(GlAvatar);
const avatarProps = avatar.props();
expect(avatarProps.size).toBe(24);
});
});
it('should render user avatar link with popover support', () => {
const { author } = note;
const avatarLink = wrapper.findComponent(GlAvatarLink);
expect(avatarLink.classes()).toContain('js-user-link');
expect(avatarLink.attributes()).toMatchObject({
href: author.path,
'data-user-id': `${author.id}`,
'data-username': `${author.username}`,
});
});
it('should render user avatar', () => {
const { author } = note;
const avatar = wrapper.findComponent(GlAvatar);
const avatarProps = avatar.props();
expect(avatarProps.src).toBe(author.avatar_url);
expect(avatarProps.entityName).toBe(author.username);
expect(avatarProps.alt).toBe(author.name);
expect(avatarProps.size).toEqual(32);
});
it('should render note header content', () => {
const noteHeader = wrapper.findComponent(NoteHeader);
const noteHeaderProps = noteHeader.props();
expect(noteHeaderProps.author).toBe(note.author);
expect(noteHeaderProps.createdAt).toBe(note.created_at);
expect(noteHeaderProps.noteId).toBe(note.id);
expect(noteHeaderProps.noteableType).toBe(NOTEABLE_TYPE_MAPPING[note.noteable_type]);
});
it('should render note actions', () => {
const { author } = note;
const noteActions = wrapper.findComponent(NoteActions);
const noteActionsProps = noteActions.props();
expect(noteActionsProps.authorId).toBe(author.id);
expect(noteActionsProps.noteId).toBe(note.id);
expect(noteActionsProps.noteUrl).toBe(note.noteable_note_url);
expect(noteActionsProps.accessLevel).toBe(note.human_access);
expect(noteActionsProps.canEdit).toBe(note.current_user.can_edit);
expect(noteActionsProps.canAwardEmoji).toBe(note.current_user.can_award_emoji);
expect(noteActionsProps.canDelete).toBe(note.current_user.can_edit);
expect(noteActionsProps.canReportAsAbuse).toBe(true);
expect(noteActionsProps.canResolve).toBe(false);
expect(noteActionsProps.resolvable).toBe(false);
expect(noteActionsProps.isResolved).toBe(false);
expect(noteActionsProps.isResolving).toBe(false);
expect(noteActionsProps.resolvedBy).toEqual({});
});
it('should render issue body', () => {
expect(findNoteBody().props().note).toMatchObject(note);
expect(findNoteBody().props().line).toBe(null);
expect(findNoteBody().props().canEdit).toBe(note.current_user.can_edit);
expect(findNoteBody().props().isEditing).toBe(false);
expect(findNoteBody().props().helpPagePath).toBe('');
});
it('prevents note preview xss', async () => {
const noteBody =
'<img src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onload="alert(1)" />';
const alertSpy = jest.spyOn(window, 'alert').mockImplementation(() => {});
store.hotUpdate({
modules: {
notes: {
actions: {
updateNote() {},
setSelectedCommentPositionHover() {},
},
},
},
});
findNoteBody().vm.$emit('handleFormUpdate', {
noteText: noteBody,
parentElement: null,
callback: () => {},
});
await waitForPromises();
expect(alertSpy).not.toHaveBeenCalled();
expect(findNoteBody().props().note.note_html).toBe(
'<img src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7">',
);
});
});
describe('internal note', () => {
it('has internal note class for internal notes', () => {
createWrapper({ note: { ...note, internal: true } });
expect(wrapper.classes()).toContain('internal-note');
});
it('does not have internal note class for external notes', () => {
createWrapper();
expect(wrapper.classes()).not.toContain('internal-note');
});
});
describe('cancel edit', () => {
beforeEach(() => {
createWrapper();
});
it('restores content of updated note', async () => {
const updatedText = 'updated note text';
store.hotUpdate({
modules: {
notes: {
actions: {
updateNote() {},
},
},
},
});
findNoteBody().vm.$emit('handleFormUpdate', {
noteText: updatedText,
parentElement: null,
callback: () => {},
});
await nextTick();
expect(findNoteBody().props().note.note_html).toBe(`<p dir="auto">${updatedText}</p>\n`);
findNoteBody().vm.$emit('cancelForm', {});
await nextTick();
expect(findNoteBody().props().note.note_html).toBe(note.note_html);
});
});
describe('formUpdateHandler', () => {
const updateNote = jest.fn();
const params = {
noteText: 'updated note text',
parentElement: null,
callback: jest.fn(),
resolveDiscussion: false,
};
const updateActions = () => {
store.hotUpdate({
modules: {
notes: {
actions: {
updateNote,
setSelectedCommentPositionHover() {},
},
},
},
});
};
beforeEach(() => {
createWrapper();
updateActions();
});
afterEach(() => updateNote.mockReset());
it('emits handleUpdateNote', () => {
const updatedNote = { ...note, note_html: `<p dir="auto">${params.noteText}</p>\n` };
findNoteBody().vm.$emit('handleFormUpdate', params);
expect(wrapper.emitted('handleUpdateNote')).toHaveLength(1);
expect(wrapper.emitted('handleUpdateNote')[0]).toEqual([
{
note: updatedNote,
noteText: params.noteText,
resolveDiscussion: params.resolveDiscussion,
position: {},
flashContainer: wrapper.vm.$el,
callback: expect.any(Function),
errorCallback: expect.any(Function),
},
]);
});
it('updates note content', async () => {
findNoteBody().vm.$emit('handleFormUpdate', params);
await nextTick();
expect(findNoteBody().props().note.note_html).toBe(`<p dir="auto">${params.noteText}</p>\n`);
expect(findNoteBody().props('isEditing')).toBe(false);
});
it('should not update note with sensitive token', () => {
const sensitiveMessage = 'token: glpat-1234567890abcdefghij';
findNoteBody().vm.$emit('handleFormUpdate', { ...params, noteText: sensitiveMessage });
expect(updateNote).not.toHaveBeenCalled();
});
it('does not stringify empty position', () => {
findNoteBody().vm.$emit('handleFormUpdate', params);
expect(updateNote.mock.calls[0][1].note.note.position).toBeUndefined();
});
it('stringifies populated position', () => {
const position = { test: true };
const expectation = JSON.stringify(position);
createWrapper({ note: { ...note, position } });
updateActions();
findNoteBody().vm.$emit('handleFormUpdate', params);
expect(updateNote.mock.calls[0][1].note.note.position).toBe(expectation);
});
describe('when updateNote returns errors', () => {
beforeEach(() => {
updateNote.mockRejectedValue({
response: { status: 422, data: { errors: 'error 1 and error 2' } },
});
});
beforeEach(() => {
findNoteBody().vm.$emit('handleFormUpdate', { ...params, noteText: 'invalid note' });
});
it('renders error message and restores content of updated note', async () => {
await waitForPromises();
expect(createAlert).toHaveBeenCalledWith({
message: sprintf(UPDATE_COMMENT_FORM.error, { reason: 'error 1 and error 2' }, false),
parent: wrapper.vm.$el,
});
expect(findNoteBody().props('isEditing')).toBe(true);
expect(findNoteBody().props().note.note_html).toBe(note.note_html);
});
});
});
describe('diffFile', () => {
it.each`
scenario | files | noteDef
${'the note has no position'} | ${undefined} | ${note}
${'the Diffs store has no data'} | ${[]} | ${{ ...note, position: singleLineNotePosition }}
`(
'returns `null` when $scenario and no diff file is provided as a prop',
({ noteDef, diffs }) => {
const storeUpdater = (rawStore) => {
const updatedStore = { ...rawStore };
if (diffs) {
updatedStore.modules.diffs.state.diffFiles = diffs;
}
return updatedStore;
};
createWrapper({ note: noteDef, discussionFile: null }, storeUpdater);
expect(findNoteBody().props().file).toBe(null);
},
);
it("returns the correct diff file from the Diffs store if it's available", () => {
createWrapper(
{
note: { ...note, position: singleLineNotePosition },
},
(rawStore) => {
const updatedStore = { ...rawStore };
updatedStore.modules.diffs.state.diffFiles = [
{ file_hash: 'abc', testId: 'diffFileTest' },
];
return updatedStore;
},
);
expect(findNoteBody().props().file.testId).toBe('diffFileTest');
});
it('returns the provided diff file if the more robust getters fail', () => {
createWrapper(
{
note: { ...note, position: singleLineNotePosition },
discussionFile: { testId: 'diffFileTest' },
},
(rawStore) => {
const updatedStore = { ...rawStore };
updatedStore.modules.diffs.state.diffFiles = [];
return updatedStore;
},
);
expect(findNoteBody().props().file.testId).toBe('diffFileTest');
});
});
});
|