Commit 8610b290 authored by Tomas Bulva's avatar Tomas Bulva Committed by Simon Knox

Issue 351689 - Prevent autocomplete searches under X characters

Changelog: changed
parent 0fce8e36
...@@ -11,6 +11,7 @@ import { ...@@ -11,6 +11,7 @@ import {
SEARCH_BOX_INDEX, SEARCH_BOX_INDEX,
SEARCH_INPUT_DESCRIPTION, SEARCH_INPUT_DESCRIPTION,
SEARCH_RESULTS_DESCRIPTION, SEARCH_RESULTS_DESCRIPTION,
SEARCH_SHORTCUTS_MIN_CHARACTERS,
} from '../constants'; } from '../constants';
import HeaderSearchAutocompleteItems from './header_search_autocomplete_items.vue'; import HeaderSearchAutocompleteItems from './header_search_autocomplete_items.vue';
import HeaderSearchDefaultItems from './header_search_default_items.vue'; import HeaderSearchDefaultItems from './header_search_default_items.vue';
...@@ -50,7 +51,7 @@ export default { ...@@ -50,7 +51,7 @@ export default {
}, },
computed: { computed: {
...mapState(['search', 'loading']), ...mapState(['search', 'loading']),
...mapGetters(['searchQuery', 'searchOptions']), ...mapGetters(['searchQuery', 'searchOptions', 'autocompleteGroupedSearchOptions']),
searchText: { searchText: {
get() { get() {
return this.search; return this.search;
...@@ -66,14 +67,20 @@ export default { ...@@ -66,14 +67,20 @@ export default {
return this.currentFocusedOption?.html_id; return this.currentFocusedOption?.html_id;
}, },
isLoggedIn() { isLoggedIn() {
return gon?.current_username; return Boolean(gon?.current_username);
}, },
showSearchDropdown() { showSearchDropdown() {
return this.showDropdown && this.isLoggedIn; const hasResultsUnderMinCharacters =
this.searchText?.length === 1 ? this?.autocompleteGroupedSearchOptions?.length > 0 : true;
return this.showDropdown && this.isLoggedIn && hasResultsUnderMinCharacters;
}, },
showDefaultItems() { showDefaultItems() {
return !this.searchText; return !this.searchText;
}, },
showShortcuts() {
return this.searchText && this.searchText?.length >= SEARCH_SHORTCUTS_MIN_CHARACTERS;
},
defaultIndex() { defaultIndex() {
if (this.showDefaultItems) { if (this.showDefaultItems) {
return SEARCH_BOX_INDEX; return SEARCH_BOX_INDEX;
...@@ -182,7 +189,10 @@ export default { ...@@ -182,7 +189,10 @@ export default {
:current-focused-option="currentFocusedOption" :current-focused-option="currentFocusedOption"
/> />
<template v-else> <template v-else>
<header-search-scoped-items :current-focused-option="currentFocusedOption" /> <header-search-scoped-items
v-if="showShortcuts"
:current-focused-option="currentFocusedOption"
/>
<header-search-autocomplete-items :current-focused-option="currentFocusedOption" /> <header-search-autocomplete-items :current-focused-option="currentFocusedOption" />
</template> </template>
</div> </div>
......
...@@ -72,8 +72,8 @@ export default { ...@@ -72,8 +72,8 @@ export default {
<template> <template>
<div> <div>
<template v-if="!loading"> <template v-if="!loading">
<div v-for="option in autocompleteGroupedSearchOptions" :key="option.category"> <div v-for="(option, index) in autocompleteGroupedSearchOptions" :key="option.category">
<gl-dropdown-divider /> <gl-dropdown-divider v-if="index > 0" />
<gl-dropdown-section-header>{{ option.category }}</gl-dropdown-section-header> <gl-dropdown-section-header>{{ option.category }}</gl-dropdown-section-header>
<gl-dropdown-item <gl-dropdown-item
v-for="data in option.data" v-for="data in option.data"
......
<script> <script>
import { GlDropdownItem } from '@gitlab/ui'; import { GlDropdownItem, GlDropdownDivider } from '@gitlab/ui';
import { mapState, mapGetters } from 'vuex'; import { mapState, mapGetters } from 'vuex';
import { __, sprintf } from '~/locale'; import { __, sprintf } from '~/locale';
...@@ -7,6 +7,7 @@ export default { ...@@ -7,6 +7,7 @@ export default {
name: 'HeaderSearchScopedItems', name: 'HeaderSearchScopedItems',
components: { components: {
GlDropdownItem, GlDropdownItem,
GlDropdownDivider,
}, },
props: { props: {
currentFocusedOption: { currentFocusedOption: {
...@@ -17,7 +18,7 @@ export default { ...@@ -17,7 +18,7 @@ export default {
}, },
computed: { computed: {
...mapState(['search']), ...mapState(['search']),
...mapGetters(['scopedSearchOptions']), ...mapGetters(['scopedSearchOptions', 'autocompleteGroupedSearchOptions']),
}, },
methods: { methods: {
isOptionFocused(option) { isOptionFocused(option) {
...@@ -53,5 +54,6 @@ export default { ...@@ -53,5 +54,6 @@ export default {
<span v-if="option.scope" class="gl-font-style-italic">{{ option.scope }}</span> <span v-if="option.scope" class="gl-font-style-italic">{{ option.scope }}</span>
</span> </span>
</gl-dropdown-item> </gl-dropdown-item>
<gl-dropdown-divider v-if="autocompleteGroupedSearchOptions.length > 0" />
</div> </div>
</template> </template>
...@@ -28,6 +28,8 @@ export const FIRST_DROPDOWN_INDEX = 0; ...@@ -28,6 +28,8 @@ export const FIRST_DROPDOWN_INDEX = 0;
export const SEARCH_BOX_INDEX = -1; export const SEARCH_BOX_INDEX = -1;
export const SEARCH_SHORTCUTS_MIN_CHARACTERS = 2;
export const SEARCH_INPUT_DESCRIPTION = 'search-input-description'; export const SEARCH_INPUT_DESCRIPTION = 'search-input-description';
export const SEARCH_RESULTS_DESCRIPTION = 'search-results-description'; export const SEARCH_RESULTS_DESCRIPTION = 'search-results-description';
...@@ -5,7 +5,9 @@ export const fetchAutocompleteOptions = ({ commit, getters }) => { ...@@ -5,7 +5,9 @@ export const fetchAutocompleteOptions = ({ commit, getters }) => {
commit(types.REQUEST_AUTOCOMPLETE); commit(types.REQUEST_AUTOCOMPLETE);
return axios return axios
.get(getters.autocompleteQuery) .get(getters.autocompleteQuery)
.then(({ data }) => commit(types.RECEIVE_AUTOCOMPLETE_SUCCESS, data)) .then(({ data }) => {
commit(types.RECEIVE_AUTOCOMPLETE_SUCCESS, data);
})
.catch(() => { .catch(() => {
commit(types.RECEIVE_AUTOCOMPLETE_ERROR); commit(types.RECEIVE_AUTOCOMPLETE_ERROR);
}); });
......
...@@ -190,7 +190,6 @@ export const autocompleteGroupedSearchOptions = (state) => { ...@@ -190,7 +190,6 @@ export const autocompleteGroupedSearchOptions = (state) => {
results.push(groupedOptions[option.category]); results.push(groupedOptions[option.category]);
} }
}); });
return results; return results;
}; };
......
...@@ -127,6 +127,7 @@ function deferredInitialisation() { ...@@ -127,6 +127,7 @@ function deferredInitialisation() {
// In case the user started searching before we bootstrapped, let's pass the search along. // In case the user started searching before we bootstrapped, let's pass the search along.
const initialSearchValue = searchInputBox.value; const initialSearchValue = searchInputBox.value;
await initHeaderSearchApp(initialSearchValue); await initHeaderSearchApp(initialSearchValue);
// this is new #search input element. We need to re-find it.
document.querySelector('#search').focus(); document.querySelector('#search').focus();
}) })
.catch(() => {}); .catch(() => {});
......
...@@ -16,6 +16,7 @@ import { ...@@ -16,6 +16,7 @@ import {
MOCK_USERNAME, MOCK_USERNAME,
MOCK_DEFAULT_SEARCH_OPTIONS, MOCK_DEFAULT_SEARCH_OPTIONS,
MOCK_SCOPED_SEARCH_OPTIONS, MOCK_SCOPED_SEARCH_OPTIONS,
MOCK_SORTED_AUTOCOMPLETE_OPTIONS,
} from '../mock_data'; } from '../mock_data';
Vue.use(Vuex); Vue.use(Vuex);
...@@ -108,6 +109,11 @@ describe('HeaderSearchApp', () => { ...@@ -108,6 +109,11 @@ describe('HeaderSearchApp', () => {
search | showDefault | showScoped | showAutocomplete | showDropdownNavigation search | showDefault | showScoped | showAutocomplete | showDropdownNavigation
${null} | ${true} | ${false} | ${false} | ${true} ${null} | ${true} | ${false} | ${false} | ${true}
${''} | ${true} | ${false} | ${false} | ${true} ${''} | ${true} | ${false} | ${false} | ${true}
${'1'} | ${false} | ${false} | ${false} | ${false}
${')'} | ${false} | ${false} | ${false} | ${false}
${'t'} | ${false} | ${false} | ${true} | ${true}
${'te'} | ${false} | ${true} | ${true} | ${true}
${'tes'} | ${false} | ${true} | ${true} | ${true}
${MOCK_SEARCH} | ${false} | ${true} | ${true} | ${true} ${MOCK_SEARCH} | ${false} | ${true} | ${true} | ${true}
`( `(
'Header Search Dropdown Items', 'Header Search Dropdown Items',
...@@ -115,7 +121,13 @@ describe('HeaderSearchApp', () => { ...@@ -115,7 +121,13 @@ describe('HeaderSearchApp', () => {
describe(`when search is ${search}`, () => { describe(`when search is ${search}`, () => {
beforeEach(() => { beforeEach(() => {
window.gon.current_username = MOCK_USERNAME; window.gon.current_username = MOCK_USERNAME;
createComponent({ search }); createComponent(
{ search },
{
autocompleteGroupedSearchOptions: () =>
search.match(/^[A-Za-z]+$/g) ? MOCK_SORTED_AUTOCOMPLETE_OPTIONS : [],
},
);
findHeaderSearchInput().vm.$emit('click'); findHeaderSearchInput().vm.$emit('click');
}); });
......
import { GlDropdownItem, GlLoadingIcon, GlAvatar, GlAlert } from '@gitlab/ui'; import { GlDropdownItem, GlLoadingIcon, GlAvatar, GlAlert, GlDropdownDivider } from '@gitlab/ui';
import { shallowMount } from '@vue/test-utils'; import { shallowMount } from '@vue/test-utils';
import Vue, { nextTick } from 'vue'; import Vue, { nextTick } from 'vue';
import Vuex from 'vuex'; import Vuex from 'vuex';
...@@ -9,7 +9,14 @@ import { ...@@ -9,7 +9,14 @@ import {
PROJECTS_CATEGORY, PROJECTS_CATEGORY,
SMALL_AVATAR_PX, SMALL_AVATAR_PX,
} from '~/header_search/constants'; } from '~/header_search/constants';
import { MOCK_GROUPED_AUTOCOMPLETE_OPTIONS, MOCK_SORTED_AUTOCOMPLETE_OPTIONS } from '../mock_data'; import {
MOCK_GROUPED_AUTOCOMPLETE_OPTIONS,
MOCK_SORTED_AUTOCOMPLETE_OPTIONS,
MOCK_GROUPED_AUTOCOMPLETE_OPTIONS_SETTINGS_HELP,
MOCK_GROUPED_AUTOCOMPLETE_OPTIONS_HELP,
MOCK_SEARCH,
MOCK_GROUPED_AUTOCOMPLETE_OPTIONS_2,
} from '../mock_data';
Vue.use(Vuex); Vue.use(Vuex);
...@@ -41,6 +48,7 @@ describe('HeaderSearchAutocompleteItems', () => { ...@@ -41,6 +48,7 @@ describe('HeaderSearchAutocompleteItems', () => {
}); });
const findDropdownItems = () => wrapper.findAllComponents(GlDropdownItem); const findDropdownItems = () => wrapper.findAllComponents(GlDropdownItem);
const findGlDropdownDividers = () => wrapper.findAllComponents(GlDropdownDivider);
const findFirstDropdownItem = () => findDropdownItems().at(0); const findFirstDropdownItem = () => findDropdownItems().at(0);
const findDropdownItemTitles = () => findDropdownItems().wrappers.map((w) => w.text()); const findDropdownItemTitles = () => findDropdownItems().wrappers.map((w) => w.text());
const findDropdownItemLinks = () => findDropdownItems().wrappers.map((w) => w.attributes('href')); const findDropdownItemLinks = () => findDropdownItems().wrappers.map((w) => w.attributes('href'));
...@@ -140,6 +148,34 @@ describe('HeaderSearchAutocompleteItems', () => { ...@@ -140,6 +148,34 @@ describe('HeaderSearchAutocompleteItems', () => {
}); });
}); });
}); });
describe.each`
search | items | dividerCount
${null} | ${[]} | ${0}
${''} | ${[]} | ${0}
${'1'} | ${[]} | ${0}
${')'} | ${[]} | ${0}
${'t'} | ${MOCK_GROUPED_AUTOCOMPLETE_OPTIONS_SETTINGS_HELP} | ${1}
${'te'} | ${MOCK_GROUPED_AUTOCOMPLETE_OPTIONS_HELP} | ${0}
${'tes'} | ${MOCK_GROUPED_AUTOCOMPLETE_OPTIONS_2} | ${1}
${MOCK_SEARCH} | ${MOCK_GROUPED_AUTOCOMPLETE_OPTIONS_2} | ${1}
`('Header Search Dropdown Dividers', ({ search, items, dividerCount }) => {
describe(`when search is ${search}`, () => {
beforeEach(() => {
createComponent(
{ search },
{
autocompleteGroupedSearchOptions: () => items,
},
{},
);
});
it(`component should have ${dividerCount} dividers`, () => {
expect(findGlDropdownDividers()).toHaveLength(dividerCount);
});
});
});
}); });
describe('watchers', () => { describe('watchers', () => {
......
import { GlDropdownItem } from '@gitlab/ui'; import { GlDropdownItem, GlDropdownDivider } from '@gitlab/ui';
import { shallowMount } from '@vue/test-utils'; import { shallowMount } from '@vue/test-utils';
import Vue from 'vue'; import Vue from 'vue';
import Vuex from 'vuex'; import Vuex from 'vuex';
import { trimText } from 'helpers/text_helper'; import { trimText } from 'helpers/text_helper';
import HeaderSearchScopedItems from '~/header_search/components/header_search_scoped_items.vue'; import HeaderSearchScopedItems from '~/header_search/components/header_search_scoped_items.vue';
import { MOCK_SEARCH, MOCK_SCOPED_SEARCH_OPTIONS } from '../mock_data'; import {
MOCK_SEARCH,
MOCK_SCOPED_SEARCH_OPTIONS,
MOCK_GROUPED_AUTOCOMPLETE_OPTIONS,
} from '../mock_data';
Vue.use(Vuex); Vue.use(Vuex);
describe('HeaderSearchScopedItems', () => { describe('HeaderSearchScopedItems', () => {
let wrapper; let wrapper;
const createComponent = (initialState, props) => { const createComponent = (initialState, mockGetters, props) => {
const store = new Vuex.Store({ const store = new Vuex.Store({
state: { state: {
search: MOCK_SEARCH, search: MOCK_SEARCH,
...@@ -19,6 +23,8 @@ describe('HeaderSearchScopedItems', () => { ...@@ -19,6 +23,8 @@ describe('HeaderSearchScopedItems', () => {
}, },
getters: { getters: {
scopedSearchOptions: () => MOCK_SCOPED_SEARCH_OPTIONS, scopedSearchOptions: () => MOCK_SCOPED_SEARCH_OPTIONS,
autocompleteGroupedSearchOptions: () => MOCK_GROUPED_AUTOCOMPLETE_OPTIONS,
...mockGetters,
}, },
}); });
...@@ -35,6 +41,7 @@ describe('HeaderSearchScopedItems', () => { ...@@ -35,6 +41,7 @@ describe('HeaderSearchScopedItems', () => {
}); });
const findDropdownItems = () => wrapper.findAllComponents(GlDropdownItem); const findDropdownItems = () => wrapper.findAllComponents(GlDropdownItem);
const findGlDropdownDivider = () => wrapper.findComponent(GlDropdownDivider);
const findFirstDropdownItem = () => findDropdownItems().at(0); const findFirstDropdownItem = () => findDropdownItems().at(0);
const findDropdownItemTitles = () => findDropdownItems().wrappers.map((w) => trimText(w.text())); const findDropdownItemTitles = () => findDropdownItems().wrappers.map((w) => trimText(w.text()));
const findDropdownItemAriaLabels = () => const findDropdownItemAriaLabels = () =>
...@@ -79,7 +86,7 @@ describe('HeaderSearchScopedItems', () => { ...@@ -79,7 +86,7 @@ describe('HeaderSearchScopedItems', () => {
`('isOptionFocused', ({ currentFocusedOption, isFocused, ariaSelected }) => { `('isOptionFocused', ({ currentFocusedOption, isFocused, ariaSelected }) => {
describe(`when currentFocusedOption.html_id is ${currentFocusedOption?.html_id}`, () => { describe(`when currentFocusedOption.html_id is ${currentFocusedOption?.html_id}`, () => {
beforeEach(() => { beforeEach(() => {
createComponent({}, { currentFocusedOption }); createComponent({}, {}, { currentFocusedOption });
}); });
it(`should${isFocused ? '' : ' not'} have gl-bg-gray-50 applied`, () => { it(`should${isFocused ? '' : ' not'} have gl-bg-gray-50 applied`, () => {
...@@ -91,5 +98,21 @@ describe('HeaderSearchScopedItems', () => { ...@@ -91,5 +98,21 @@ describe('HeaderSearchScopedItems', () => {
}); });
}); });
}); });
describe.each`
autosuggestResults | showDivider
${[]} | ${false}
${MOCK_GROUPED_AUTOCOMPLETE_OPTIONS} | ${true}
`('scoped search items', ({ autosuggestResults, showDivider }) => {
describe(`when when we have ${autosuggestResults.length} auto-sugest results`, () => {
beforeEach(() => {
createComponent({}, { autocompleteGroupedSearchOptions: () => autosuggestResults }, {});
});
it(`divider should${showDivider ? '' : ' not'} be shown`, () => {
expect(findGlDropdownDivider().exists()).toBe(showDivider);
});
});
});
}); });
}); });
...@@ -226,3 +226,98 @@ export const MOCK_SORTED_AUTOCOMPLETE_OPTIONS = [ ...@@ -226,3 +226,98 @@ export const MOCK_SORTED_AUTOCOMPLETE_OPTIONS = [
url: 'help/gitlab', url: 'help/gitlab',
}, },
]; ];
export const MOCK_GROUPED_AUTOCOMPLETE_OPTIONS_HELP = [
{
category: 'Help',
data: [
{
html_id: 'autocomplete-Help-1',
category: 'Help',
label: 'Rake Tasks Help',
url: '/help/raketasks/index',
},
{
html_id: 'autocomplete-Help-2',
category: 'Help',
label: 'System Hooks Help',
url: '/help/system_hooks/system_hooks',
},
],
},
];
export const MOCK_GROUPED_AUTOCOMPLETE_OPTIONS_SETTINGS_HELP = [
{
category: 'Settings',
data: [
{
html_id: 'autocomplete-Settings-0',
category: 'Settings',
label: 'User settings',
url: '/-/profile',
},
{
html_id: 'autocomplete-Settings-3',
category: 'Settings',
label: 'Admin Section',
url: '/admin',
},
],
},
{
category: 'Help',
data: [
{
html_id: 'autocomplete-Help-1',
category: 'Help',
label: 'Rake Tasks Help',
url: '/help/raketasks/index',
},
{
html_id: 'autocomplete-Help-2',
category: 'Help',
label: 'System Hooks Help',
url: '/help/system_hooks/system_hooks',
},
],
},
];
export const MOCK_GROUPED_AUTOCOMPLETE_OPTIONS_2 = [
{
category: 'Groups',
data: [
{
html_id: 'autocomplete-Groups-0',
category: 'Groups',
id: 148,
label: 'Jashkenas / Test Subgroup / test-subgroup',
url: '/jashkenas/test-subgroup/test-subgroup',
avatar_url: '',
},
{
html_id: 'autocomplete-Groups-1',
category: 'Groups',
id: 147,
label: 'Jashkenas / Test Subgroup',
url: '/jashkenas/test-subgroup',
avatar_url: '',
},
],
},
{
category: 'Projects',
data: [
{
html_id: 'autocomplete-Projects-2',
category: 'Projects',
id: 1,
value: 'Gitlab Test',
label: 'Gitlab Org / Gitlab Test',
url: '/gitlab-org/gitlab-test',
avatar_url: '/uploads/-/system/project/avatar/1/icons8-gitlab-512.png',
},
],
},
];
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