2020-08-17 17:09:56 -04:00
|
|
|
import { getByText } from '@testing-library/dom';
|
2021-02-14 13:09:20 -05:00
|
|
|
import { mount } from '@vue/test-utils';
|
|
|
|
import { cloneDeep } from 'lodash';
|
2019-08-09 22:36:32 -04:00
|
|
|
import DiffExpansionCell from '~/diffs/components/diff_expansion_cell.vue';
|
2020-11-19 13:09:13 -05:00
|
|
|
import { INLINE_DIFF_VIEW_TYPE } from '~/diffs/constants';
|
2021-02-14 13:09:20 -05:00
|
|
|
import { getPreviousLineIndex } from '~/diffs/store/utils';
|
|
|
|
import { createStore } from '~/mr_notes/stores';
|
2019-08-09 22:36:32 -04:00
|
|
|
import diffFileMockData from '../mock_data/diff_file';
|
|
|
|
|
|
|
|
const EXPAND_UP_CLASS = '.js-unfold';
|
|
|
|
const EXPAND_DOWN_CLASS = '.js-unfold-down';
|
2020-02-20 13:08:51 -05:00
|
|
|
const lineSources = {
|
|
|
|
[INLINE_DIFF_VIEW_TYPE]: 'highlighted_diff_lines',
|
|
|
|
};
|
|
|
|
const lineHandlers = {
|
2020-12-23 16:10:24 -05:00
|
|
|
[INLINE_DIFF_VIEW_TYPE]: (line) => line,
|
2020-02-20 13:08:51 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
function makeLoadMoreLinesPayload({
|
|
|
|
sinceLine,
|
|
|
|
toLine,
|
|
|
|
oldLineNumber,
|
|
|
|
diffViewType,
|
|
|
|
fileHash,
|
|
|
|
nextLineNumbers = {},
|
|
|
|
unfold = false,
|
|
|
|
bottom = false,
|
|
|
|
isExpandDown = false,
|
|
|
|
}) {
|
|
|
|
return {
|
|
|
|
endpoint: 'contextLinesPath',
|
|
|
|
params: {
|
|
|
|
since: sinceLine,
|
|
|
|
to: toLine,
|
|
|
|
offset: toLine + 1 - oldLineNumber,
|
|
|
|
view: diffViewType,
|
|
|
|
unfold,
|
|
|
|
bottom,
|
|
|
|
},
|
|
|
|
lineNumbers: {
|
|
|
|
oldLineNumber,
|
|
|
|
newLineNumber: toLine + 1,
|
|
|
|
},
|
|
|
|
nextLineNumbers,
|
|
|
|
fileHash,
|
|
|
|
isExpandDown,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
function getLine(file, type, index) {
|
|
|
|
const source = lineSources[type];
|
|
|
|
const handler = lineHandlers[type];
|
|
|
|
|
|
|
|
return handler(file[source][index]);
|
|
|
|
}
|
2019-08-09 22:36:32 -04:00
|
|
|
|
|
|
|
describe('DiffExpansionCell', () => {
|
2020-02-20 13:08:51 -05:00
|
|
|
let mockFile;
|
|
|
|
let mockLine;
|
|
|
|
let store;
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
mockFile = cloneDeep(diffFileMockData);
|
2020-08-21 14:10:24 -04:00
|
|
|
mockLine = getLine(mockFile, INLINE_DIFF_VIEW_TYPE, 8);
|
2020-02-20 13:08:51 -05:00
|
|
|
store = createStore();
|
|
|
|
store.state.diffs.diffFiles = [mockFile];
|
2020-03-20 11:09:17 -04:00
|
|
|
jest.spyOn(store, 'dispatch').mockReturnValue(Promise.resolve());
|
2020-02-20 13:08:51 -05:00
|
|
|
});
|
2019-08-09 22:36:32 -04:00
|
|
|
|
|
|
|
const createComponent = (options = {}) => {
|
|
|
|
const defaults = {
|
2020-02-20 13:08:51 -05:00
|
|
|
fileHash: mockFile.file_hash,
|
2019-08-09 22:36:32 -04:00
|
|
|
contextLinesPath: 'contextLinesPath',
|
2020-02-20 13:08:51 -05:00
|
|
|
line: mockLine,
|
2019-08-09 22:36:32 -04:00
|
|
|
isTop: false,
|
|
|
|
isBottom: false,
|
|
|
|
};
|
2021-01-06 07:10:58 -05:00
|
|
|
const propsData = { ...defaults, ...options };
|
2019-08-09 22:36:32 -04:00
|
|
|
|
2021-01-06 07:10:58 -05:00
|
|
|
return mount(DiffExpansionCell, { store, propsData });
|
2019-08-09 22:36:32 -04:00
|
|
|
};
|
|
|
|
|
2021-01-06 07:10:58 -05:00
|
|
|
const findExpandUp = (wrapper) => wrapper.find(EXPAND_UP_CLASS);
|
|
|
|
const findExpandDown = (wrapper) => wrapper.find(EXPAND_DOWN_CLASS);
|
|
|
|
const findExpandAll = ({ element }) => getByText(element, 'Show all unchanged lines');
|
2020-02-20 13:08:51 -05:00
|
|
|
|
2019-08-09 22:36:32 -04:00
|
|
|
describe('top row', () => {
|
|
|
|
it('should have "expand up" and "show all" option', () => {
|
2021-01-06 07:10:58 -05:00
|
|
|
const wrapper = createComponent({
|
2019-08-09 22:36:32 -04:00
|
|
|
isTop: true,
|
|
|
|
});
|
|
|
|
|
2021-01-06 07:10:58 -05:00
|
|
|
expect(findExpandUp(wrapper).exists()).toBe(true);
|
|
|
|
expect(findExpandDown(wrapper).exists()).toBe(false);
|
|
|
|
expect(findExpandAll(wrapper)).not.toBe(null);
|
2019-08-09 22:36:32 -04:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('middle row', () => {
|
|
|
|
it('should have "expand down", "show all", "expand up" option', () => {
|
2021-01-06 07:10:58 -05:00
|
|
|
const wrapper = createComponent();
|
2019-08-09 22:36:32 -04:00
|
|
|
|
2021-01-06 07:10:58 -05:00
|
|
|
expect(findExpandUp(wrapper).exists()).toBe(true);
|
|
|
|
expect(findExpandDown(wrapper).exists()).toBe(true);
|
|
|
|
expect(findExpandAll(wrapper)).not.toBe(null);
|
2019-08-09 22:36:32 -04:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('bottom row', () => {
|
|
|
|
it('should have "expand down" and "show all" option', () => {
|
2021-01-06 07:10:58 -05:00
|
|
|
const wrapper = createComponent({
|
2019-08-09 22:36:32 -04:00
|
|
|
isBottom: true,
|
|
|
|
});
|
|
|
|
|
2021-01-06 07:10:58 -05:00
|
|
|
expect(findExpandUp(wrapper).exists()).toBe(false);
|
|
|
|
expect(findExpandDown(wrapper).exists()).toBe(true);
|
|
|
|
expect(findExpandAll(wrapper)).not.toBe(null);
|
2020-02-20 13:08:51 -05:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('any row', () => {
|
|
|
|
[
|
2020-08-21 14:10:24 -04:00
|
|
|
{ diffViewType: INLINE_DIFF_VIEW_TYPE, lineIndex: 8, file: { parallel_diff_lines: [] } },
|
|
|
|
].forEach(({ diffViewType, file, lineIndex }) => {
|
2020-02-20 13:08:51 -05:00
|
|
|
describe(`with diffViewType (${diffViewType})`, () => {
|
|
|
|
beforeEach(() => {
|
2020-08-21 14:10:24 -04:00
|
|
|
mockLine = getLine(mockFile, diffViewType, lineIndex);
|
2020-02-20 13:08:51 -05:00
|
|
|
store.state.diffs.diffFiles = [{ ...mockFile, ...file }];
|
|
|
|
store.state.diffs.diffViewType = diffViewType;
|
|
|
|
});
|
|
|
|
|
|
|
|
it('does not initially dispatch anything', () => {
|
|
|
|
expect(store.dispatch).not.toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('on expand all clicked, dispatch loadMoreLines', () => {
|
|
|
|
const oldLineNumber = mockLine.meta_data.old_pos;
|
|
|
|
const newLineNumber = mockLine.meta_data.new_pos;
|
|
|
|
const previousIndex = getPreviousLineIndex(diffViewType, mockFile, {
|
|
|
|
oldLineNumber,
|
|
|
|
newLineNumber,
|
|
|
|
});
|
|
|
|
|
2021-01-06 07:10:58 -05:00
|
|
|
const wrapper = createComponent();
|
2020-02-20 13:08:51 -05:00
|
|
|
|
2021-01-06 07:10:58 -05:00
|
|
|
findExpandAll(wrapper).click();
|
2020-02-20 13:08:51 -05:00
|
|
|
|
|
|
|
expect(store.dispatch).toHaveBeenCalledWith(
|
|
|
|
'diffs/loadMoreLines',
|
|
|
|
makeLoadMoreLinesPayload({
|
|
|
|
fileHash: mockFile.file_hash,
|
|
|
|
toLine: newLineNumber - 1,
|
|
|
|
sinceLine: previousIndex,
|
|
|
|
oldLineNumber,
|
|
|
|
diffViewType,
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('on expand up clicked, dispatch loadMoreLines', () => {
|
|
|
|
mockLine.meta_data.old_pos = 200;
|
|
|
|
mockLine.meta_data.new_pos = 200;
|
|
|
|
|
|
|
|
const oldLineNumber = mockLine.meta_data.old_pos;
|
|
|
|
const newLineNumber = mockLine.meta_data.new_pos;
|
|
|
|
|
2021-01-06 07:10:58 -05:00
|
|
|
const wrapper = createComponent();
|
2020-02-20 13:08:51 -05:00
|
|
|
|
2021-01-06 07:10:58 -05:00
|
|
|
findExpandUp(wrapper).trigger('click');
|
2020-02-20 13:08:51 -05:00
|
|
|
|
|
|
|
expect(store.dispatch).toHaveBeenCalledWith(
|
|
|
|
'diffs/loadMoreLines',
|
|
|
|
makeLoadMoreLinesPayload({
|
|
|
|
fileHash: mockFile.file_hash,
|
|
|
|
toLine: newLineNumber - 1,
|
|
|
|
sinceLine: 179,
|
|
|
|
oldLineNumber,
|
|
|
|
diffViewType,
|
|
|
|
unfold: true,
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('on expand down clicked, dispatch loadMoreLines', () => {
|
2020-08-21 14:10:24 -04:00
|
|
|
mockFile[lineSources[diffViewType]][lineIndex + 1] = cloneDeep(
|
|
|
|
mockFile[lineSources[diffViewType]][lineIndex],
|
2020-02-20 13:08:51 -05:00
|
|
|
);
|
2020-08-21 14:10:24 -04:00
|
|
|
const nextLine = getLine(mockFile, diffViewType, lineIndex + 1);
|
2020-02-20 13:08:51 -05:00
|
|
|
|
|
|
|
nextLine.meta_data.old_pos = 300;
|
|
|
|
nextLine.meta_data.new_pos = 300;
|
|
|
|
mockLine.meta_data.old_pos = 200;
|
|
|
|
mockLine.meta_data.new_pos = 200;
|
|
|
|
|
2021-01-06 07:10:58 -05:00
|
|
|
const wrapper = createComponent();
|
2020-02-20 13:08:51 -05:00
|
|
|
|
2021-01-06 07:10:58 -05:00
|
|
|
findExpandDown(wrapper).trigger('click');
|
2020-02-20 13:08:51 -05:00
|
|
|
|
|
|
|
expect(store.dispatch).toHaveBeenCalledWith('diffs/loadMoreLines', {
|
|
|
|
endpoint: 'contextLinesPath',
|
|
|
|
params: {
|
|
|
|
since: 1,
|
|
|
|
to: 21, // the load amount, plus 1 line
|
|
|
|
offset: 0,
|
|
|
|
view: diffViewType,
|
|
|
|
unfold: true,
|
|
|
|
bottom: true,
|
|
|
|
},
|
|
|
|
lineNumbers: {
|
|
|
|
// when expanding down, these are based on the previous line, 0, in this case
|
|
|
|
oldLineNumber: 0,
|
|
|
|
newLineNumber: 0,
|
|
|
|
},
|
|
|
|
nextLineNumbers: { old_line: 200, new_line: 200 },
|
|
|
|
fileHash: mockFile.file_hash,
|
|
|
|
isExpandDown: true,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2019-08-09 22:36:32 -04:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|