Commit 7a3ca41a authored by Thomas Randolph's avatar Thomas Randolph

Add utilities for file reviews

Also adds helpers for getting MR information
only available within hard-coded URLs
parent f0fc6785
function getFileReviewsKey(mrPath) {
return `${mrPath}-file-reviews`;
}
export function getReviewsForMergeRequest(mrPath) {
const reviewsForMr = localStorage.getItem(getFileReviewsKey(mrPath));
let reviews = {};
if (reviewsForMr) {
try {
reviews = JSON.parse(reviewsForMr);
} catch (err) {
reviews = {};
}
}
return reviews;
}
export function setReviewsForMergeRequest(mrPath, reviews) {
localStorage.setItem(getFileReviewsKey(mrPath), JSON.stringify(reviews));
return reviews;
}
export function isFileReviewed(reviews, file) {
const fileReviews = reviews[file.file_identifier_hash];
return file?.id && fileReviews?.length ? new Set(fileReviews).has(file.id) : false;
}
export function reviewable(file) {
return Boolean(file.id) && Boolean(file.file_identifier_hash);
}
export function markFileReview(reviews, file, reviewed = true) {
const usableReviews = { ...(reviews || {}) };
let updatedReviews = usableReviews;
let fileReviews;
if (reviewable(file)) {
fileReviews = new Set([...(usableReviews[file.file_identifier_hash] || [])]);
if (reviewed) {
fileReviews.add(file.id);
} else {
fileReviews.delete(file.id);
}
updatedReviews = {
...usableReviews,
[file.file_identifier_hash]: Array.from(fileReviews),
};
if (updatedReviews[file.file_identifier_hash].length === 0) {
delete updatedReviews[file.file_identifier_hash];
}
}
return updatedReviews;
}
export function getDerivedMergeRequestInformation({ endpoint } = {}) {
const mrPath = endpoint
?.split('/')
.slice(0, -1)
.join('/');
return {
mrPath,
};
}
import { useLocalStorageSpy } from 'helpers/local_storage_helper';
import {
getReviewsForMergeRequest,
setReviewsForMergeRequest,
isFileReviewed,
markFileReview,
reviewable,
} from '~/diffs/utils/file_reviews';
function getDefaultReviews() {
return {
abc: ['123', '098'],
};
}
describe('File Review(s) utilities', () => {
const mrPath = 'my/fake/mr/42';
const storageKey = `${mrPath}-file-reviews`;
const file = { id: '123', file_identifier_hash: 'abc' };
const storedValue = JSON.stringify(getDefaultReviews());
let reviews;
useLocalStorageSpy();
beforeEach(() => {
reviews = getDefaultReviews();
localStorage.clear();
});
describe('getReviewsForMergeRequest', () => {
it('fetches the appropriate stored reviews from localStorage', () => {
getReviewsForMergeRequest(mrPath);
expect(localStorage.getItem).toHaveBeenCalledTimes(1);
expect(localStorage.getItem).toHaveBeenCalledWith(storageKey);
});
it('returns an empty object if there have never been stored reviews for this MR', () => {
expect(getReviewsForMergeRequest(mrPath)).toStrictEqual({});
});
it.each`
data
${'+++'}
${'{ lookinGood: "yeah!", missingClosingBrace: "yeah :(" '}
`(
"returns an empty object if the stored reviews are corrupted/aren't parseable as JSON (like: $data)",
({ data }) => {
localStorage.getItem.mockReturnValueOnce(data);
expect(getReviewsForMergeRequest(mrPath)).toStrictEqual({});
},
);
it('fetches the reviews for the MR if they exist', () => {
localStorage.setItem(storageKey, storedValue);
expect(getReviewsForMergeRequest(mrPath)).toStrictEqual(reviews);
});
});
describe('setReviewsForMergeRequest', () => {
it('sets the new value to localStorage', () => {
setReviewsForMergeRequest(mrPath, reviews);
expect(localStorage.setItem).toHaveBeenCalledTimes(1);
expect(localStorage.setItem).toHaveBeenCalledWith(storageKey, storedValue);
});
it('returns the new value for chainability', () => {
expect(setReviewsForMergeRequest(mrPath, reviews)).toStrictEqual(reviews);
});
});
describe('isFileReviewed', () => {
it.each`
description | diffFile | fileReviews
${'the file does not have an `id`'} | ${{ ...file, id: undefined }} | ${getDefaultReviews()}
${'there are no reviews for the file'} | ${file} | ${{ ...getDefaultReviews(), abc: undefined }}
`('returns `false` if $description', ({ diffFile, fileReviews }) => {
expect(isFileReviewed(fileReviews, diffFile)).toBe(false);
});
it("returns `true` for a file if it's available in the provided reviews", () => {
expect(isFileReviewed(reviews, file)).toBe(true);
});
});
describe('reviewable', () => {
it.each`
response | diffFile | description
${true} | ${file} | ${'has an `.id` and a `.file_identifier_hash`'}
${false} | ${{ file_identifier_hash: 'abc' }} | ${'does not have an `.id`'}
${false} | ${{ ...file, id: undefined }} | ${'has an undefined `.id`'}
${false} | ${{ ...file, id: null }} | ${'has a null `.id`'}
${false} | ${{ ...file, id: 0 }} | ${'has an `.id` set to 0'}
${false} | ${{ ...file, id: false }} | ${'has an `.id` set to false'}
${false} | ${{ id: '123' }} | ${'does not have a `.file_identifier_hash`'}
${false} | ${{ ...file, file_identifier_hash: undefined }} | ${'has an undefined `.file_identifier_hash`'}
${false} | ${{ ...file, file_identifier_hash: null }} | ${'has a null `.file_identifier_hash`'}
${false} | ${{ ...file, file_identifier_hash: 0 }} | ${'has a `.file_identifier_hash` set to 0'}
${false} | ${{ ...file, file_identifier_hash: false }} | ${'has a `.file_identifier_hash` set to false'}
`('returns `$response` when the file $description`', ({ response, diffFile }) => {
expect(reviewable(diffFile)).toBe(response);
});
});
describe('markFileReview', () => {
it("adds a review when there's nothing that already exists", () => {
expect(markFileReview(null, file)).toStrictEqual({ abc: ['123'] });
});
it("overwrites an existing review if it's for the same file (identifier hash)", () => {
expect(markFileReview(reviews, file)).toStrictEqual(getDefaultReviews());
});
it('removes a review from the list when `reviewed` is `false`', () => {
expect(markFileReview(reviews, file, false)).toStrictEqual({ abc: ['098'] });
});
it('adds a new review if the file ID is new', () => {
const updatedFile = { ...file, id: '098' };
const allReviews = markFileReview({ abc: ['123'] }, updatedFile);
expect(allReviews).toStrictEqual(getDefaultReviews());
expect(allReviews.abc).toStrictEqual(['123', '098']);
});
it.each`
description | diffFile
${'missing an `.id`'} | ${{ file_identifier_hash: 'abc' }}
${'missing a `.file_identifier_hash`'} | ${{ id: '123' }}
`("doesn't modify the reviews if the file is $description", ({ diffFile }) => {
expect(markFileReview(reviews, diffFile)).toStrictEqual(getDefaultReviews());
});
it('removes the file key if there are no more reviews for it', () => {
let updated = markFileReview(reviews, file, false);
updated = markFileReview(updated, { ...file, id: '098' }, false);
expect(updated).toStrictEqual({});
});
});
});
import { getDerivedMergeRequestInformation } from '~/diffs/utils/merge_request';
import { diffMetadata } from '../mock_data/diff_metadata';
describe('Merge Request utilities', () => {
describe('getDerivedMergeRequestInformation', () => {
const endpoint = `${diffMetadata.latest_version_path}.json?searchParam=irrelevant`;
const mrPath = diffMetadata.latest_version_path.replace(/\/diffs$/, '');
it.each`
argument | response
${{ endpoint }} | ${{ mrPath }}
${{}} | ${{ mrPath: undefined }}
${{ endpoint: undefined }} | ${{ mrPath: undefined }}
${{ endpoint: null }} | ${{ mrPath: undefined }}
`('generates the correct derived results based on $argument', ({ argument, response }) => {
expect(getDerivedMergeRequestInformation(argument)).toStrictEqual(response);
});
});
});
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