diff options
Diffstat (limited to 'spec/frontend/diffs/utils')
-rw-r--r-- | spec/frontend/diffs/utils/diff_file_spec.js | 30 | ||||
-rw-r--r-- | spec/frontend/diffs/utils/file_reviews_spec.js | 146 | ||||
-rw-r--r-- | spec/frontend/diffs/utils/merge_request_spec.js | 31 | ||||
-rw-r--r-- | spec/frontend/diffs/utils/uuids_spec.js | 2 |
4 files changed, 203 insertions, 6 deletions
diff --git a/spec/frontend/diffs/utils/diff_file_spec.js b/spec/frontend/diffs/utils/diff_file_spec.js index 2e6247b8c07..2de8db28e71 100644 --- a/spec/frontend/diffs/utils/diff_file_spec.js +++ b/spec/frontend/diffs/utils/diff_file_spec.js @@ -1,6 +1,8 @@ import { prepareRawDiffFile } from '~/diffs/utils/diff_file'; function getDiffFiles() { + const loadFull = 'namespace/project/-/merge_requests/12345/diff_for_path?file_identifier=abc'; + return [ { blob: { @@ -8,6 +10,7 @@ function getDiffFiles() { }, file_hash: 'ABC', // This file is just a normal file file_identifier_hash: 'ABC1', + load_collapsed_diff_url: loadFull, }, { blob: { @@ -15,6 +18,7 @@ function getDiffFiles() { }, file_hash: 'DEF', // This file replaces a symlink file_identifier_hash: 'DEF1', + load_collapsed_diff_url: loadFull, a_mode: '0', b_mode: '0755', }, @@ -24,6 +28,7 @@ function getDiffFiles() { }, file_hash: 'DEF', // This symlink is replaced by a file file_identifier_hash: 'DEF2', + load_collapsed_diff_url: loadFull, a_mode: '120000', b_mode: '0', }, @@ -33,6 +38,7 @@ function getDiffFiles() { }, file_hash: 'GHI', // This symlink replaces a file file_identifier_hash: 'GHI1', + load_collapsed_diff_url: loadFull, a_mode: '0', b_mode: '120000', }, @@ -42,6 +48,7 @@ function getDiffFiles() { }, file_hash: 'GHI', // This file is replaced by a symlink file_identifier_hash: 'GHI2', + load_collapsed_diff_url: loadFull, a_mode: '0755', b_mode: '0', }, @@ -86,11 +93,11 @@ describe('diff_file utilities', () => { it.each` fileIndex | id - ${0} | ${'8dcd585e-a421-4dab-a04e-6f88c81b7b4c'} - ${1} | ${'3f178b78-392b-44a4-bd7d-5d6192208a97'} - ${2} | ${'3d9e1354-cddf-4a11-8234-f0413521b2e5'} - ${3} | ${'460f005b-d29d-43c1-9a08-099a7c7f08de'} - ${4} | ${'d8c89733-6ce1-4455-ae3d-f8aad6ee99f9'} + ${0} | ${'68296a4f-f1c7-445a-bd0e-6e3b02c4eec0'} + ${1} | ${'051c9bb8-cdba-4eb7-b8d1-508906e6d8ba'} + ${2} | ${'ed3d53d5-5da0-412d-a3c6-7213f84e88d3'} + ${3} | ${'39d998dc-bc69-4b19-a6af-41e4369c2bd5'} + ${4} | ${'7072d115-ce39-423c-8346-9fcad58cd68e'} `('sets the file id properly { id: $id } on normal diff files', ({ fileIndex, id }) => { const preppedFile = prepareRawDiffFile({ file: files[fileIndex], @@ -122,5 +129,18 @@ describe('diff_file utilities', () => { expect(preppedFile).not.toHaveProp('id'); }); + + it('does not set the id property if the file is missing a `load_collapsed_diff_url` property', () => { + const fileMissingContentSha = { ...files[0] }; + + delete fileMissingContentSha.load_collapsed_diff_url; + + const preppedFile = prepareRawDiffFile({ + file: fileMissingContentSha, + allFiles: files, + }); + + expect(preppedFile).not.toHaveProp('id'); + }); }); }); diff --git a/spec/frontend/diffs/utils/file_reviews_spec.js b/spec/frontend/diffs/utils/file_reviews_spec.js new file mode 100644 index 00000000000..819426ee75f --- /dev/null +++ b/spec/frontend/diffs/utils/file_reviews_spec.js @@ -0,0 +1,146 @@ +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({}); + }); + }); +}); diff --git a/spec/frontend/diffs/utils/merge_request_spec.js b/spec/frontend/diffs/utils/merge_request_spec.js new file mode 100644 index 00000000000..8c7b1e1f2a5 --- /dev/null +++ b/spec/frontend/diffs/utils/merge_request_spec.js @@ -0,0 +1,31 @@ +import { getDerivedMergeRequestInformation } from '~/diffs/utils/merge_request'; +import { diffMetadata } from '../mock_data/diff_metadata'; + +describe('Merge Request utilities', () => { + const derivedMrInfo = { + mrPath: '/gitlab-org/gitlab-test/-/merge_requests/4', + userOrGroup: 'gitlab-org', + project: 'gitlab-test', + id: '4', + }; + const unparseableEndpoint = { + mrPath: undefined, + userOrGroup: undefined, + project: undefined, + id: undefined, + }; + + describe('getDerivedMergeRequestInformation', () => { + const endpoint = `${diffMetadata.latest_version_path}.json?searchParam=irrelevant`; + + it.each` + argument | response + ${{ endpoint }} | ${derivedMrInfo} + ${{}} | ${unparseableEndpoint} + ${{ endpoint: undefined }} | ${unparseableEndpoint} + ${{ endpoint: null }} | ${unparseableEndpoint} + `('generates the correct derived results based on $argument', ({ argument, response }) => { + expect(getDerivedMergeRequestInformation(argument)).toStrictEqual(response); + }); + }); +}); diff --git a/spec/frontend/diffs/utils/uuids_spec.js b/spec/frontend/diffs/utils/uuids_spec.js index 79d3ebadd4f..8d0a01e8cbd 100644 --- a/spec/frontend/diffs/utils/uuids_spec.js +++ b/spec/frontend/diffs/utils/uuids_spec.js @@ -32,7 +32,7 @@ describe('UUIDs Util', () => { const ids = uuids({ count: 11 }); expect(ids.length).toEqual(11); - expect(ids.every(id => UUIDV4.test(id))).toEqual(true); + expect(ids.every((id) => UUIDV4.test(id))).toEqual(true); }); it.each` |