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

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

961 lines
26 KiB
JavaScript
Raw Normal View History

2021-03-11 19:13:27 +05:30
import { INLINE_DIFF_VIEW_TYPE, INLINE_DIFF_LINES_KEY } from '~/diffs/constants';
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 * as types from '~/diffs/store/mutation_types';
2021-03-11 19:13:27 +05:30
import mutations from '~/diffs/store/mutations';
2020-04-22 19:07:51 +05:30
import * as utils from '~/diffs/store/utils';
2022-08-13 15:12:31 +05:30
import { getDiffFileMock } from '../mock_data/diff_file';
2018-11-08 19:23:39 +05:30
describe('DiffsStoreMutations', () => {
describe('SET_BASE_CONFIG', () => {
2021-04-29 21:17:54 +05:30
it.each`
prop | value
${'endpoint'} | ${'/diffs/endpoint'}
${'projectPath'} | ${'/root/project'}
${'endpointUpdateUser'} | ${'/user/preferences'}
`('should set the $prop property into state', ({ prop, value }) => {
2018-11-08 19:23:39 +05:30
const state = {};
2021-04-29 21:17:54 +05:30
mutations[types.SET_BASE_CONFIG](state, { [prop]: value });
2018-12-13 13:39:08 +05:30
2021-04-29 21:17:54 +05:30
expect(state[prop]).toEqual(value);
2018-11-08 19:23:39 +05:30
});
});
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);
});
});
2021-11-11 11:23:49 +05:30
describe('SET_BATCH_LOADING_STATE', () => {
2020-01-01 13:55:28 +05:30
it('should set loading state', () => {
const state = {};
2021-11-11 11:23:49 +05:30
mutations[types.SET_BATCH_LOADING_STATE](state, false);
2020-01-01 13:55:28 +05:30
2021-11-11 11:23:49 +05:30
expect(state.batchLoadingState).toEqual(false);
2020-01-01 13:55:28 +05:30
});
});
2020-03-13 15:44:24 +05:30
describe('SET_RETRIEVING_BATCHES', () => {
it('should set retrievingBatches state', () => {
const state = {};
mutations[types.SET_RETRIEVING_BATCHES](state, false);
expect(state.retrievingBatches).toEqual(false);
});
});
2020-04-22 19:07:51 +05:30
describe('SET_DIFF_FILES', () => {
it('should set diffFiles in state', () => {
const state = {};
mutations[types.SET_DIFF_FILES](state, ['file', 'another file']);
expect(state.diffFiles.length).toEqual(2);
});
it('should not set anything except diffFiles in state', () => {
const state = {};
mutations[types.SET_DIFF_FILES](state, ['file', 'another file']);
expect(Object.keys(state)).toEqual(['diffFiles']);
});
});
2021-02-22 17:27:13 +05:30
describe('SET_DIFF_METADATA', () => {
it('should overwrite state with the camelCased data that is passed in', () => {
2022-08-13 15:12:31 +05:30
const diffFileMockData = getDiffFileMock();
2020-03-13 15:44:24 +05:30
const state = {
2021-02-22 17:27:13 +05:30
diffFiles: [],
2020-03-13 15:44:24 +05:30
};
2018-11-20 20:47:30 +05:30
const diffMock = {
diff_files: [diffFileMockData],
};
2021-02-22 17:27:13 +05:30
const metaMock = {
other_key: 'value',
};
2018-11-20 20:47:30 +05:30
2021-02-22 17:27:13 +05:30
mutations[types.SET_DIFF_METADATA](state, diffMock);
expect(state.diffFiles[0]).toEqual(diffFileMockData);
2018-11-20 20:47:30 +05:30
2021-02-22 17:27:13 +05:30
mutations[types.SET_DIFF_METADATA](state, metaMock);
expect(state.diffFiles[0]).toEqual(diffFileMockData);
expect(state.otherKey).toEqual('value');
2020-03-13 15:44:24 +05:30
});
2020-01-01 13:55:28 +05:30
});
2021-02-22 17:27:13 +05:30
describe('SET_DIFF_DATA_BATCH_DATA', () => {
2020-01-01 13:55:28 +05:30
it('should set diff data batch type properly', () => {
2023-05-27 22:25:52 +05:30
const mockFile = getDiffFileMock();
const state = {
diffFiles: [],
treeEntries: { [mockFile.file_path]: { fileHash: mockFile.file_hash } },
};
2020-01-01 13:55:28 +05:30
const diffMock = {
2023-05-27 22:25:52 +05:30
diff_files: [mockFile],
2020-01-01 13:55:28 +05:30
};
mutations[types.SET_DIFF_DATA_BATCH](state, diffMock);
expect(state.diffFiles[0].renderIt).toEqual(true);
2018-11-20 20:47:30 +05:30
expect(state.diffFiles[0].collapsed).toEqual(false);
2023-05-27 22:25:52 +05:30
expect(state.treeEntries[mockFile.file_path].diffLoaded).toBe(true);
2018-11-20 20:47:30 +05:30
});
});
2020-04-08 14:13:33 +05:30
describe('SET_COVERAGE_DATA', () => {
it('should set coverage data properly', () => {
const state = { coverageFiles: {} };
2021-03-08 18:12:59 +05:30
const coverage = { 'app.js': { 1: 0, 2: 1 } };
2020-04-08 14:13:33 +05:30
mutations[types.SET_COVERAGE_DATA](state, coverage);
expect(state.coverageFiles).toEqual(coverage);
2022-01-26 12:08:38 +05:30
expect(state.coverageLoaded).toEqual(true);
2020-04-08 14:13:33 +05:30
});
});
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('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: [
2022-07-16 23:28:13 +05:30
{
old_line: 1,
new_line: 1,
line_code: 'ff9200_1_1',
discussions: [],
hasForm: false,
type: 'expanded',
},
2019-02-15 15:39:39 +05:30
],
2018-11-08 19:23:39 +05:30
fileHash: 'ff9200',
params: {
bottom: true,
},
2019-10-12 21:52:04 +05:30
isExpandDown: false,
nextLineNumbers: {},
2018-11-08 19:23:39 +05:30
};
const diffFile = {
2019-02-15 15:39:39 +05:30
file_hash: options.fileHash,
2021-02-22 17:27:13 +05:30
[INLINE_DIFF_LINES_KEY]: [],
2018-11-08 19:23:39 +05:30
};
2020-04-08 14:13:33 +05:30
const state = { diffFiles: [diffFile], diffViewType: 'viewType' };
2019-02-15 15:39:39 +05:30
const lines = [{ old_line: 1, new_line: 1 }];
2018-11-08 19:23:39 +05:30
2020-04-22 19:07:51 +05:30
jest.spyOn(utils, 'findDiffFile').mockImplementation(() => diffFile);
jest.spyOn(utils, 'removeMatchLine').mockImplementation(() => null);
jest.spyOn(utils, 'addLineReferences').mockImplementation(() => lines);
jest.spyOn(utils, 'addContextLines').mockImplementation(() => null);
2018-11-08 19:23:39 +05:30
mutations[types.ADD_CONTEXT_LINES](state, options);
2020-04-22 19:07:51 +05:30
expect(utils.findDiffFile).toHaveBeenCalledWith(state.diffFiles, options.fileHash);
expect(utils.removeMatchLine).toHaveBeenCalledWith(
2018-11-08 19:23:39 +05:30
diffFile,
options.lineNumbers,
options.params.bottom,
);
2018-12-13 13:39:08 +05:30
2020-04-22 19:07:51 +05:30
expect(utils.addLineReferences).toHaveBeenCalledWith(
2018-11-08 19:23:39 +05:30
options.contextLines,
options.lineNumbers,
options.params.bottom,
2019-10-12 21:52:04 +05:30
options.isExpandDown,
options.nextLineNumbers,
2018-11-08 19:23:39 +05:30
);
2018-12-13 13:39:08 +05:30
2020-04-22 19:07:51 +05:30
expect(utils.addContextLines).toHaveBeenCalledWith({
2021-02-22 17:27:13 +05:30
inlineLines: diffFile[INLINE_DIFF_LINES_KEY],
2018-11-08 19:23:39 +05:30
contextLines: options.contextLines,
bottom: options.params.bottom,
lineNumbers: options.lineNumbers,
2019-10-12 21:52:04 +05:30
isExpandDown: false,
2018-11-08 19:23:39 +05:30
});
});
});
describe('ADD_COLLAPSED_DIFFS', () => {
it('should update the state with the given data for the given file hash', () => {
const fileHash = 123;
2019-07-07 11:18:12 +05:30
const state = {
2020-03-13 15:44:24 +05:30
diffFiles: [{}, { content_sha: 'abc', file_hash: fileHash, existing_field: 0 }],
2019-07-07 11:18:12 +05:30
};
const data = {
diff_files: [
2020-03-13 15:44:24 +05:30
{
content_sha: 'abc',
file_hash: fileHash,
extra_field: 1,
existing_field: 1,
viewer: { name: 'text' },
},
2019-07-07 11:18:12 +05:30
],
};
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',
2021-02-22 17:27:13 +05:30
[INLINE_DIFF_LINES_KEY]: [
2019-02-15 15:39:39 +05:30
{
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,
});
2021-02-22 17:27:13 +05:30
expect(state.diffFiles[0][INLINE_DIFF_LINES_KEY][0].discussions.length).toEqual(1);
expect(state.diffFiles[0][INLINE_DIFF_LINES_KEY][0].discussions[0].id).toEqual(1);
2019-02-15 15:39:39 +05:30
});
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',
2021-02-22 17:27:13 +05:30
[INLINE_DIFF_LINES_KEY]: [
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,
});
2021-02-22 17:27:13 +05:30
expect(state.diffFiles[0][INLINE_DIFF_LINES_KEY][0].discussions.length).toEqual(1);
expect(state.diffFiles[0][INLINE_DIFF_LINES_KEY][0].discussions[0].id).toEqual(1);
2019-02-15 15:39:39 +05:30
mutations[types.SET_LINE_DISCUSSIONS_FOR_FILE](state, {
discussion,
diffPositionByLineCode,
});
2021-02-22 17:27:13 +05:30
expect(state.diffFiles[0][INLINE_DIFF_LINES_KEY][0].discussions.length).toEqual(1);
expect(state.diffFiles[0][INLINE_DIFF_LINES_KEY][0].discussions[0].id).toEqual(1);
2019-02-15 15:39:39 +05:30
});
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',
2021-02-22 17:27:13 +05:30
[INLINE_DIFF_LINES_KEY]: [
2019-02-15 15:39:39 +05:30
{
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,
});
2021-02-22 17:27:13 +05:30
expect(state.diffFiles[0][INLINE_DIFF_LINES_KEY][0].discussions.length).toEqual(1);
expect(state.diffFiles[0][INLINE_DIFF_LINES_KEY][0].discussions[0].id).toEqual(1);
2019-02-15 15:39:39 +05:30
mutations[types.SET_LINE_DISCUSSIONS_FOR_FILE](state, {
discussion: {
...discussion,
resolved: true,
notes: ['test'],
},
diffPositionByLineCode,
});
2021-02-22 17:27:13 +05:30
expect(state.diffFiles[0][INLINE_DIFF_LINES_KEY][0].discussions[0].notes.length).toBe(1);
expect(state.diffFiles[0][INLINE_DIFF_LINES_KEY][0].discussions[0].resolved).toBe(true);
2019-02-15 15:39:39 +05:30
});
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',
2021-02-22 17:27:13 +05:30
[INLINE_DIFF_LINES_KEY]: [
2019-02-15 15:39:39 +05:30
{
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,
});
2021-02-22 17:27:13 +05:30
expect(state.diffFiles[0][INLINE_DIFF_LINES_KEY][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',
2021-02-22 17:27:13 +05:30
[INLINE_DIFF_LINES_KEY]: [
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,
});
2021-02-22 17:27:13 +05:30
expect(state.diffFiles[0][INLINE_DIFF_LINES_KEY][0].discussions.length).toEqual(1);
expect(state.diffFiles[0][INLINE_DIFF_LINES_KEY][0].discussions[0].id).toEqual(1);
2018-12-05 23:21:45 +05:30
});
2020-04-22 19:07:51 +05:30
it('should add discussions by line_codes and positions attributes', () => {
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',
2021-02-22 17:27:13 +05:30
[INLINE_DIFF_LINES_KEY]: [
2020-04-22 19:07:51 +05:30
{
line_code: 'ABC_1',
discussions: [],
},
],
},
],
};
const discussion = {
id: 1,
line_code: 'ABC_2',
line_codes: ['ABC_1'],
diff_discussion: true,
resolvable: true,
original_position: {},
position: {},
positions: [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,
});
2021-02-22 17:27:13 +05:30
expect(state.diffFiles[0][INLINE_DIFF_LINES_KEY][0].discussions).toHaveLength(1);
expect(state.diffFiles[0][INLINE_DIFF_LINES_KEY][0].discussions[0].id).toBe(1);
2020-04-22 19:07:51 +05:30
});
2020-11-24 15:15:51 +05:30
it('should add discussion to file', () => {
const state = {
latestDiff: true,
diffFiles: [
{
file_hash: 'ABC',
discussions: [],
2021-02-22 17:27:13 +05:30
[INLINE_DIFF_LINES_KEY]: [],
2020-11-24 15:15:51 +05:30
},
],
};
const discussion = {
id: 1,
line_code: 'ABC_1',
diff_discussion: true,
resolvable: true,
diff_file: {
file_hash: state.diffFiles[0].file_hash,
},
};
mutations[types.SET_LINE_DISCUSSIONS_FOR_FILE](state, {
discussion,
diffPositionByLineCode: null,
});
expect(state.diffFiles[0].discussions.length).toEqual(1);
});
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',
2021-02-22 17:27:13 +05:30
[INLINE_DIFF_LINES_KEY]: [
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
2021-02-22 17:27:13 +05:30
expect(state.diffFiles[0][INLINE_DIFF_LINES_KEY][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);
});
});
2021-02-22 17:27:13 +05:30
describe('SET_SHOW_TREE_LIST', () => {
it('sets showTreeList', () => {
2018-12-05 23:21:45 +05:30
const state = createState();
2021-02-22 17:27:13 +05:30
mutations[types.SET_SHOW_TREE_LIST](state, true);
2018-12-05 23:21:45 +05:30
expect(state.showTreeList).toBe(true, 'Failed to toggle showTreeList to true');
});
});
2021-12-11 22:18:48 +05:30
describe('SET_CURRENT_DIFF_FILE', () => {
2018-12-05 23:21:45 +05:30
it('updates currentDiffFileId', () => {
const state = createState();
2021-12-11 22:18:48 +05:30
mutations[types.SET_CURRENT_DIFF_FILE](state, 'somefileid');
2018-12-05 23:21:45 +05:30
expect(state.currentDiffFileId).toBe('somefileid');
});
});
2019-02-15 15:39:39 +05:30
2021-12-11 22:18:48 +05:30
describe('SET_DIFF_FILE_VIEWED', () => {
let state;
beforeEach(() => {
state = {
viewedDiffFileIds: { 123: true },
};
});
it.each`
id | bool | outcome
${'abc'} | ${true} | ${{ 123: true, abc: true }}
${'123'} | ${false} | ${{ 123: false }}
`('sets the viewed files list to $bool for the id $id', ({ id, bool, outcome }) => {
mutations[types.SET_DIFF_FILE_VIEWED](state, { id, seen: bool });
expect(state.viewedDiffFileIds).toEqual(outcome);
});
});
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',
2021-02-22 17:27:13 +05:30
[INLINE_DIFF_LINES_KEY]: [
2019-02-15 15:39:39 +05:30
{ 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',
});
2021-02-22 17:27:13 +05:30
expect(file[INLINE_DIFF_LINES_KEY][0].hasForm).toBe(true);
expect(file[INLINE_DIFF_LINES_KEY][1].hasForm).toBe(false);
2019-02-15 15:39:39 +05:30
});
});
2019-03-02 22:35:43 +05:30
describe('SET_TREE_DATA', () => {
it('sets treeEntries and tree in state', () => {
const state = {
treeEntries: {},
tree: [],
2020-10-24 23:57:45 +05:30
isTreeLoaded: false,
2019-03-02 22:35:43 +05:30
};
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']);
2020-10-24 23:57:45 +05:30
expect(state.isTreeLoaded).toEqual(true);
2019-03-02 22:35:43 +05:30
});
});
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,
2020-04-22 19:07:51 +05:30
diffFiles: ['test'],
2019-03-02 22:35:43 +05:30
};
mutations[types.SET_SHOW_WHITESPACE](state, false);
expect(state.showWhitespace).toBe(false);
2020-04-22 19:07:51 +05:30
expect(state.diffFiles).toEqual([]);
2019-03-02 22:35:43 +05:30
});
});
2019-07-07 11:18:12 +05:30
describe('REQUEST_FULL_DIFF', () => {
it('sets isLoadingFullFile to true', () => {
const state = {
diffFiles: [{ file_path: 'test', isLoadingFullFile: false }],
};
mutations[types.REQUEST_FULL_DIFF](state, 'test');
expect(state.diffFiles[0].isLoadingFullFile).toBe(true);
});
});
describe('RECEIVE_FULL_DIFF_ERROR', () => {
it('sets isLoadingFullFile to false', () => {
const state = {
diffFiles: [{ file_path: 'test', isLoadingFullFile: true }],
};
mutations[types.RECEIVE_FULL_DIFF_ERROR](state, 'test');
expect(state.diffFiles[0].isLoadingFullFile).toBe(false);
});
});
describe('RECEIVE_FULL_DIFF_SUCCESS', () => {
it('sets isLoadingFullFile to false', () => {
const state = {
diffFiles: [
{
file_path: 'test',
isLoadingFullFile: true,
isShowingFullFile: false,
2021-02-22 17:27:13 +05:30
[INLINE_DIFF_LINES_KEY]: [],
2019-07-07 11:18:12 +05:30
},
],
};
mutations[types.RECEIVE_FULL_DIFF_SUCCESS](state, { filePath: 'test', data: [] });
expect(state.diffFiles[0].isLoadingFullFile).toBe(false);
});
it('sets isShowingFullFile to true', () => {
const state = {
diffFiles: [
{
file_path: 'test',
isLoadingFullFile: true,
isShowingFullFile: false,
2021-02-22 17:27:13 +05:30
[INLINE_DIFF_LINES_KEY]: [],
2019-07-07 11:18:12 +05:30
},
],
};
mutations[types.RECEIVE_FULL_DIFF_SUCCESS](state, { filePath: 'test', data: [] });
expect(state.diffFiles[0].isShowingFullFile).toBe(true);
});
});
describe('SET_FILE_COLLAPSED', () => {
it('sets collapsed', () => {
const state = {
2021-01-03 14:25:43 +05:30
diffFiles: [{ file_path: 'test', viewer: { automaticallyCollapsed: false } }],
2019-07-07 11:18:12 +05:30
};
mutations[types.SET_FILE_COLLAPSED](state, { filePath: 'test', collapsed: true });
2021-01-03 14:25:43 +05:30
expect(state.diffFiles[0].viewer.automaticallyCollapsed).toBe(true);
2019-07-07 11:18:12 +05:30
});
});
2019-07-31 22:56:46 +05:30
describe('SET_CURRENT_VIEW_DIFF_FILE_LINES', () => {
2021-02-22 17:27:13 +05:30
it(`sets the highlighted lines`, () => {
const file = { file_path: 'test', [INLINE_DIFF_LINES_KEY]: [] };
const state = {
diffFiles: [file],
};
mutations[types.SET_CURRENT_VIEW_DIFF_FILE_LINES](state, {
filePath: 'test',
lines: ['test'],
2019-07-31 22:56:46 +05:30
});
2021-02-22 17:27:13 +05:30
expect(file[INLINE_DIFF_LINES_KEY]).toEqual(['test']);
2019-07-31 22:56:46 +05:30
});
});
describe('ADD_CURRENT_VIEW_DIFF_FILE_LINES', () => {
2021-02-22 17:27:13 +05:30
it('pushes to inline lines', () => {
const file = { file_path: 'test', [INLINE_DIFF_LINES_KEY]: [] };
const state = {
diffFiles: [file],
};
mutations[types.ADD_CURRENT_VIEW_DIFF_FILE_LINES](state, {
filePath: 'test',
line: 'test',
});
expect(file[INLINE_DIFF_LINES_KEY]).toEqual(['test']);
mutations[types.ADD_CURRENT_VIEW_DIFF_FILE_LINES](state, {
filePath: 'test',
line: 'test2',
2019-07-31 22:56:46 +05:30
});
2021-02-22 17:27:13 +05:30
expect(file[INLINE_DIFF_LINES_KEY]).toEqual(['test', 'test2']);
2019-07-31 22:56:46 +05:30
});
});
describe('TOGGLE_DIFF_FILE_RENDERING_MORE', () => {
it('toggles renderingLines on file', () => {
const file = { file_path: 'test', renderingLines: false };
const state = {
diffFiles: [file],
};
mutations[types.TOGGLE_DIFF_FILE_RENDERING_MORE](state, 'test');
expect(file.renderingLines).toBe(true);
mutations[types.TOGGLE_DIFF_FILE_RENDERING_MORE](state, 'test');
expect(file.renderingLines).toBe(false);
});
});
2019-09-04 21:01:54 +05:30
2020-05-24 23:13:21 +05:30
describe('SET_DIFF_FILE_VIEWER', () => {
it("should update the correct diffFile's viewer property", () => {
const state = {
diffFiles: [
{ file_path: 'SearchString', viewer: 'OLD VIEWER' },
{ file_path: 'OtherSearchString' },
{ file_path: 'SomeOtherString' },
],
};
mutations[types.SET_DIFF_FILE_VIEWER](state, {
filePath: 'SearchString',
viewer: 'NEW VIEWER',
});
expect(state.diffFiles[0].viewer).toEqual('NEW VIEWER');
expect(state.diffFiles[1].viewer).not.toBeDefined();
expect(state.diffFiles[2].viewer).not.toBeDefined();
mutations[types.SET_DIFF_FILE_VIEWER](state, {
filePath: 'OtherSearchString',
viewer: 'NEW VIEWER',
});
expect(state.diffFiles[0].viewer).toEqual('NEW VIEWER');
expect(state.diffFiles[1].viewer).toEqual('NEW VIEWER');
expect(state.diffFiles[2].viewer).not.toBeDefined();
});
});
2019-09-04 21:01:54 +05:30
describe('SET_SHOW_SUGGEST_POPOVER', () => {
it('sets showSuggestPopover to false', () => {
const state = { showSuggestPopover: true };
mutations[types.SET_SHOW_SUGGEST_POPOVER](state);
expect(state.showSuggestPopover).toBe(false);
});
});
2021-02-22 17:27:13 +05:30
describe('SET_FILE_BY_FILE', () => {
it.each`
value | opposite
${true} | ${false}
${false} | ${true}
`('sets viewDiffsFileByFile to $value', ({ value, opposite }) => {
const state = { viewDiffsFileByFile: opposite };
mutations[types.SET_FILE_BY_FILE](state, value);
expect(state.viewDiffsFileByFile).toBe(value);
});
});
2021-03-08 18:12:59 +05:30
describe('SET_MR_FILE_REVIEWS', () => {
it.each`
newReviews | oldReviews
${{ abc: ['123'] }} | ${{}}
${{ abc: [] }} | ${{ abc: ['123'] }}
${{}} | ${{ abc: ['123'] }}
`('sets mrReviews to $newReviews', ({ newReviews, oldReviews }) => {
const state = { mrReviews: oldReviews };
mutations[types.SET_MR_FILE_REVIEWS](state, newReviews);
expect(state.mrReviews).toStrictEqual(newReviews);
});
});
2018-11-08 19:23:39 +05:30
});