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

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

218 lines
6.6 KiB
JavaScript
Raw Normal View History

2021-03-11 19:13:27 +05:30
import { mount } from '@vue/test-utils';
2019-10-12 21:52:04 +05:30
import DiffExpansionCell from '~/diffs/components/diff_expansion_cell.vue';
2021-02-22 17:27:13 +05:30
import { INLINE_DIFF_VIEW_TYPE } from '~/diffs/constants';
2021-03-11 19:13:27 +05:30
import { getPreviousLineIndex } from '~/diffs/store/utils';
import { createStore } from '~/mr_notes/stores';
2022-08-13 15:12:31 +05:30
import { getDiffFileMock } from '../mock_data/diff_file';
2019-10-12 21:52:04 +05:30
const EXPAND_UP_CLASS = '.js-unfold';
const EXPAND_DOWN_CLASS = '.js-unfold-down';
2020-04-08 14:13:33 +05:30
const lineSources = {
[INLINE_DIFF_VIEW_TYPE]: 'highlighted_diff_lines',
};
const lineHandlers = {
2021-03-08 18:12:59 +05:30
[INLINE_DIFF_VIEW_TYPE]: (line) => line,
2020-04-08 14:13:33 +05:30
};
function makeLoadMoreLinesPayload({
sinceLine,
toLine,
oldLineNumber,
fileHash,
nextLineNumbers = {},
unfold = false,
bottom = false,
isExpandDown = false,
}) {
return {
2022-08-13 15:12:31 +05:30
endpoint: getDiffFileMock().context_lines_path,
2020-04-08 14:13:33 +05:30
params: {
since: sinceLine,
to: toLine,
offset: toLine + 1 - oldLineNumber,
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-10-12 21:52:04 +05:30
describe('DiffExpansionCell', () => {
2020-04-08 14:13:33 +05:30
let mockFile;
let mockLine;
let store;
beforeEach(() => {
2022-08-13 15:12:31 +05:30
mockFile = getDiffFileMock();
2020-11-24 15:15:51 +05:30
mockLine = getLine(mockFile, INLINE_DIFF_VIEW_TYPE, 8);
2020-04-08 14:13:33 +05:30
store = createStore();
store.state.diffs.diffFiles = [mockFile];
2020-04-22 19:07:51 +05:30
jest.spyOn(store, 'dispatch').mockReturnValue(Promise.resolve());
2020-04-08 14:13:33 +05:30
});
2019-10-12 21:52:04 +05:30
const createComponent = (options = {}) => {
const defaults = {
2020-04-08 14:13:33 +05:30
fileHash: mockFile.file_hash,
line: mockLine,
2019-10-12 21:52:04 +05:30
isTop: false,
isBottom: false,
2022-07-16 23:28:13 +05:30
file: mockFile,
inline: true,
2019-10-12 21:52:04 +05:30
};
2021-03-08 18:12:59 +05:30
const propsData = { ...defaults, ...options };
2019-10-12 21:52:04 +05:30
2021-03-08 18:12:59 +05:30
return mount(DiffExpansionCell, { store, propsData });
2019-10-12 21:52:04 +05:30
};
2021-03-08 18:12:59 +05:30
const findExpandUp = (wrapper) => wrapper.find(EXPAND_UP_CLASS);
const findExpandDown = (wrapper) => wrapper.find(EXPAND_DOWN_CLASS);
2022-07-23 23:45:48 +05:30
const findExpandAll = (wrapper) => wrapper.find('.js-unfold-all');
2020-04-08 14:13:33 +05:30
2019-10-12 21:52:04 +05:30
describe('top row', () => {
it('should have "expand up" and "show all" option', () => {
2021-03-08 18:12:59 +05:30
const wrapper = createComponent({
2019-10-12 21:52:04 +05:30
isTop: true,
});
2021-03-08 18:12:59 +05:30
expect(findExpandUp(wrapper).exists()).toBe(true);
2022-04-04 11:22:00 +05:30
expect(findExpandUp(wrapper).attributes('disabled')).not.toBeDefined();
2021-03-08 18:12:59 +05:30
expect(findExpandAll(wrapper)).not.toBe(null);
2019-10-12 21:52:04 +05:30
});
});
describe('middle row', () => {
it('should have "expand down", "show all", "expand up" option', () => {
2021-03-08 18:12:59 +05:30
const wrapper = createComponent();
2019-10-12 21:52:04 +05:30
2021-03-08 18:12:59 +05:30
expect(findExpandUp(wrapper).exists()).toBe(true);
expect(findExpandDown(wrapper).exists()).toBe(true);
expect(findExpandAll(wrapper)).not.toBe(null);
2019-10-12 21:52:04 +05:30
});
});
describe('bottom row', () => {
it('should have "expand down" and "show all" option', () => {
2021-03-08 18:12:59 +05:30
const wrapper = createComponent({
2019-10-12 21:52:04 +05:30
isBottom: true,
});
2021-03-08 18:12:59 +05:30
expect(findExpandDown(wrapper).exists()).toBe(true);
2022-04-04 11:22:00 +05:30
expect(findExpandDown(wrapper).attributes('disabled')).not.toBeDefined();
2021-03-08 18:12:59 +05:30
expect(findExpandAll(wrapper)).not.toBe(null);
2020-04-08 14:13:33 +05:30
});
});
describe('any row', () => {
2022-08-13 15:12:31 +05:30
[{ diffViewType: INLINE_DIFF_VIEW_TYPE, lineIndex: 8, file: getDiffFileMock() }].forEach(
({ diffViewType, file, lineIndex }) => {
describe(`with diffViewType (${diffViewType})`, () => {
beforeEach(() => {
mockLine = getLine(mockFile, diffViewType, lineIndex);
store.state.diffs.diffFiles = [{ ...mockFile, ...file }];
store.state.diffs.diffViewType = diffViewType;
2020-04-08 14:13:33 +05:30
});
2022-08-13 15:12:31 +05:30
it('does not initially dispatch anything', () => {
expect(store.dispatch).not.toHaveBeenCalled();
});
2020-04-08 14:13:33 +05:30
2022-08-13 15:12:31 +05:30
it('on expand all clicked, dispatch loadMoreLines', () => {
const oldLineNumber = mockLine.meta_data.old_pos;
const newLineNumber = mockLine.meta_data.new_pos;
const previousIndex = getPreviousLineIndex(mockFile, {
2020-04-08 14:13:33 +05:30
oldLineNumber,
2022-08-13 15:12:31 +05:30
newLineNumber,
});
const wrapper = createComponent({ file, lineCountBetween: 10 });
findExpandAll(wrapper).trigger('click');
expect(store.dispatch).toHaveBeenCalledWith(
'diffs/loadMoreLines',
makeLoadMoreLinesPayload({
fileHash: mockFile.file_hash,
toLine: newLineNumber - 1,
sinceLine: previousIndex,
oldLineNumber,
}),
);
});
2020-04-08 14:13:33 +05:30
2022-08-13 15:12:31 +05:30
it('on expand up clicked, dispatch loadMoreLines', () => {
mockLine.meta_data.old_pos = 200;
mockLine.meta_data.new_pos = 200;
2020-04-08 14:13:33 +05:30
2022-08-13 15:12:31 +05:30
const oldLineNumber = mockLine.meta_data.old_pos;
const newLineNumber = mockLine.meta_data.new_pos;
2020-04-08 14:13:33 +05:30
2022-08-13 15:12:31 +05:30
const wrapper = createComponent({ file });
2020-04-08 14:13:33 +05:30
2022-08-13 15:12:31 +05:30
findExpandUp(wrapper).trigger('click');
2020-04-08 14:13:33 +05:30
2022-08-13 15:12:31 +05:30
expect(store.dispatch).toHaveBeenCalledWith(
'diffs/loadMoreLines',
makeLoadMoreLinesPayload({
fileHash: mockFile.file_hash,
toLine: newLineNumber - 1,
sinceLine: 179,
oldLineNumber,
diffViewType,
unfold: true,
}),
);
});
2020-04-08 14:13:33 +05:30
2022-08-13 15:12:31 +05:30
it('on expand down clicked, dispatch loadMoreLines', () => {
mockFile[lineSources[diffViewType]][lineIndex + 1] = getDiffFileMock()[
lineSources[diffViewType]
][lineIndex];
const nextLine = getLine(mockFile, diffViewType, lineIndex + 1);
nextLine.meta_data.old_pos = 300;
nextLine.meta_data.new_pos = 300;
mockLine.meta_data.old_pos = 200;
mockLine.meta_data.new_pos = 200;
const wrapper = createComponent({ file });
findExpandDown(wrapper).trigger('click');
expect(store.dispatch).toHaveBeenCalledWith('diffs/loadMoreLines', {
endpoint: mockFile.context_lines_path,
params: {
since: 1,
to: 21, // the load amount, plus 1 line
offset: 0,
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,
});
2020-04-08 14:13:33 +05:30
});
});
2022-08-13 15:12:31 +05:30
},
);
2019-10-12 21:52:04 +05:30
});
});