debian-mirror-gitlab/spec/frontend/notes/components/noteable_note_spec.js

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

455 lines
13 KiB
JavaScript
Raw Normal View History

2022-04-04 11:22:00 +05:30
import Vue, { nextTick } from 'vue';
2021-04-29 21:17:54 +05:30
import Vuex from 'vuex';
2022-08-13 15:12:31 +05:30
import { GlAvatar } from '@gitlab/ui';
2023-07-09 08:55:56 +05:30
import { mountExtended } from 'helpers/vue_test_utils_helper';
2021-04-17 20:07:23 +05:30
import waitForPromises from 'helpers/wait_for_promises';
2021-04-29 21:17:54 +05:30
import DiffsModule from '~/diffs/store/modules';
2019-07-07 11:18:12 +05:30
import NoteActions from '~/notes/components/note_actions.vue';
import NoteBody from '~/notes/components/note_body.vue';
2021-03-11 19:13:27 +05:30
import NoteHeader from '~/notes/components/note_header.vue';
import issueNote from '~/notes/components/noteable_note.vue';
2021-04-29 21:17:54 +05:30
import NotesModule from '~/notes/stores/modules';
2022-06-21 17:19:12 +05:30
import { NOTEABLE_TYPE_MAPPING } from '~/notes/constants';
2018-03-17 18:26:18 +05:30
import { noteableDataMock, notesDataMock, note } from '../mock_data';
2021-04-29 21:17:54 +05:30
Vue.use(Vuex);
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',
},
},
};
2018-03-17 18:26:18 +05:30
describe('issue_note', () => {
2018-11-08 19:23:39 +05:30
let store;
2019-07-07 11:18:12 +05:30
let wrapper;
2023-04-23 21:23:45 +05:30
const REPORT_ABUSE_PATH = '/abuse_reports/add_category';
2023-07-09 08:55:56 +05:30
const findNoteBody = () => wrapper.findComponent(NoteBody);
const findMultilineComment = () => wrapper.findByTestId('multiline-comment');
2018-03-17 18:26:18 +05:30
2021-04-29 21:17:54 +05:30
const createWrapper = (props = {}, storeUpdater = (s) => s) => {
store = new Vuex.Store(
storeUpdater({
modules: {
notes: NotesModule(),
diffs: DiffsModule(),
},
}),
);
2018-03-17 18:26:18 +05:30
store.dispatch('setNoteableData', noteableDataMock);
store.dispatch('setNotesData', notesDataMock);
2023-07-09 08:55:56 +05:30
wrapper = mountExtended(issueNote, {
2018-03-17 18:26:18 +05:30
store,
propsData: {
note,
2021-04-17 20:07:23 +05:30
...props,
2018-03-17 18:26:18 +05:30
},
2020-07-28 23:09:34 +05:30
stubs: [
'note-header',
'user-avatar-link',
'note-actions',
'note-body',
'multiline-comment-form',
],
2023-04-23 21:23:45 +05:30
provide: {
reportAbusePath: REPORT_ABUSE_PATH,
},
2019-07-07 11:18:12 +05:30
});
2021-04-17 20:07:23 +05:30
};
2018-03-17 18:26:18 +05:30
2020-06-23 00:09:42 +05:30
describe('mutiline comments', () => {
2021-04-17 20:07:23 +05:30
beforeEach(() => {
createWrapper();
});
it('should render if has multiline comment', async () => {
2020-06-23 00:09:42 +05:30
const position = {
line_range: {
2020-07-28 23:09:34 +05:30
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',
},
2020-06-23 00:09:42 +05:30
},
};
2020-07-28 23:09:34 +05:30
const line = {
line_code: 'abc_1_1',
type: null,
old_line: '1',
new_line: '1',
};
2020-06-23 00:09:42 +05:30
wrapper.setProps({
note: { ...note, position },
2020-07-28 23:09:34 +05:30
discussionRoot: true,
line,
2020-06-23 00:09:42 +05:30
});
2022-04-04 11:22:00 +05:30
await nextTick();
2021-04-17 20:07:23 +05:30
expect(findMultilineComment().text()).toBe('Comment on lines 1 to 2');
2020-06-23 00:09:42 +05:30
});
2022-04-04 11:22:00 +05:30
it('should only render if it has everything it needs', async () => {
2020-10-24 23:57:45 +05:30
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,
2020-07-28 23:09:34 +05:30
});
2022-04-04 11:22:00 +05:30
await nextTick();
expect(findMultilineComment().exists()).toBe(false);
2020-07-28 23:09:34 +05:30
});
2022-04-04 11:22:00 +05:30
it('should not render if has single line comment', async () => {
2020-06-23 00:09:42 +05:30
const position = {
line_range: {
2020-07-28 23:09:34 +05:30
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',
},
2020-06-23 00:09:42 +05:30
},
};
2020-07-28 23:09:34 +05:30
const line = {
line_code: 'abc_1_1',
type: null,
old_line: '1',
new_line: '1',
};
2020-06-23 00:09:42 +05:30
wrapper.setProps({
note: { ...note, position },
2020-07-28 23:09:34 +05:30
discussionRoot: true,
line,
2020-06-23 00:09:42 +05:30
});
2022-04-04 11:22:00 +05:30
await nextTick();
expect(findMultilineComment().exists()).toBe(false);
2020-06-23 00:09:42 +05:30
});
it('should not render if `line_range` is unavailable', () => {
expect(findMultilineComment().exists()).toBe(false);
});
});
2021-04-17 20:07:23 +05:30
describe('rendering', () => {
beforeEach(() => {
createWrapper();
});
2019-07-07 11:18:12 +05:30
2021-11-18 22:05:49 +05:30
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,
});
2022-04-04 11:22:00 +05:30
await nextTick();
2021-11-18 22:05:49 +05:30
2022-08-13 15:12:31 +05:30
const avatar = wrapper.findComponent(GlAvatar);
const avatarProps = avatar.props();
expect(avatarProps.size).toBe(24);
2021-11-18 22:05:49 +05:30
});
});
2022-08-13 15:12:31 +05:30
it('should render user avatar', () => {
2021-04-17 20:07:23 +05:30
const { author } = note;
2022-08-13 15:12:31 +05:30
const avatar = wrapper.findComponent(GlAvatar);
2021-04-17 20:07:23 +05:30
const avatarProps = avatar.props();
2018-03-17 18:26:18 +05:30
2022-08-13 15:12:31 +05:30
expect(avatarProps.src).toBe(author.avatar_url);
expect(avatarProps.entityName).toBe(author.username);
expect(avatarProps.alt).toBe(author.name);
2022-11-25 23:54:43 +05:30
expect(avatarProps.size).toEqual(32);
2021-04-17 20:07:23 +05:30
});
2018-12-13 13:39:08 +05:30
2021-04-17 20:07:23 +05:30
it('should render note header content', () => {
const noteHeader = wrapper.findComponent(NoteHeader);
const noteHeaderProps = noteHeader.props();
2018-03-17 18:26:18 +05:30
2021-04-17 20:07:23 +05:30
expect(noteHeaderProps.author).toBe(note.author);
expect(noteHeaderProps.createdAt).toBe(note.created_at);
expect(noteHeaderProps.noteId).toBe(note.id);
2022-06-21 17:19:12 +05:30
expect(noteHeaderProps.noteableType).toBe(NOTEABLE_TYPE_MAPPING[note.noteable_type]);
2021-04-17 20:07:23 +05:30
});
2018-03-17 18:26:18 +05:30
2021-04-17 20:07:23 +05:30
it('should render note actions', () => {
const { author } = note;
const noteActions = wrapper.findComponent(NoteActions);
const noteActionsProps = noteActions.props();
2019-07-07 11:18:12 +05:30
2021-04-17 20:07:23 +05:30
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({});
});
2018-03-17 18:26:18 +05:30
2021-04-17 20:07:23 +05:30
it('should render issue body', () => {
2023-07-09 08:55:56 +05:30
expect(findNoteBody().props().note).toBe(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('');
2019-07-07 11:18:12 +05:30
});
2018-03-17 18:26:18 +05:30
2021-04-17 20:07:23 +05:30
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({
2021-04-29 21:17:54 +05:30
modules: {
notes: {
actions: {
updateNote() {},
setSelectedCommentPositionHover() {},
},
},
2021-04-17 20:07:23 +05:30
},
});
2023-07-09 08:55:56 +05:30
findNoteBody().vm.$emit('handleFormUpdate', {
2021-10-27 15:23:28 +05:30
noteText: noteBody,
parentElement: null,
callback: () => {},
});
2018-03-17 18:26:18 +05:30
2021-04-17 20:07:23 +05:30
await waitForPromises();
2018-03-17 18:26:18 +05:30
expect(alertSpy).not.toHaveBeenCalled();
2023-07-09 08:55:56 +05:30
expect(findNoteBody().props().note.note_html).toBe(
2022-08-27 11:52:29 +05:30
'<img src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7">',
2021-09-30 23:02:18 +05:30
);
2020-04-22 19:07:51 +05:30
});
2018-03-17 18:26:18 +05:30
});
2022-08-27 11:52:29 +05:30
describe('internal note', () => {
it('has internal note class for internal notes', () => {
2022-10-11 01:57:18 +05:30
createWrapper({ note: { ...note, internal: true } });
2022-08-27 11:52:29 +05:30
expect(wrapper.classes()).toContain('internal-note');
});
it('does not have internal note class for external notes', () => {
createWrapper({ note });
expect(wrapper.classes()).not.toContain('internal-note');
});
});
2018-03-17 18:26:18 +05:30
describe('cancel edit', () => {
2021-04-17 20:07:23 +05:30
beforeEach(() => {
createWrapper();
});
it('restores content of updated note', async () => {
2019-07-07 11:18:12 +05:30
const updatedText = 'updated note text';
store.hotUpdate({
2021-04-29 21:17:54 +05:30
modules: {
notes: {
actions: {
updateNote() {},
},
},
2019-07-07 11:18:12 +05:30
},
2018-03-17 18:26:18 +05:30
});
2019-07-07 11:18:12 +05:30
2023-07-09 08:55:56 +05:30
findNoteBody().vm.$emit('handleFormUpdate', {
2021-10-27 15:23:28 +05:30
noteText: updatedText,
parentElement: null,
callback: () => {},
});
2019-07-07 11:18:12 +05:30
2022-04-04 11:22:00 +05:30
await nextTick();
2021-04-17 20:07:23 +05:30
2023-07-09 08:55:56 +05:30
expect(findNoteBody().props().note.note_html).toBe(`<p dir="auto">${updatedText}</p>\n`);
2021-04-17 20:07:23 +05:30
2023-07-09 08:55:56 +05:30
findNoteBody().vm.$emit('cancelForm', {});
2022-04-04 11:22:00 +05:30
await nextTick();
2021-04-17 20:07:23 +05:30
2023-07-09 08:55:56 +05:30
expect(findNoteBody().props().note.note_html).toBe(note.note_html);
2021-04-17 20:07:23 +05:30
});
});
describe('formUpdateHandler', () => {
const updateNote = jest.fn();
2021-10-27 15:23:28 +05:30
const params = {
noteText: '',
parentElement: null,
callback: jest.fn(),
resolveDiscussion: false,
};
2021-04-17 20:07:23 +05:30
const updateActions = () => {
store.hotUpdate({
2021-04-29 21:17:54 +05:30
modules: {
notes: {
actions: {
updateNote,
setSelectedCommentPositionHover() {},
},
},
2021-04-17 20:07:23 +05:30
},
});
};
afterEach(() => updateNote.mockReset());
it('responds to handleFormUpdate', () => {
createWrapper();
updateActions();
2023-07-09 08:55:56 +05:30
findNoteBody().vm.$emit('handleFormUpdate', params);
2022-08-27 11:52:29 +05:30
expect(wrapper.emitted('handleUpdateNote')).toHaveLength(1);
2021-04-17 20:07:23 +05:30
});
2023-06-20 00:43:36 +05:30
it('should not update note with sensitive token', () => {
const sensitiveMessage = 'token: glpat-1234567890abcdefghij';
createWrapper();
updateActions();
2023-07-09 08:55:56 +05:30
findNoteBody().vm.$emit('handleFormUpdate', { ...params, noteText: sensitiveMessage });
2023-06-20 00:43:36 +05:30
expect(updateNote).not.toHaveBeenCalled();
});
2021-04-17 20:07:23 +05:30
it('does not stringify empty position', () => {
createWrapper();
updateActions();
2023-07-09 08:55:56 +05:30
findNoteBody().vm.$emit('handleFormUpdate', params);
2021-04-17 20:07:23 +05:30
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();
2023-07-09 08:55:56 +05:30
findNoteBody().vm.$emit('handleFormUpdate', params);
2021-04-17 20:07:23 +05:30
expect(updateNote.mock.calls[0][1].note.note.position).toBe(expectation);
2018-03-17 18:26:18 +05:30
});
});
2021-04-29 21:17:54 +05:30
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);
2023-07-09 08:55:56 +05:30
expect(findNoteBody().props().file).toBe(null);
2021-04-29 21:17:54 +05:30
},
);
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;
},
);
2023-07-09 08:55:56 +05:30
expect(findNoteBody().props().file.testId).toBe('diffFileTest');
2021-04-29 21:17:54 +05:30
});
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;
},
);
2023-07-09 08:55:56 +05:30
expect(findNoteBody().props().file.testId).toBe('diffFileTest');
2021-04-29 21:17:54 +05:30
});
});
2018-03-17 18:26:18 +05:30
});