Commit dc1b3423 authored by Nicolò Maria Mezzopera's avatar Nicolò Maria Mezzopera

Merge branch '345745_02-better-search-queries' into 'master'

Global Search Refactor - Fix Scoped Queries

See merge request gitlab-org/gitlab!76475
parents 9c8ca355 020eb30f
import { omitBy, isNil } from 'lodash';
import { objectToQuery } from '~/lib/utils/url_utility';
import {
......@@ -12,23 +13,29 @@ import {
} from '../constants';
export const searchQuery = (state) => {
const query = {
search: state.search,
nav_source: 'navbar',
project_id: state.searchContext.project?.id,
group_id: state.searchContext.group?.id,
scope: state.searchContext.scope,
};
const query = omitBy(
{
search: state.search,
nav_source: 'navbar',
project_id: state.searchContext.project?.id,
group_id: state.searchContext.group?.id,
scope: state.searchContext?.scope,
},
isNil,
);
return `${state.searchPath}?${objectToQuery(query)}`;
};
export const autocompleteQuery = (state) => {
const query = {
term: state.search,
project_id: state.searchContext.project?.id,
project_ref: state.searchContext.ref,
};
const query = omitBy(
{
term: state.search,
project_id: state.searchContext.project?.id,
project_ref: state.searchContext?.ref,
},
isNil,
);
return `${state.autocompletePath}?${objectToQuery(query)}`;
};
......@@ -82,42 +89,43 @@ export const defaultSearchOptions = (state, getters) => {
};
export const projectUrl = (state) => {
if (!state.searchContext.project || !state.searchContext.group) {
return null;
}
const query = {
search: state.search,
nav_source: 'navbar',
project_id: state.searchContext.project.id,
group_id: state.searchContext.group.id,
scope: state.searchContext.scope,
};
const query = omitBy(
{
search: state.search,
nav_source: 'navbar',
project_id: state.searchContext?.project?.id,
group_id: state.searchContext?.group?.id,
scope: state.searchContext?.scope,
},
isNil,
);
return `${state.searchPath}?${objectToQuery(query)}`;
};
export const groupUrl = (state) => {
if (!state.searchContext.group) {
return null;
}
const query = {
search: state.search,
nav_source: 'navbar',
group_id: state.searchContext.group.id,
scope: state.searchContext.scope,
};
const query = omitBy(
{
search: state.search,
nav_source: 'navbar',
group_id: state.searchContext?.group?.id,
scope: state.searchContext?.scope,
},
isNil,
);
return `${state.searchPath}?${objectToQuery(query)}`;
};
export const allUrl = (state) => {
const query = {
search: state.search,
nav_source: 'navbar',
scope: state.searchContext.scope,
};
const query = omitBy(
{
search: state.search,
nav_source: 'navbar',
scope: state.searchContext?.scope,
},
isNil,
);
return `${state.searchPath}?${objectToQuery(query)}`;
};
......
......@@ -37,18 +37,20 @@ describe('Header Search Store Getters', () => {
});
describe.each`
group | project | expectedPath
${null} | ${null} | ${`${MOCK_SEARCH_PATH}?search=${MOCK_SEARCH}&nav_source=navbar&project_id=undefined&group_id=undefined&scope=issues`}
${MOCK_GROUP} | ${null} | ${`${MOCK_SEARCH_PATH}?search=${MOCK_SEARCH}&nav_source=navbar&project_id=undefined&group_id=${MOCK_GROUP.id}&scope=issues`}
${MOCK_GROUP} | ${MOCK_PROJECT} | ${`${MOCK_SEARCH_PATH}?search=${MOCK_SEARCH}&nav_source=navbar&project_id=${MOCK_PROJECT.id}&group_id=${MOCK_GROUP.id}&scope=issues`}
`('searchQuery', ({ group, project, expectedPath }) => {
describe(`when group is ${group?.name} and project is ${project?.name}`, () => {
group | project | scope | expectedPath
${null} | ${null} | ${null} | ${`${MOCK_SEARCH_PATH}?search=${MOCK_SEARCH}&nav_source=navbar`}
${MOCK_GROUP} | ${null} | ${null} | ${`${MOCK_SEARCH_PATH}?search=${MOCK_SEARCH}&nav_source=navbar&group_id=${MOCK_GROUP.id}`}
${null} | ${MOCK_PROJECT} | ${null} | ${`${MOCK_SEARCH_PATH}?search=${MOCK_SEARCH}&nav_source=navbar&project_id=${MOCK_PROJECT.id}`}
${MOCK_GROUP} | ${MOCK_PROJECT} | ${null} | ${`${MOCK_SEARCH_PATH}?search=${MOCK_SEARCH}&nav_source=navbar&project_id=${MOCK_PROJECT.id}&group_id=${MOCK_GROUP.id}`}
${MOCK_GROUP} | ${MOCK_PROJECT} | ${'issues'} | ${`${MOCK_SEARCH_PATH}?search=${MOCK_SEARCH}&nav_source=navbar&project_id=${MOCK_PROJECT.id}&group_id=${MOCK_GROUP.id}&scope=issues`}
`('searchQuery', ({ group, project, scope, expectedPath }) => {
describe(`when group is ${group?.name}, project is ${project?.name}, and scope is ${scope}`, () => {
beforeEach(() => {
createState({
searchContext: {
group,
project,
scope: 'issues',
scope,
},
});
state.search = MOCK_SEARCH;
......@@ -62,8 +64,9 @@ describe('Header Search Store Getters', () => {
describe.each`
project | ref | expectedPath
${null} | ${null} | ${`${MOCK_AUTOCOMPLETE_PATH}?term=${MOCK_SEARCH}&project_id=undefined&project_ref=null`}
${MOCK_PROJECT} | ${null} | ${`${MOCK_AUTOCOMPLETE_PATH}?term=${MOCK_SEARCH}&project_id=${MOCK_PROJECT.id}&project_ref=null`}
${null} | ${null} | ${`${MOCK_AUTOCOMPLETE_PATH}?term=${MOCK_SEARCH}`}
${MOCK_PROJECT} | ${null} | ${`${MOCK_AUTOCOMPLETE_PATH}?term=${MOCK_SEARCH}&project_id=${MOCK_PROJECT.id}`}
${null} | ${MOCK_PROJECT.id} | ${`${MOCK_AUTOCOMPLETE_PATH}?term=${MOCK_SEARCH}&project_ref=${MOCK_PROJECT.id}`}
${MOCK_PROJECT} | ${MOCK_PROJECT.id} | ${`${MOCK_AUTOCOMPLETE_PATH}?term=${MOCK_SEARCH}&project_id=${MOCK_PROJECT.id}&project_ref=${MOCK_PROJECT.id}`}
`('autocompleteQuery', ({ project, ref, expectedPath }) => {
describe(`when project is ${project?.name} and project ref is ${ref}`, () => {
......@@ -132,18 +135,20 @@ describe('Header Search Store Getters', () => {
});
describe.each`
group | project | expectedPath
${null} | ${null} | ${null}
${MOCK_GROUP} | ${null} | ${null}
${MOCK_GROUP} | ${MOCK_PROJECT} | ${`${MOCK_SEARCH_PATH}?search=${MOCK_SEARCH}&nav_source=navbar&project_id=${MOCK_PROJECT.id}&group_id=${MOCK_GROUP.id}&scope=issues`}
`('projectUrl', ({ group, project, expectedPath }) => {
describe(`when group is ${group?.name} and project is ${project?.name}`, () => {
group | project | scope | expectedPath
${null} | ${null} | ${null} | ${`${MOCK_SEARCH_PATH}?search=${MOCK_SEARCH}&nav_source=navbar`}
${MOCK_GROUP} | ${null} | ${null} | ${`${MOCK_SEARCH_PATH}?search=${MOCK_SEARCH}&nav_source=navbar&group_id=${MOCK_GROUP.id}`}
${null} | ${MOCK_PROJECT} | ${null} | ${`${MOCK_SEARCH_PATH}?search=${MOCK_SEARCH}&nav_source=navbar&project_id=${MOCK_PROJECT.id}`}
${MOCK_GROUP} | ${MOCK_PROJECT} | ${null} | ${`${MOCK_SEARCH_PATH}?search=${MOCK_SEARCH}&nav_source=navbar&project_id=${MOCK_PROJECT.id}&group_id=${MOCK_GROUP.id}`}
${MOCK_GROUP} | ${MOCK_PROJECT} | ${'issues'} | ${`${MOCK_SEARCH_PATH}?search=${MOCK_SEARCH}&nav_source=navbar&project_id=${MOCK_PROJECT.id}&group_id=${MOCK_GROUP.id}&scope=issues`}
`('projectUrl', ({ group, project, scope, expectedPath }) => {
describe(`when group is ${group?.name}, project is ${project?.name}, and scope is ${scope}`, () => {
beforeEach(() => {
createState({
searchContext: {
group,
project,
scope: 'issues',
scope,
},
});
state.search = MOCK_SEARCH;
......@@ -156,18 +161,20 @@ describe('Header Search Store Getters', () => {
});
describe.each`
group | project | expectedPath
${null} | ${null} | ${null}
${MOCK_GROUP} | ${null} | ${`${MOCK_SEARCH_PATH}?search=${MOCK_SEARCH}&nav_source=navbar&group_id=${MOCK_GROUP.id}&scope=issues`}
${MOCK_GROUP} | ${MOCK_PROJECT} | ${`${MOCK_SEARCH_PATH}?search=${MOCK_SEARCH}&nav_source=navbar&group_id=${MOCK_GROUP.id}&scope=issues`}
`('groupUrl', ({ group, project, expectedPath }) => {
describe(`when group is ${group?.name} and project is ${project?.name}`, () => {
group | project | scope | expectedPath
${null} | ${null} | ${null} | ${`${MOCK_SEARCH_PATH}?search=${MOCK_SEARCH}&nav_source=navbar`}
${MOCK_GROUP} | ${null} | ${null} | ${`${MOCK_SEARCH_PATH}?search=${MOCK_SEARCH}&nav_source=navbar&group_id=${MOCK_GROUP.id}`}
${null} | ${MOCK_PROJECT} | ${null} | ${`${MOCK_SEARCH_PATH}?search=${MOCK_SEARCH}&nav_source=navbar`}
${MOCK_GROUP} | ${MOCK_PROJECT} | ${null} | ${`${MOCK_SEARCH_PATH}?search=${MOCK_SEARCH}&nav_source=navbar&group_id=${MOCK_GROUP.id}`}
${MOCK_GROUP} | ${MOCK_PROJECT} | ${'issues'} | ${`${MOCK_SEARCH_PATH}?search=${MOCK_SEARCH}&nav_source=navbar&group_id=${MOCK_GROUP.id}&scope=issues`}
`('groupUrl', ({ group, project, scope, expectedPath }) => {
describe(`when group is ${group?.name}, project is ${project?.name}, and scope is ${scope}`, () => {
beforeEach(() => {
createState({
searchContext: {
group,
project,
scope: 'issues',
scope,
},
});
state.search = MOCK_SEARCH;
......@@ -179,20 +186,29 @@ describe('Header Search Store Getters', () => {
});
});
describe('allUrl', () => {
const expectedPath = `${MOCK_SEARCH_PATH}?search=${MOCK_SEARCH}&nav_source=navbar&scope=issues`;
beforeEach(() => {
createState({
searchContext: {
scope: 'issues',
},
describe.each`
group | project | scope | expectedPath
${null} | ${null} | ${null} | ${`${MOCK_SEARCH_PATH}?search=${MOCK_SEARCH}&nav_source=navbar`}
${MOCK_GROUP} | ${null} | ${null} | ${`${MOCK_SEARCH_PATH}?search=${MOCK_SEARCH}&nav_source=navbar`}
${null} | ${MOCK_PROJECT} | ${null} | ${`${MOCK_SEARCH_PATH}?search=${MOCK_SEARCH}&nav_source=navbar`}
${MOCK_GROUP} | ${MOCK_PROJECT} | ${null} | ${`${MOCK_SEARCH_PATH}?search=${MOCK_SEARCH}&nav_source=navbar`}
${MOCK_GROUP} | ${MOCK_PROJECT} | ${'issues'} | ${`${MOCK_SEARCH_PATH}?search=${MOCK_SEARCH}&nav_source=navbar&scope=issues`}
`('allUrl', ({ group, project, scope, expectedPath }) => {
describe(`when group is ${group?.name}, project is ${project?.name}, and scope is ${scope}`, () => {
beforeEach(() => {
createState({
searchContext: {
group,
project,
scope,
},
});
state.search = MOCK_SEARCH;
});
state.search = MOCK_SEARCH;
});
it(`should return ${expectedPath}`, () => {
expect(getters.allUrl(state)).toBe(expectedPath);
it(`should return ${expectedPath}`, () => {
expect(getters.allUrl(state)).toBe(expectedPath);
});
});
});
......
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