debian-mirror-gitlab/spec/javascripts/diffs/store/mutations_spec.js

678 lines
20 KiB
JavaScript
Raw Normal View History

2018-12-05 23:21:45 +05:30
import createState from '~/diffs/store/modules/diff_state';
2018-11-08 19:23:39 +05:30
import mutations from '~/diffs/store/mutations';
import * as types from '~/diffs/store/mutation_types';
import { INLINE_DIFF_VIEW_TYPE } from '~/diffs/constants';
2018-11-20 20:47:30 +05:30
import diffFileMockData from '../mock_data/diff_file';
2018-11-08 19:23:39 +05:30
describe('DiffsStoreMutations', () => {
describe('SET_BASE_CONFIG', () => {
it('should set endpoint and project path', () => {
const state = {};
const endpoint = '/diffs/endpoint';
const projectPath = '/root/project';
mutations[types.SET_BASE_CONFIG](state, { endpoint, projectPath });
2018-12-13 13:39:08 +05:30
2018-11-08 19:23:39 +05:30
expect(state.endpoint).toEqual(endpoint);
expect(state.projectPath).toEqual(projectPath);
});
});
describe('SET_LOADING', () => {
it('should set loading state', () => {
const state = {};
mutations[types.SET_LOADING](state, false);
2018-12-13 13:39:08 +05:30
2018-11-08 19:23:39 +05:30
expect(state.isLoading).toEqual(false);
});
});
2018-11-20 20:47:30 +05:30
describe('SET_DIFF_DATA', () => {
it('should set diff data type properly', () => {
const state = {};
const diffMock = {
diff_files: [diffFileMockData],
};
mutations[types.SET_DIFF_DATA](state, diffMock);
2019-02-15 15:39:39 +05:30
const firstLine = state.diffFiles[0].parallel_diff_lines[0];
2018-11-20 20:47:30 +05:30
expect(firstLine.right.text).toBeUndefined();
expect(state.diffFiles[0].renderIt).toEqual(true);
expect(state.diffFiles[0].collapsed).toEqual(false);
});
});
2018-11-08 19:23:39 +05:30
describe('SET_DIFF_VIEW_TYPE', () => {
it('should set diff view type properly', () => {
const state = {};
mutations[types.SET_DIFF_VIEW_TYPE](state, INLINE_DIFF_VIEW_TYPE);
2018-12-13 13:39:08 +05:30
2018-11-08 19:23:39 +05:30
expect(state.diffViewType).toEqual(INLINE_DIFF_VIEW_TYPE);
});
});
describe('EXPAND_ALL_FILES', () => {
it('should change the collapsed prop from diffFiles', () => {
const diffFile = {
2019-05-30 16:15:17 +05:30
collapsed: true,
2018-11-08 19:23:39 +05:30
};
const state = { expandAllFiles: true, diffFiles: [diffFile] };
mutations[types.EXPAND_ALL_FILES](state);
2018-12-13 13:39:08 +05:30
2019-05-30 16:15:17 +05:30
expect(state.diffFiles[0].collapsed).toEqual(false);
2018-11-08 19:23:39 +05:30
});
});
describe('ADD_CONTEXT_LINES', () => {
it('should call utils.addContextLines with proper params', () => {
const options = {
lineNumbers: { oldLineNumber: 1, newLineNumber: 2 },
2019-02-15 15:39:39 +05:30
contextLines: [
{ old_line: 1, new_line: 1, line_code: 'ff9200_1_1', discussions: [], hasForm: false },
],
2018-11-08 19:23:39 +05:30
fileHash: 'ff9200',
params: {
bottom: true,
},
};
const diffFile = {
2019-02-15 15:39:39 +05:30
file_hash: options.fileHash,
highlighted_diff_lines: [],
parallel_diff_lines: [],
2018-11-08 19:23:39 +05:30
};
const state = { diffFiles: [diffFile] };
2019-02-15 15:39:39 +05:30
const lines = [{ old_line: 1, new_line: 1 }];
2018-11-08 19:23:39 +05:30
const findDiffFileSpy = spyOnDependency(mutations, 'findDiffFile').and.returnValue(diffFile);
const removeMatchLineSpy = spyOnDependency(mutations, 'removeMatchLine');
const lineRefSpy = spyOnDependency(mutations, 'addLineReferences').and.returnValue(lines);
const addContextLinesSpy = spyOnDependency(mutations, 'addContextLines');
mutations[types.ADD_CONTEXT_LINES](state, options);
expect(findDiffFileSpy).toHaveBeenCalledWith(state.diffFiles, options.fileHash);
expect(removeMatchLineSpy).toHaveBeenCalledWith(
diffFile,
options.lineNumbers,
options.params.bottom,
);
2018-12-13 13:39:08 +05:30
2018-11-08 19:23:39 +05:30
expect(lineRefSpy).toHaveBeenCalledWith(
options.contextLines,
options.lineNumbers,
options.params.bottom,
);
2018-12-13 13:39:08 +05:30
2018-11-08 19:23:39 +05:30
expect(addContextLinesSpy).toHaveBeenCalledWith({
2019-02-15 15:39:39 +05:30
inlineLines: diffFile.highlighted_diff_lines,
parallelLines: diffFile.parallel_diff_lines,
2018-11-08 19:23:39 +05:30
contextLines: options.contextLines,
bottom: options.params.bottom,
lineNumbers: options.lineNumbers,
});
});
});
describe('ADD_COLLAPSED_DIFFS', () => {
it('should update the state with the given data for the given file hash', () => {
const fileHash = 123;
2019-05-30 16:15:17 +05:30
const state = { diffFiles: [{}, { file_hash: fileHash, existing_field: 0 }] };
const data = { diff_files: [{ file_hash: fileHash, extra_field: 1, existing_field: 1 }] };
2018-11-08 19:23:39 +05:30
2018-11-20 20:47:30 +05:30
mutations[types.ADD_COLLAPSED_DIFFS](state, { file: state.diffFiles[1], data });
2018-12-13 13:39:08 +05:30
2019-02-15 15:39:39 +05:30
expect(state.diffFiles[1].file_hash).toEqual(fileHash);
expect(state.diffFiles[1].existing_field).toEqual(1);
expect(state.diffFiles[1].extra_field).toEqual(1);
2018-11-08 19:23:39 +05:30
});
});
2018-11-20 20:47:30 +05:30
describe('SET_LINE_DISCUSSIONS_FOR_FILE', () => {
it('should add discussions to the given line', () => {
const diffPosition = {
2019-02-15 15:39:39 +05:30
base_sha: 'ed13df29948c41ba367caa757ab3ec4892509910',
head_sha: 'b921914f9a834ac47e6fd9420f78db0f83559130',
new_line: null,
new_path: '500-lines-4.txt',
old_line: 5,
old_path: '500-lines-4.txt',
start_sha: 'ed13df29948c41ba367caa757ab3ec4892509910',
};
const state = {
latestDiff: true,
diffFiles: [
{
file_hash: 'ABC',
parallel_diff_lines: [
{
left: {
line_code: 'ABC_1',
discussions: [],
},
right: {
line_code: 'ABC_1',
discussions: [],
},
},
],
highlighted_diff_lines: [
{
line_code: 'ABC_1',
discussions: [],
},
],
},
],
};
const discussion = {
id: 1,
line_code: 'ABC_1',
diff_discussion: true,
resolvable: true,
original_position: diffPosition,
position: diffPosition,
diff_file: {
file_hash: state.diffFiles[0].file_hash,
},
};
const diffPositionByLineCode = {
ABC_1: diffPosition,
};
mutations[types.SET_LINE_DISCUSSIONS_FOR_FILE](state, {
discussion,
diffPositionByLineCode,
});
expect(state.diffFiles[0].parallel_diff_lines[0].left.discussions.length).toEqual(1);
expect(state.diffFiles[0].parallel_diff_lines[0].left.discussions[0].id).toEqual(1);
expect(state.diffFiles[0].parallel_diff_lines[0].right.discussions).toEqual([]);
expect(state.diffFiles[0].highlighted_diff_lines[0].discussions.length).toEqual(1);
expect(state.diffFiles[0].highlighted_diff_lines[0].discussions[0].id).toEqual(1);
});
it('should not duplicate discussions on line', () => {
const diffPosition = {
base_sha: 'ed13df29948c41ba367caa757ab3ec4892509910',
head_sha: 'b921914f9a834ac47e6fd9420f78db0f83559130',
new_line: null,
new_path: '500-lines-4.txt',
old_line: 5,
old_path: '500-lines-4.txt',
start_sha: 'ed13df29948c41ba367caa757ab3ec4892509910',
2018-11-20 20:47:30 +05:30
};
const state = {
2018-12-05 23:21:45 +05:30
latestDiff: true,
2018-11-20 20:47:30 +05:30
diffFiles: [
{
2019-02-15 15:39:39 +05:30
file_hash: 'ABC',
parallel_diff_lines: [
2018-11-20 20:47:30 +05:30
{
left: {
2019-02-15 15:39:39 +05:30
line_code: 'ABC_1',
2018-11-20 20:47:30 +05:30
discussions: [],
},
right: {
2019-02-15 15:39:39 +05:30
line_code: 'ABC_1',
2018-11-20 20:47:30 +05:30
discussions: [],
},
},
],
2019-02-15 15:39:39 +05:30
highlighted_diff_lines: [
2018-11-20 20:47:30 +05:30
{
2019-02-15 15:39:39 +05:30
line_code: 'ABC_1',
2018-11-20 20:47:30 +05:30
discussions: [],
},
],
},
],
};
2018-12-13 13:39:08 +05:30
const discussion = {
id: 1,
line_code: 'ABC_1',
diff_discussion: true,
resolvable: true,
original_position: diffPosition,
position: diffPosition,
diff_file: {
2019-02-15 15:39:39 +05:30
file_hash: state.diffFiles[0].file_hash,
2018-11-20 20:47:30 +05:30
},
2018-12-13 13:39:08 +05:30
};
2018-11-20 20:47:30 +05:30
const diffPositionByLineCode = {
ABC_1: diffPosition,
};
mutations[types.SET_LINE_DISCUSSIONS_FOR_FILE](state, {
2018-12-13 13:39:08 +05:30
discussion,
2018-11-20 20:47:30 +05:30
diffPositionByLineCode,
});
2019-02-15 15:39:39 +05:30
expect(state.diffFiles[0].parallel_diff_lines[0].left.discussions.length).toEqual(1);
expect(state.diffFiles[0].parallel_diff_lines[0].left.discussions[0].id).toEqual(1);
expect(state.diffFiles[0].parallel_diff_lines[0].right.discussions).toEqual([]);
2018-11-20 20:47:30 +05:30
2019-02-15 15:39:39 +05:30
expect(state.diffFiles[0].highlighted_diff_lines[0].discussions.length).toEqual(1);
expect(state.diffFiles[0].highlighted_diff_lines[0].discussions[0].id).toEqual(1);
mutations[types.SET_LINE_DISCUSSIONS_FOR_FILE](state, {
discussion,
diffPositionByLineCode,
});
expect(state.diffFiles[0].parallel_diff_lines[0].left.discussions.length).toEqual(1);
expect(state.diffFiles[0].parallel_diff_lines[0].left.discussions[0].id).toEqual(1);
expect(state.diffFiles[0].parallel_diff_lines[0].right.discussions).toEqual([]);
expect(state.diffFiles[0].highlighted_diff_lines[0].discussions.length).toEqual(1);
expect(state.diffFiles[0].highlighted_diff_lines[0].discussions[0].id).toEqual(1);
});
it('updates existing discussion', () => {
const diffPosition = {
base_sha: 'ed13df29948c41ba367caa757ab3ec4892509910',
head_sha: 'b921914f9a834ac47e6fd9420f78db0f83559130',
new_line: null,
new_path: '500-lines-4.txt',
old_line: 5,
old_path: '500-lines-4.txt',
start_sha: 'ed13df29948c41ba367caa757ab3ec4892509910',
};
const state = {
latestDiff: true,
diffFiles: [
{
file_hash: 'ABC',
parallel_diff_lines: [
{
left: {
line_code: 'ABC_1',
discussions: [],
},
right: {
line_code: 'ABC_1',
discussions: [],
},
},
],
highlighted_diff_lines: [
{
line_code: 'ABC_1',
discussions: [],
},
],
},
],
};
const discussion = {
id: 1,
line_code: 'ABC_1',
diff_discussion: true,
resolvable: true,
original_position: diffPosition,
position: diffPosition,
diff_file: {
file_hash: state.diffFiles[0].file_hash,
},
};
const diffPositionByLineCode = {
ABC_1: diffPosition,
};
mutations[types.SET_LINE_DISCUSSIONS_FOR_FILE](state, {
discussion,
diffPositionByLineCode,
});
expect(state.diffFiles[0].parallel_diff_lines[0].left.discussions.length).toEqual(1);
expect(state.diffFiles[0].parallel_diff_lines[0].left.discussions[0].id).toEqual(1);
expect(state.diffFiles[0].parallel_diff_lines[0].right.discussions).toEqual([]);
expect(state.diffFiles[0].highlighted_diff_lines[0].discussions.length).toEqual(1);
expect(state.diffFiles[0].highlighted_diff_lines[0].discussions[0].id).toEqual(1);
mutations[types.SET_LINE_DISCUSSIONS_FOR_FILE](state, {
discussion: {
...discussion,
resolved: true,
notes: ['test'],
},
diffPositionByLineCode,
});
expect(state.diffFiles[0].parallel_diff_lines[0].left.discussions[0].notes.length).toBe(1);
expect(state.diffFiles[0].highlighted_diff_lines[0].discussions[0].notes.length).toBe(1);
expect(state.diffFiles[0].parallel_diff_lines[0].left.discussions[0].resolved).toBe(true);
expect(state.diffFiles[0].highlighted_diff_lines[0].discussions[0].resolved).toBe(true);
});
it('should not duplicate inline diff discussions', () => {
const diffPosition = {
base_sha: 'ed13df29948c41ba367caa757ab3ec4892509910',
head_sha: 'b921914f9a834ac47e6fd9420f78db0f83559130',
new_line: null,
new_path: '500-lines-4.txt',
old_line: 5,
old_path: '500-lines-4.txt',
start_sha: 'ed13df29948c41ba367caa757ab3ec4892509910',
};
const state = {
latestDiff: true,
diffFiles: [
{
file_hash: 'ABC',
highlighted_diff_lines: [
{
line_code: 'ABC_1',
discussions: [
{
id: 1,
line_code: 'ABC_1',
diff_discussion: true,
resolvable: true,
original_position: diffPosition,
position: diffPosition,
diff_file: {
file_hash: 'ABC',
},
},
],
},
{
line_code: 'ABC_2',
discussions: [],
},
],
},
],
};
const discussion = {
id: 2,
line_code: 'ABC_2',
diff_discussion: true,
resolvable: true,
original_position: diffPosition,
position: diffPosition,
diff_file: {
file_hash: state.diffFiles[0].file_hash,
},
};
const diffPositionByLineCode = {
ABC_2: diffPosition,
};
mutations[types.SET_LINE_DISCUSSIONS_FOR_FILE](state, {
discussion,
diffPositionByLineCode,
});
expect(state.diffFiles[0].highlighted_diff_lines[0].discussions.length).toBe(1);
2018-11-20 20:47:30 +05:30
});
2018-12-05 23:21:45 +05:30
it('should add legacy discussions to the given line', () => {
const diffPosition = {
2019-02-15 15:39:39 +05:30
base_sha: 'ed13df29948c41ba367caa757ab3ec4892509910',
head_sha: 'b921914f9a834ac47e6fd9420f78db0f83559130',
new_line: null,
new_path: '500-lines-4.txt',
old_line: 5,
old_path: '500-lines-4.txt',
start_sha: 'ed13df29948c41ba367caa757ab3ec4892509910',
line_code: 'ABC_1',
2018-12-05 23:21:45 +05:30
};
const state = {
latestDiff: true,
diffFiles: [
{
2019-02-15 15:39:39 +05:30
file_hash: 'ABC',
parallel_diff_lines: [
2018-12-05 23:21:45 +05:30
{
left: {
2019-02-15 15:39:39 +05:30
line_code: 'ABC_1',
2018-12-05 23:21:45 +05:30
discussions: [],
},
right: {
2019-02-15 15:39:39 +05:30
line_code: 'ABC_1',
2018-12-05 23:21:45 +05:30
discussions: [],
},
},
],
2019-02-15 15:39:39 +05:30
highlighted_diff_lines: [
2018-12-05 23:21:45 +05:30
{
2019-02-15 15:39:39 +05:30
line_code: 'ABC_1',
2018-12-05 23:21:45 +05:30
discussions: [],
},
],
},
],
};
2018-12-13 13:39:08 +05:30
const discussion = {
id: 1,
line_code: 'ABC_1',
diff_discussion: true,
active: true,
diff_file: {
2019-02-15 15:39:39 +05:30
file_hash: state.diffFiles[0].file_hash,
2018-12-05 23:21:45 +05:30
},
2018-12-13 13:39:08 +05:30
};
2018-12-05 23:21:45 +05:30
const diffPositionByLineCode = {
ABC_1: diffPosition,
};
mutations[types.SET_LINE_DISCUSSIONS_FOR_FILE](state, {
2018-12-13 13:39:08 +05:30
discussion,
2018-12-05 23:21:45 +05:30
diffPositionByLineCode,
});
2019-02-15 15:39:39 +05:30
expect(state.diffFiles[0].parallel_diff_lines[0].left.discussions.length).toEqual(1);
expect(state.diffFiles[0].parallel_diff_lines[0].left.discussions[0].id).toEqual(1);
2018-12-05 23:21:45 +05:30
2019-02-15 15:39:39 +05:30
expect(state.diffFiles[0].highlighted_diff_lines[0].discussions.length).toEqual(1);
expect(state.diffFiles[0].highlighted_diff_lines[0].discussions[0].id).toEqual(1);
2018-12-05 23:21:45 +05:30
});
2018-11-20 20:47:30 +05:30
});
describe('REMOVE_LINE_DISCUSSIONS', () => {
it('should remove the existing discussions on the given line', () => {
const state = {
diffFiles: [
{
2019-02-15 15:39:39 +05:30
file_hash: 'ABC',
parallel_diff_lines: [
2018-11-20 20:47:30 +05:30
{
left: {
2019-02-15 15:39:39 +05:30
line_code: 'ABC_1',
2018-11-20 20:47:30 +05:30
discussions: [
{
id: 1,
line_code: 'ABC_1',
2019-02-15 15:39:39 +05:30
notes: [],
2018-11-20 20:47:30 +05:30
},
{
id: 2,
line_code: 'ABC_1',
2019-02-15 15:39:39 +05:30
notes: [],
2018-11-20 20:47:30 +05:30
},
],
},
right: {
2019-02-15 15:39:39 +05:30
line_code: 'ABC_1',
2018-11-20 20:47:30 +05:30
discussions: [],
},
},
],
2019-02-15 15:39:39 +05:30
highlighted_diff_lines: [
2018-11-20 20:47:30 +05:30
{
2019-02-15 15:39:39 +05:30
line_code: 'ABC_1',
2018-11-20 20:47:30 +05:30
discussions: [
{
id: 1,
line_code: 'ABC_1',
2019-02-15 15:39:39 +05:30
notes: [],
2018-11-20 20:47:30 +05:30
},
{
id: 2,
line_code: 'ABC_1',
2019-02-15 15:39:39 +05:30
notes: [],
2018-11-20 20:47:30 +05:30
},
],
},
],
},
],
};
mutations[types.REMOVE_LINE_DISCUSSIONS_FOR_FILE](state, {
fileHash: 'ABC',
lineCode: 'ABC_1',
});
2018-12-13 13:39:08 +05:30
2019-02-15 15:39:39 +05:30
expect(state.diffFiles[0].parallel_diff_lines[0].left.discussions.length).toEqual(0);
expect(state.diffFiles[0].highlighted_diff_lines[0].discussions.length).toEqual(0);
2018-11-20 20:47:30 +05:30
});
});
2018-12-05 23:21:45 +05:30
describe('TOGGLE_FOLDER_OPEN', () => {
it('toggles entry opened prop', () => {
const state = {
treeEntries: {
path: {
opened: false,
},
},
};
mutations[types.TOGGLE_FOLDER_OPEN](state, 'path');
expect(state.treeEntries.path.opened).toBe(true);
});
});
describe('TOGGLE_SHOW_TREE_LIST', () => {
it('toggles showTreeList', () => {
const state = createState();
mutations[types.TOGGLE_SHOW_TREE_LIST](state);
expect(state.showTreeList).toBe(false, 'Failed to toggle showTreeList to false');
mutations[types.TOGGLE_SHOW_TREE_LIST](state);
expect(state.showTreeList).toBe(true, 'Failed to toggle showTreeList to true');
});
});
describe('UPDATE_CURRENT_DIFF_FILE_ID', () => {
it('updates currentDiffFileId', () => {
const state = createState();
mutations[types.UPDATE_CURRENT_DIFF_FILE_ID](state, 'somefileid');
expect(state.currentDiffFileId).toBe('somefileid');
});
});
2019-02-15 15:39:39 +05:30
describe('Set highlighted row', () => {
it('sets highlighted row', () => {
const state = createState();
mutations[types.SET_HIGHLIGHTED_ROW](state, 'ABC_123');
expect(state.highlightedRow).toBe('ABC_123');
});
});
describe('TOGGLE_LINE_HAS_FORM', () => {
it('sets hasForm on lines', () => {
const file = {
file_hash: 'hash',
parallel_diff_lines: [
{ left: { line_code: '123', hasForm: false }, right: {} },
{ left: {}, right: { line_code: '124', hasForm: false } },
],
highlighted_diff_lines: [
{ line_code: '123', hasForm: false },
{ line_code: '124', hasForm: false },
],
};
const state = {
diffFiles: [file],
};
mutations[types.TOGGLE_LINE_HAS_FORM](state, {
lineCode: '123',
hasForm: true,
fileHash: 'hash',
});
expect(file.highlighted_diff_lines[0].hasForm).toBe(true);
expect(file.highlighted_diff_lines[1].hasForm).toBe(false);
expect(file.parallel_diff_lines[0].left.hasForm).toBe(true);
expect(file.parallel_diff_lines[1].right.hasForm).toBe(false);
});
});
2019-03-02 22:35:43 +05:30
describe('SET_TREE_DATA', () => {
it('sets treeEntries and tree in state', () => {
const state = {
treeEntries: {},
tree: [],
};
mutations[types.SET_TREE_DATA](state, {
treeEntries: { file: { name: 'index.js' } },
tree: ['tree'],
});
expect(state.treeEntries).toEqual({
file: {
name: 'index.js',
},
});
expect(state.tree).toEqual(['tree']);
});
});
describe('SET_RENDER_TREE_LIST', () => {
it('sets renderTreeList', () => {
const state = {
renderTreeList: true,
};
mutations[types.SET_RENDER_TREE_LIST](state, false);
expect(state.renderTreeList).toBe(false);
});
});
describe('SET_SHOW_WHITESPACE', () => {
it('sets showWhitespace', () => {
const state = {
showWhitespace: true,
};
mutations[types.SET_SHOW_WHITESPACE](state, false);
expect(state.showWhitespace).toBe(false);
});
});
2018-11-08 19:23:39 +05:30
});