Commit 6865570d authored by Thomas Randolph's avatar Thomas Randolph

Add MR File Reviews to Vuex

Also adds a getter to sort the reviews into
an array organized in the same order as
diffFiles.
parent 7a3ca41a
......@@ -50,6 +50,8 @@ import {
} from '../constants';
import { diffViewerModes } from '~/ide/constants';
import { isCollapsed } from '../utils/diff_file';
import { getDerivedMergeRequestInformation } from '../utils/merge_request';
import { markFileReview, setReviewsForMergeRequest } from '../utils/file_reviews';
export const setBaseConfig = ({ commit }, options) => {
const {
......@@ -61,6 +63,7 @@ export const setBaseConfig = ({ commit }, options) => {
dismissEndpoint,
showSuggestPopover,
viewDiffsFileByFile,
mrReviews,
} = options;
commit(types.SET_BASE_CONFIG, {
endpoint,
......@@ -71,6 +74,7 @@ export const setBaseConfig = ({ commit }, options) => {
dismissEndpoint,
showSuggestPopover,
viewDiffsFileByFile,
mrReviews,
});
};
......@@ -741,3 +745,13 @@ export const setFileByFile = ({ commit }, { fileByFile }) => {
mergeUrlParams({ [DIFF_FILE_BY_FILE_COOKIE_NAME]: fileViewMode }, window.location.href),
);
};
export function reviewFile({ commit, state, getters }, { file, reviewed = true }) {
const { mrPath } = getDerivedMergeRequestInformation({ endpoint: file.load_collapsed_diff_url });
const reviews = setReviewsForMergeRequest(
mrPath,
markFileReview(getters.fileReviews(state), file, reviewed),
);
commit(types.SET_MR_FILE_REVIEWS, reviews);
}
import { __, n__ } from '~/locale';
import { parallelizeDiffLines } from './utils';
import { isFileReviewed } from '../utils/file_reviews';
import {
PARALLEL_DIFF_VIEW_TYPE,
INLINE_DIFF_VIEW_TYPE,
......@@ -149,3 +150,7 @@ export const diffLines = state => (file, unifiedDiffComponents) => {
state.diffViewType === INLINE_DIFF_VIEW_TYPE,
);
};
export function fileReviews(state) {
return state.diffFiles.map(file => isFileReviewed(state.mrReviews, file));
}
......@@ -45,4 +45,5 @@ export default () => ({
fileFinderVisible: false,
dismissEndpoint: '',
showSuggestPopover: true,
mrReviews: {},
});
......@@ -7,6 +7,8 @@ export const SET_DIFF_METADATA = 'SET_DIFF_METADATA';
export const SET_DIFF_DATA_BATCH = 'SET_DIFF_DATA_BATCH';
export const SET_DIFF_FILES = 'SET_DIFF_FILES';
export const SET_MR_FILE_REVIEWS = 'SET_MR_FILE_REVIEWS';
export const SET_DIFF_VIEW_TYPE = 'SET_DIFF_VIEW_TYPE';
export const SET_COVERAGE_DATA = 'SET_COVERAGE_DATA';
export const SET_MERGE_REQUEST_DIFFS = 'SET_MERGE_REQUEST_DIFFS';
......
......@@ -37,6 +37,7 @@ export default {
dismissEndpoint,
showSuggestPopover,
viewDiffsFileByFile,
mrReviews,
} = options;
Object.assign(state, {
endpoint,
......@@ -47,6 +48,7 @@ export default {
dismissEndpoint,
showSuggestPopover,
viewDiffsFileByFile,
mrReviews,
});
},
......@@ -353,4 +355,7 @@ export default {
[types.SET_FILE_BY_FILE](state, fileByFile) {
state.viewDiffsFileByFile = fileByFile;
},
[types.SET_MR_FILE_REVIEWS](state, newReviews) {
state.mrReviews = newReviews;
},
};
......@@ -49,6 +49,7 @@ import {
setCurrentDiffFileIdFromNote,
navigateToDiffFileIndex,
setFileByFile,
reviewFile,
} from '~/diffs/store/actions';
import eventHub from '~/notes/event_hub';
import * as types from '~/diffs/store/mutation_types';
......@@ -1472,4 +1473,46 @@ describe('DiffsStoreActions', () => {
);
});
});
describe('reviewFile', () => {
const file = {
id: '123',
file_identifier_hash: 'abc',
load_collapsed_diff_url: 'gitlab-org/gitlab-test/-/merge_requests/1/diffs',
};
it.each`
reviews | diffFile | reviewed
${{ abc: ['123'] }} | ${file} | ${true}
${{}} | ${file} | ${false}
`(
'sets reviews ($reviews) to localStorage and state for file $file if it is marked reviewed=$reviewed',
({ reviews, diffFile, reviewed }) => {
const commitSpy = jest.fn();
const getterSpy = jest.fn().mockReturnValue([]);
reviewFile(
{
commit: commitSpy,
getters: {
fileReviews: getterSpy,
},
state: {
mrReviews: { abc: ['123'] },
},
},
{
file: diffFile,
reviewed,
},
);
expect(localStorage.setItem).toHaveBeenCalledTimes(1);
expect(localStorage.setItem).toHaveBeenCalledWith(
'gitlab-org/gitlab-test/-/merge_requests/1-file-reviews',
JSON.stringify(reviews),
);
expect(commitSpy).toHaveBeenCalledWith(types.SET_MR_FILE_REVIEWS, reviews);
},
);
});
});
......@@ -372,4 +372,26 @@ describe('Diffs Module Getters', () => {
});
});
});
describe('fileReviews', () => {
const file1 = { id: '123', file_identifier_hash: 'abc' };
const file2 = { id: '098', file_identifier_hash: 'abc' };
it.each`
reviews | files | fileReviews
${{}} | ${[file1, file2]} | ${[false, false]}
${{ abc: ['123'] }} | ${[file1, file2]} | ${[true, false]}
${{ abc: ['098'] }} | ${[file1, file2]} | ${[false, true]}
${{ def: ['123'] }} | ${[file1, file2]} | ${[false, false]}
${{ abc: ['123'], def: ['098'] }} | ${[]} | ${[]}
`(
'returns $fileReviews based on the diff files in state and the existing reviews $reviews',
({ reviews, files, fileReviews }) => {
localState.diffFiles = files;
localState.mrReviews = reviews;
expect(getters.fileReviews(localState)).toStrictEqual(fileReviews);
},
);
});
});
......@@ -906,4 +906,19 @@ describe('DiffsStoreMutations', () => {
expect(state.viewDiffsFileByFile).toBe(value);
});
});
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);
});
});
});
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment