Commit 6f4d35be authored by Phil Hughes's avatar Phil Hughes

Merge branch 'kp-fix-roadmap-incorrect-filters-fetch' into 'master'

Fix unnecessary epics fetch request on Roadmap with no filter token value

See merge request gitlab-org/gitlab!72774
parents 0c4e0b2c 689b1f69
...@@ -171,15 +171,17 @@ export default { ...@@ -171,15 +171,17 @@ export default {
* This watcher listens for updates to `filterValue` on * This watcher listens for updates to `filterValue` on
* such instances. :( * such instances. :(
*/ */
filterValue(value) { filterValue(newValue, oldValue) {
const [firstVal] = value; const [firstVal] = newValue;
if ( if (
!this.initialRender && !this.initialRender &&
value.length === 1 && newValue.length === 1 &&
firstVal.type === 'filtered-search-term' && firstVal.type === 'filtered-search-term' &&
!firstVal.value.data !firstVal.value.data
) { ) {
this.$emit('onFilter', []); const filtersCleared =
oldValue[0].type !== 'filtered-search-term' || oldValue[0].value.data !== '';
this.$emit('onFilter', [], filtersCleared);
} }
// Set initial render flag to false // Set initial render flag to false
......
...@@ -150,9 +150,11 @@ export default { ...@@ -150,9 +150,11 @@ export default {
this.setEpicsState(epicsState); this.setEpicsState(epicsState);
this.fetchEpics(); this.fetchEpics();
}, },
handleFilterEpics(filters) { handleFilterEpics(filters, cleared) {
this.setFilterParams(this.getFilterParams(filters)); if (filters.length || cleared) {
this.fetchEpics(); this.setFilterParams(this.getFilterParams(filters));
this.fetchEpics();
}
}, },
handleSortEpics(sortedBy) { handleSortEpics(sortedBy) {
this.setSortedBy(sortedBy); this.setSortedBy(sortedBy);
......
...@@ -277,6 +277,18 @@ describe('RoadmapFilters', () => { ...@@ -277,6 +277,18 @@ describe('RoadmapFilters', () => {
expect(wrapper.vm.fetchEpics).toHaveBeenCalled(); expect(wrapper.vm.fetchEpics).toHaveBeenCalled();
}); });
it('does not set filters params or fetch epics when onFilter event is triggered with empty filters array and cleared param set to false', async () => {
jest.spyOn(wrapper.vm, 'setFilterParams');
jest.spyOn(wrapper.vm, 'fetchEpics');
filteredSearchBar.vm.$emit('onFilter', [], false);
await wrapper.vm.$nextTick();
expect(wrapper.vm.setFilterParams).not.toHaveBeenCalled();
expect(wrapper.vm.fetchEpics).not.toHaveBeenCalled();
});
describe('when user is logged in', () => { describe('when user is logged in', () => {
beforeAll(() => { beforeAll(() => {
gon.current_user_id = 1; gon.current_user_id = 1;
......
...@@ -25,6 +25,7 @@ import { ...@@ -25,6 +25,7 @@ import {
tokenValueMilestone, tokenValueMilestone,
tokenValueMembership, tokenValueMembership,
tokenValueConfidential, tokenValueConfidential,
tokenValueEmpty,
} from './mock_data'; } from './mock_data';
jest.mock('~/vue_shared/components/filtered_search_bar/filtered_search_utils', () => ({ jest.mock('~/vue_shared/components/filtered_search_bar/filtered_search_utils', () => ({
...@@ -43,6 +44,7 @@ const createComponent = ({ ...@@ -43,6 +44,7 @@ const createComponent = ({
recentSearchesStorageKey = 'requirements', recentSearchesStorageKey = 'requirements',
tokens = mockAvailableTokens, tokens = mockAvailableTokens,
sortOptions, sortOptions,
initialFilterValue = [],
showCheckbox = false, showCheckbox = false,
checkboxChecked = false, checkboxChecked = false,
searchInputPlaceholder = 'Filter requirements', searchInputPlaceholder = 'Filter requirements',
...@@ -55,6 +57,7 @@ const createComponent = ({ ...@@ -55,6 +57,7 @@ const createComponent = ({
recentSearchesStorageKey, recentSearchesStorageKey,
tokens, tokens,
sortOptions, sortOptions,
initialFilterValue,
showCheckbox, showCheckbox,
checkboxChecked, checkboxChecked,
searchInputPlaceholder, searchInputPlaceholder,
...@@ -193,19 +196,27 @@ describe('FilteredSearchBarRoot', () => { ...@@ -193,19 +196,27 @@ describe('FilteredSearchBarRoot', () => {
describe('watchers', () => { describe('watchers', () => {
describe('filterValue', () => { describe('filterValue', () => {
it('emits component event `onFilter` with empty array when `filterValue` is cleared by GlFilteredSearch', () => { it('emits component event `onFilter` with empty array and false when filter was never selected', () => {
wrapper = createComponent({ initialFilterValue: [tokenValueEmpty] });
wrapper.setData({ wrapper.setData({
initialRender: false, initialRender: false,
filterValue: [ filterValue: [tokenValueEmpty],
{ });
type: 'filtered-search-term',
value: { data: '' }, return wrapper.vm.$nextTick(() => {
}, expect(wrapper.emitted('onFilter')[0]).toEqual([[], false]);
], });
});
it('emits component event `onFilter` with empty array and true when initially selected filter value was cleared', () => {
wrapper = createComponent({ initialFilterValue: [tokenValueLabel] });
wrapper.setData({
initialRender: false,
filterValue: [tokenValueEmpty],
}); });
return wrapper.vm.$nextTick(() => { return wrapper.vm.$nextTick(() => {
expect(wrapper.emitted('onFilter')[0]).toEqual([[]]); expect(wrapper.emitted('onFilter')[0]).toEqual([[], true]);
}); });
}); });
}); });
......
...@@ -282,6 +282,11 @@ export const tokenValuePlain = { ...@@ -282,6 +282,11 @@ export const tokenValuePlain = {
value: { data: 'foo' }, value: { data: 'foo' },
}; };
export const tokenValueEmpty = {
type: 'filtered-search-term',
value: { data: '' },
};
export const tokenValueEpic = { export const tokenValueEpic = {
type: 'epic_iid', type: 'epic_iid',
value: { value: {
......
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