2021-03-08 18:12:59 +05:30
|
|
|
import { shallowMount } from '@vue/test-utils';
|
2021-03-11 19:13:27 +05:30
|
|
|
import Vue from 'vue';
|
2021-01-29 00:20:46 +05:30
|
|
|
import Vuex from 'vuex';
|
|
|
|
import DiffView from '~/diffs/components/diff_view.vue';
|
|
|
|
|
|
|
|
describe('DiffView', () => {
|
|
|
|
const DiffExpansionCell = { template: `<div/>` };
|
|
|
|
const DiffRow = { template: `<div/>` };
|
|
|
|
const DiffCommentCell = { template: `<div/>` };
|
|
|
|
const DraftNote = { template: `<div/>` };
|
2021-03-08 18:12:59 +05:30
|
|
|
const showCommentForm = jest.fn();
|
|
|
|
const setSelectedCommentPosition = jest.fn();
|
|
|
|
const getDiffRow = (wrapper) => wrapper.findComponent(DiffRow).vm;
|
|
|
|
|
|
|
|
const createWrapper = (props) => {
|
|
|
|
Vue.use(Vuex);
|
2021-01-29 00:20:46 +05:30
|
|
|
|
|
|
|
const batchComments = {
|
|
|
|
getters: {
|
|
|
|
shouldRenderDraftRow: () => false,
|
|
|
|
shouldRenderParallelDraftRow: () => () => true,
|
|
|
|
draftForLine: () => false,
|
|
|
|
draftsForFile: () => false,
|
|
|
|
hasParallelDraftLeft: () => false,
|
|
|
|
hasParallelDraftRight: () => false,
|
|
|
|
},
|
|
|
|
namespaced: true,
|
|
|
|
};
|
2021-03-08 18:12:59 +05:30
|
|
|
const diffs = {
|
|
|
|
actions: { showCommentForm },
|
|
|
|
getters: { commitId: () => 'abc123' },
|
|
|
|
namespaced: true,
|
|
|
|
};
|
2021-01-29 00:20:46 +05:30
|
|
|
const notes = {
|
2021-03-08 18:12:59 +05:30
|
|
|
actions: { setSelectedCommentPosition },
|
2021-01-29 00:20:46 +05:30
|
|
|
state: { selectedCommentPosition: null, selectedCommentPositionHover: null },
|
|
|
|
};
|
|
|
|
|
|
|
|
const store = new Vuex.Store({
|
|
|
|
modules: { diffs, notes, batchComments },
|
|
|
|
});
|
|
|
|
|
|
|
|
const propsData = {
|
|
|
|
diffFile: {},
|
|
|
|
diffLines: [],
|
|
|
|
...props,
|
|
|
|
};
|
|
|
|
const stubs = { DiffExpansionCell, DiffRow, DiffCommentCell, DraftNote };
|
2021-03-08 18:12:59 +05:30
|
|
|
return shallowMount(DiffView, { propsData, store, stubs });
|
2021-01-29 00:20:46 +05:30
|
|
|
};
|
|
|
|
|
|
|
|
it('renders a match line', () => {
|
|
|
|
const wrapper = createWrapper({ diffLines: [{ isMatchLineLeft: true }] });
|
|
|
|
expect(wrapper.find(DiffExpansionCell).exists()).toBe(true);
|
|
|
|
});
|
|
|
|
|
|
|
|
it.each`
|
2021-03-11 19:13:27 +05:30
|
|
|
type | side | container | sides | total
|
|
|
|
${'parallel'} | ${'left'} | ${'.old'} | ${{ left: { lineDraft: {}, renderDiscussion: true }, right: { lineDraft: {}, renderDiscussion: true } }} | ${2}
|
|
|
|
${'parallel'} | ${'right'} | ${'.new'} | ${{ left: { lineDraft: {}, renderDiscussion: true }, right: { lineDraft: {}, renderDiscussion: true } }} | ${2}
|
|
|
|
${'inline'} | ${'left'} | ${'.old'} | ${{ left: { lineDraft: {}, renderDiscussion: true } }} | ${1}
|
|
|
|
${'inline'} | ${'left'} | ${'.old'} | ${{ left: { lineDraft: {}, renderDiscussion: true } }} | ${1}
|
|
|
|
${'inline'} | ${'left'} | ${'.old'} | ${{ left: { lineDraft: {}, renderDiscussion: true } }} | ${1}
|
2021-01-29 00:20:46 +05:30
|
|
|
`(
|
|
|
|
'renders a $type comment row with comment cell on $side',
|
|
|
|
({ type, container, sides, total }) => {
|
|
|
|
const wrapper = createWrapper({
|
|
|
|
diffLines: [{ renderCommentRow: true, ...sides }],
|
|
|
|
inline: type === 'inline',
|
|
|
|
});
|
|
|
|
expect(wrapper.findAll(DiffCommentCell).length).toBe(total);
|
2021-03-08 18:12:59 +05:30
|
|
|
expect(wrapper.find(container).find(DiffCommentCell).exists()).toBe(true);
|
2021-01-29 00:20:46 +05:30
|
|
|
},
|
|
|
|
);
|
|
|
|
|
|
|
|
it('renders a draft row', () => {
|
|
|
|
const wrapper = createWrapper({
|
|
|
|
diffLines: [{ renderCommentRow: true, left: { lineDraft: { isDraft: true } } }],
|
|
|
|
});
|
|
|
|
expect(wrapper.find(DraftNote).exists()).toBe(true);
|
|
|
|
});
|
2021-03-08 18:12:59 +05:30
|
|
|
|
|
|
|
describe('drag operations', () => {
|
|
|
|
it('sets `dragStart` onStartDragging', () => {
|
|
|
|
const wrapper = createWrapper({ diffLines: [{}] });
|
|
|
|
|
|
|
|
wrapper.findComponent(DiffRow).vm.$emit('startdragging', { test: true });
|
|
|
|
expect(wrapper.vm.dragStart).toEqual({ test: true });
|
|
|
|
});
|
|
|
|
|
|
|
|
it('does not call `setSelectedCommentPosition` on different chunks onDragOver', () => {
|
|
|
|
const wrapper = createWrapper({ diffLines: [{}] });
|
|
|
|
const diffRow = getDiffRow(wrapper);
|
|
|
|
|
|
|
|
diffRow.$emit('startdragging', { chunk: 0 });
|
|
|
|
diffRow.$emit('enterdragging', { chunk: 1 });
|
|
|
|
|
|
|
|
expect(setSelectedCommentPosition).not.toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
|
|
|
|
it.each`
|
|
|
|
start | end | expectation
|
|
|
|
${1} | ${2} | ${{ start: { index: 1 }, end: { index: 2 } }}
|
|
|
|
${2} | ${1} | ${{ start: { index: 1 }, end: { index: 2 } }}
|
|
|
|
${1} | ${1} | ${{ start: { index: 1 }, end: { index: 1 } }}
|
|
|
|
`(
|
|
|
|
'calls `setSelectedCommentPosition` with correct `updatedLineRange`',
|
|
|
|
({ start, end, expectation }) => {
|
|
|
|
const wrapper = createWrapper({ diffLines: [{}] });
|
|
|
|
const diffRow = getDiffRow(wrapper);
|
|
|
|
|
|
|
|
diffRow.$emit('startdragging', { chunk: 1, index: start });
|
|
|
|
diffRow.$emit('enterdragging', { chunk: 1, index: end });
|
|
|
|
|
|
|
|
const arg = setSelectedCommentPosition.mock.calls[0][1];
|
|
|
|
|
|
|
|
expect(arg).toMatchObject(expectation);
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
|
|
|
it('sets `dragStart` to null onStopDragging', () => {
|
|
|
|
const wrapper = createWrapper({ diffLines: [{}] });
|
|
|
|
const diffRow = getDiffRow(wrapper);
|
|
|
|
|
|
|
|
diffRow.$emit('startdragging', { test: true });
|
|
|
|
expect(wrapper.vm.dragStart).toEqual({ test: true });
|
|
|
|
|
|
|
|
diffRow.$emit('stopdragging');
|
|
|
|
expect(wrapper.vm.dragStart).toBeNull();
|
|
|
|
expect(showCommentForm).toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
});
|
2021-01-29 00:20:46 +05:30
|
|
|
});
|