Commit e48d161f authored by Andrew Fontaine's avatar Andrew Fontaine

Merge branch...

Merge branch '18140-add-to-next-and-previous-commit-buttons-when-viewing-commits-in-merge-request' into 'master'

Add backstage code that allows Diffs app to dynamically respond to changes to `state` `commit`

See merge request gitlab-org/gitlab!29830
parents 91420cd5 504c358f
...@@ -7,6 +7,7 @@ import createFlash from '~/flash'; ...@@ -7,6 +7,7 @@ import createFlash from '~/flash';
import PanelResizer from '~/vue_shared/components/panel_resizer.vue'; import PanelResizer from '~/vue_shared/components/panel_resizer.vue';
import glFeatureFlagsMixin from '~/vue_shared/mixins/gl_feature_flags_mixin'; import glFeatureFlagsMixin from '~/vue_shared/mixins/gl_feature_flags_mixin';
import { isSingleViewStyle } from '~/helpers/diffs_helper'; import { isSingleViewStyle } from '~/helpers/diffs_helper';
import { updateHistory } from '~/lib/utils/url_utility';
import eventHub from '../../notes/event_hub'; import eventHub from '../../notes/event_hub';
import CompareVersions from './compare_versions.vue'; import CompareVersions from './compare_versions.vue';
import DiffFile from './diff_file.vue'; import DiffFile from './diff_file.vue';
...@@ -140,6 +141,20 @@ export default { ...@@ -140,6 +141,20 @@ export default {
}, },
}, },
watch: { watch: {
commit(newCommit, oldCommit) {
const commitChangedAfterRender = newCommit && !this.isLoading;
const commitIsDifferent = oldCommit && newCommit.id !== oldCommit.id;
const url = window?.location ? String(window.location) : '';
if (commitChangedAfterRender && commitIsDifferent) {
updateHistory({
title: document.title,
url: url.replace(oldCommit.id, newCommit.id),
});
this.refetchDiffData();
this.adjustView();
}
},
diffViewType() { diffViewType() {
if (this.needsReload() || this.needsFirstLoad()) { if (this.needsReload() || this.needsFirstLoad()) {
this.refetchDiffData(); this.refetchDiffData();
......
...@@ -14,10 +14,13 @@ import TreeList from '~/diffs/components/tree_list.vue'; ...@@ -14,10 +14,13 @@ import TreeList from '~/diffs/components/tree_list.vue';
import { INLINE_DIFF_VIEW_TYPE, PARALLEL_DIFF_VIEW_TYPE } from '~/diffs/constants'; import { INLINE_DIFF_VIEW_TYPE, PARALLEL_DIFF_VIEW_TYPE } from '~/diffs/constants';
import createDiffsStore from '../create_diffs_store'; import createDiffsStore from '../create_diffs_store';
import axios from '~/lib/utils/axios_utils'; import axios from '~/lib/utils/axios_utils';
import * as urlUtils from '~/lib/utils/url_utility';
import diffsMockData from '../mock_data/merge_request_diffs'; import diffsMockData from '../mock_data/merge_request_diffs';
const mergeRequestDiff = { version_index: 1 }; const mergeRequestDiff = { version_index: 1 };
const TEST_ENDPOINT = `${TEST_HOST}/diff/endpoint`; const TEST_ENDPOINT = `${TEST_HOST}/diff/endpoint`;
const COMMIT_URL = '[BASE URL]/OLD';
const UPDATED_COMMIT_URL = '[BASE URL]/NEW';
describe('diffs/components/app', () => { describe('diffs/components/app', () => {
const oldMrTabs = window.mrTabs; const oldMrTabs = window.mrTabs;
...@@ -602,6 +605,70 @@ describe('diffs/components/app', () => { ...@@ -602,6 +605,70 @@ describe('diffs/components/app', () => {
}); });
}); });
describe('commit watcher', () => {
const spy = () => {
jest.spyOn(wrapper.vm, 'refetchDiffData').mockImplementation(() => {});
jest.spyOn(wrapper.vm, 'adjustView').mockImplementation(() => {});
};
let location;
beforeAll(() => {
location = window.location;
delete window.location;
window.location = COMMIT_URL;
document.title = 'My Title';
});
beforeEach(() => {
jest.spyOn(urlUtils, 'updateHistory');
});
afterAll(() => {
window.location = location;
});
it('when the commit changes and the app is not loading it should update the history, refetch the diff data, and update the view', () => {
createComponent({}, ({ state }) => {
state.diffs.commit = { ...state.diffs.commit, id: 'OLD' };
});
spy();
store.state.diffs.commit = { id: 'NEW' };
return wrapper.vm.$nextTick().then(() => {
expect(urlUtils.updateHistory).toHaveBeenCalledWith({
title: document.title,
url: UPDATED_COMMIT_URL,
});
expect(wrapper.vm.refetchDiffData).toHaveBeenCalled();
expect(wrapper.vm.adjustView).toHaveBeenCalled();
});
});
it.each`
isLoading | oldSha | newSha
${true} | ${'OLD'} | ${'NEW'}
${false} | ${'NEW'} | ${'NEW'}
`(
'given `{ "isLoading": $isLoading, "oldSha": "$oldSha", "newSha": "$newSha" }`, nothing should happen',
({ isLoading, oldSha, newSha }) => {
createComponent({}, ({ state }) => {
state.diffs.isLoading = isLoading;
state.diffs.commit = { ...state.diffs.commit, id: oldSha };
});
spy();
store.state.diffs.commit = { id: newSha };
return wrapper.vm.$nextTick().then(() => {
expect(urlUtils.updateHistory).not.toHaveBeenCalled();
expect(wrapper.vm.refetchDiffData).not.toHaveBeenCalled();
expect(wrapper.vm.adjustView).not.toHaveBeenCalled();
});
},
);
});
describe('diffs', () => { describe('diffs', () => {
it('should render compare versions component', () => { it('should render compare versions component', () => {
createComponent({}, ({ state }) => { createComponent({}, ({ state }) => {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment