Commit fbd80c0a authored by Tim Zallmann's avatar Tim Zallmann

Merge branch 'fl-mr-refactor-performance-improvements' into 'master'

Improves MR refactor getters and state and adds specs

Closes #48937

See merge request gitlab-org/gitlab-ce!20429
parents 4841b7ea adb7f45a
...@@ -20,16 +20,13 @@ export default { ...@@ -20,16 +20,13 @@ export default {
}, },
}, },
computed: { computed: {
...mapGetters(['commit']), ...mapGetters(['commitId']),
normalizedDiffLines() { normalizedDiffLines() {
return this.diffLines.map(line => (line.richText ? trimFirstCharOfLineContent(line) : line)); return this.diffLines.map(line => (line.richText ? trimFirstCharOfLineContent(line) : line));
}, },
diffLinesLength() { diffLinesLength() {
return this.normalizedDiffLines.length; return this.normalizedDiffLines.length;
}, },
commitId() {
return this.commit && this.commit.id;
},
userColorScheme() { userColorScheme() {
return window.gon.user_color_scheme; return window.gon.user_color_scheme;
}, },
......
...@@ -21,7 +21,7 @@ export default { ...@@ -21,7 +21,7 @@ export default {
}, },
}, },
computed: { computed: {
...mapGetters(['commit']), ...mapGetters(['commitId']),
parallelDiffLines() { parallelDiffLines() {
return this.diffLines.map(line => { return this.diffLines.map(line => {
const parallelLine = Object.assign({}, line); const parallelLine = Object.assign({}, line);
...@@ -44,9 +44,6 @@ export default { ...@@ -44,9 +44,6 @@ export default {
diffLinesLength() { diffLinesLength() {
return this.parallelDiffLines.length; return this.parallelDiffLines.length;
}, },
commitId() {
return this.commit && this.commit.id;
},
userColorScheme() { userColorScheme() {
return window.gon.user_color_scheme; return window.gon.user_color_scheme;
}, },
......
import { PARALLEL_DIFF_VIEW_TYPE, INLINE_DIFF_VIEW_TYPE } from '../constants'; import { PARALLEL_DIFF_VIEW_TYPE, INLINE_DIFF_VIEW_TYPE } from '../constants';
export default { export const isParallelView = state => state.diffViewType === PARALLEL_DIFF_VIEW_TYPE;
isParallelView(state) {
return state.diffViewType === PARALLEL_DIFF_VIEW_TYPE; export const isInlineView = state => state.diffViewType === INLINE_DIFF_VIEW_TYPE;
},
isInlineView(state) { export const areAllFilesCollapsed = state => state.diffFiles.every(file => file.collapsed);
return state.diffViewType === INLINE_DIFF_VIEW_TYPE;
}, export const commitId = state => (state.commit && state.commit.id ? state.commit.id : null);
areAllFilesCollapsed(state) {
return state.diffFiles.every(file => file.collapsed); // prevent babel-plugin-rewire from generating an invalid default during karma tests
}, export default () => {};
commit(state) {
return state.commit;
},
};
import Cookies from 'js-cookie';
import { getParameterValues } from '~/lib/utils/url_utility';
import { INLINE_DIFF_VIEW_TYPE, DIFF_VIEW_COOKIE_NAME } from '../../constants';
const viewTypeFromQueryString = getParameterValues('view')[0];
const viewTypeFromCookie = Cookies.get(DIFF_VIEW_COOKIE_NAME);
const defaultViewType = INLINE_DIFF_VIEW_TYPE;
export default () => ({
isLoading: true,
endpoint: '',
basePath: '',
commit: null,
diffFiles: [],
mergeRequestDiffs: [],
diffLineCommentForms: {},
diffViewType: viewTypeFromQueryString || viewTypeFromCookie || defaultViewType,
});
import Cookies from 'js-cookie';
import { getParameterValues } from '~/lib/utils/url_utility';
import actions from '../actions'; import actions from '../actions';
import getters from '../getters'; import * as getters from '../getters';
import mutations from '../mutations'; import mutations from '../mutations';
import { INLINE_DIFF_VIEW_TYPE, DIFF_VIEW_COOKIE_NAME } from '../../constants'; import createState from './diff_state';
const viewTypeFromQueryString = getParameterValues('view')[0];
const viewTypeFromCookie = Cookies.get(DIFF_VIEW_COOKIE_NAME);
const defaultViewType = INLINE_DIFF_VIEW_TYPE;
export default { export default {
state: { state: createState(),
isLoading: true,
endpoint: '',
basePath: '',
commit: null,
diffFiles: [],
mergeRequestDiffs: [],
diffLineCommentForms: {},
diffViewType: viewTypeFromQueryString || viewTypeFromCookie || defaultViewType,
},
getters, getters,
actions, actions,
mutations, mutations,
......
...@@ -66,15 +66,10 @@ export default { ...@@ -66,15 +66,10 @@ export default {
}, },
[types.EXPAND_ALL_FILES](state) { [types.EXPAND_ALL_FILES](state) {
const diffFiles = []; // eslint-disable-next-line no-param-reassign
state.diffFiles = state.diffFiles.map(file => ({
state.diffFiles.forEach(file => { ...file,
diffFiles.push({ collapsed: false,
...file, }));
collapsed: false,
});
});
Object.assign(state, { diffFiles });
}, },
}; };
---
title: Structure getters for diff Store properly and adds specs
merge_request:
author:
type: fixed
import getters from '~/diffs/store/getters'; import * as getters from '~/diffs/store/getters';
import state from '~/diffs/store/modules/diff_state';
import { PARALLEL_DIFF_VIEW_TYPE, INLINE_DIFF_VIEW_TYPE } from '~/diffs/constants'; import { PARALLEL_DIFF_VIEW_TYPE, INLINE_DIFF_VIEW_TYPE } from '~/diffs/constants';
describe('DiffsStoreGetters', () => { describe('DiffsStoreGetters', () => {
let localState;
beforeEach(() => {
localState = state();
});
describe('isParallelView', () => { describe('isParallelView', () => {
it('should return true if view set to parallel view', () => { it('should return true if view set to parallel view', () => {
expect(getters.isParallelView({ diffViewType: PARALLEL_DIFF_VIEW_TYPE })).toBeTruthy(); localState.diffViewType = PARALLEL_DIFF_VIEW_TYPE;
expect(getters.isParallelView(localState)).toEqual(true);
}); });
it('should return false if view not to parallel view', () => { it('should return false if view not to parallel view', () => {
expect(getters.isParallelView({ diffViewType: 'foo' })).toBeFalsy(); localState.diffViewType = INLINE_DIFF_VIEW_TYPE;
expect(getters.isParallelView(localState)).toEqual(false);
}); });
}); });
describe('isInlineView', () => { describe('isInlineView', () => {
it('should return true if view set to inline view', () => { it('should return true if view set to inline view', () => {
expect(getters.isInlineView({ diffViewType: INLINE_DIFF_VIEW_TYPE })).toBeTruthy(); localState.diffViewType = INLINE_DIFF_VIEW_TYPE;
expect(getters.isInlineView(localState)).toEqual(true);
}); });
it('should return false if view not to inline view', () => { it('should return false if view not to inline view', () => {
expect(getters.isInlineView({ diffViewType: PARALLEL_DIFF_VIEW_TYPE })).toBeFalsy(); localState.diffViewType = PARALLEL_DIFF_VIEW_TYPE;
expect(getters.isInlineView(localState)).toEqual(false);
});
});
describe('areAllFilesCollapsed', () => {
it('returns true when all files are collapsed', () => {
localState.diffFiles = [{ collapsed: true }, { collapsed: true }];
expect(getters.areAllFilesCollapsed(localState)).toEqual(true);
});
it('returns false when at least one file is not collapsed', () => {
localState.diffFiles = [{ collapsed: false }, { collapsed: true }];
expect(getters.areAllFilesCollapsed(localState)).toEqual(false);
});
});
describe('commitId', () => {
it('returns commit id when is set', () => {
const commitID = '800f7a91';
localState.commit = {
id: commitID,
};
expect(getters.commitId(localState)).toEqual(commitID);
});
it('returns null when no commit is set', () => {
expect(getters.commitId(localState)).toEqual(null);
}); });
}); });
}); });
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