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

420 lines
12 KiB
JavaScript
Raw Normal View History

2021-04-29 21:17:54 +05:30
import { mount } from '@vue/test-utils';
import Vue from 'vue';
import Vuex from 'vuex';
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';
2021-03-11 19:13:27 +05:30
import UserAvatarLink from '~/vue_shared/components/user_avatar/user_avatar_link.vue';
2021-04-29 21:17:54 +05:30
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;
2020-06-23 00:09:42 +05:30
const findMultilineComment = () => wrapper.find('[data-testid="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);
2021-04-29 21:17:54 +05:30
wrapper = mount(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',
],
2019-07-07 11:18:12 +05:30
});
2021-04-17 20:07:23 +05:30
};
2018-03-17 18:26:18 +05:30
afterEach(() => {
2019-07-07 11:18:12 +05:30
wrapper.destroy();
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
});
2021-04-17 20:07:23 +05:30
await wrapper.vm.$nextTick();
expect(findMultilineComment().text()).toBe('Comment on lines 1 to 2');
2020-06-23 00:09:42 +05:30
});
2020-10-24 23:57:45 +05:30
it('should only render if it has everything it needs', () => {
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
});
return wrapper.vm.$nextTick().then(() => {
expect(findMultilineComment().exists()).toBe(false);
});
});
2020-06-23 00:09:42 +05:30
it('should not render if has single line comment', () => {
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
});
return wrapper.vm.$nextTick().then(() => {
expect(findMultilineComment().exists()).toBe(false);
});
});
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-04-17 20:07:23 +05:30
it('should render user information', () => {
const { author } = note;
const avatar = wrapper.findComponent(UserAvatarLink);
const avatarProps = avatar.props();
2018-03-17 18:26:18 +05:30
2021-04-17 20:07:23 +05:30
expect(avatarProps.linkHref).toBe(author.path);
expect(avatarProps.imgSrc).toBe(author.avatar_url);
expect(avatarProps.imgAlt).toBe(author.name);
expect(avatarProps.imgSize).toBe(40);
});
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);
});
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.reportAbusePath).toBe(note.report_abuse_path);
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', () => {
const noteBody = wrapper.findComponent(NoteBody);
const noteBodyProps = noteBody.props();
expect(noteBodyProps.note).toBe(note);
expect(noteBodyProps.line).toBe(null);
expect(noteBodyProps.canEdit).toBe(note.current_user.can_edit);
expect(noteBodyProps.isEditing).toBe(false);
expect(noteBodyProps.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(() => {});
const noteBodyComponent = wrapper.findComponent(NoteBody);
store.hotUpdate({
2021-04-29 21:17:54 +05:30
modules: {
notes: {
actions: {
updateNote() {},
setSelectedCommentPositionHover() {},
},
},
2021-04-17 20:07:23 +05:30
},
});
2021-10-27 15:23:28 +05:30
noteBodyComponent.vm.$emit('handleFormUpdate', {
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();
2021-09-30 23:02:18 +05:30
expect(wrapper.vm.note.note_html).toBe(
'<p><img src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7"></p>\n',
);
2020-04-22 19:07:51 +05:30
});
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
});
2021-04-17 20:07:23 +05:30
const noteBody = wrapper.findComponent(NoteBody);
2019-07-07 11:18:12 +05:30
noteBody.vm.resetAutoSave = () => {};
2021-10-27 15:23:28 +05:30
noteBody.vm.$emit('handleFormUpdate', {
noteText: updatedText,
parentElement: null,
callback: () => {},
});
2019-07-07 11:18:12 +05:30
2021-04-17 20:07:23 +05:30
await wrapper.vm.$nextTick();
let noteBodyProps = noteBody.props();
2021-09-30 23:02:18 +05:30
expect(noteBodyProps.note.note_html).toBe(`<p>${updatedText}</p>\n`);
2021-04-17 20:07:23 +05:30
2021-10-27 15:23:28 +05:30
noteBody.vm.$emit('cancelForm', {});
2021-04-17 20:07:23 +05:30
await wrapper.vm.$nextTick();
noteBodyProps = noteBody.props();
expect(noteBodyProps.note.note_html).toBe(note.note_html);
});
});
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();
2021-10-27 15:23:28 +05:30
wrapper.findComponent(NoteBody).vm.$emit('handleFormUpdate', params);
2021-04-17 20:07:23 +05:30
expect(wrapper.emitted('handleUpdateNote')).toBeTruthy();
});
it('does not stringify empty position', () => {
createWrapper();
updateActions();
2021-10-27 15:23:28 +05:30
wrapper.findComponent(NoteBody).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();
2021-10-27 15:23:28 +05:30
wrapper.findComponent(NoteBody).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);
expect(wrapper.vm.diffFile).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(wrapper.vm.diffFile.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(wrapper.vm.diffFile.testId).toBe('diffFileTest');
});
});
2018-03-17 18:26:18 +05:30
});