Commit 504c358f authored by Thomas Randolph's avatar Thomas Randolph

Change the Diffs app in response to the state `commit` changing

parent eca8d9af
......@@ -7,6 +7,7 @@ import createFlash from '~/flash';
import PanelResizer from '~/vue_shared/components/panel_resizer.vue';
import glFeatureFlagsMixin from '~/vue_shared/mixins/gl_feature_flags_mixin';
import { isSingleViewStyle } from '~/helpers/diffs_helper';
import { updateHistory } from '~/lib/utils/url_utility';
import eventHub from '../../notes/event_hub';
import CompareVersions from './compare_versions.vue';
import DiffFile from './diff_file.vue';
......@@ -140,6 +141,20 @@ export default {
},
},
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() {
if (this.needsReload() || this.needsFirstLoad()) {
this.refetchDiffData();
......
......@@ -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 createDiffsStore from '../create_diffs_store';
import axios from '~/lib/utils/axios_utils';
import * as urlUtils from '~/lib/utils/url_utility';
import diffsMockData from '../mock_data/merge_request_diffs';
const mergeRequestDiff = { version_index: 1 };
const TEST_ENDPOINT = `${TEST_HOST}/diff/endpoint`;
const COMMIT_URL = '[BASE URL]/OLD';
const UPDATED_COMMIT_URL = '[BASE URL]/NEW';
describe('diffs/components/app', () => {
const oldMrTabs = window.mrTabs;
......@@ -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', () => {
it('should render compare versions component', () => {
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