2021-03-11 19:13:27 +05:30
|
|
|
import { GlLoadingIcon } from '@gitlab/ui';
|
2022-04-04 11:22:00 +05:30
|
|
|
import { shallowMount } from '@vue/test-utils';
|
|
|
|
import Vue from 'vue';
|
2019-12-04 20:38:33 +05:30
|
|
|
import Vuex from 'vuex';
|
|
|
|
import DiffContentComponent from '~/diffs/components/diff_content.vue';
|
2021-03-11 19:13:27 +05:30
|
|
|
import DiffDiscussions from '~/diffs/components/diff_discussions.vue';
|
|
|
|
import DiffView from '~/diffs/components/diff_view.vue';
|
2019-12-04 20:38:33 +05:30
|
|
|
import { IMAGE_DIFF_POSITION_TYPE } from '~/diffs/constants';
|
|
|
|
import { diffViewerModes } from '~/ide/constants';
|
2021-03-11 19:13:27 +05:30
|
|
|
import NoteForm from '~/notes/components/note_form.vue';
|
|
|
|
import NoPreviewViewer from '~/vue_shared/components/diff_viewer/viewers/no_preview.vue';
|
|
|
|
import NotDiffableViewer from '~/vue_shared/components/diff_viewer/viewers/not_diffable.vue';
|
|
|
|
import diffFileMockData from '../mock_data/diff_file';
|
2019-12-04 20:38:33 +05:30
|
|
|
|
2022-04-04 11:22:00 +05:30
|
|
|
Vue.use(Vuex);
|
2019-12-04 20:38:33 +05:30
|
|
|
|
|
|
|
describe('DiffContent', () => {
|
|
|
|
let wrapper;
|
|
|
|
|
|
|
|
const saveDiffDiscussionMock = jest.fn();
|
|
|
|
const closeDiffFileCommentFormMock = jest.fn();
|
|
|
|
|
|
|
|
const noteableTypeGetterMock = jest.fn();
|
|
|
|
const getUserDataGetterMock = jest.fn();
|
|
|
|
|
|
|
|
const isInlineViewGetterMock = jest.fn();
|
|
|
|
const isParallelViewGetterMock = jest.fn();
|
|
|
|
const getCommentFormForDiffFileGetterMock = jest.fn();
|
|
|
|
|
|
|
|
const defaultProps = {
|
|
|
|
diffFile: JSON.parse(JSON.stringify(diffFileMockData)),
|
|
|
|
};
|
|
|
|
|
2021-01-29 00:20:46 +05:30
|
|
|
const createComponent = ({ props, state, provide } = {}) => {
|
2019-12-04 20:38:33 +05:30
|
|
|
const fakeStore = new Vuex.Store({
|
|
|
|
getters: {
|
|
|
|
getNoteableData() {
|
|
|
|
return {
|
|
|
|
current_user: {
|
|
|
|
can_create_note: true,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
},
|
|
|
|
noteableType: noteableTypeGetterMock,
|
|
|
|
getUserData: getUserDataGetterMock,
|
|
|
|
},
|
|
|
|
modules: {
|
|
|
|
/*
|
|
|
|
we need extra batchComments since vue-test-utils does not
|
|
|
|
stub async components properly
|
|
|
|
*/
|
|
|
|
batchComments: {
|
|
|
|
namespaced: true,
|
|
|
|
getters: {
|
|
|
|
draftsForFile: () => () => true,
|
2021-01-29 00:20:46 +05:30
|
|
|
draftForLine: () => () => true,
|
|
|
|
shouldRenderDraftRow: () => () => true,
|
|
|
|
hasParallelDraftLeft: () => () => true,
|
|
|
|
hasParallelDraftRight: () => () => true,
|
2019-12-04 20:38:33 +05:30
|
|
|
},
|
|
|
|
},
|
|
|
|
diffs: {
|
|
|
|
namespaced: true,
|
|
|
|
state: {
|
|
|
|
projectPath: 'project/path',
|
|
|
|
endpoint: 'endpoint',
|
|
|
|
...state,
|
|
|
|
},
|
|
|
|
getters: {
|
|
|
|
isInlineView: isInlineViewGetterMock,
|
|
|
|
isParallelView: isParallelViewGetterMock,
|
|
|
|
getCommentFormForDiffFile: getCommentFormForDiffFileGetterMock,
|
2021-02-22 17:27:13 +05:30
|
|
|
diffLines: () => () => [...diffFileMockData.parallel_diff_lines],
|
2021-09-04 01:27:46 +05:30
|
|
|
fileLineCodequality: () => () => [],
|
2019-12-04 20:38:33 +05:30
|
|
|
},
|
|
|
|
actions: {
|
|
|
|
saveDiffDiscussion: saveDiffDiscussionMock,
|
|
|
|
closeDiffFileCommentForm: closeDiffFileCommentFormMock,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2021-01-29 00:20:46 +05:30
|
|
|
const glFeatures = provide ? { ...provide.glFeatures } : {};
|
|
|
|
|
2019-12-04 20:38:33 +05:30
|
|
|
wrapper = shallowMount(DiffContentComponent, {
|
|
|
|
propsData: {
|
|
|
|
...defaultProps,
|
|
|
|
...props,
|
|
|
|
},
|
|
|
|
store: fakeStore,
|
2021-01-29 00:20:46 +05:30
|
|
|
provide: { glFeatures },
|
2019-12-04 20:38:33 +05:30
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
wrapper.destroy();
|
|
|
|
wrapper = null;
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('with text based files', () => {
|
|
|
|
afterEach(() => {
|
2021-03-08 18:12:59 +05:30
|
|
|
[isParallelViewGetterMock, isInlineViewGetterMock].forEach((m) => m.mockRestore());
|
2019-12-04 20:38:33 +05:30
|
|
|
});
|
|
|
|
|
|
|
|
const textDiffFile = { ...defaultProps.diffFile, viewer: { name: diffViewerModes.text } };
|
|
|
|
|
2021-02-22 17:27:13 +05:30
|
|
|
it('should render diff view if `unifiedDiffComponents` are true', () => {
|
2021-01-29 00:20:46 +05:30
|
|
|
createComponent({
|
|
|
|
props: { diffFile: textDiffFile },
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(wrapper.find(DiffView).exists()).toBe(true);
|
|
|
|
});
|
|
|
|
|
2019-12-04 20:38:33 +05:30
|
|
|
it('renders rendering more lines loading icon', () => {
|
|
|
|
createComponent({ props: { diffFile: { ...textDiffFile, renderingLines: true } } });
|
|
|
|
|
|
|
|
expect(wrapper.find(GlLoadingIcon).exists()).toBe(true);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('with empty files', () => {
|
|
|
|
const emptyDiffFile = {
|
|
|
|
...defaultProps.diffFile,
|
|
|
|
viewer: { name: diffViewerModes.text },
|
|
|
|
highlighted_diff_lines: [],
|
|
|
|
parallel_diff_lines: [],
|
|
|
|
};
|
|
|
|
|
|
|
|
it('should render a no preview view if viewer set to no preview', () => {
|
|
|
|
createComponent({
|
|
|
|
props: { diffFile: { ...emptyDiffFile, viewer: { name: diffViewerModes.no_preview } } },
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(wrapper.find(NoPreviewViewer).exists()).toBe(true);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should render not diffable view if viewer set to non_diffable', () => {
|
|
|
|
createComponent({
|
|
|
|
props: { diffFile: { ...emptyDiffFile, viewer: { name: diffViewerModes.not_diffable } } },
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(wrapper.find(NotDiffableViewer).exists()).toBe(true);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('with image files', () => {
|
|
|
|
const imageDiffFile = { ...defaultProps.diffFile, viewer: { name: diffViewerModes.image } };
|
|
|
|
|
|
|
|
it('renders diff file discussions', () => {
|
|
|
|
getCommentFormForDiffFileGetterMock.mockReturnValue(() => true);
|
|
|
|
createComponent({
|
|
|
|
props: {
|
|
|
|
diffFile: { ...imageDiffFile, discussions: [{ name: 'discussion-stub ' }] },
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(wrapper.find(DiffDiscussions).exists()).toBe(true);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('emits saveDiffDiscussion when note-form emits `handleFormUpdate`', () => {
|
|
|
|
const noteStub = {};
|
|
|
|
getCommentFormForDiffFileGetterMock.mockReturnValue(() => true);
|
|
|
|
const currentDiffFile = { ...imageDiffFile, discussions: [{ name: 'discussion-stub ' }] };
|
|
|
|
createComponent({
|
|
|
|
props: {
|
|
|
|
diffFile: currentDiffFile,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
wrapper.find(NoteForm).vm.$emit('handleFormUpdate', noteStub);
|
2020-11-24 15:15:51 +05:30
|
|
|
expect(saveDiffDiscussionMock).toHaveBeenCalledWith(expect.any(Object), {
|
|
|
|
note: noteStub,
|
|
|
|
formData: {
|
|
|
|
noteableData: expect.any(Object),
|
|
|
|
diffFile: currentDiffFile,
|
|
|
|
positionType: IMAGE_DIFF_POSITION_TYPE,
|
|
|
|
x: undefined,
|
|
|
|
y: undefined,
|
|
|
|
width: undefined,
|
|
|
|
height: undefined,
|
|
|
|
noteableType: undefined,
|
2019-12-04 20:38:33 +05:30
|
|
|
},
|
2020-11-24 15:15:51 +05:30
|
|
|
});
|
2019-12-04 20:38:33 +05:30
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|