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

230 lines
6.7 KiB
JavaScript
Raw Normal View History

2019-10-12 21:52:04 +05:30
import Vue from 'vue';
2020-04-08 14:13:33 +05:30
import { cloneDeep } from 'lodash';
2020-04-22 19:07:51 +05:30
import { createComponentWithStore } from 'helpers/vue_mount_component_helper';
2019-10-12 21:52:04 +05:30
import { createStore } from '~/mr_notes/stores';
import DiffExpansionCell from '~/diffs/components/diff_expansion_cell.vue';
2020-04-08 14:13:33 +05:30
import { getPreviousLineIndex } from '~/diffs/store/utils';
import { INLINE_DIFF_VIEW_TYPE, PARALLEL_DIFF_VIEW_TYPE } from '~/diffs/constants';
2019-10-12 21:52:04 +05:30
import diffFileMockData from '../mock_data/diff_file';
2020-07-28 23:09:34 +05:30
import { getByText } from '@testing-library/dom';
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 LINE_TO_USE = 5;
const lineSources = {
[INLINE_DIFF_VIEW_TYPE]: 'highlighted_diff_lines',
[PARALLEL_DIFF_VIEW_TYPE]: 'parallel_diff_lines',
};
const lineHandlers = {
[INLINE_DIFF_VIEW_TYPE]: line => line,
[PARALLEL_DIFF_VIEW_TYPE]: line => line.right || line.left,
};
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-10-12 21:52:04 +05:30
describe('DiffExpansionCell', () => {
2020-04-08 14:13:33 +05:30
let mockFile;
let mockLine;
let store;
let vm;
beforeEach(() => {
mockFile = cloneDeep(diffFileMockData);
mockLine = getLine(mockFile, INLINE_DIFF_VIEW_TYPE, LINE_TO_USE);
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 cmp = Vue.extend(DiffExpansionCell);
const defaults = {
2020-04-08 14:13:33 +05:30
fileHash: mockFile.file_hash,
2019-10-12 21:52:04 +05:30
contextLinesPath: 'contextLinesPath',
2020-04-08 14:13:33 +05:30
line: mockLine,
2019-10-12 21:52:04 +05:30
isTop: false,
isBottom: false,
};
2020-05-24 23:13:21 +05:30
const props = { ...defaults, ...options };
2019-10-12 21:52:04 +05:30
2020-04-08 14:13:33 +05:30
vm = createComponentWithStore(cmp, store, props).$mount();
2019-10-12 21:52:04 +05:30
};
2020-04-08 14:13:33 +05:30
const findExpandUp = () => vm.$el.querySelector(EXPAND_UP_CLASS);
const findExpandDown = () => vm.$el.querySelector(EXPAND_DOWN_CLASS);
2020-07-28 23:09:34 +05:30
const findExpandAll = () => getByText(vm.$el, 'Show unchanged lines');
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', () => {
2020-04-08 14:13:33 +05:30
createComponent({
2019-10-12 21:52:04 +05:30
isTop: true,
});
2020-04-08 14:13:33 +05:30
expect(findExpandUp()).not.toBe(null);
expect(findExpandDown()).toBe(null);
expect(findExpandAll()).not.toBe(null);
2019-10-12 21:52:04 +05:30
});
});
describe('middle row', () => {
it('should have "expand down", "show all", "expand up" option', () => {
2020-04-08 14:13:33 +05:30
createComponent();
2019-10-12 21:52:04 +05:30
2020-04-08 14:13:33 +05:30
expect(findExpandUp()).not.toBe(null);
expect(findExpandDown()).not.toBe(null);
expect(findExpandAll()).not.toBe(null);
2019-10-12 21:52:04 +05:30
});
});
describe('bottom row', () => {
it('should have "expand down" and "show all" option', () => {
2020-04-08 14:13:33 +05:30
createComponent({
2019-10-12 21:52:04 +05:30
isBottom: true,
});
2020-04-08 14:13:33 +05:30
expect(findExpandUp()).toBe(null);
expect(findExpandDown()).not.toBe(null);
expect(findExpandAll()).not.toBe(null);
});
});
describe('any row', () => {
[
{ diffViewType: INLINE_DIFF_VIEW_TYPE, file: { parallel_diff_lines: [] } },
{ diffViewType: PARALLEL_DIFF_VIEW_TYPE, file: { highlighted_diff_lines: [] } },
].forEach(({ diffViewType, file }) => {
describe(`with diffViewType (${diffViewType})`, () => {
beforeEach(() => {
mockLine = getLine(mockFile, diffViewType, LINE_TO_USE);
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,
});
createComponent();
findExpandAll().click();
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;
createComponent();
findExpandUp().click();
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', () => {
mockFile[lineSources[diffViewType]][LINE_TO_USE + 1] = cloneDeep(
mockFile[lineSources[diffViewType]][LINE_TO_USE],
);
const nextLine = getLine(mockFile, diffViewType, LINE_TO_USE + 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;
createComponent();
findExpandDown().click();
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-10-12 21:52:04 +05:30
});
});
});