Commit 0f8cb7b6 authored by Phil Hughes's avatar Phil Hughes

Fixes the diffs tree list showing incorrectly

This fixes the merge request tree list showing incorrectly
when the metadata comes back before any diffs get returned.

This also changes the logic to hide the tree list
when the window width is small.
parent 2fdd4ce2
<script> <script>
import { mapState, mapGetters, mapActions } from 'vuex'; import { mapState, mapGetters, mapActions } from 'vuex';
import { GlLoadingIcon, GlPagination, GlSprintf } from '@gitlab/ui'; import { GlLoadingIcon, GlPagination, GlSprintf } from '@gitlab/ui';
import { GlBreakpointInstance as bp } from '@gitlab/ui/dist/utils';
import Mousetrap from 'mousetrap'; import Mousetrap from 'mousetrap';
import { __ } from '~/locale'; import { __ } from '~/locale';
import { getParameterByName, parseBoolean } from '~/lib/utils/common_utils'; import { getParameterByName, parseBoolean } from '~/lib/utils/common_utils';
...@@ -316,7 +317,7 @@ export default { ...@@ -316,7 +317,7 @@ export default {
'setHighlightedRow', 'setHighlightedRow',
'cacheTreeListWidth', 'cacheTreeListWidth',
'scrollToFile', 'scrollToFile',
'toggleShowTreeList', 'setShowTreeList',
'navigateToDiffFileIndex', 'navigateToDiffFileIndex',
]), ]),
navigateToDiffFileNumber(number) { navigateToDiffFileNumber(number) {
...@@ -343,7 +344,7 @@ export default { ...@@ -343,7 +344,7 @@ export default {
this.fetchDiffFilesMeta() this.fetchDiffFilesMeta()
.then(({ real_size }) => { .then(({ real_size }) => {
this.diffFilesLength = parseInt(real_size, 10); this.diffFilesLength = parseInt(real_size, 10);
if (toggleTree) this.hideTreeListIfJustOneFile(); if (toggleTree) this.setTreeDisplay();
this.startDiffRendering(); this.startDiffRendering();
}) })
...@@ -353,6 +354,7 @@ export default { ...@@ -353,6 +354,7 @@ export default {
this.fetchDiffFilesBatch() this.fetchDiffFilesBatch()
.then(() => { .then(() => {
if (toggleTree) this.setTreeDisplay();
// Guarantee the discussions are assigned after the batch finishes. // Guarantee the discussions are assigned after the batch finishes.
// Just watching the length of the discussions or the diff files // Just watching the length of the discussions or the diff files
// isn't enough, because with split diff loading, neither will // isn't enough, because with split diff loading, neither will
...@@ -422,12 +424,17 @@ export default { ...@@ -422,12 +424,17 @@ export default {
this.scrollToFile(this.diffFiles[targetIndex].file_path); this.scrollToFile(this.diffFiles[targetIndex].file_path);
} }
}, },
hideTreeListIfJustOneFile() { setTreeDisplay() {
const storedTreeShow = localStorage.getItem(MR_TREE_SHOW_KEY); const storedTreeShow = localStorage.getItem(MR_TREE_SHOW_KEY);
let showTreeList = true;
if ((storedTreeShow === null && this.diffFiles.length <= 1) || storedTreeShow === 'false') { if (storedTreeShow !== null) {
this.toggleShowTreeList(false); showTreeList = Boolean(storedTreeShow);
} else if (!bp.isDesktop() || (!this.isBatchLoading && this.diffFiles.length <= 1)) {
showTreeList = false;
} }
return this.setShowTreeList({ showTreeList, saving: false });
}, },
}, },
minTreeWidth: MIN_TREE_WIDTH, minTreeWidth: MIN_TREE_WIDTH,
......
...@@ -65,11 +65,7 @@ export default { ...@@ -65,11 +65,7 @@ export default {
polyfillSticky(this.$el); polyfillSticky(this.$el);
}, },
methods: { methods: {
...mapActions('diffs', [ ...mapActions('diffs', ['setInlineDiffViewType', 'setParallelDiffViewType', 'setShowTreeList']),
'setInlineDiffViewType',
'setParallelDiffViewType',
'toggleShowTreeList',
]),
expandAllFiles() { expandAllFiles() {
eventHub.$emit(EVT_EXPAND_ALL_FILES); eventHub.$emit(EVT_EXPAND_ALL_FILES);
}, },
...@@ -92,7 +88,7 @@ export default { ...@@ -92,7 +88,7 @@ export default {
class="gl-mr-3 js-toggle-tree-list" class="gl-mr-3 js-toggle-tree-list"
:title="toggleFileBrowserTitle" :title="toggleFileBrowserTitle"
:selected="showTreeList" :selected="showTreeList"
@click="toggleShowTreeList" @click="setShowTreeList({ showTreeList: !showTreeList })"
/> />
<gl-sprintf <gl-sprintf
v-if="showDropdowns" v-if="showDropdowns"
......
...@@ -447,11 +447,11 @@ export const scrollToFile = ({ state, commit }, path) => { ...@@ -447,11 +447,11 @@ export const scrollToFile = ({ state, commit }, path) => {
commit(types.VIEW_DIFF_FILE, fileHash); commit(types.VIEW_DIFF_FILE, fileHash);
}; };
export const toggleShowTreeList = ({ commit, state }, saving = true) => { export const setShowTreeList = ({ commit }, { showTreeList, saving = true }) => {
commit(types.TOGGLE_SHOW_TREE_LIST); commit(types.SET_SHOW_TREE_LIST, showTreeList);
if (saving) { if (saving) {
localStorage.setItem(MR_TREE_SHOW_KEY, state.showTreeList); localStorage.setItem(MR_TREE_SHOW_KEY, showTreeList);
} }
}; };
......
...@@ -17,7 +17,7 @@ export const RENDER_FILE = 'RENDER_FILE'; ...@@ -17,7 +17,7 @@ export const RENDER_FILE = 'RENDER_FILE';
export const SET_LINE_DISCUSSIONS_FOR_FILE = 'SET_LINE_DISCUSSIONS_FOR_FILE'; export const SET_LINE_DISCUSSIONS_FOR_FILE = 'SET_LINE_DISCUSSIONS_FOR_FILE';
export const REMOVE_LINE_DISCUSSIONS_FOR_FILE = 'REMOVE_LINE_DISCUSSIONS_FOR_FILE'; export const REMOVE_LINE_DISCUSSIONS_FOR_FILE = 'REMOVE_LINE_DISCUSSIONS_FOR_FILE';
export const TOGGLE_FOLDER_OPEN = 'TOGGLE_FOLDER_OPEN'; export const TOGGLE_FOLDER_OPEN = 'TOGGLE_FOLDER_OPEN';
export const TOGGLE_SHOW_TREE_LIST = 'TOGGLE_SHOW_TREE_LIST'; export const SET_SHOW_TREE_LIST = 'SET_SHOW_TREE_LIST';
export const VIEW_DIFF_FILE = 'VIEW_DIFF_FILE'; export const VIEW_DIFF_FILE = 'VIEW_DIFF_FILE';
export const OPEN_DIFF_FILE_COMMENT_FORM = 'OPEN_DIFF_FILE_COMMENT_FORM'; export const OPEN_DIFF_FILE_COMMENT_FORM = 'OPEN_DIFF_FILE_COMMENT_FORM';
......
...@@ -247,8 +247,8 @@ export default { ...@@ -247,8 +247,8 @@ export default {
[types.TOGGLE_FOLDER_OPEN](state, path) { [types.TOGGLE_FOLDER_OPEN](state, path) {
state.treeEntries[path].opened = !state.treeEntries[path].opened; state.treeEntries[path].opened = !state.treeEntries[path].opened;
}, },
[types.TOGGLE_SHOW_TREE_LIST](state) { [types.SET_SHOW_TREE_LIST](state, showTreeList) {
state.showTreeList = !state.showTreeList; state.showTreeList = showTreeList;
}, },
[types.VIEW_DIFF_FILE](state, fileId) { [types.VIEW_DIFF_FILE](state, fileId) {
state.currentDiffFileId = fileId; state.currentDiffFileId = fileId;
......
...@@ -638,47 +638,47 @@ describe('diffs/components/app', () => { ...@@ -638,47 +638,47 @@ describe('diffs/components/app', () => {
}); });
}); });
describe('hideTreeListIfJustOneFile', () => { describe('setTreeDisplay', () => {
let toggleShowTreeList; let setShowTreeList;
beforeEach(() => { beforeEach(() => {
toggleShowTreeList = jest.fn(); setShowTreeList = jest.fn();
}); });
afterEach(() => { afterEach(() => {
localStorage.removeItem('mr_tree_show'); localStorage.removeItem('mr_tree_show');
}); });
it('calls toggleShowTreeList when only 1 file', () => { it('calls setShowTreeList when only 1 file', () => {
createComponent({}, ({ state }) => { createComponent({}, ({ state }) => {
state.diffs.diffFiles.push({ sha: '123' }); state.diffs.diffFiles.push({ sha: '123' });
}); });
wrapper.setMethods({ wrapper.setMethods({
toggleShowTreeList, setShowTreeList,
}); });
wrapper.vm.hideTreeListIfJustOneFile(); wrapper.vm.setTreeDisplay();
expect(toggleShowTreeList).toHaveBeenCalledWith(false); expect(setShowTreeList).toHaveBeenCalledWith({ showTreeList: false, saving: false });
}); });
it('does not call toggleShowTreeList when more than 1 file', () => { it('calls setShowTreeList with true when more than 1 file is in diffs array', () => {
createComponent({}, ({ state }) => { createComponent({}, ({ state }) => {
state.diffs.diffFiles.push({ sha: '123' }); state.diffs.diffFiles.push({ sha: '123' });
state.diffs.diffFiles.push({ sha: '124' }); state.diffs.diffFiles.push({ sha: '124' });
}); });
wrapper.setMethods({ wrapper.setMethods({
toggleShowTreeList, setShowTreeList,
}); });
wrapper.vm.hideTreeListIfJustOneFile(); wrapper.vm.setTreeDisplay();
expect(toggleShowTreeList).not.toHaveBeenCalled(); expect(setShowTreeList).toHaveBeenCalledWith({ showTreeList: true, saving: false });
}); });
it('does not call toggleShowTreeList when localStorage is set', () => { it('calls setShowTreeList with localstorage value', () => {
localStorage.setItem('mr_tree_show', 'true'); localStorage.setItem('mr_tree_show', 'true');
createComponent({}, ({ state }) => { createComponent({}, ({ state }) => {
...@@ -686,12 +686,12 @@ describe('diffs/components/app', () => { ...@@ -686,12 +686,12 @@ describe('diffs/components/app', () => {
}); });
wrapper.setMethods({ wrapper.setMethods({
toggleShowTreeList, setShowTreeList,
}); });
wrapper.vm.hideTreeListIfJustOneFile(); wrapper.vm.setTreeDisplay();
expect(toggleShowTreeList).not.toHaveBeenCalled(); expect(setShowTreeList).toHaveBeenCalledWith({ showTreeList: true, saving: false });
}); });
}); });
......
...@@ -32,7 +32,7 @@ import { ...@@ -32,7 +32,7 @@ import {
setHighlightedRow, setHighlightedRow,
toggleTreeOpen, toggleTreeOpen,
scrollToFile, scrollToFile,
toggleShowTreeList, setShowTreeList,
renderFileForDiscussionId, renderFileForDiscussionId,
setRenderTreeList, setRenderTreeList,
setShowWhitespace, setShowWhitespace,
...@@ -901,15 +901,22 @@ describe('DiffsStoreActions', () => { ...@@ -901,15 +901,22 @@ describe('DiffsStoreActions', () => {
}); });
}); });
describe('toggleShowTreeList', () => { describe('setShowTreeList', () => {
it('commits toggle', done => { it('commits toggle', done => {
testAction(toggleShowTreeList, null, {}, [{ type: types.TOGGLE_SHOW_TREE_LIST }], [], done); testAction(
setShowTreeList,
{ showTreeList: true },
{},
[{ type: types.SET_SHOW_TREE_LIST, payload: true }],
[],
done,
);
}); });
it('updates localStorage', () => { it('updates localStorage', () => {
jest.spyOn(localStorage, 'setItem').mockImplementation(() => {}); jest.spyOn(localStorage, 'setItem').mockImplementation(() => {});
toggleShowTreeList({ commit() {}, state: { showTreeList: true } }); setShowTreeList({ commit() {} }, { showTreeList: true });
expect(localStorage.setItem).toHaveBeenCalledWith('mr_tree_show', true); expect(localStorage.setItem).toHaveBeenCalledWith('mr_tree_show', true);
}); });
...@@ -917,7 +924,7 @@ describe('DiffsStoreActions', () => { ...@@ -917,7 +924,7 @@ describe('DiffsStoreActions', () => {
it('does not update localStorage', () => { it('does not update localStorage', () => {
jest.spyOn(localStorage, 'setItem').mockImplementation(() => {}); jest.spyOn(localStorage, 'setItem').mockImplementation(() => {});
toggleShowTreeList({ commit() {}, state: { showTreeList: true } }, false); setShowTreeList({ commit() {} }, { showTreeList: true, saving: false });
expect(localStorage.setItem).not.toHaveBeenCalled(); expect(localStorage.setItem).not.toHaveBeenCalled();
}); });
......
...@@ -621,15 +621,11 @@ describe('DiffsStoreMutations', () => { ...@@ -621,15 +621,11 @@ describe('DiffsStoreMutations', () => {
}); });
}); });
describe('TOGGLE_SHOW_TREE_LIST', () => { describe('SET_SHOW_TREE_LIST', () => {
it('toggles showTreeList', () => { it('sets showTreeList', () => {
const state = createState(); const state = createState();
mutations[types.TOGGLE_SHOW_TREE_LIST](state); mutations[types.SET_SHOW_TREE_LIST](state, true);
expect(state.showTreeList).toBe(false, 'Failed to toggle showTreeList to false');
mutations[types.TOGGLE_SHOW_TREE_LIST](state);
expect(state.showTreeList).toBe(true, 'Failed to toggle showTreeList to true'); expect(state.showTreeList).toBe(true, 'Failed to toggle showTreeList to true');
}); });
......
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