Commit d29c4eae authored by Kushal Pandya's avatar Kushal Pandya

Fix history item select event bug

Fixes bug where `history-item-selected` event emitted
`onFilter` on component but didn't include correct payload.
parent ca7aa622
...@@ -83,6 +83,7 @@ export default { ...@@ -83,6 +83,7 @@ export default {
return { return {
initialRender: true, initialRender: true,
recentSearchesPromise: null, recentSearchesPromise: null,
recentSearches: [],
filterValue: this.initialFilterValue, filterValue: this.initialFilterValue,
selectedSortOption, selectedSortOption,
selectedSortDirection, selectedSortDirection,
...@@ -180,11 +181,9 @@ export default { ...@@ -180,11 +181,9 @@ export default {
this.recentSearchesStore.state.recentSearches.concat(searches), this.recentSearchesStore.state.recentSearches.concat(searches),
); );
this.recentSearchesService.save(resultantSearches); this.recentSearchesService.save(resultantSearches);
this.recentSearches = resultantSearches;
}); });
}, },
getRecentSearches() {
return this.recentSearchesStore?.state.recentSearches;
},
handleSortOptionClick(sortBy) { handleSortOptionClick(sortBy) {
this.selectedSortOption = sortBy; this.selectedSortOption = sortBy;
this.$emit('onSort', sortBy.sortDirection[this.selectedSortDirection]); this.$emit('onSort', sortBy.sortDirection[this.selectedSortDirection]);
...@@ -196,9 +195,13 @@ export default { ...@@ -196,9 +195,13 @@ export default {
: SortDirection.ascending; : SortDirection.ascending;
this.$emit('onSort', this.selectedSortOption.sortDirection[this.selectedSortDirection]); this.$emit('onSort', this.selectedSortOption.sortDirection[this.selectedSortDirection]);
}, },
handleHistoryItemSelected(filters) {
this.$emit('onFilter', filters);
},
handleClearHistory() { handleClearHistory() {
const resultantSearches = this.recentSearchesStore.setRecentSearches([]); const resultantSearches = this.recentSearchesStore.setRecentSearches([]);
this.recentSearchesService.save(resultantSearches); this.recentSearchesService.save(resultantSearches);
this.recentSearches = [];
}, },
handleFilterSubmit(filters) { handleFilterSubmit(filters) {
if (this.recentSearchesStorageKey) { if (this.recentSearchesStorageKey) {
...@@ -207,6 +210,7 @@ export default { ...@@ -207,6 +210,7 @@ export default {
if (filters.length) { if (filters.length) {
const resultantSearches = this.recentSearchesStore.addRecentSearch(filters); const resultantSearches = this.recentSearchesStore.addRecentSearch(filters);
this.recentSearchesService.save(resultantSearches); this.recentSearchesService.save(resultantSearches);
this.recentSearches = resultantSearches;
} }
}) })
.catch(() => { .catch(() => {
...@@ -225,16 +229,15 @@ export default { ...@@ -225,16 +229,15 @@ export default {
v-model="filterValue" v-model="filterValue"
:placeholder="searchInputPlaceholder" :placeholder="searchInputPlaceholder"
:available-tokens="tokens" :available-tokens="tokens"
:history-items="getRecentSearches()" :history-items="recentSearches"
class="flex-grow-1" class="flex-grow-1"
@history-item-selected="$emit('onFilter', filters)" @history-item-selected="handleHistoryItemSelected"
@clear-history="handleClearHistory" @clear-history="handleClearHistory"
@submit="handleFilterSubmit" @submit="handleFilterSubmit"
@clear="$emit('onFilter', [])"
> >
<template #history-item="{ historyItem }"> <template #history-item="{ historyItem }">
<template v-for="token in historyItem"> <template v-for="(token, index) in historyItem">
<span v-if="typeof token === 'string'" :key="token" class="gl-px-1">"{{ token }}"</span> <span v-if="typeof token === 'string'" :key="index" class="gl-px-1">"{{ token }}"</span>
<span v-else :key="`${token.type}-${token.value.data}`" class="gl-px-1"> <span v-else :key="`${token.type}-${token.value.data}`" class="gl-px-1">
<span v-if="tokenTitles[token.type]" <span v-if="tokenTitles[token.type]"
>{{ tokenTitles[token.type] }} :{{ token.value.operator }}</span >{{ tokenTitles[token.type] }} :{{ token.value.operator }}</span
......
...@@ -10,7 +10,6 @@ import { updateHistory, setUrlParams } from '~/lib/utils/url_utility'; ...@@ -10,7 +10,6 @@ import { updateHistory, setUrlParams } from '~/lib/utils/url_utility';
import FilteredSearchBar from '~/vue_shared/components/filtered_search_bar/filtered_search_bar_root.vue'; import FilteredSearchBar from '~/vue_shared/components/filtered_search_bar/filtered_search_bar_root.vue';
import AuthorToken from '~/vue_shared/components/filtered_search_bar/tokens/author_token.vue'; import AuthorToken from '~/vue_shared/components/filtered_search_bar/tokens/author_token.vue';
import { ANY_AUTHOR } from '~/vue_shared/components/filtered_search_bar/constants'; import { ANY_AUTHOR } from '~/vue_shared/components/filtered_search_bar/constants';
import RecentSearchesStorageKeys from 'ee/filtered_search/recent_searches_storage_keys';
import RequirementsTabs from './requirements_tabs.vue'; import RequirementsTabs from './requirements_tabs.vue';
import RequirementsLoading from './requirements_loading.vue'; import RequirementsLoading from './requirements_loading.vue';
...@@ -28,7 +27,6 @@ import { FilterState, AvailableSortOptions, DEFAULT_PAGE_SIZE } from '../constan ...@@ -28,7 +27,6 @@ import { FilterState, AvailableSortOptions, DEFAULT_PAGE_SIZE } from '../constan
export default { export default {
DEFAULT_PAGE_SIZE, DEFAULT_PAGE_SIZE,
AvailableSortOptions, AvailableSortOptions,
requirementsRecentSearchesKey: RecentSearchesStorageKeys.requirements,
components: { components: {
GlPagination, GlPagination,
FilteredSearchBar, FilteredSearchBar,
...@@ -526,7 +524,7 @@ export default { ...@@ -526,7 +524,7 @@ export default {
:sort-options="$options.AvailableSortOptions" :sort-options="$options.AvailableSortOptions"
:initial-filter-value="getFilteredSearchValue()" :initial-filter-value="getFilteredSearchValue()"
:initial-sort-by="sortBy" :initial-sort-by="sortBy"
:recent-searches-storage-key="$options.requirementsRecentSearchesKey" recent-searches-storage-key="requirements"
class="row-content-block" class="row-content-block"
@onFilter="handleFilterRequirements" @onFilter="handleFilterRequirements"
@onSort="handleSortRequirements" @onSort="handleSortRequirements"
......
...@@ -766,7 +766,7 @@ describe('RequirementsRoot', () => { ...@@ -766,7 +766,7 @@ describe('RequirementsRoot', () => {
}, },
]); ]);
expect(wrapper.find(FilteredSearchBarRoot).props('recentSearchesStorageKey')).toBe( expect(wrapper.find(FilteredSearchBarRoot).props('recentSearchesStorageKey')).toBe(
'requirements-recent-searches', 'requirements',
); );
}); });
......
...@@ -139,14 +139,6 @@ describe('FilteredSearchBarRoot', () => { ...@@ -139,14 +139,6 @@ describe('FilteredSearchBarRoot', () => {
}); });
}); });
describe('getRecentSearches', () => {
it('returns array of strings representing recent searches', () => {
wrapper.vm.recentSearchesStore.setRecentSearches(['foo']);
expect(wrapper.vm.getRecentSearches()).toEqual(['foo']);
});
});
describe('handleSortOptionClick', () => { describe('handleSortOptionClick', () => {
it('emits component event `onSort` with selected sort by value', () => { it('emits component event `onSort` with selected sort by value', () => {
wrapper.vm.handleSortOptionClick(mockSortOptions[1]); wrapper.vm.handleSortOptionClick(mockSortOptions[1]);
...@@ -178,6 +170,14 @@ describe('FilteredSearchBarRoot', () => { ...@@ -178,6 +170,14 @@ describe('FilteredSearchBarRoot', () => {
}); });
}); });
describe('handleHistoryItemSelected', () => {
it('emits `onFilter` event with provided filters param', () => {
wrapper.vm.handleHistoryItemSelected(mockHistoryItems[0]);
expect(wrapper.emitted('onFilter')[0]).toEqual([mockHistoryItems[0]]);
});
});
describe('handleClearHistory', () => { describe('handleClearHistory', () => {
it('clears search history from recent searches store', () => { it('clears search history from recent searches store', () => {
jest.spyOn(wrapper.vm.recentSearchesStore, 'setRecentSearches').mockReturnValue([]); jest.spyOn(wrapper.vm.recentSearchesStore, 'setRecentSearches').mockReturnValue([]);
...@@ -187,7 +187,7 @@ describe('FilteredSearchBarRoot', () => { ...@@ -187,7 +187,7 @@ describe('FilteredSearchBarRoot', () => {
expect(wrapper.vm.recentSearchesStore.setRecentSearches).toHaveBeenCalledWith([]); expect(wrapper.vm.recentSearchesStore.setRecentSearches).toHaveBeenCalledWith([]);
expect(wrapper.vm.recentSearchesService.save).toHaveBeenCalledWith([]); expect(wrapper.vm.recentSearchesService.save).toHaveBeenCalledWith([]);
expect(wrapper.vm.getRecentSearches()).toEqual([]); expect(wrapper.vm.recentSearches).toEqual([]);
}); });
}); });
...@@ -223,6 +223,16 @@ describe('FilteredSearchBarRoot', () => { ...@@ -223,6 +223,16 @@ describe('FilteredSearchBarRoot', () => {
}); });
}); });
it('sets `recentSearches` data prop with array of searches', () => {
jest.spyOn(wrapper.vm.recentSearchesService, 'save');
wrapper.vm.handleFilterSubmit(mockFilters);
return wrapper.vm.recentSearchesPromise.then(() => {
expect(wrapper.vm.recentSearches).toEqual([mockFilters]);
});
});
it('emits component event `onFilter` with provided filters param', () => { it('emits component event `onFilter` with provided filters param', () => {
wrapper.vm.handleFilterSubmit(mockFilters); wrapper.vm.handleFilterSubmit(mockFilters);
...@@ -236,10 +246,9 @@ describe('FilteredSearchBarRoot', () => { ...@@ -236,10 +246,9 @@ describe('FilteredSearchBarRoot', () => {
wrapper.setData({ wrapper.setData({
selectedSortOption: mockSortOptions[0], selectedSortOption: mockSortOptions[0],
selectedSortDirection: SortDirection.descending, selectedSortDirection: SortDirection.descending,
recentSearches: mockHistoryItems,
}); });
wrapper.vm.recentSearchesStore.setRecentSearches(mockHistoryItems);
return wrapper.vm.$nextTick(); return wrapper.vm.$nextTick();
}); });
......
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