diff options
Diffstat (limited to 'spec/frontend/diffs')
-rw-r--r-- | spec/frontend/diffs/components/app_spec.js | 105 | ||||
-rw-r--r-- | spec/frontend/diffs/components/commit_item_spec.js | 2 | ||||
-rw-r--r-- | spec/frontend/diffs/components/diff_content_spec.js | 7 | ||||
-rw-r--r-- | spec/frontend/diffs/components/diff_expansion_cell_spec.js | 5 | ||||
-rw-r--r-- | spec/frontend/diffs/store/actions_spec.js | 8 | ||||
-rw-r--r-- | spec/frontend/diffs/store/mutations_spec.js | 292 | ||||
-rw-r--r-- | spec/frontend/diffs/store/utils_spec.js | 217 |
7 files changed, 126 insertions, 510 deletions
diff --git a/spec/frontend/diffs/components/app_spec.js b/spec/frontend/diffs/components/app_spec.js index 225710eab63..3f7d4bfe9f1 100644 --- a/spec/frontend/diffs/components/app_spec.js +++ b/spec/frontend/diffs/components/app_spec.js @@ -12,7 +12,6 @@ import HiddenFilesWarning from '~/diffs/components/hidden_files_warning.vue'; import CollapsedFilesWarning from '~/diffs/components/collapsed_files_warning.vue'; import CommitWidget from '~/diffs/components/commit_widget.vue'; import TreeList from '~/diffs/components/tree_list.vue'; -import { INLINE_DIFF_VIEW_TYPE, PARALLEL_DIFF_VIEW_TYPE } from '~/diffs/constants'; import createDiffsStore from '../create_diffs_store'; import axios from '~/lib/utils/axios_utils'; import * as urlUtils from '~/lib/utils/url_utility'; @@ -75,12 +74,6 @@ describe('diffs/components/app', () => { }); } - function getOppositeViewType(currentViewType) { - return currentViewType === INLINE_DIFF_VIEW_TYPE - ? PARALLEL_DIFF_VIEW_TYPE - : INLINE_DIFF_VIEW_TYPE; - } - beforeEach(() => { // setup globals (needed for component to mount :/) window.mrTabs = { @@ -125,104 +118,6 @@ describe('diffs/components/app', () => { wrapper.vm.$nextTick(done); }); - describe('when the diff view type changes and it should load a single diff view style', () => { - const noLinesDiff = { - highlighted_diff_lines: [], - parallel_diff_lines: [], - }; - const parallelLinesDiff = { - highlighted_diff_lines: [], - parallel_diff_lines: ['line'], - }; - const inlineLinesDiff = { - highlighted_diff_lines: ['line'], - parallel_diff_lines: [], - }; - const fullDiff = { - highlighted_diff_lines: ['line'], - parallel_diff_lines: ['line'], - }; - - function expectFetchToOccur({ vueInstance, done = () => {}, existingFiles = 1 } = {}) { - vueInstance.$nextTick(() => { - expect(vueInstance.diffFiles.length).toEqual(existingFiles); - expect(vueInstance.fetchDiffFilesBatch).toHaveBeenCalled(); - - done(); - }); - } - - it('fetches diffs if it has none', done => { - wrapper.vm.isLatestVersion = () => false; - - store.state.diffs.diffViewType = getOppositeViewType(wrapper.vm.diffViewType); - - expectFetchToOccur({ vueInstance: wrapper.vm, existingFiles: 0, done }); - }); - - it('fetches diffs if it has both view styles, but no lines in either', done => { - wrapper.vm.isLatestVersion = () => false; - - store.state.diffs.diffFiles.push(noLinesDiff); - store.state.diffs.diffViewType = getOppositeViewType(wrapper.vm.diffViewType); - - expectFetchToOccur({ vueInstance: wrapper.vm, done }); - }); - - it('fetches diffs if it only has inline view style', done => { - wrapper.vm.isLatestVersion = () => false; - - store.state.diffs.diffFiles.push(inlineLinesDiff); - store.state.diffs.diffViewType = getOppositeViewType(wrapper.vm.diffViewType); - - expectFetchToOccur({ vueInstance: wrapper.vm, done }); - }); - - it('fetches diffs if it only has parallel view style', done => { - wrapper.vm.isLatestVersion = () => false; - - store.state.diffs.diffFiles.push(parallelLinesDiff); - store.state.diffs.diffViewType = getOppositeViewType(wrapper.vm.diffViewType); - - expectFetchToOccur({ vueInstance: wrapper.vm, done }); - }); - - it('fetches batch diffs if it has none', done => { - store.state.diffs.diffViewType = getOppositeViewType(wrapper.vm.diffViewType); - - expectFetchToOccur({ vueInstance: wrapper.vm, existingFiles: 0, done }); - }); - - it('fetches batch diffs if it has both view styles, but no lines in either', done => { - store.state.diffs.diffFiles.push(noLinesDiff); - store.state.diffs.diffViewType = getOppositeViewType(wrapper.vm.diffViewType); - - expectFetchToOccur({ vueInstance: wrapper.vm, done }); - }); - - it('fetches batch diffs if it only has inline view style', done => { - store.state.diffs.diffFiles.push(inlineLinesDiff); - store.state.diffs.diffViewType = getOppositeViewType(wrapper.vm.diffViewType); - - expectFetchToOccur({ vueInstance: wrapper.vm, done }); - }); - - it('fetches batch diffs if it only has parallel view style', done => { - store.state.diffs.diffFiles.push(parallelLinesDiff); - store.state.diffs.diffViewType = getOppositeViewType(wrapper.vm.diffViewType); - - expectFetchToOccur({ vueInstance: wrapper.vm, done }); - }); - - it('does not fetch batch diffs if it has already fetched both styles of diff', () => { - store.state.diffs.diffFiles.push(fullDiff); - store.state.diffs.diffViewType = getOppositeViewType(wrapper.vm.diffViewType); - - expect(wrapper.vm.diffFiles.length).toEqual(1); - expect(wrapper.vm.fetchDiffFilesBatch).not.toHaveBeenCalled(); - }); - }); - it('calls batch methods if diffsBatchLoad is enabled, and not latest version', done => { expect(wrapper.vm.diffFilesLength).toEqual(0); wrapper.vm.isLatestVersion = () => false; diff --git a/spec/frontend/diffs/components/commit_item_spec.js b/spec/frontend/diffs/components/commit_item_spec.js index 9e4fcddd1b4..8a7eb6aaca6 100644 --- a/spec/frontend/diffs/components/commit_item_spec.js +++ b/spec/frontend/diffs/components/commit_item_spec.js @@ -84,7 +84,7 @@ describe('diffs/components/commit_item', () => { it('renders commit sha', () => { const shaElement = getShaElement(); - const labelElement = shaElement.find('[data-testid="commit-sha-group"] button'); + const labelElement = shaElement.find('[data-testid="commit-sha-short-id"]'); const buttonElement = shaElement.find('button.input-group-text'); expect(labelElement.text()).toEqual(commit.short_id); diff --git a/spec/frontend/diffs/components/diff_content_spec.js b/spec/frontend/diffs/components/diff_content_spec.js index e3a6f7f16a9..c0532e392d1 100644 --- a/spec/frontend/diffs/components/diff_content_spec.js +++ b/spec/frontend/diffs/components/diff_content_spec.js @@ -12,7 +12,6 @@ import DiffDiscussions from '~/diffs/components/diff_discussions.vue'; import { IMAGE_DIFF_POSITION_TYPE } from '~/diffs/constants'; import diffFileMockData from '../mock_data/diff_file'; import { diffViewerModes } from '~/ide/constants'; -import { diffLines } from '~/diffs/store/getters'; import DiffView from '~/diffs/components/diff_view.vue'; const localVue = createLocalVue(); @@ -74,7 +73,7 @@ describe('DiffContent', () => { isInlineView: isInlineViewGetterMock, isParallelView: isParallelViewGetterMock, getCommentFormForDiffFile: getCommentFormForDiffFileGetterMock, - diffLines, + diffLines: () => () => [...diffFileMockData.parallel_diff_lines], }, actions: { saveDiffDiscussion: saveDiffDiscussionMock, @@ -122,11 +121,11 @@ describe('DiffContent', () => { expect(wrapper.find(ParallelDiffView).exists()).toBe(true); }); - it('should render diff view if `unifiedDiffLines` & `unifiedDiffComponents` are true', () => { + it('should render diff view if `unifiedDiffComponents` are true', () => { isParallelViewGetterMock.mockReturnValue(true); createComponent({ props: { diffFile: textDiffFile }, - provide: { glFeatures: { unifiedDiffLines: true, unifiedDiffComponents: true } }, + provide: { glFeatures: { unifiedDiffComponents: true } }, }); expect(wrapper.find(DiffView).exists()).toBe(true); diff --git a/spec/frontend/diffs/components/diff_expansion_cell_spec.js b/spec/frontend/diffs/components/diff_expansion_cell_spec.js index 81e08f09f62..a3b4b5c3abb 100644 --- a/spec/frontend/diffs/components/diff_expansion_cell_spec.js +++ b/spec/frontend/diffs/components/diff_expansion_cell_spec.js @@ -5,18 +5,16 @@ import { getByText } from '@testing-library/dom'; import { createStore } from '~/mr_notes/stores'; import DiffExpansionCell from '~/diffs/components/diff_expansion_cell.vue'; import { getPreviousLineIndex } from '~/diffs/store/utils'; -import { INLINE_DIFF_VIEW_TYPE, PARALLEL_DIFF_VIEW_TYPE } from '~/diffs/constants'; +import { INLINE_DIFF_VIEW_TYPE } from '~/diffs/constants'; import diffFileMockData from '../mock_data/diff_file'; const EXPAND_UP_CLASS = '.js-unfold'; const EXPAND_DOWN_CLASS = '.js-unfold-down'; const lineSources = { [INLINE_DIFF_VIEW_TYPE]: 'highlighted_diff_lines', - [PARALLEL_DIFF_VIEW_TYPE]: 'parallel_diff_lines', }; const lineHandlers = { [INLINE_DIFF_VIEW_TYPE]: line => line, - [PARALLEL_DIFF_VIEW_TYPE]: line => line.right || line.left, }; function makeLoadMoreLinesPayload({ @@ -126,7 +124,6 @@ describe('DiffExpansionCell', () => { describe('any row', () => { [ { diffViewType: INLINE_DIFF_VIEW_TYPE, lineIndex: 8, file: { parallel_diff_lines: [] } }, - { diffViewType: PARALLEL_DIFF_VIEW_TYPE, lineIndex: 7, file: { highlighted_diff_lines: [] } }, ].forEach(({ diffViewType, file, lineIndex }) => { describe(`with diffViewType (${diffViewType})`, () => { beforeEach(() => { diff --git a/spec/frontend/diffs/store/actions_spec.js b/spec/frontend/diffs/store/actions_spec.js index 0af5ddd9764..f010e88a1a7 100644 --- a/spec/frontend/diffs/store/actions_spec.js +++ b/spec/frontend/diffs/store/actions_spec.js @@ -1236,10 +1236,6 @@ describe('DiffsStoreActions', () => { { diffViewType: 'inline' }, [ { - type: 'SET_HIDDEN_VIEW_DIFF_FILE_LINES', - payload: { filePath: 'path', lines: ['test'] }, - }, - { type: 'SET_CURRENT_VIEW_DIFF_FILE_LINES', payload: { filePath: 'path', lines: ['test'] }, }, @@ -1259,10 +1255,6 @@ describe('DiffsStoreActions', () => { { diffViewType: 'inline' }, [ { - type: 'SET_HIDDEN_VIEW_DIFF_FILE_LINES', - payload: { filePath: 'path', lines }, - }, - { type: 'SET_CURRENT_VIEW_DIFF_FILE_LINES', payload: { filePath: 'path', lines: lines.slice(0, 200) }, }, diff --git a/spec/frontend/diffs/store/mutations_spec.js b/spec/frontend/diffs/store/mutations_spec.js index c0645faf89e..ae23cea157d 100644 --- a/spec/frontend/diffs/store/mutations_spec.js +++ b/spec/frontend/diffs/store/mutations_spec.js @@ -1,7 +1,7 @@ import createState from '~/diffs/store/modules/diff_state'; import mutations from '~/diffs/store/mutations'; import * as types from '~/diffs/store/mutation_types'; -import { INLINE_DIFF_VIEW_TYPE } from '~/diffs/constants'; +import { INLINE_DIFF_VIEW_TYPE, INLINE_DIFF_LINES_KEY } from '~/diffs/constants'; import diffFileMockData from '../mock_data/diff_file'; import * as utils from '~/diffs/store/utils'; @@ -74,7 +74,7 @@ describe('DiffsStoreMutations', () => { { content_sha: diffFileMockData.content_sha, file_hash: diffFileMockData.file_hash, - highlighted_diff_lines: [], + [INLINE_DIFF_LINES_KEY]: [], }, ], }; @@ -84,11 +84,11 @@ describe('DiffsStoreMutations', () => { mutations[types.SET_DIFF_DATA](state, diffMock); - expect(state.diffFiles[0].parallel_diff_lines).toBeUndefined(); + expect(state.diffFiles[0][INLINE_DIFF_LINES_KEY]).toEqual([]); }); }); - describe('SET_DIFFSET_DIFF_DATA_BATCH_DATA', () => { + describe('SET_DIFF_DATA_BATCH_DATA', () => { it('should set diff data batch type properly', () => { const state = { diffFiles: [] }; const diffMock = { @@ -97,9 +97,6 @@ describe('DiffsStoreMutations', () => { mutations[types.SET_DIFF_DATA_BATCH](state, diffMock); - const firstLine = state.diffFiles[0].parallel_diff_lines[0]; - - expect(firstLine.right.text).toBeUndefined(); expect(state.diffFiles[0].renderIt).toEqual(true); expect(state.diffFiles[0].collapsed).toEqual(false); }); @@ -142,8 +139,7 @@ describe('DiffsStoreMutations', () => { }; const diffFile = { file_hash: options.fileHash, - highlighted_diff_lines: [], - parallel_diff_lines: [], + [INLINE_DIFF_LINES_KEY]: [], }; const state = { diffFiles: [diffFile], diffViewType: 'viewType' }; const lines = [{ old_line: 1, new_line: 1 }]; @@ -171,9 +167,7 @@ describe('DiffsStoreMutations', () => { ); expect(utils.addContextLines).toHaveBeenCalledWith({ - inlineLines: diffFile.highlighted_diff_lines, - parallelLines: diffFile.parallel_diff_lines, - diffViewType: 'viewType', + inlineLines: diffFile[INLINE_DIFF_LINES_KEY], contextLines: options.contextLines, bottom: options.params.bottom, lineNumbers: options.lineNumbers, @@ -225,19 +219,7 @@ describe('DiffsStoreMutations', () => { diffFiles: [ { file_hash: 'ABC', - parallel_diff_lines: [ - { - left: { - line_code: 'ABC_1', - discussions: [], - }, - right: { - line_code: 'ABC_2', - discussions: [], - }, - }, - ], - highlighted_diff_lines: [ + [INLINE_DIFF_LINES_KEY]: [ { line_code: 'ABC_1', discussions: [], @@ -267,12 +249,8 @@ describe('DiffsStoreMutations', () => { diffPositionByLineCode, }); - expect(state.diffFiles[0].parallel_diff_lines[0].left.discussions.length).toEqual(1); - expect(state.diffFiles[0].parallel_diff_lines[0].left.discussions[0].id).toEqual(1); - expect(state.diffFiles[0].parallel_diff_lines[0].right.discussions).toEqual([]); - - expect(state.diffFiles[0].highlighted_diff_lines[0].discussions.length).toEqual(1); - expect(state.diffFiles[0].highlighted_diff_lines[0].discussions[0].id).toEqual(1); + expect(state.diffFiles[0][INLINE_DIFF_LINES_KEY][0].discussions.length).toEqual(1); + expect(state.diffFiles[0][INLINE_DIFF_LINES_KEY][0].discussions[0].id).toEqual(1); }); it('should not duplicate discussions on line', () => { @@ -291,19 +269,7 @@ describe('DiffsStoreMutations', () => { diffFiles: [ { file_hash: 'ABC', - parallel_diff_lines: [ - { - left: { - line_code: 'ABC_1', - discussions: [], - }, - right: { - line_code: 'ABC_2', - discussions: [], - }, - }, - ], - highlighted_diff_lines: [ + [INLINE_DIFF_LINES_KEY]: [ { line_code: 'ABC_1', discussions: [], @@ -333,24 +299,16 @@ describe('DiffsStoreMutations', () => { diffPositionByLineCode, }); - expect(state.diffFiles[0].parallel_diff_lines[0].left.discussions.length).toEqual(1); - expect(state.diffFiles[0].parallel_diff_lines[0].left.discussions[0].id).toEqual(1); - expect(state.diffFiles[0].parallel_diff_lines[0].right.discussions).toEqual([]); - - expect(state.diffFiles[0].highlighted_diff_lines[0].discussions.length).toEqual(1); - expect(state.diffFiles[0].highlighted_diff_lines[0].discussions[0].id).toEqual(1); + expect(state.diffFiles[0][INLINE_DIFF_LINES_KEY][0].discussions.length).toEqual(1); + expect(state.diffFiles[0][INLINE_DIFF_LINES_KEY][0].discussions[0].id).toEqual(1); mutations[types.SET_LINE_DISCUSSIONS_FOR_FILE](state, { discussion, diffPositionByLineCode, }); - expect(state.diffFiles[0].parallel_diff_lines[0].left.discussions.length).toEqual(1); - expect(state.diffFiles[0].parallel_diff_lines[0].left.discussions[0].id).toEqual(1); - expect(state.diffFiles[0].parallel_diff_lines[0].right.discussions).toEqual([]); - - expect(state.diffFiles[0].highlighted_diff_lines[0].discussions.length).toEqual(1); - expect(state.diffFiles[0].highlighted_diff_lines[0].discussions[0].id).toEqual(1); + expect(state.diffFiles[0][INLINE_DIFF_LINES_KEY][0].discussions.length).toEqual(1); + expect(state.diffFiles[0][INLINE_DIFF_LINES_KEY][0].discussions[0].id).toEqual(1); }); it('updates existing discussion', () => { @@ -369,19 +327,7 @@ describe('DiffsStoreMutations', () => { diffFiles: [ { file_hash: 'ABC', - parallel_diff_lines: [ - { - left: { - line_code: 'ABC_1', - discussions: [], - }, - right: { - line_code: 'ABC_2', - discussions: [], - }, - }, - ], - highlighted_diff_lines: [ + [INLINE_DIFF_LINES_KEY]: [ { line_code: 'ABC_1', discussions: [], @@ -411,12 +357,8 @@ describe('DiffsStoreMutations', () => { diffPositionByLineCode, }); - expect(state.diffFiles[0].parallel_diff_lines[0].left.discussions.length).toEqual(1); - expect(state.diffFiles[0].parallel_diff_lines[0].left.discussions[0].id).toEqual(1); - expect(state.diffFiles[0].parallel_diff_lines[0].right.discussions).toEqual([]); - - expect(state.diffFiles[0].highlighted_diff_lines[0].discussions.length).toEqual(1); - expect(state.diffFiles[0].highlighted_diff_lines[0].discussions[0].id).toEqual(1); + expect(state.diffFiles[0][INLINE_DIFF_LINES_KEY][0].discussions.length).toEqual(1); + expect(state.diffFiles[0][INLINE_DIFF_LINES_KEY][0].discussions[0].id).toEqual(1); mutations[types.SET_LINE_DISCUSSIONS_FOR_FILE](state, { discussion: { @@ -427,11 +369,8 @@ describe('DiffsStoreMutations', () => { diffPositionByLineCode, }); - expect(state.diffFiles[0].parallel_diff_lines[0].left.discussions[0].notes.length).toBe(1); - expect(state.diffFiles[0].highlighted_diff_lines[0].discussions[0].notes.length).toBe(1); - - expect(state.diffFiles[0].parallel_diff_lines[0].left.discussions[0].resolved).toBe(true); - expect(state.diffFiles[0].highlighted_diff_lines[0].discussions[0].resolved).toBe(true); + expect(state.diffFiles[0][INLINE_DIFF_LINES_KEY][0].discussions[0].notes.length).toBe(1); + expect(state.diffFiles[0][INLINE_DIFF_LINES_KEY][0].discussions[0].resolved).toBe(true); }); it('should not duplicate inline diff discussions', () => { @@ -450,7 +389,7 @@ describe('DiffsStoreMutations', () => { diffFiles: [ { file_hash: 'ABC', - highlighted_diff_lines: [ + [INLINE_DIFF_LINES_KEY]: [ { line_code: 'ABC_1', discussions: [ @@ -472,7 +411,6 @@ describe('DiffsStoreMutations', () => { discussions: [], }, ], - parallel_diff_lines: [], }, ], }; @@ -497,7 +435,7 @@ describe('DiffsStoreMutations', () => { diffPositionByLineCode, }); - expect(state.diffFiles[0].highlighted_diff_lines[0].discussions.length).toBe(1); + expect(state.diffFiles[0][INLINE_DIFF_LINES_KEY][0].discussions.length).toBe(1); }); it('should add legacy discussions to the given line', () => { @@ -517,19 +455,7 @@ describe('DiffsStoreMutations', () => { diffFiles: [ { file_hash: 'ABC', - parallel_diff_lines: [ - { - left: { - line_code: 'ABC_1', - discussions: [], - }, - right: { - line_code: 'ABC_1', - discussions: [], - }, - }, - ], - highlighted_diff_lines: [ + [INLINE_DIFF_LINES_KEY]: [ { line_code: 'ABC_1', discussions: [], @@ -557,11 +483,8 @@ describe('DiffsStoreMutations', () => { diffPositionByLineCode, }); - expect(state.diffFiles[0].parallel_diff_lines[0].left.discussions.length).toEqual(1); - expect(state.diffFiles[0].parallel_diff_lines[0].left.discussions[0].id).toEqual(1); - - expect(state.diffFiles[0].highlighted_diff_lines[0].discussions.length).toEqual(1); - expect(state.diffFiles[0].highlighted_diff_lines[0].discussions[0].id).toEqual(1); + expect(state.diffFiles[0][INLINE_DIFF_LINES_KEY][0].discussions.length).toEqual(1); + expect(state.diffFiles[0][INLINE_DIFF_LINES_KEY][0].discussions[0].id).toEqual(1); }); it('should add discussions by line_codes and positions attributes', () => { @@ -580,19 +503,7 @@ describe('DiffsStoreMutations', () => { diffFiles: [ { file_hash: 'ABC', - parallel_diff_lines: [ - { - left: { - line_code: 'ABC_1', - discussions: [], - }, - right: { - line_code: 'ABC_1', - discussions: [], - }, - }, - ], - highlighted_diff_lines: [ + [INLINE_DIFF_LINES_KEY]: [ { line_code: 'ABC_1', discussions: [], @@ -624,11 +535,8 @@ describe('DiffsStoreMutations', () => { diffPositionByLineCode, }); - expect(state.diffFiles[0].parallel_diff_lines[0].left.discussions).toHaveLength(1); - expect(state.diffFiles[0].parallel_diff_lines[0].left.discussions[0].id).toBe(1); - - expect(state.diffFiles[0].highlighted_diff_lines[0].discussions).toHaveLength(1); - expect(state.diffFiles[0].highlighted_diff_lines[0].discussions[0].id).toBe(1); + expect(state.diffFiles[0][INLINE_DIFF_LINES_KEY][0].discussions).toHaveLength(1); + expect(state.diffFiles[0][INLINE_DIFF_LINES_KEY][0].discussions[0].id).toBe(1); }); it('should add discussion to file', () => { @@ -638,8 +546,7 @@ describe('DiffsStoreMutations', () => { { file_hash: 'ABC', discussions: [], - parallel_diff_lines: [], - highlighted_diff_lines: [], + [INLINE_DIFF_LINES_KEY]: [], }, ], }; @@ -668,30 +575,7 @@ describe('DiffsStoreMutations', () => { diffFiles: [ { file_hash: 'ABC', - parallel_diff_lines: [ - { - left: { - line_code: 'ABC_1', - discussions: [ - { - id: 1, - line_code: 'ABC_1', - notes: [], - }, - { - id: 2, - line_code: 'ABC_1', - notes: [], - }, - ], - }, - right: { - line_code: 'ABC_1', - discussions: [], - }, - }, - ], - highlighted_diff_lines: [ + [INLINE_DIFF_LINES_KEY]: [ { line_code: 'ABC_1', discussions: [ @@ -717,8 +601,7 @@ describe('DiffsStoreMutations', () => { lineCode: 'ABC_1', }); - expect(state.diffFiles[0].parallel_diff_lines[0].left.discussions.length).toEqual(0); - expect(state.diffFiles[0].highlighted_diff_lines[0].discussions.length).toEqual(0); + expect(state.diffFiles[0][INLINE_DIFF_LINES_KEY][0].discussions.length).toEqual(0); }); }); @@ -776,11 +659,7 @@ describe('DiffsStoreMutations', () => { it('sets hasForm on lines', () => { const file = { file_hash: 'hash', - parallel_diff_lines: [ - { left: { line_code: '123', hasForm: false }, right: {} }, - { left: {}, right: { line_code: '124', hasForm: false } }, - ], - highlighted_diff_lines: [ + [INLINE_DIFF_LINES_KEY]: [ { line_code: '123', hasForm: false }, { line_code: '124', hasForm: false }, ], @@ -795,11 +674,8 @@ describe('DiffsStoreMutations', () => { fileHash: 'hash', }); - expect(file.highlighted_diff_lines[0].hasForm).toBe(true); - expect(file.highlighted_diff_lines[1].hasForm).toBe(false); - - expect(file.parallel_diff_lines[0].left.hasForm).toBe(true); - expect(file.parallel_diff_lines[1].right.hasForm).toBe(false); + expect(file[INLINE_DIFF_LINES_KEY][0].hasForm).toBe(true); + expect(file[INLINE_DIFF_LINES_KEY][1].hasForm).toBe(false); }); }); @@ -885,8 +761,7 @@ describe('DiffsStoreMutations', () => { file_path: 'test', isLoadingFullFile: true, isShowingFullFile: false, - highlighted_diff_lines: [], - parallel_diff_lines: [], + [INLINE_DIFF_LINES_KEY]: [], }, ], }; @@ -903,8 +778,7 @@ describe('DiffsStoreMutations', () => { file_path: 'test', isLoadingFullFile: true, isShowingFullFile: false, - highlighted_diff_lines: [], - parallel_diff_lines: [], + [INLINE_DIFF_LINES_KEY]: [], }, ], }; @@ -927,80 +801,42 @@ describe('DiffsStoreMutations', () => { }); }); - describe('SET_HIDDEN_VIEW_DIFF_FILE_LINES', () => { - [ - { current: 'highlighted', hidden: 'parallel', diffViewType: 'inline' }, - { current: 'parallel', hidden: 'highlighted', diffViewType: 'parallel' }, - ].forEach(({ current, hidden, diffViewType }) => { - it(`sets the ${hidden} lines when diff view is ${diffViewType}`, () => { - const file = { file_path: 'test', parallel_diff_lines: [], highlighted_diff_lines: [] }; - const state = { - diffFiles: [file], - diffViewType, - }; - - mutations[types.SET_HIDDEN_VIEW_DIFF_FILE_LINES](state, { - filePath: 'test', - lines: ['test'], - }); - - expect(file[`${current}_diff_lines`]).toEqual([]); - expect(file[`${hidden}_diff_lines`]).toEqual(['test']); - }); - }); - }); - describe('SET_CURRENT_VIEW_DIFF_FILE_LINES', () => { - [ - { current: 'highlighted', hidden: 'parallel', diffViewType: 'inline' }, - { current: 'parallel', hidden: 'highlighted', diffViewType: 'parallel' }, - ].forEach(({ current, hidden, diffViewType }) => { - it(`sets the ${current} lines when diff view is ${diffViewType}`, () => { - const file = { file_path: 'test', parallel_diff_lines: [], highlighted_diff_lines: [] }; - const state = { - diffFiles: [file], - diffViewType, - }; - - mutations[types.SET_CURRENT_VIEW_DIFF_FILE_LINES](state, { - filePath: 'test', - lines: ['test'], - }); - - expect(file[`${current}_diff_lines`]).toEqual(['test']); - expect(file[`${hidden}_diff_lines`]).toEqual([]); + it(`sets the highlighted lines`, () => { + const file = { file_path: 'test', [INLINE_DIFF_LINES_KEY]: [] }; + const state = { + diffFiles: [file], + }; + + mutations[types.SET_CURRENT_VIEW_DIFF_FILE_LINES](state, { + filePath: 'test', + lines: ['test'], }); + + expect(file[INLINE_DIFF_LINES_KEY]).toEqual(['test']); }); }); describe('ADD_CURRENT_VIEW_DIFF_FILE_LINES', () => { - [ - { current: 'highlighted', hidden: 'parallel', diffViewType: 'inline' }, - { current: 'parallel', hidden: 'highlighted', diffViewType: 'parallel' }, - ].forEach(({ current, hidden, diffViewType }) => { - it(`pushes to ${current} lines when diff view is ${diffViewType}`, () => { - const file = { file_path: 'test', parallel_diff_lines: [], highlighted_diff_lines: [] }; - const state = { - diffFiles: [file], - diffViewType, - }; - - mutations[types.ADD_CURRENT_VIEW_DIFF_FILE_LINES](state, { - filePath: 'test', - line: 'test', - }); - - expect(file[`${current}_diff_lines`]).toEqual(['test']); - expect(file[`${hidden}_diff_lines`]).toEqual([]); - - mutations[types.ADD_CURRENT_VIEW_DIFF_FILE_LINES](state, { - filePath: 'test', - line: 'test2', - }); - - expect(file[`${current}_diff_lines`]).toEqual(['test', 'test2']); - expect(file[`${hidden}_diff_lines`]).toEqual([]); + it('pushes to inline lines', () => { + const file = { file_path: 'test', [INLINE_DIFF_LINES_KEY]: [] }; + const state = { + diffFiles: [file], + }; + + mutations[types.ADD_CURRENT_VIEW_DIFF_FILE_LINES](state, { + filePath: 'test', + line: 'test', }); + + expect(file[INLINE_DIFF_LINES_KEY]).toEqual(['test']); + + mutations[types.ADD_CURRENT_VIEW_DIFF_FILE_LINES](state, { + filePath: 'test', + line: 'test2', + }); + + expect(file[INLINE_DIFF_LINES_KEY]).toEqual(['test', 'test2']); }); }); diff --git a/spec/frontend/diffs/store/utils_spec.js b/spec/frontend/diffs/store/utils_spec.js index 866be0abd22..181d562661f 100644 --- a/spec/frontend/diffs/store/utils_spec.js +++ b/spec/frontend/diffs/store/utils_spec.js @@ -10,7 +10,7 @@ import { OLD_LINE_TYPE, MATCH_LINE_TYPE, INLINE_DIFF_VIEW_TYPE, - PARALLEL_DIFF_VIEW_TYPE, + INLINE_DIFF_LINES_KEY, } from '~/diffs/constants'; import { MERGE_REQUEST_NOTEABLE_TYPE } from '~/notes/constants'; import diffFileMockData from '../mock_data/diff_file'; @@ -20,14 +20,6 @@ import { noteableDataMock } from '../../notes/mock_data'; const getDiffFileMock = () => JSON.parse(JSON.stringify(diffFileMockData)); const getDiffMetadataMock = () => JSON.parse(JSON.stringify(diffMetadata)); -function extractLinesFromFile(file) { - const unpackedParallel = file.parallel_diff_lines - .flatMap(({ left, right }) => [left, right]) - .filter(Boolean); - - return [...file.highlighted_diff_lines, ...unpackedParallel]; -} - describe('DiffsStoreUtils', () => { describe('findDiffFile', () => { const files = [{ file_hash: 1, name: 'one' }]; @@ -45,7 +37,7 @@ describe('DiffsStoreUtils', () => { }); }); - describe('findIndexInInlineLines and findIndexInParallelLines', () => { + describe('findIndexInInlineLines', () => { const expectSet = (method, lines, invalidLines) => { expect(method(lines, { oldLineNumber: 3, newLineNumber: 5 })).toEqual(4); expect(method(invalidLines || lines, { oldLineNumber: 32, newLineNumber: 53 })).toEqual(-1); @@ -53,44 +45,26 @@ describe('DiffsStoreUtils', () => { describe('findIndexInInlineLines', () => { it('should return correct index for given line numbers', () => { - expectSet(utils.findIndexInInlineLines, getDiffFileMock().highlighted_diff_lines); - }); - }); - - describe('findIndexInParallelLines', () => { - it('should return correct index for given line numbers', () => { - expectSet(utils.findIndexInParallelLines, getDiffFileMock().parallel_diff_lines, []); + expectSet(utils.findIndexInInlineLines, getDiffFileMock()[INLINE_DIFF_LINES_KEY]); }); }); }); describe('getPreviousLineIndex', () => { - [ - { diffViewType: INLINE_DIFF_VIEW_TYPE, file: { parallel_diff_lines: [] } }, - { diffViewType: PARALLEL_DIFF_VIEW_TYPE, file: { highlighted_diff_lines: [] } }, - ].forEach(({ diffViewType, file }) => { - describe(`with diffViewType (${diffViewType}) in split diffs`, () => { - let diffFile; - - beforeEach(() => { - diffFile = { ...clone(diffFileMockData), ...file }; - }); + describe(`with diffViewType (inline) in split diffs`, () => { + let diffFile; - it('should return the correct previous line number', () => { - const emptyLines = - diffViewType === INLINE_DIFF_VIEW_TYPE - ? diffFile.parallel_diff_lines - : diffFile.highlighted_diff_lines; - - // This expectation asserts that we cannot possibly be using the opposite view type lines in the next expectation - expect(emptyLines.length).toBe(0); - expect( - utils.getPreviousLineIndex(diffViewType, diffFile, { - oldLineNumber: 3, - newLineNumber: 5, - }), - ).toBe(4); - }); + beforeEach(() => { + diffFile = { ...clone(diffFileMockData) }; + }); + + it('should return the correct previous line number', () => { + expect( + utils.getPreviousLineIndex(INLINE_DIFF_VIEW_TYPE, diffFile, { + oldLineNumber: 3, + newLineNumber: 5, + }), + ).toBe(4); }); }); }); @@ -100,82 +74,50 @@ describe('DiffsStoreUtils', () => { const diffFile = getDiffFileMock(); const lineNumbers = { oldLineNumber: 3, newLineNumber: 5 }; const inlineIndex = utils.findIndexInInlineLines( - diffFile.highlighted_diff_lines, - lineNumbers, - ); - const parallelIndex = utils.findIndexInParallelLines( - diffFile.parallel_diff_lines, + diffFile[INLINE_DIFF_LINES_KEY], lineNumbers, ); - const atInlineIndex = diffFile.highlighted_diff_lines[inlineIndex]; - const atParallelIndex = diffFile.parallel_diff_lines[parallelIndex]; + const atInlineIndex = diffFile[INLINE_DIFF_LINES_KEY][inlineIndex]; utils.removeMatchLine(diffFile, lineNumbers, false); - expect(diffFile.highlighted_diff_lines[inlineIndex]).not.toEqual(atInlineIndex); - expect(diffFile.parallel_diff_lines[parallelIndex]).not.toEqual(atParallelIndex); + expect(diffFile[INLINE_DIFF_LINES_KEY][inlineIndex]).not.toEqual(atInlineIndex); utils.removeMatchLine(diffFile, lineNumbers, true); - expect(diffFile.highlighted_diff_lines[inlineIndex + 1]).not.toEqual(atInlineIndex); - expect(diffFile.parallel_diff_lines[parallelIndex + 1]).not.toEqual(atParallelIndex); + expect(diffFile[INLINE_DIFF_LINES_KEY][inlineIndex + 1]).not.toEqual(atInlineIndex); }); }); describe('addContextLines', () => { - [INLINE_DIFF_VIEW_TYPE, PARALLEL_DIFF_VIEW_TYPE].forEach(diffViewType => { - it(`should add context lines for ${diffViewType}`, () => { - const diffFile = getDiffFileMock(); - const inlineLines = diffFile.highlighted_diff_lines; - const parallelLines = diffFile.parallel_diff_lines; - const lineNumbers = { oldLineNumber: 3, newLineNumber: 5 }; - const contextLines = [{ lineNumber: 42, line_code: '123' }]; - const options = { inlineLines, parallelLines, contextLines, lineNumbers, diffViewType }; - const inlineIndex = utils.findIndexInInlineLines(inlineLines, lineNumbers); - const parallelIndex = utils.findIndexInParallelLines(parallelLines, lineNumbers); - const normalizedParallelLine = { - left: options.contextLines[0], - right: options.contextLines[0], - line_code: '123', - }; + it(`should add context lines`, () => { + const diffFile = getDiffFileMock(); + const inlineLines = diffFile[INLINE_DIFF_LINES_KEY]; + const lineNumbers = { oldLineNumber: 3, newLineNumber: 5 }; + const contextLines = [{ lineNumber: 42, line_code: '123' }]; + const options = { inlineLines, contextLines, lineNumbers }; + const inlineIndex = utils.findIndexInInlineLines(inlineLines, lineNumbers); - utils.addContextLines(options); + utils.addContextLines(options); - if (diffViewType === INLINE_DIFF_VIEW_TYPE) { - expect(inlineLines[inlineIndex]).toEqual(contextLines[0]); - } else { - expect(parallelLines[parallelIndex]).toEqual(normalizedParallelLine); - } - }); + expect(inlineLines[inlineIndex]).toEqual(contextLines[0]); + }); - it(`should add context lines properly with bottom parameter for ${diffViewType}`, () => { - const diffFile = getDiffFileMock(); - const inlineLines = diffFile.highlighted_diff_lines; - const parallelLines = diffFile.parallel_diff_lines; - const lineNumbers = { oldLineNumber: 3, newLineNumber: 5 }; - const contextLines = [{ lineNumber: 42, line_code: '123' }]; - const options = { - inlineLines, - parallelLines, - contextLines, - lineNumbers, - bottom: true, - diffViewType, - }; - const normalizedParallelLine = { - left: options.contextLines[0], - right: options.contextLines[0], - line_code: '123', - }; + it(`should add context lines properly with bottom parameter`, () => { + const diffFile = getDiffFileMock(); + const inlineLines = diffFile[INLINE_DIFF_LINES_KEY]; + const lineNumbers = { oldLineNumber: 3, newLineNumber: 5 }; + const contextLines = [{ lineNumber: 42, line_code: '123' }]; + const options = { + inlineLines, + contextLines, + lineNumbers, + bottom: true, + }; - utils.addContextLines(options); + utils.addContextLines(options); - if (diffViewType === INLINE_DIFF_VIEW_TYPE) { - expect(inlineLines[inlineLines.length - 1]).toEqual(contextLines[0]); - } else { - expect(parallelLines[parallelLines.length - 1]).toEqual(normalizedParallelLine); - } - }); + expect(inlineLines[inlineLines.length - 1]).toEqual(contextLines[0]); }); }); @@ -195,7 +137,6 @@ describe('DiffsStoreUtils', () => { new_line: 3, old_line: 1, }, - diffViewType: PARALLEL_DIFF_VIEW_TYPE, linePosition: LINE_POSITION_LEFT, lineRange: { start_line_code: 'abc_1_1', end_line_code: 'abc_2_2' }, }; @@ -256,7 +197,6 @@ describe('DiffsStoreUtils', () => { new_line: 3, old_line: 1, }, - diffViewType: PARALLEL_DIFF_VIEW_TYPE, linePosition: LINE_POSITION_LEFT, }; @@ -424,20 +364,6 @@ describe('DiffsStoreUtils', () => { expect(preppedLine).toEqual(correctLine); }); - it('returns a nested object with "left" and "right" lines + the line code for `parallel` lines', () => { - preppedLine = utils.prepareLineForRenamedFile({ - diffViewType: PARALLEL_DIFF_VIEW_TYPE, - line: sourceLine, - index: lineIndex, - diffFile, - }); - - expect(Object.keys(preppedLine)).toEqual(['left', 'right', 'line_code']); - expect(preppedLine.left).toEqual(correctLine); - expect(preppedLine.right).toEqual(correctLine); - expect(preppedLine.line_code).toEqual(correctLine.line_code); - }); - it.each` brokenSymlink ${false} @@ -474,13 +400,13 @@ describe('DiffsStoreUtils', () => { preparedDiff = { diff_files: [mock] }; splitInlineDiff = { - diff_files: [{ ...mock, parallel_diff_lines: undefined }], + diff_files: [{ ...mock }], }; splitParallelDiff = { - diff_files: [{ ...mock, highlighted_diff_lines: undefined }], + diff_files: [{ ...mock, [INLINE_DIFF_LINES_KEY]: undefined }], }; completedDiff = { - diff_files: [{ ...mock, highlighted_diff_lines: undefined }], + diff_files: [{ ...mock, [INLINE_DIFF_LINES_KEY]: undefined }], }; preparedDiff.diff_files = utils.prepareDiffData(preparedDiff); @@ -490,19 +416,7 @@ describe('DiffsStoreUtils', () => { }); it('sets the renderIt and collapsed attribute on files', () => { - const firstParallelDiffLine = preparedDiff.diff_files[0].parallel_diff_lines[2]; - - expect(firstParallelDiffLine.left.discussions.length).toBe(0); - expect(firstParallelDiffLine.left).not.toHaveAttr('text'); - expect(firstParallelDiffLine.right.discussions.length).toBe(0); - expect(firstParallelDiffLine.right).not.toHaveAttr('text'); - const firstParallelChar = firstParallelDiffLine.right.rich_text.charAt(0); - - expect(firstParallelChar).not.toBe(' '); - expect(firstParallelChar).not.toBe('+'); - expect(firstParallelChar).not.toBe('-'); - - const checkLine = preparedDiff.diff_files[0].highlighted_diff_lines[0]; + const checkLine = preparedDiff.diff_files[0][INLINE_DIFF_LINES_KEY][0]; expect(checkLine.discussions.length).toBe(0); expect(checkLine).not.toHaveAttr('text'); @@ -516,29 +430,14 @@ describe('DiffsStoreUtils', () => { expect(preparedDiff.diff_files[0].collapsed).toBeFalsy(); }); - it('adds line_code to all lines', () => { - expect( - preparedDiff.diff_files[0].parallel_diff_lines.filter(line => !line.line_code), - ).toHaveLength(0); - }); - - it('uses right line code if left has none', () => { - const firstLine = preparedDiff.diff_files[0].parallel_diff_lines[0]; - - expect(firstLine.line_code).toEqual(firstLine.right.line_code); - }); - it('guarantees an empty array for both diff styles', () => { - expect(splitInlineDiff.diff_files[0].parallel_diff_lines.length).toEqual(0); - expect(splitInlineDiff.diff_files[0].highlighted_diff_lines.length).toBeGreaterThan(0); - expect(splitParallelDiff.diff_files[0].parallel_diff_lines.length).toBeGreaterThan(0); - expect(splitParallelDiff.diff_files[0].highlighted_diff_lines.length).toEqual(0); + expect(splitInlineDiff.diff_files[0][INLINE_DIFF_LINES_KEY].length).toBeGreaterThan(0); + expect(splitParallelDiff.diff_files[0][INLINE_DIFF_LINES_KEY].length).toEqual(0); }); it('merges existing diff files with newly loaded diff files to ensure split diffs are eventually completed', () => { expect(completedDiff.diff_files.length).toEqual(1); - expect(completedDiff.diff_files[0].parallel_diff_lines.length).toBeGreaterThan(0); - expect(completedDiff.diff_files[0].highlighted_diff_lines.length).toBeGreaterThan(0); + expect(completedDiff.diff_files[0][INLINE_DIFF_LINES_KEY].length).toBeGreaterThan(0); }); it('leaves files in the existing state', () => { @@ -555,11 +454,11 @@ describe('DiffsStoreUtils', () => { it('completes an existing split diff without overwriting existing diffs', () => { // The current state has a file that has only loaded inline lines - const priorFiles = [{ ...mock, parallel_diff_lines: [] }]; + const priorFiles = [{ ...mock }]; // The next (batch) load loads two files: the other half of that file, and a new file const fakeBatch = [ - { ...mock, highlighted_diff_lines: undefined }, - { ...mock, highlighted_diff_lines: undefined, content_sha: 'ABC', file_hash: 'DEF' }, + { ...mock, [INLINE_DIFF_LINES_KEY]: undefined }, + { ...mock, [INLINE_DIFF_LINES_KEY]: undefined, content_sha: 'ABC', file_hash: 'DEF' }, ]; const updatedFilesList = utils.prepareDiffData({ diff_files: fakeBatch }, priorFiles); @@ -584,7 +483,7 @@ describe('DiffsStoreUtils', () => { ...splitInlineDiff.diff_files, ...splitParallelDiff.diff_files, ...completedDiff.diff_files, - ].flatMap(file => extractLinesFromFile(file)); + ].flatMap(file => [...file[INLINE_DIFF_LINES_KEY]]); lines.forEach(line => { expect(line.commentsDisabled).toBe(false); @@ -608,8 +507,7 @@ describe('DiffsStoreUtils', () => { }); it('guarantees an empty array of lines for both diff styles', () => { - expect(preparedDiffFiles[0].parallel_diff_lines.length).toEqual(0); - expect(preparedDiffFiles[0].highlighted_diff_lines.length).toEqual(0); + expect(preparedDiffFiles[0][INLINE_DIFF_LINES_KEY].length).toEqual(0); }); it('leaves files in the existing state', () => { @@ -647,8 +545,7 @@ describe('DiffsStoreUtils', () => { fileMock, { ...metaMock.diff_files[0], - highlighted_diff_lines: [], - parallel_diff_lines: [], + [INLINE_DIFF_LINES_KEY]: [], }, ]); }); @@ -1217,7 +1114,7 @@ describe('DiffsStoreUtils', () => { it('converts inline diff lines to parallel diff lines', () => { const file = getDiffFileMock(); - expect(utils.parallelizeDiffLines(file.highlighted_diff_lines)).toEqual( + expect(utils.parallelizeDiffLines(file[INLINE_DIFF_LINES_KEY])).toEqual( file.parallel_diff_lines, ); }); |