diff options
Diffstat (limited to 'spec/frontend/diffs')
-rw-r--r-- | spec/frontend/diffs/components/app_spec.js | 28 | ||||
-rw-r--r-- | spec/frontend/diffs/components/diff_file_header_spec.js | 60 | ||||
-rw-r--r-- | spec/frontend/diffs/components/diff_file_spec.js | 16 | ||||
-rw-r--r-- | spec/frontend/diffs/store/actions_spec.js | 14 | ||||
-rw-r--r-- | spec/frontend/diffs/store/mutations_spec.js | 10 | ||||
-rw-r--r-- | spec/frontend/diffs/store/utils_spec.js | 28 | ||||
-rw-r--r-- | spec/frontend/diffs/utils/merge_request_spec.js | 16 | ||||
-rw-r--r-- | spec/frontend/diffs/utils/sort_errors_by_file_spec.js | 52 |
8 files changed, 208 insertions, 16 deletions
diff --git a/spec/frontend/diffs/components/app_spec.js b/spec/frontend/diffs/components/app_spec.js index e10aad6214c..212def72b90 100644 --- a/spec/frontend/diffs/components/app_spec.js +++ b/spec/frontend/diffs/components/app_spec.js @@ -6,6 +6,7 @@ import Vue, { nextTick } from 'vue'; import Vuex from 'vuex'; import setWindowLocation from 'helpers/set_window_location_helper'; import { TEST_HOST } from 'spec/test_constants'; + import App from '~/diffs/components/app.vue'; import CommitWidget from '~/diffs/components/commit_widget.vue'; import CompareVersions from '~/diffs/components/compare_versions.vue'; @@ -17,6 +18,8 @@ import DiffsFileTree from '~/diffs/components/diffs_file_tree.vue'; import CollapsedFilesWarning from '~/diffs/components/collapsed_files_warning.vue'; import HiddenFilesWarning from '~/diffs/components/hidden_files_warning.vue'; +import eventHub from '~/diffs/event_hub'; + import axios from '~/lib/utils/axios_utils'; import { HTTP_STATUS_OK } from '~/lib/utils/http_status'; import { Mousetrap } from '~/lib/mousetrap'; @@ -760,4 +763,29 @@ describe('diffs/components/app', () => { ); }); }); + + describe('autoscroll', () => { + let loadSpy; + + beforeEach(() => { + createComponent(); + loadSpy = jest.spyOn(wrapper.vm, 'loadCollapsedDiff').mockResolvedValue('resolved'); + }); + + it('does nothing if the location hash does not include a file hash', () => { + window.location.hash = 'not_a_file_hash'; + + eventHub.$emit('doneLoadingBatches'); + + expect(loadSpy).not.toHaveBeenCalled(); + }); + + it('requests that the correct file be loaded', () => { + window.location.hash = '1c497fbb3a46b78edf04cc2a2fa33f67e3ffbe2a_0_1'; + + eventHub.$emit('doneLoadingBatches'); + + expect(loadSpy).toHaveBeenCalledWith({ file: store.state.diffs.diffFiles[0] }); + }); + }); }); diff --git a/spec/frontend/diffs/components/diff_file_header_spec.js b/spec/frontend/diffs/components/diff_file_header_spec.js index b089825090b..b0d98e0e4a6 100644 --- a/spec/frontend/diffs/components/diff_file_header_spec.js +++ b/spec/frontend/diffs/components/diff_file_header_spec.js @@ -8,8 +8,12 @@ import { mockTracking, triggerEvent } from 'helpers/tracking_helper'; import DiffFileHeader from '~/diffs/components/diff_file_header.vue'; import { DIFF_FILE_AUTOMATIC_COLLAPSE, DIFF_FILE_MANUAL_COLLAPSE } from '~/diffs/constants'; -import { reviewFile } from '~/diffs/store/actions'; -import { SET_DIFF_FILE_VIEWED, SET_MR_FILE_REVIEWS } from '~/diffs/store/mutation_types'; +import { reviewFile, setFileForcedOpen } from '~/diffs/store/actions'; +import { + SET_DIFF_FILE_VIEWED, + SET_MR_FILE_REVIEWS, + SET_FILE_FORCED_OPEN, +} from '~/diffs/store/mutation_types'; import { diffViewerModes } from '~/ide/constants'; import { scrollToElement } from '~/lib/utils/common_utils'; import { truncateSha } from '~/lib/utils/text_utility'; @@ -67,6 +71,7 @@ describe('DiffFileHeader component', () => { toggleFullDiff: jest.fn(), setCurrentFileHash: jest.fn(), setFileCollapsedByUser: jest.fn(), + setFileForcedOpen: jest.fn(), reviewFile: jest.fn(), }, }, @@ -138,6 +143,19 @@ describe('DiffFileHeader component', () => { expect(wrapper.emitted().toggleFile).toBeDefined(); }); + it('when header is clicked it triggers the action that removes the value that forces a file to be uncollapsed', () => { + createComponent(); + findHeader().trigger('click'); + + return testAction( + setFileForcedOpen, + { filePath: diffFile.file_path, forced: false }, + {}, + [{ type: SET_FILE_FORCED_OPEN, payload: { filePath: diffFile.file_path, forced: false } }], + [], + ); + }); + it('when collapseIcon is clicked emits toggleFile', async () => { createComponent({ props: { collapsible: true } }); findCollapseButton().vm.$emit('click', new Event('click')); @@ -643,6 +661,44 @@ describe('DiffFileHeader component', () => { expect(Boolean(wrapper.emitted().toggleFile)).toBe(fires); }, ); + + it('removes the property that forces a file to be shown when the file review is toggled', () => { + createComponent({ + props: { + diffFile: { + ...diffFile, + viewer: { + ...diffFile.viewer, + automaticallyCollapsed: false, + manuallyCollapsed: null, + }, + }, + showLocalFileReviews: true, + addMergeRequestButtons: true, + expanded: false, + }, + }); + + findReviewFileCheckbox().vm.$emit('change', true); + + testAction( + setFileForcedOpen, + { filePath: diffFile.file_path, forced: false }, + {}, + [{ type: SET_FILE_FORCED_OPEN, payload: { filePath: diffFile.file_path, forced: false } }], + [], + ); + + findReviewFileCheckbox().vm.$emit('change', false); + + testAction( + setFileForcedOpen, + { filePath: diffFile.file_path, forced: false }, + {}, + [{ type: SET_FILE_FORCED_OPEN, payload: { filePath: diffFile.file_path, forced: false } }], + [], + ); + }); }); it('should render the comment on files button', () => { diff --git a/spec/frontend/diffs/components/diff_file_spec.js b/spec/frontend/diffs/components/diff_file_spec.js index 53f135471b7..13efd3584b4 100644 --- a/spec/frontend/diffs/components/diff_file_spec.js +++ b/spec/frontend/diffs/components/diff_file_spec.js @@ -324,6 +324,22 @@ describe('DiffFile', () => { }); describe('collapsing', () => { + describe('forced open', () => { + it('should have content even when it is automatically collapsed', () => { + makeFileAutomaticallyCollapsed(store); + + expect(findDiffContentArea(wrapper).element.children.length).toBe(1); + expect(wrapper.classes('has-body')).toBe(true); + }); + + it('should have content even when it is manually collapsed', () => { + makeFileManuallyCollapsed(store); + + expect(findDiffContentArea(wrapper).element.children.length).toBe(1); + expect(wrapper.classes('has-body')).toBe(true); + }); + }); + describe(`\`${EVT_EXPAND_ALL_FILES}\` event`, () => { beforeEach(() => { jest.spyOn(wrapper.vm, 'handleToggle').mockImplementation(() => {}); diff --git a/spec/frontend/diffs/store/actions_spec.js b/spec/frontend/diffs/store/actions_spec.js index 387407a7e4d..18e81232b5c 100644 --- a/spec/frontend/diffs/store/actions_spec.js +++ b/spec/frontend/diffs/store/actions_spec.js @@ -1627,6 +1627,7 @@ describe('DiffsStoreActions', () => { name: updatedViewerName, automaticallyCollapsed: false, manuallyCollapsed: false, + forceOpen: false, }; const testData = [{ rich_text: 'test' }, { rich_text: 'file2' }]; let renamedFile; @@ -1673,7 +1674,7 @@ describe('DiffsStoreActions', () => { }); }); - describe('setFileUserCollapsed', () => { + describe('setFileCollapsedByUser', () => { it('commits SET_FILE_COLLAPSED', () => { return testAction( diffActions.setFileCollapsedByUser, @@ -1690,6 +1691,17 @@ describe('DiffsStoreActions', () => { }); }); + describe('setFileForcedOpen', () => { + it('commits SET_FILE_FORCED_OPEN', () => { + return testAction(diffActions.setFileForcedOpen, { filePath: 'test', forced: true }, null, [ + { + type: types.SET_FILE_FORCED_OPEN, + payload: { filePath: 'test', forced: true }, + }, + ]); + }); + }); + describe('setExpandedDiffLines', () => { beforeEach(() => { utils.idleCallback.mockImplementation((cb) => { diff --git a/spec/frontend/diffs/store/mutations_spec.js b/spec/frontend/diffs/store/mutations_spec.js index e87c5d0a9b1..fdcf7c3eeab 100644 --- a/spec/frontend/diffs/store/mutations_spec.js +++ b/spec/frontend/diffs/store/mutations_spec.js @@ -1055,4 +1055,14 @@ describe('DiffsStoreMutations', () => { expect(state.diffFiles[0].drafts[0]).toEqual('test'); }); }); + + describe('SET_FILE_FORCED_OPEN', () => { + it('sets the forceOpen property of a diff file viewer correctly', () => { + const state = { diffFiles: [{ file_path: 'abc', viewer: { forceOpen: 'not-a-boolean' } }] }; + + mutations[types.SET_FILE_FORCED_OPEN](state, { filePath: 'abc', force: true }); + + expect(state.diffFiles[0].viewer.forceOpen).toBe(true); + }); + }); }); diff --git a/spec/frontend/diffs/store/utils_spec.js b/spec/frontend/diffs/store/utils_spec.js index 24cb8158739..720b72f4965 100644 --- a/spec/frontend/diffs/store/utils_spec.js +++ b/spec/frontend/diffs/store/utils_spec.js @@ -927,19 +927,21 @@ describe('DiffsStoreUtils', () => { describe('parseUrlHashAsFileHash', () => { it.each` - input | currentDiffId | resultId - ${'#note_12345'} | ${'1A2B3C'} | ${'1A2B3C'} - ${'note_12345'} | ${'1A2B3C'} | ${'1A2B3C'} - ${'#note_12345'} | ${undefined} | ${null} - ${'note_12345'} | ${undefined} | ${null} - ${'#diff-content-12345'} | ${undefined} | ${'12345'} - ${'diff-content-12345'} | ${undefined} | ${'12345'} - ${'#diff-content-12345'} | ${'98765'} | ${'12345'} - ${'diff-content-12345'} | ${'98765'} | ${'12345'} - ${'#e334a2a10f036c00151a04cea7938a5d4213a818'} | ${undefined} | ${'e334a2a10f036c00151a04cea7938a5d4213a818'} - ${'e334a2a10f036c00151a04cea7938a5d4213a818'} | ${undefined} | ${'e334a2a10f036c00151a04cea7938a5d4213a818'} - ${'#Z334a2a10f036c00151a04cea7938a5d4213a818'} | ${undefined} | ${null} - ${'Z334a2a10f036c00151a04cea7938a5d4213a818'} | ${undefined} | ${null} + input | currentDiffId | resultId + ${'#note_12345'} | ${'1A2B3C'} | ${'1A2B3C'} + ${'note_12345'} | ${'1A2B3C'} | ${'1A2B3C'} + ${'#note_12345'} | ${undefined} | ${null} + ${'note_12345'} | ${undefined} | ${null} + ${'#diff-content-12345'} | ${undefined} | ${'12345'} + ${'diff-content-12345'} | ${undefined} | ${'12345'} + ${'#diff-content-12345'} | ${'98765'} | ${'12345'} + ${'diff-content-12345'} | ${'98765'} | ${'12345'} + ${'#e334a2a10f036c00151a04cea7938a5d4213a818'} | ${undefined} | ${'e334a2a10f036c00151a04cea7938a5d4213a818'} + ${'e334a2a10f036c00151a04cea7938a5d4213a818'} | ${undefined} | ${'e334a2a10f036c00151a04cea7938a5d4213a818'} + ${'#Z334a2a10f036c00151a04cea7938a5d4213a818'} | ${undefined} | ${null} + ${'Z334a2a10f036c00151a04cea7938a5d4213a818'} | ${undefined} | ${null} + ${'#e334a2a10f036c00151a04cea7938a5d4213a818_0_42'} | ${undefined} | ${'e334a2a10f036c00151a04cea7938a5d4213a818'} + ${'e334a2a10f036c00151a04cea7938a5d4213a818_0_42'} | ${undefined} | ${'e334a2a10f036c00151a04cea7938a5d4213a818'} `('returns $resultId for $input and $currentDiffId', ({ input, currentDiffId, resultId }) => { expect(utils.parseUrlHashAsFileHash(input, currentDiffId)).toBe(resultId); }); diff --git a/spec/frontend/diffs/utils/merge_request_spec.js b/spec/frontend/diffs/utils/merge_request_spec.js index 11c0efb9a9c..f5145b3c4c7 100644 --- a/spec/frontend/diffs/utils/merge_request_spec.js +++ b/spec/frontend/diffs/utils/merge_request_spec.js @@ -1,6 +1,7 @@ import { updateChangesTabCount, getDerivedMergeRequestInformation, + extractFileHash, } from '~/diffs/utils/merge_request'; import { ZERO_CHANGES_ALT_DISPLAY } from '~/diffs/constants'; import { diffMetadata } from '../mock_data/diff_metadata'; @@ -128,4 +129,19 @@ describe('Merge Request utilities', () => { }); }); }); + + describe('extractFileHash', () => { + const sha1Like = 'abcdef1234567890abcdef1234567890abcdef12'; + const sha1LikeToo = 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'; + + it('returns undefined when a SHA1-like string cannot be found in the input', () => { + expect(extractFileHash({ input: 'something' })).toBe(undefined); + }); + + it('returns the first matching string of SHA1-like characters in the input', () => { + const fullString = `#${sha1Like}_34_42--${sha1LikeToo}`; + + expect(extractFileHash({ input: fullString })).toBe(sha1Like); + }); + }); }); diff --git a/spec/frontend/diffs/utils/sort_errors_by_file_spec.js b/spec/frontend/diffs/utils/sort_errors_by_file_spec.js new file mode 100644 index 00000000000..ca8a8ec3516 --- /dev/null +++ b/spec/frontend/diffs/utils/sort_errors_by_file_spec.js @@ -0,0 +1,52 @@ +import { sortFindingsByFile } from '~/diffs/utils/sort_findings_by_file'; + +describe('sort_findings_by_file utilities', () => { + const mockDescription = 'mockDescription'; + const mockSeverity = 'mockseverity'; + const mockLine = '00'; + const mockFile1 = 'file1.js'; + const mockFile2 = 'file2.rb'; + const emptyResponse = { + files: {}, + }; + + const unsortedFindings = [ + { + severity: mockSeverity, + filePath: mockFile1, + line: mockLine, + description: mockDescription, + }, + { + severity: mockSeverity, + filePath: mockFile2, + line: mockLine, + description: mockDescription, + }, + ]; + const sortedFindings = { + files: { + [mockFile1]: [ + { + line: mockLine, + description: mockDescription, + severity: mockSeverity, + }, + ], + [mockFile2]: [ + { + line: mockLine, + description: mockDescription, + severity: mockSeverity, + }, + ], + }, + }; + + it('sorts Findings correctly', () => { + expect(sortFindingsByFile(unsortedFindings)).toEqual(sortedFindings); + }); + it('does not throw error when given no input', () => { + expect(sortFindingsByFile()).toEqual(emptyResponse); + }); +}); |