From 8a0ae01ddfceec129075256b19a0cc634ae5bb6e Mon Sep 17 00:00:00 2001 From: Aymeric Giraudet Date: Fri, 12 Jul 2024 14:09:18 +0200 Subject: [PATCH] fix(cache): support nested hierarchical refinements (#6283) --- .../src/requestBuilder.js | 7 +- .../src/lib/__tests__/server.test.ts | 76 ++++++++++++++++++- 2 files changed, 78 insertions(+), 5 deletions(-) diff --git a/packages/algoliasearch-helper/src/requestBuilder.js b/packages/algoliasearch-helper/src/requestBuilder.js index 32287127ff..ba28640454 100644 --- a/packages/algoliasearch-helper/src/requestBuilder.js +++ b/packages/algoliasearch-helper/src/requestBuilder.js @@ -109,11 +109,10 @@ var requestBuilder = { params.facetFilters = filteredFacetFilters.concat( parent.attribute + ':' + parent.value ); + } else if (filteredFacetFilters.length > 0) { + params.facetFilters = filteredFacetFilters; } else { - params.facetFilters = - filteredFacetFilters.length > 0 - ? filteredFacetFilters - : undefined; + delete params.facetFilters; } queries.push({ indexName: index, params: params }); diff --git a/packages/instantsearch.js/src/lib/__tests__/server.test.ts b/packages/instantsearch.js/src/lib/__tests__/server.test.ts index 44b0e1731d..b5b2888799 100644 --- a/packages/instantsearch.js/src/lib/__tests__/server.test.ts +++ b/packages/instantsearch.js/src/lib/__tests__/server.test.ts @@ -3,7 +3,11 @@ import { createSearchClient, } from '@instantsearch/mocks'; -import { connectConfigure, connectSearchBox } from '../../connectors'; +import { + connectConfigure, + connectHierarchicalMenu, + connectSearchBox, +} from '../../connectors'; import instantsearch from '../../index.es'; import { index } from '../../widgets'; import { getInitialResults, waitForResults } from '../server'; @@ -347,4 +351,74 @@ describe('getInitialResults', () => { expectedInitialResults ); }); + + test('returns correct requestParams with nested hierarchical facets', async () => { + const search = instantsearch({ + indexName: 'indexName', + searchClient: createSearchClient(), + initialUiState: { + indexName: { + hierarchicalMenu: { + 'hierarchicalCategories.lvl0': ['Appliances', 'Fans'], + }, + }, + }, + }); + + search.addWidgets([ + connectHierarchicalMenu(() => {})({ + attributes: [ + 'hierarchicalCategories.lvl0', + 'hierarchicalCategories.lvl1', + ], + }), + ]); + + search.start(); + + const requestParams = await waitForResults(search); + + expect(requestParams).toMatchInlineSnapshot(` + [ + { + "facetFilters": [ + [ + "hierarchicalCategories.lvl1:Appliances > Fans", + ], + ], + "facets": [ + "hierarchicalCategories.lvl0", + "hierarchicalCategories.lvl1", + ], + "maxValuesPerFacet": 10, + }, + { + "analytics": false, + "clickAnalytics": false, + "facetFilters": [ + [ + "hierarchicalCategories.lvl0:Appliances", + ], + ], + "facets": [ + "hierarchicalCategories.lvl0", + "hierarchicalCategories.lvl1", + ], + "hitsPerPage": 0, + "maxValuesPerFacet": 10, + "page": 0, + }, + { + "analytics": false, + "clickAnalytics": false, + "facets": [ + "hierarchicalCategories.lvl0", + ], + "hitsPerPage": 0, + "maxValuesPerFacet": 10, + "page": 0, + }, + ] + `); + }); });