Commit 020eb30f authored by Zack Cuddy's avatar Zack Cuddy Committed by Nicolò Maria Mezzopera

Global Search Refactor - Fix Scoped Queries

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