debian-mirror-gitlab/spec/frontend/diffs/components/diff_view_spec.js

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

154 lines
6.2 KiB
JavaScript
Raw Normal View History

2021-03-08 18:12:59 +05:30
import { shallowMount } from '@vue/test-utils';
2022-08-13 15:12:31 +05:30
import Vue, { nextTick } from 'vue';
2021-01-29 00:20:46 +05:30
import Vuex from 'vuex';
import DiffView from '~/diffs/components/diff_view.vue';
2022-10-11 01:57:18 +05:30
import DiffLine from '~/diffs/components/diff_line.vue';
2022-08-13 15:12:31 +05:30
import { diffCodeQuality } from '../mock_data/diff_code_quality';
2021-01-29 00:20:46 +05:30
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;
2022-08-13 15:12:31 +05:30
const createWrapper = (props, provide = {}) => {
2021-03-08 18:12:59 +05:30
Vue.use(Vuex);
2021-01-29 00:20:46 +05:30
const batchComments = {
getters: {
shouldRenderDraftRow: () => false,
shouldRenderParallelDraftRow: () => () => true,
2022-11-25 23:54:43 +05:30
draftsForLine: () => false,
2021-01-29 00:20:46 +05:30
draftsForFile: () => false,
hasParallelDraftLeft: () => false,
hasParallelDraftRight: () => false,
},
namespaced: true,
};
2021-03-08 18:12:59 +05:30
const diffs = {
actions: { showCommentForm },
2021-09-30 23:02:18 +05:30
getters: { commitId: () => 'abc123', fileLineCoverage: () => ({}) },
2021-03-08 18:12:59 +05:30
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 = {
2021-09-30 23:02:18 +05:30
diffFile: { file_hash: '123' },
2021-01-29 00:20:46 +05:30
diffLines: [],
...props,
};
const stubs = { DiffExpansionCell, DiffRow, DiffCommentCell, DraftNote };
2022-08-13 15:12:31 +05:30
return shallowMount(DiffView, { propsData, store, stubs, provide });
2021-01-29 00:20:46 +05:30
};
2022-10-11 01:57:18 +05:30
it('does not render a diff-line component when there is no finding', () => {
2022-08-13 15:12:31 +05:30
const wrapper = createWrapper();
2022-10-11 01:57:18 +05:30
expect(wrapper.findComponent(DiffLine).exists()).toBe(false);
2022-08-13 15:12:31 +05:30
});
2022-10-11 01:57:18 +05:30
it('does render a diff-line component with the correct props when there is a finding & refactorCodeQualityInlineFindings flag is true', async () => {
2022-08-13 15:12:31 +05:30
const wrapper = createWrapper(diffCodeQuality, {
glFeatures: { refactorCodeQualityInlineFindings: true },
});
wrapper.findComponent(DiffRow).vm.$emit('toggleCodeQualityFindings', 2);
await nextTick();
2022-10-11 01:57:18 +05:30
expect(wrapper.findComponent(DiffLine).props('line')).toBe(diffCodeQuality.diffLines[2]);
2022-08-13 15:12:31 +05:30
});
2022-10-11 01:57:18 +05:30
it('does not render a diff-line component when there is a finding & refactorCodeQualityInlineFindings flag is false', async () => {
2022-08-13 15:12:31 +05:30
const wrapper = createWrapper(diffCodeQuality, {
glFeatures: { refactorCodeQualityInlineFindings: false },
});
wrapper.findComponent(DiffRow).vm.$emit('toggleCodeQualityFindings', 2);
await nextTick();
2022-10-11 01:57:18 +05:30
expect(wrapper.findComponent(DiffLine).exists()).toBe(false);
2022-08-13 15:12:31 +05:30
});
2021-01-29 00:20:46 +05:30
it.each`
2022-11-25 23:54:43 +05:30
type | side | container | sides | total
${'parallel'} | ${'left'} | ${'.old'} | ${{ left: { lineDrafts: [], renderDiscussion: true }, right: { lineDrafts: [], renderDiscussion: true } }} | ${2}
${'parallel'} | ${'right'} | ${'.new'} | ${{ left: { lineDrafts: [], renderDiscussion: true }, right: { lineDrafts: [], renderDiscussion: true } }} | ${2}
${'inline'} | ${'left'} | ${'.old'} | ${{ left: { lineDrafts: [], renderDiscussion: true } }} | ${1}
${'inline'} | ${'left'} | ${'.old'} | ${{ left: { lineDrafts: [], renderDiscussion: true } }} | ${1}
${'inline'} | ${'left'} | ${'.old'} | ${{ left: { lineDrafts: [], 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',
});
2022-10-11 01:57:18 +05:30
expect(wrapper.findAllComponents(DiffCommentCell).length).toBe(total);
expect(wrapper.find(container).findComponent(DiffCommentCell).exists()).toBe(true);
2021-01-29 00:20:46 +05:30
},
);
it('renders a draft row', () => {
const wrapper = createWrapper({
2022-11-25 23:54:43 +05:30
diffLines: [{ renderCommentRow: true, left: { lineDrafts: [{ isDraft: true }] } }],
2021-01-29 00:20:46 +05:30
});
2022-10-11 01:57:18 +05:30
expect(wrapper.findComponent(DraftNote).exists()).toBe(true);
2021-01-29 00:20:46 +05:30
});
2021-03-08 18:12:59 +05:30
describe('drag operations', () => {
it('sets `dragStart` onStartDragging', () => {
const wrapper = createWrapper({ diffLines: [{}] });
2021-09-30 23:02:18 +05:30
wrapper.findComponent(DiffRow).vm.$emit('startdragging', { line: { test: true } });
expect(wrapper.vm.idState.dragStart).toEqual({ test: true });
2021-03-08 18:12:59 +05:30
});
it('does not call `setSelectedCommentPosition` on different chunks onDragOver', () => {
const wrapper = createWrapper({ diffLines: [{}] });
const diffRow = getDiffRow(wrapper);
2021-09-30 23:02:18 +05:30
diffRow.$emit('startdragging', { line: { chunk: 0 } });
2021-03-08 18:12:59 +05:30
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);
2021-09-30 23:02:18 +05:30
diffRow.$emit('startdragging', { line: { chunk: 1, index: start } });
2021-03-08 18:12:59 +05:30
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);
2021-09-30 23:02:18 +05:30
diffRow.$emit('startdragging', { line: { test: true } });
expect(wrapper.vm.idState.dragStart).toEqual({ test: true });
2021-03-08 18:12:59 +05:30
diffRow.$emit('stopdragging');
2021-09-30 23:02:18 +05:30
expect(wrapper.vm.idState.dragStart).toBeNull();
2021-03-08 18:12:59 +05:30
expect(showCommentForm).toHaveBeenCalled();
});
});
2021-01-29 00:20:46 +05:30
});