diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2020-12-18 03:10:04 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2020-12-18 03:10:04 +0300 |
commit | 35aeb676e824679d7e1ec8772c668b91b1dbc1bc (patch) | |
tree | 392f452d92c8a4bb15ea24322ae6532f45912243 /spec | |
parent | 43678813e8265b8a00b3039fce155f4c20947a7a (diff) |
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'spec')
-rw-r--r-- | spec/frontend/diffs/store/actions_spec.js | 43 | ||||
-rw-r--r-- | spec/frontend/diffs/store/getters_spec.js | 22 | ||||
-rw-r--r-- | spec/frontend/diffs/store/mutations_spec.js | 15 | ||||
-rw-r--r-- | spec/frontend/diffs/utils/file_reviews_spec.js | 146 | ||||
-rw-r--r-- | spec/frontend/diffs/utils/merge_request_spec.js | 19 | ||||
-rw-r--r-- | spec/frontend/ide/components/repo_editor_spec.js | 12 | ||||
-rw-r--r-- | spec/frontend/ide/services/index_spec.js | 15 | ||||
-rw-r--r-- | spec/models/project_spec.rb | 22 |
8 files changed, 272 insertions, 22 deletions
diff --git a/spec/frontend/diffs/store/actions_spec.js b/spec/frontend/diffs/store/actions_spec.js index fef7676e795..55e744e573b 100644 --- a/spec/frontend/diffs/store/actions_spec.js +++ b/spec/frontend/diffs/store/actions_spec.js @@ -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); + }, + ); + }); }); diff --git a/spec/frontend/diffs/store/getters_spec.js b/spec/frontend/diffs/store/getters_spec.js index 7e936c561fc..8cbcefa28d1 100644 --- a/spec/frontend/diffs/store/getters_spec.js +++ b/spec/frontend/diffs/store/getters_spec.js @@ -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); + }, + ); + }); }); diff --git a/spec/frontend/diffs/store/mutations_spec.js b/spec/frontend/diffs/store/mutations_spec.js index 13e7cad835d..be187c49a20 100644 --- a/spec/frontend/diffs/store/mutations_spec.js +++ b/spec/frontend/diffs/store/mutations_spec.js @@ -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); + }); + }); }); 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..e949a70dfe7 --- /dev/null +++ b/spec/frontend/diffs/utils/merge_request_spec.js @@ -0,0 +1,19 @@ +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); + }); + }); +}); diff --git a/spec/frontend/ide/components/repo_editor_spec.js b/spec/frontend/ide/components/repo_editor_spec.js index 71a4f08cfb4..7f80fb56ee0 100644 --- a/spec/frontend/ide/components/repo_editor_spec.js +++ b/spec/frontend/ide/components/repo_editor_spec.js @@ -205,6 +205,8 @@ describe('RepoEditor', () => { beforeEach(done => { vm.file.name = 'file.dat'; vm.file.content = '🐱'; // non-ascii binary content + jest.spyOn(vm.editor, 'createInstance').mockImplementation(); + jest.spyOn(vm.editor, 'createDiffInstance').mockImplementation(); vm.$nextTick(done); }); @@ -212,6 +214,16 @@ describe('RepoEditor', () => { it('does not render the IDE', () => { expect(vm.shouldHideEditor).toBeTruthy(); }); + + it('does not call createInstance', async () => { + // Mirror the act's in the `createEditorInstance` + vm.createEditorInstance(); + + await vm.$nextTick(); + + expect(vm.editor.createInstance).not.toHaveBeenCalled(); + expect(vm.editor.createDiffInstance).not.toHaveBeenCalled(); + }); }); describe('createEditorInstance', () => { diff --git a/spec/frontend/ide/services/index_spec.js b/spec/frontend/ide/services/index_spec.js index d2c32a81811..e9bdb9197bb 100644 --- a/spec/frontend/ide/services/index_spec.js +++ b/spec/frontend/ide/services/index_spec.js @@ -117,6 +117,21 @@ describe('IDE services', () => { it('sends a request to file.rawPath', () => { return services.getRawFileData(file).then(raw => { + expect(axios.get).toHaveBeenCalledWith(file.rawPath, { + transformResponse: [expect.any(Function)], + }); + expect(raw).toEqual('raw content'); + }); + }); + + it('returns arraybuffer for binary files', () => { + file.binary = true; + + return services.getRawFileData(file).then(raw => { + expect(axios.get).toHaveBeenCalledWith(file.rawPath, { + transformResponse: [expect.any(Function)], + responseType: 'arraybuffer', + }); expect(raw).toEqual('raw content'); }); }); diff --git a/spec/models/project_spec.rb b/spec/models/project_spec.rb index a71b0eb842a..0a6af753283 100644 --- a/spec/models/project_spec.rb +++ b/spec/models/project_spec.rb @@ -6275,28 +6275,6 @@ RSpec.describe Project, factory_default: :keep do end end - describe '#alerts_service_activated?' do - let!(:project) { create(:project) } - - subject { project.alerts_service_activated? } - - context 'when project has an activated alerts service' do - before do - create(:alerts_service, project: project) - end - - it { is_expected.to be_truthy } - end - - context 'when project has an inactive alerts service' do - before do - create(:alerts_service, :inactive, project: project) - end - - it { is_expected.to be_falsey } - end - end - describe '#prometheus_service_active?' do let(:project) { create(:project) } |