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

248 lines
7.3 KiB
JavaScript
Raw Normal View History

2020-04-08 14:13:33 +05:30
import { escape } from 'lodash';
2020-06-23 00:09:42 +05:30
import { mount, createLocalVue } from '@vue/test-utils';
2018-11-08 19:23:39 +05:30
import createStore from '~/notes/stores';
2018-03-17 18:26:18 +05:30
import issueNote from '~/notes/components/noteable_note.vue';
2019-07-07 11:18:12 +05:30
import NoteHeader from '~/notes/components/note_header.vue';
import UserAvatarLink from '~/vue_shared/components/user_avatar/user_avatar_link.vue';
import NoteActions from '~/notes/components/note_actions.vue';
import NoteBody from '~/notes/components/note_body.vue';
2018-03-17 18:26:18 +05:30
import { noteableDataMock, notesDataMock, note } from '../mock_data';
2020-06-23 00:09:42 +05:30
jest.mock('~/vue_shared/mixins/gl_feature_flags_mixin', () => () => ({
inject: {
glFeatures: {
from: 'glFeatures',
default: () => ({ multilineComments: true }),
},
},
}));
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
beforeEach(() => {
2018-11-08 19:23:39 +05:30
store = createStore();
2018-03-17 18:26:18 +05:30
store.dispatch('setNoteableData', noteableDataMock);
store.dispatch('setNotesData', notesDataMock);
2019-07-07 11:18:12 +05:30
const localVue = createLocalVue();
2020-06-23 00:09:42 +05:30
wrapper = mount(localVue.extend(issueNote), {
2018-03-17 18:26:18 +05:30
store,
propsData: {
note,
},
2019-07-07 11:18:12 +05:30
localVue,
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
});
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', () => {
it('should render if has multiline 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_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
});
return wrapper.vm.$nextTick().then(() => {
expect(findMultilineComment().text()).toEqual('Comment on lines 1 to 2');
});
});
2020-07-28 23:09:34 +05:30
it('should render multiline comment if editing discussion root', () => {
wrapper.setProps({ discussionRoot: true });
wrapper.vm.isEditing = true;
return wrapper.vm.$nextTick().then(() => {
expect(findMultilineComment().exists()).toBe(true);
});
});
it('should not render multiline comment form unless it is the discussion root', () => {
wrapper.setProps({ discussionRoot: false });
wrapper.vm.isEditing = true;
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);
});
});
2018-03-17 18:26:18 +05:30
it('should render user information', () => {
2019-07-07 11:18:12 +05:30
const { author } = note;
const avatar = wrapper.find(UserAvatarLink);
const avatarProps = avatar.props();
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-03-17 18:26:18 +05:30
});
it('should render note header content', () => {
2019-07-07 11:18:12 +05:30
const noteHeader = wrapper.find(NoteHeader);
const noteHeaderProps = noteHeader.props();
2018-12-13 13:39:08 +05:30
2019-07-07 11:18:12 +05:30
expect(noteHeaderProps.author).toEqual(note.author);
expect(noteHeaderProps.createdAt).toEqual(note.created_at);
expect(noteHeaderProps.noteId).toEqual(note.id);
2018-03-17 18:26:18 +05:30
});
it('should render note actions', () => {
2019-07-07 11:18:12 +05:30
const { author } = note;
const noteActions = wrapper.find(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.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
});
it('should render issue body', () => {
2019-07-07 11:18:12 +05:30
const noteBody = wrapper.find(NoteBody);
const noteBodyProps = noteBody.props();
expect(noteBodyProps.note).toEqual(note);
expect(noteBodyProps.line).toBe(null);
expect(noteBodyProps.canEdit).toBe(note.current_user.can_edit);
expect(noteBodyProps.isEditing).toBe(false);
expect(noteBodyProps.helpPagePath).toBe('');
2018-03-17 18:26:18 +05:30
});
2018-11-08 19:23:39 +05:30
it('prevents note preview xss', done => {
2018-03-17 18:26:18 +05:30
const imgSrc = 'data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7';
const noteBody = `<img src="${imgSrc}" onload="alert(1)" />`;
2020-04-22 19:07:51 +05:30
const alertSpy = jest.spyOn(window, 'alert');
2019-07-07 11:18:12 +05:30
store.hotUpdate({
actions: {
updateNote() {},
2020-07-28 23:09:34 +05:30
setSelectedCommentPositionHover() {},
2019-07-07 11:18:12 +05:30
},
});
const noteBodyComponent = wrapper.find(NoteBody);
2018-03-17 18:26:18 +05:30
2019-07-07 11:18:12 +05:30
noteBodyComponent.vm.$emit('handleFormUpdate', noteBody, null, () => {});
2018-03-17 18:26:18 +05:30
2020-04-22 19:07:51 +05:30
setImmediate(() => {
2018-03-17 18:26:18 +05:30
expect(alertSpy).not.toHaveBeenCalled();
2020-04-08 14:13:33 +05:30
expect(wrapper.vm.note.note_html).toEqual(escape(noteBody));
2018-03-17 18:26:18 +05:30
done();
2020-04-22 19:07:51 +05:30
});
2018-03-17 18:26:18 +05:30
});
describe('cancel edit', () => {
2018-11-08 19:23:39 +05:30
it('restores content of updated note', done => {
2019-07-07 11:18:12 +05:30
const updatedText = 'updated note text';
store.hotUpdate({
actions: {
updateNote() {},
},
2018-03-17 18:26:18 +05:30
});
2019-07-07 11:18:12 +05:30
const noteBody = wrapper.find(NoteBody);
noteBody.vm.resetAutoSave = () => {};
noteBody.vm.$emit('handleFormUpdate', updatedText, null, () => {});
wrapper.vm
.$nextTick()
.then(() => {
const noteBodyProps = noteBody.props();
expect(noteBodyProps.note.note_html).toBe(updatedText);
noteBody.vm.$emit('cancelForm');
})
.then(() => wrapper.vm.$nextTick())
.then(() => {
const noteBodyProps = noteBody.props();
expect(noteBodyProps.note.note_html).toBe(note.note_html);
})
.then(done)
.catch(done.fail);
2018-03-17 18:26:18 +05:30
});
});
});