Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set correctly the filters in History Query when selecting a Query Preview with filters #1325

Merged
merged 16 commits into from
Oct 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
HistoryQueriesMutations,
HistoryQueriesState
} from '../types';
import { InternalSearchResponse } from '../../../search/index';
import { resetHistoryQueriesStateWith } from './utils';

describe('testing history queries module actions', () => {
Expand Down Expand Up @@ -279,12 +280,43 @@ describe('testing history queries module actions', () => {
const requestFilters: Record<string, Filter[]> = {
categoryPaths: [
{
id: 'categoryIds:c018019b6',
id: 'categoryIds:66dd06d9f',
selected: true,
modelName: 'HierarchicalFilter'
}
]
};
const selectedFilters: Filter[] = [
{
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
facetId: 'categoryPaths',
id: 'categoryIds:66dd06d9f',
label: 'suede',
modelName: 'HierarchicalFilter',
selected: true,
totalResults: 2
}
];
const responseFacets: InternalSearchResponse['facets'] = [
{
filters: [
{
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
facetId: 'categoryPaths',
id: 'categoryIds:66dd06d9f',
label: 'suede',
modelName: 'HierarchicalFilter',
selected: true,
totalResults: 2
}
],
id: 'categoryPaths',
label: 'categoryPaths',
modelName: 'HierarchicalFacet'
}
];
let gato: HistoryQuery, perro: HistoryQuery;

beforeEach(() => {
Expand Down Expand Up @@ -369,9 +401,8 @@ describe('testing history queries module actions', () => {
});

// eslint-disable-next-line max-len
it('updates a history query if search response change because a filter is selected', async () => {
it('updates a history query when the search response changes because a filter is selected', async () => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's worth testing with facets with __unknown__ as id. Create a new test for it and where done 👍

gato.totalResults = 50;
const selectedFilters = Object.values(requestFilters)[0];
resetStateWith({ historyQueries: [gato, perro] });
await store.dispatch('updateHistoryQueriesWithSearchResponse', {
request: {
Expand All @@ -380,6 +411,33 @@ describe('testing history queries module actions', () => {
filters: requestFilters
},
status: 'success',
facets: responseFacets,
results,
totalResults
});
expectHistoryQueriesToEqual([{ ...gato, totalResults, selectedFilters }, perro]);
});

// eslint-disable-next-line max-len
it('updates a history query when the search response changes although the facet id is unknown', async () => {
gato.totalResults = 50;
resetStateWith({ historyQueries: [gato, perro] });
await store.dispatch('updateHistoryQueriesWithSearchResponse', {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Testing this doesn't make much sense to me. The response shouldn't return facets with id __unknown__. The backend should know the id of the facet and __unknown__ is a key used only in the front to identify a filter which we don't know the facet id.

The test I was suggesting was the behaviour of the action when a Query preview was selected. Selecting a QP creates a HQ of that request, but the QP creates the filters with facet id __unknown__. The test should check that the filters map correctly when the requestFilters have an unknown facet id.

request: {
query: 'gato',
page: 1,
filters: {
__unknown__: [
{
id: 'categoryIds:66dd06d9f',
selected: true,
modelName: 'RawFilter'
}
]
}
},
status: 'success',
facets: responseFacets,
results,
totalResults
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,10 @@ export const updateHistoryQueriesWithSearchResponse: HistoryQueriesXStoreModule[
const historyQuery = state.historyQueries[indexOfHistoryQuery];
const isCurrentSessionHistoryQuery = historyQuery.timestamp > state.sessionTimeStampInMs;
if (!isCurrentSessionHistoryQuery || historyQuery.totalResults == null || searchResponse) {
const filters = createHistoryQueriesFiltersList(searchResponse.request.filters);
const filters = getHistoryQueriesFiltersList(
searchResponse.facets,
searchResponse.request.filters
);

const newHistoryQueries = state.historyQueries.slice();
newHistoryQueries[indexOfHistoryQuery] = {
Expand All @@ -46,20 +49,39 @@ export const updateHistoryQueriesWithSearchResponse: HistoryQueriesXStoreModule[
};

/**
* Take filters from the request and push them into a list.
* Creates a selected filters list by comparing request filters and response facets.
* Uses the 'filter.id' to match and merge the objects in a single one with all the keys.
*
* @param responseFacets - Facets from the response.
* @param requestFilters - Filters from the request.
*
* @returns A list of selected filters in the history query.
*
*/
function createHistoryQueriesFiltersList(
function getHistoryQueriesFiltersList(
responseFacets: InternalSearchResponse['facets'],
requestFilters: InternalSearchResponse['request']['filters']
): Filter[] {
return requestFilters
? Object.values(requestFilters).reduce((accFilters, filters) => {
accFilters.push(...filters);
if (!requestFilters || !responseFacets) {
return [];
} else {
return Object.entries(requestFilters).flatMap(([facetId, facetFilters]) => {
const matchingFacet =
facetId !== '__unknown__' ? responseFacets.find(facet => facet.id === facetId) : null;

return facetFilters.reduce<Filter[]>((accFilters, requestFilter) => {
const matchingFilter = matchingFacet
? matchingFacet.filters.find(filter => filter.id === requestFilter.id)
: responseFacets
.flatMap(facet => facet.filters)
.find(filter => filter.id === requestFilter.id);

if (matchingFilter) {
accFilters.push({ ...matchingFilter, selected: requestFilter.selected });
}

return accFilters;
}, [])
: [];
}, []);
});
}
}