diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2020-09-19 04:45:44 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2020-09-19 04:45:44 +0300 |
commit | 85dc423f7090da0a52c73eb66faf22ddb20efff9 (patch) | |
tree | 9160f299afd8c80c038f08e1545be119f5e3f1e1 /spec/frontend/diffs/store | |
parent | 15c2c8c66dbe422588e5411eee7e68f1fa440bb8 (diff) |
Add latest changes from gitlab-org/gitlab@13-4-stable-ee
Diffstat (limited to 'spec/frontend/diffs/store')
-rw-r--r-- | spec/frontend/diffs/store/actions_spec.js | 207 | ||||
-rw-r--r-- | spec/frontend/diffs/store/mutations_spec.js | 83 | ||||
-rw-r--r-- | spec/frontend/diffs/store/utils_spec.js | 55 |
3 files changed, 107 insertions, 238 deletions
diff --git a/spec/frontend/diffs/store/actions_spec.js b/spec/frontend/diffs/store/actions_spec.js index 5fef35d6c5b..4f647b0cd41 100644 --- a/spec/frontend/diffs/store/actions_spec.js +++ b/spec/frontend/diffs/store/actions_spec.js @@ -13,7 +13,6 @@ import { } from '~/diffs/constants'; import { setBaseConfig, - fetchDiffFiles, fetchDiffFilesBatch, fetchDiffFilesMeta, fetchCoverageFiles, @@ -101,7 +100,6 @@ describe('DiffsStoreActions', () => { const projectPath = '/root/project'; const dismissEndpoint = '/-/user_callouts'; const showSuggestPopover = false; - const useSingleDiffStyle = false; testAction( setBaseConfig, @@ -113,7 +111,6 @@ describe('DiffsStoreActions', () => { projectPath, dismissEndpoint, showSuggestPopover, - useSingleDiffStyle, }, { endpoint: '', @@ -123,7 +120,6 @@ describe('DiffsStoreActions', () => { projectPath: '', dismissEndpoint: '', showSuggestPopover: true, - useSingleDiffStyle: true, }, [ { @@ -136,7 +132,6 @@ describe('DiffsStoreActions', () => { projectPath, dismissEndpoint, showSuggestPopover, - useSingleDiffStyle, }, }, ], @@ -146,39 +141,6 @@ describe('DiffsStoreActions', () => { }); }); - describe('fetchDiffFiles', () => { - it('should fetch diff files', done => { - const endpoint = '/fetch/diff/files?view=inline&w=1'; - const mock = new MockAdapter(axios); - const res = { diff_files: 1, merge_request_diffs: [] }; - mock.onGet(endpoint).reply(200, res); - - testAction( - fetchDiffFiles, - {}, - { endpoint, diffFiles: [], showWhitespace: false, diffViewType: 'inline' }, - [ - { type: types.SET_LOADING, payload: true }, - { type: types.SET_LOADING, payload: false }, - { type: types.SET_MERGE_REQUEST_DIFFS, payload: res.merge_request_diffs }, - { type: types.SET_DIFF_DATA, payload: res }, - ], - [], - () => { - mock.restore(); - done(); - }, - ); - - fetchDiffFiles({ state: { endpoint }, commit: () => null }) - .then(data => { - expect(data).toEqual(res); - done(); - }) - .catch(done.fail); - }); - }); - describe('fetchDiffFilesBatch', () => { let mock; @@ -223,16 +185,16 @@ describe('DiffsStoreActions', () => { testAction( fetchDiffFilesBatch, {}, - { endpointBatch, useSingleDiffStyle: true, diffViewType: 'inline' }, + { endpointBatch, diffViewType: 'inline' }, [ { type: types.SET_BATCH_LOADING, payload: true }, { type: types.SET_RETRIEVING_BATCHES, payload: true }, { type: types.SET_DIFF_DATA_BATCH, payload: { diff_files: res1.diff_files } }, { type: types.SET_BATCH_LOADING, payload: false }, - { type: types.UPDATE_CURRENT_DIFF_FILE_ID, payload: 'test' }, + { type: types.VIEW_DIFF_FILE, payload: 'test' }, { type: types.SET_DIFF_DATA_BATCH, payload: { diff_files: res2.diff_files } }, { type: types.SET_BATCH_LOADING, payload: false }, - { type: types.UPDATE_CURRENT_DIFF_FILE_ID, payload: 'test2' }, + { type: types.VIEW_DIFF_FILE, payload: 'test2' }, { type: types.SET_RETRIEVING_BATCHES, payload: false }, ], [], @@ -253,7 +215,6 @@ describe('DiffsStoreActions', () => { commit: () => {}, state: { endpointBatch: `${endpointBatch}?view=${otherView}`, - useSingleDiffStyle: true, diffViewType: viewStyle, }, }) @@ -283,7 +244,7 @@ describe('DiffsStoreActions', () => { testAction( fetchDiffFilesMeta, {}, - { endpointMetadata }, + { endpointMetadata, diffViewType: 'inline' }, [ { type: types.SET_LOADING, payload: true }, { type: types.SET_LOADING, payload: false }, @@ -299,146 +260,6 @@ describe('DiffsStoreActions', () => { }); }); - describe('when the single diff view feature flag is off', () => { - describe('fetchDiffFiles', () => { - it('should fetch diff files', done => { - const endpoint = '/fetch/diff/files?w=1'; - const mock = new MockAdapter(axios); - const res = { diff_files: 1, merge_request_diffs: [] }; - mock.onGet(endpoint).reply(200, res); - - testAction( - fetchDiffFiles, - {}, - { - endpoint, - diffFiles: [], - showWhitespace: false, - diffViewType: 'inline', - useSingleDiffStyle: false, - currentDiffFileId: null, - }, - [ - { type: types.SET_LOADING, payload: true }, - { type: types.SET_LOADING, payload: false }, - { type: types.SET_MERGE_REQUEST_DIFFS, payload: res.merge_request_diffs }, - { type: types.SET_DIFF_DATA, payload: res }, - ], - [], - () => { - mock.restore(); - done(); - }, - ); - - fetchDiffFiles({ state: { endpoint }, commit: () => null }) - .then(data => { - expect(data).toEqual(res); - done(); - }) - .catch(done.fail); - }); - }); - - describe('fetchDiffFilesBatch', () => { - let mock; - - beforeEach(() => { - mock = new MockAdapter(axios); - }); - - afterEach(() => { - mock.restore(); - }); - - it('should fetch batch diff files', done => { - const endpointBatch = '/fetch/diffs_batch'; - const res1 = { diff_files: [{ file_hash: 'test' }], pagination: { next_page: 2 } }; - const res2 = { diff_files: [{ file_hash: 'test2' }], pagination: {} }; - mock - .onGet(mergeUrlParams({ per_page: DIFFS_PER_PAGE, w: '1', page: 1 }, endpointBatch)) - .reply(200, res1) - .onGet(mergeUrlParams({ per_page: DIFFS_PER_PAGE, w: '1', page: 2 }, endpointBatch)) - .reply(200, res2); - - testAction( - fetchDiffFilesBatch, - {}, - { endpointBatch, useSingleDiffStyle: false, currentDiffFileId: null }, - [ - { type: types.SET_BATCH_LOADING, payload: true }, - { type: types.SET_RETRIEVING_BATCHES, payload: true }, - { type: types.SET_DIFF_DATA_BATCH, payload: { diff_files: res1.diff_files } }, - { type: types.SET_BATCH_LOADING, payload: false }, - { type: types.UPDATE_CURRENT_DIFF_FILE_ID, payload: 'test' }, - { type: types.SET_DIFF_DATA_BATCH, payload: { diff_files: res2.diff_files } }, - { type: types.SET_BATCH_LOADING, payload: false }, - { type: types.UPDATE_CURRENT_DIFF_FILE_ID, payload: 'test2' }, - { type: types.SET_RETRIEVING_BATCHES, payload: false }, - ], - [], - done, - ); - }); - - it.each` - querystrings | requestUrl - ${'?view=parallel'} | ${'/fetch/diffs_batch?view=parallel'} - ${'?view=inline'} | ${'/fetch/diffs_batch?view=inline'} - ${''} | ${'/fetch/diffs_batch'} - `( - 'should use the endpoint $requestUrl if the endpointBatch in state includes `$querystrings` as a querystring', - ({ querystrings, requestUrl }) => { - const endpointBatch = '/fetch/diffs_batch'; - - fetchDiffFilesBatch({ - commit: () => {}, - state: { - endpointBatch: `${endpointBatch}${querystrings}`, - diffViewType: 'inline', - }, - }) - .then(() => { - expect(mock.history.get[0].url).toEqual(requestUrl); - }) - .catch(() => {}); - }, - ); - }); - - describe('fetchDiffFilesMeta', () => { - const endpointMetadata = '/fetch/diffs_metadata.json'; - const noFilesData = { ...diffMetadata }; - let mock; - - beforeEach(() => { - mock = new MockAdapter(axios); - - delete noFilesData.diff_files; - - mock.onGet(endpointMetadata).reply(200, diffMetadata); - }); - it('should fetch diff meta information', done => { - testAction( - fetchDiffFilesMeta, - {}, - { endpointMetadata, useSingleDiffStyle: false }, - [ - { type: types.SET_LOADING, payload: true }, - { type: types.SET_LOADING, payload: false }, - { type: types.SET_MERGE_REQUEST_DIFFS, payload: diffMetadata.merge_request_diffs }, - { type: types.SET_DIFF_DATA, payload: noFilesData }, - ], - [], - () => { - mock.restore(); - done(); - }, - ); - }); - }); - }); - describe('fetchCoverageFiles', () => { let mock; const endpointCoverage = '/fetch'; @@ -479,7 +300,7 @@ describe('DiffsStoreActions', () => { it('should mark currently selected diff and set lineHash and fileHash of highlightedRow', () => { testAction(setHighlightedRow, 'ABC_123', {}, [ { type: types.SET_HIGHLIGHTED_ROW, payload: 'ABC_123' }, - { type: types.UPDATE_CURRENT_DIFF_FILE_ID, payload: 'ABC' }, + { type: types.VIEW_DIFF_FILE, payload: 'ABC' }, ]); }); }); @@ -589,7 +410,7 @@ describe('DiffsStoreActions', () => { testAction( assignDiscussionsToDiff, [], - { diffFiles: [], useSingleDiffStyle: true }, + { diffFiles: [] }, [], [{ type: 'setCurrentDiffFileIdFromNote', payload: '123' }], done, @@ -1083,7 +904,7 @@ describe('DiffsStoreActions', () => { expect(document.location.hash).toBe('#test'); }); - it('commits UPDATE_CURRENT_DIFF_FILE_ID', () => { + it('commits VIEW_DIFF_FILE', () => { const state = { treeEntries: { path: { @@ -1094,7 +915,7 @@ describe('DiffsStoreActions', () => { scrollToFile({ state, commit }, 'path'); - expect(commit).toHaveBeenCalledWith(types.UPDATE_CURRENT_DIFF_FILE_ID, 'test'); + expect(commit).toHaveBeenCalledWith(types.VIEW_DIFF_FILE, 'test'); }); }); @@ -1592,7 +1413,7 @@ describe('DiffsStoreActions', () => { }); describe('setCurrentDiffFileIdFromNote', () => { - it('commits UPDATE_CURRENT_DIFF_FILE_ID', () => { + it('commits VIEW_DIFF_FILE', () => { const commit = jest.fn(); const state = { diffFiles: [{ file_hash: '123' }] }; const rootGetters = { @@ -1602,10 +1423,10 @@ describe('DiffsStoreActions', () => { setCurrentDiffFileIdFromNote({ commit, state, rootGetters }, '1'); - expect(commit).toHaveBeenCalledWith(types.UPDATE_CURRENT_DIFF_FILE_ID, '123'); + expect(commit).toHaveBeenCalledWith(types.VIEW_DIFF_FILE, '123'); }); - it('does not commit UPDATE_CURRENT_DIFF_FILE_ID when discussion has no diff_file', () => { + it('does not commit VIEW_DIFF_FILE when discussion has no diff_file', () => { const commit = jest.fn(); const state = { diffFiles: [{ file_hash: '123' }] }; const rootGetters = { @@ -1618,7 +1439,7 @@ describe('DiffsStoreActions', () => { expect(commit).not.toHaveBeenCalled(); }); - it('does not commit UPDATE_CURRENT_DIFF_FILE_ID when diff file does not exist', () => { + it('does not commit VIEW_DIFF_FILE when diff file does not exist', () => { const commit = jest.fn(); const state = { diffFiles: [{ file_hash: '123' }] }; const rootGetters = { @@ -1633,12 +1454,12 @@ describe('DiffsStoreActions', () => { }); describe('navigateToDiffFileIndex', () => { - it('commits UPDATE_CURRENT_DIFF_FILE_ID', done => { + it('commits VIEW_DIFF_FILE', done => { testAction( navigateToDiffFileIndex, 0, { diffFiles: [{ file_hash: '123' }] }, - [{ type: types.UPDATE_CURRENT_DIFF_FILE_ID, payload: '123' }], + [{ type: types.VIEW_DIFF_FILE, payload: '123' }], [], done, ); diff --git a/spec/frontend/diffs/store/mutations_spec.js b/spec/frontend/diffs/store/mutations_spec.js index 70047899612..e1d855ae0cf 100644 --- a/spec/frontend/diffs/store/mutations_spec.js +++ b/spec/frontend/diffs/store/mutations_spec.js @@ -11,13 +11,11 @@ describe('DiffsStoreMutations', () => { const state = {}; const endpoint = '/diffs/endpoint'; const projectPath = '/root/project'; - const useSingleDiffStyle = false; - mutations[types.SET_BASE_CONFIG](state, { endpoint, projectPath, useSingleDiffStyle }); + mutations[types.SET_BASE_CONFIG](state, { endpoint, projectPath }); expect(state.endpoint).toEqual(endpoint); expect(state.projectPath).toEqual(projectPath); - expect(state.useSingleDiffStyle).toEqual(useSingleDiffStyle); }); }); @@ -70,12 +68,13 @@ describe('DiffsStoreMutations', () => { }); describe('SET_DIFF_DATA', () => { - it('should set diff data type properly', () => { + it('should not modify the existing state', () => { const state = { diffFiles: [ { - ...diffFileMockData, - parallel_diff_lines: [], + content_sha: diffFileMockData.content_sha, + file_hash: diffFileMockData.file_hash, + highlighted_diff_lines: [], }, ], }; @@ -85,43 +84,7 @@ describe('DiffsStoreMutations', () => { mutations[types.SET_DIFF_DATA](state, diffMock); - const firstLine = state.diffFiles[0].parallel_diff_lines[0]; - - expect(firstLine.right.text).toBeUndefined(); - expect(state.diffFiles.length).toEqual(1); - expect(state.diffFiles[0].renderIt).toEqual(true); - expect(state.diffFiles[0].collapsed).toEqual(false); - }); - - describe('given diffsBatchLoad feature flag is enabled', () => { - beforeEach(() => { - gon.features = { diffsBatchLoad: true }; - }); - - afterEach(() => { - delete gon.features; - }); - - it('should not modify the existing state', () => { - const state = { - diffFiles: [ - { - content_sha: diffFileMockData.content_sha, - file_hash: diffFileMockData.file_hash, - highlighted_diff_lines: [], - }, - ], - }; - const diffMock = { - diff_files: [diffFileMockData], - }; - - mutations[types.SET_DIFF_DATA](state, diffMock); - - // If the batch load is enabled, there shouldn't be any processing - // done on the existing state object, so we shouldn't have this. - expect(state.diffFiles[0].parallel_diff_lines).toBeUndefined(); - }); + expect(state.diffFiles[0].parallel_diff_lines).toBeUndefined(); }); }); @@ -682,6 +645,36 @@ describe('DiffsStoreMutations', () => { expect(state.diffFiles[0].highlighted_diff_lines[0].discussions).toHaveLength(1); expect(state.diffFiles[0].highlighted_diff_lines[0].discussions[0].id).toBe(1); }); + + it('should add discussion to file', () => { + const state = { + latestDiff: true, + diffFiles: [ + { + file_hash: 'ABC', + discussions: [], + parallel_diff_lines: [], + highlighted_diff_lines: [], + }, + ], + }; + const discussion = { + id: 1, + line_code: 'ABC_1', + diff_discussion: true, + resolvable: true, + diff_file: { + file_hash: state.diffFiles[0].file_hash, + }, + }; + + mutations[types.SET_LINE_DISCUSSIONS_FOR_FILE](state, { + discussion, + diffPositionByLineCode: null, + }); + + expect(state.diffFiles[0].discussions.length).toEqual(1); + }); }); describe('REMOVE_LINE_DISCUSSIONS', () => { @@ -774,11 +767,11 @@ describe('DiffsStoreMutations', () => { }); }); - describe('UPDATE_CURRENT_DIFF_FILE_ID', () => { + describe('VIEW_DIFF_FILE', () => { it('updates currentDiffFileId', () => { const state = createState(); - mutations[types.UPDATE_CURRENT_DIFF_FILE_ID](state, 'somefileid'); + mutations[types.VIEW_DIFF_FILE](state, 'somefileid'); expect(state.currentDiffFileId).toBe('somefileid'); }); diff --git a/spec/frontend/diffs/store/utils_spec.js b/spec/frontend/diffs/store/utils_spec.js index 62c82468ea0..39a482c85ae 100644 --- a/spec/frontend/diffs/store/utils_spec.js +++ b/spec/frontend/diffs/store/utils_spec.js @@ -1167,4 +1167,59 @@ describe('DiffsStoreUtils', () => { expect(utils.getDefaultWhitespace(undefined, '0')).toBe(true); }); }); + + describe('isAdded', () => { + it.each` + type | expected + ${'new'} | ${true} + ${'new-nonewline'} | ${true} + ${'old'} | ${false} + `('returns $expected when type is $type', ({ type, expected }) => { + expect(utils.isAdded({ type })).toBe(expected); + }); + }); + + describe('isRemoved', () => { + it.each` + type | expected + ${'old'} | ${true} + ${'old-nonewline'} | ${true} + ${'new'} | ${false} + `('returns $expected when type is $type', ({ type, expected }) => { + expect(utils.isRemoved({ type })).toBe(expected); + }); + }); + + describe('isUnchanged', () => { + it.each` + type | expected + ${null} | ${true} + ${'new'} | ${false} + ${'old'} | ${false} + `('returns $expected when type is $type', ({ type, expected }) => { + expect(utils.isUnchanged({ type })).toBe(expected); + }); + }); + + describe('isMeta', () => { + it.each` + type | expected + ${'match'} | ${true} + ${'new-nonewline'} | ${true} + ${'old-nonewline'} | ${true} + ${'new'} | ${false} + `('returns $expected when type is $type', ({ type, expected }) => { + expect(utils.isMeta({ type })).toBe(expected); + }); + }); + + describe('parallelizeDiffLines', () => { + it('converts inline diff lines to parallel diff lines', () => { + const file = getDiffFileMock(); + + expect(utils.parallelizeDiffLines(file.highlighted_diff_lines)).toEqual( + file.parallel_diff_lines, + ); + }); + }); }); |