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 5 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,9 @@ import {
HistoryQueriesMutations,
HistoryQueriesState
} from '../types';
import { InternalSearchResponse } from '../../../search/index';
// eslint-disable-next-line max-len
import { createHistoryQueriesFiltersList } from '../actions/update-history-queries-with-search-response.action';
import { resetHistoryQueriesStateWith } from './utils';

describe('testing history queries module actions', () => {
Expand Down Expand Up @@ -279,12 +282,31 @@ describe('testing history queries module actions', () => {
const requestFilters: Record<string, Filter[]> = {
categoryPaths: [
{
id: 'categoryIds:c018019b6',
id: 'categoryIds:66dd06d9f',
selected: true,
modelName: 'HierarchicalFilter'
}
]
};
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 +391,9 @@ 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];
const selectedFilters = createHistoryQueriesFiltersList(responseFacets);
annacv marked this conversation as resolved.
Show resolved Hide resolved
resetStateWith({ historyQueries: [gato, perro] });
await store.dispatch('updateHistoryQueriesWithSearchResponse', {
request: {
Expand All @@ -380,6 +402,7 @@ describe('testing history queries module actions', () => {
filters: requestFilters
},
status: 'success',
facets: responseFacets,
results,
totalResults
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ 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 = createHistoryQueriesFiltersList(searchResponse.facets);
annacv marked this conversation as resolved.
Show resolved Hide resolved

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

/**
* Take filters from the request and push them into a list.
* Take facets from the response and push the selected filters into a list.
*
* @param requestFilters - Filters from the request.
* @param responseFacets - Facets from the response.
*
* @returns A list of selected filters in the history query.
*
*/
function createHistoryQueriesFiltersList(
requestFilters: InternalSearchResponse['request']['filters']
export function createHistoryQueriesFiltersList(
annacv marked this conversation as resolved.
Show resolved Hide resolved
annacv marked this conversation as resolved.
Show resolved Hide resolved
responseFacets: InternalSearchResponse['facets']
): Filter[] {
return requestFilters
? Object.values(requestFilters).reduce((accFilters, filters) => {
accFilters.push(...filters);
return responseFacets
? Object.values(responseFacets).reduce((accFilters: Filter[], facet) => {
facet.filters.forEach(filter => {
return filter.selected ? accFilters.push(filter) : [];
});
return accFilters;
}, [])
annacv marked this conversation as resolved.
Show resolved Hide resolved
: [];
Expand Down