From ba51dd66d2f2b7157e6ff1e23ccc8b13e4e7c240 Mon Sep 17 00:00:00 2001 From: Dhaya <154633+dhayab@users.noreply.github.com> Date: Tue, 14 Mar 2023 19:14:35 +0100 Subject: [PATCH 1/2] feat(connectCurrentRefinements): provide indexId of refinements in transformItems --- .../connectCurrentRefinements-test.ts | 37 ++++++++++++++----- .../connectCurrentRefinements.ts | 10 +++++ .../current-refinements-test.ts.snap | 9 +++++ .../__tests__/useCurrentRefinements.test.tsx | 2 + 4 files changed, 48 insertions(+), 10 deletions(-) diff --git a/packages/instantsearch.js/src/connectors/current-refinements/__tests__/connectCurrentRefinements-test.ts b/packages/instantsearch.js/src/connectors/current-refinements/__tests__/connectCurrentRefinements-test.ts index 6afb6a8c49..ae2383442b 100644 --- a/packages/instantsearch.js/src/connectors/current-refinements/__tests__/connectCurrentRefinements-test.ts +++ b/packages/instantsearch.js/src/connectors/current-refinements/__tests__/connectCurrentRefinements-test.ts @@ -211,6 +211,7 @@ See documentation: https://www.algolia.com/doc/api-reference/widgets/current-ref { attribute: 'category', indexName: 'indexName', + indexId: 'indexName', label: 'category', refine: expect.any(Function), refinements: [ @@ -317,6 +318,7 @@ See documentation: https://www.algolia.com/doc/api-reference/widgets/current-ref { attribute: 'category', indexName: 'indexName', + indexId: 'indexName', label: 'category', refine: expect.any(Function), refinements: [ @@ -736,6 +738,7 @@ See documentation: https://www.algolia.com/doc/api-reference/widgets/current-ref expect(firstRenderingOptions.items).toEqual([ { indexName: 'indexName', + indexId: 'indexName', attribute: 'facet1', label: 'facet1', refinements: [ @@ -780,6 +783,7 @@ See documentation: https://www.algolia.com/doc/api-reference/widgets/current-ref expect(items).toEqual([ { indexName: 'indexName', + indexId: 'firstIndex', attribute: 'facet1', label: 'facet1', refinements: [ @@ -794,6 +798,7 @@ See documentation: https://www.algolia.com/doc/api-reference/widgets/current-ref }, { indexName: 'indexName', + indexId: 'firstIndex', attribute: 'facet2', label: 'facet2', refinements: [ @@ -833,6 +838,10 @@ See documentation: https://www.algolia.com/doc/api-reference/widgets/current-ref }); it('provides the items from multiple scoped results', () => { + const indexHelper = algoliasearchHelper({} as any, 'indexName', { + facets: ['facet3', 'facet4'], + }); + const rendering = jest.fn(); const customCurrentRefinements = connectCurrentRefinements(rendering); const widget = customCurrentRefinements({}); @@ -850,6 +859,10 @@ See documentation: https://www.algolia.com/doc/api-reference/widgets/current-ref .addFacetRefinement('facet1', 'facetValue') .addFacetRefinement('facet2', 'facetValue'); + indexHelper + .addFacetRefinement('facet3', 'facetValue') + .addFacetRefinement('facet4', 'facetValue'); + widget.render!( createRenderOptions({ scopedResults: [ @@ -858,16 +871,16 @@ See documentation: https://www.algolia.com/doc/api-reference/widgets/current-ref helper, results: new SearchResults(helper.state, [ createSingleSearchResponse({ - index: 'firstIndex', + index: 'indexName', }), ]), }, { indexId: 'secondIndex', - helper, - results: new SearchResults(helper.state, [ + helper: indexHelper, + results: new SearchResults(indexHelper.state, [ createSingleSearchResponse({ - index: 'secondIndex', + index: 'indexName', }), ]), }, @@ -884,6 +897,7 @@ See documentation: https://www.algolia.com/doc/api-reference/widgets/current-ref expect(items).toEqual([ { indexName: 'indexName', + indexId: 'firstIndex', attribute: 'facet1', label: 'facet1', refinements: [ @@ -898,6 +912,7 @@ See documentation: https://www.algolia.com/doc/api-reference/widgets/current-ref }, { indexName: 'indexName', + indexId: 'firstIndex', attribute: 'facet2', label: 'facet2', refinements: [ @@ -912,11 +927,12 @@ See documentation: https://www.algolia.com/doc/api-reference/widgets/current-ref }, { indexName: 'indexName', - attribute: 'facet1', - label: 'facet1', + indexId: 'secondIndex', + attribute: 'facet3', + label: 'facet3', refinements: [ { - attribute: 'facet1', + attribute: 'facet3', label: 'facetValue', type: 'facet', value: 'facetValue', @@ -926,11 +942,12 @@ See documentation: https://www.algolia.com/doc/api-reference/widgets/current-ref }, { indexName: 'indexName', - attribute: 'facet2', - label: 'facet2', + indexId: 'secondIndex', + attribute: 'facet4', + label: 'facet4', refinements: [ { - attribute: 'facet2', + attribute: 'facet4', label: 'facetValue', type: 'facet', value: 'facetValue', diff --git a/packages/instantsearch.js/src/connectors/current-refinements/connectCurrentRefinements.ts b/packages/instantsearch.js/src/connectors/current-refinements/connectCurrentRefinements.ts index 22e43b732d..31362473ca 100644 --- a/packages/instantsearch.js/src/connectors/current-refinements/connectCurrentRefinements.ts +++ b/packages/instantsearch.js/src/connectors/current-refinements/connectCurrentRefinements.ts @@ -73,6 +73,11 @@ export type CurrentRefinementsConnectorParamsItem = { */ indexName: string; + /** + * The index id as provided to the index widget. + */ + indexId: string; + /** * The attribute on which the refinement is applied. */ @@ -229,6 +234,7 @@ const connectCurrentRefinements: CurrentRefinementsConnector = getRefinementsItems({ results: {}, helper, + indexId: helper.state.index, includedAttributes, excludedAttributes, }), @@ -244,6 +250,7 @@ const connectCurrentRefinements: CurrentRefinementsConnector = getRefinementsItems({ results: scopedResult.results, helper: scopedResult.helper, + indexId: scopedResult.indexId, includedAttributes, excludedAttributes, }), @@ -271,11 +278,13 @@ const connectCurrentRefinements: CurrentRefinementsConnector = function getRefinementsItems({ results, helper, + indexId, includedAttributes, excludedAttributes, }: { results: SearchResults | Record; helper: AlgoliaSearchHelper; + indexId: string; includedAttributes: CurrentRefinementsConnectorParams['includedAttributes']; excludedAttributes: CurrentRefinementsConnectorParams['excludedAttributes']; }): CurrentRefinementsConnectorParamsItem[] { @@ -298,6 +307,7 @@ function getRefinementsItems({ ...allItems.filter((item) => item.attribute !== currentItem.attribute), { indexName: helper.state.index, + indexId, attribute: currentItem.attribute, label: currentItem.attribute, refinements: items diff --git a/packages/instantsearch.js/src/widgets/current-refinements/__tests__/__snapshots__/current-refinements-test.ts.snap b/packages/instantsearch.js/src/widgets/current-refinements/__tests__/__snapshots__/current-refinements-test.ts.snap index 0fdb5d304e..6200117cc8 100644 --- a/packages/instantsearch.js/src/widgets/current-refinements/__tests__/__snapshots__/current-refinements-test.ts.snap +++ b/packages/instantsearch.js/src/widgets/current-refinements/__tests__/__snapshots__/current-refinements-test.ts.snap @@ -16,6 +16,7 @@ exports[`currentRefinements() render() DOM output renders correctly 1`] = ` "items": [ { "attribute": "facet", + "indexId": "index_name", "indexName": "indexName", "label": "facet", "refine": [Function], @@ -30,6 +31,7 @@ exports[`currentRefinements() render() DOM output renders correctly 1`] = ` }, { "attribute": "facetExclude", + "indexId": "index_name", "indexName": "indexName", "label": "facetExclude", "refine": [Function], @@ -44,6 +46,7 @@ exports[`currentRefinements() render() DOM output renders correctly 1`] = ` }, { "attribute": "rating", + "indexId": "index_name", "indexName": "indexName", "label": "rating", "refine": [Function], @@ -64,6 +67,7 @@ exports[`currentRefinements() render() DOM output renders correctly 1`] = ` }, { "attribute": "brand", + "indexId": "index_name", "indexName": "indexName", "label": "brand", "refine": [Function], @@ -84,6 +88,7 @@ exports[`currentRefinements() render() DOM output renders correctly 1`] = ` }, { "attribute": "hierarchicalCategories.lvl0", + "indexId": "index_name", "indexName": "indexName", "label": "hierarchicalCategories.lvl0", "refine": [Function], @@ -100,6 +105,7 @@ exports[`currentRefinements() render() DOM output renders correctly 1`] = ` }, { "attribute": "price", + "indexId": "index_name", "indexName": "indexName", "label": "price", "refine": [Function], @@ -122,6 +128,7 @@ exports[`currentRefinements() render() DOM output renders correctly 1`] = ` }, { "attribute": "_tags", + "indexId": "index_name", "indexName": "indexName", "label": "_tags", "refine": [Function], @@ -177,6 +184,7 @@ exports[`currentRefinements() render() should render twice { { attribute: 'brand', indexName: 'indexName', + indexId: 'indexName', label: 'brand', refine: expect.any(Function), refinements: [ @@ -93,6 +94,7 @@ describe('useCurrentRefinements', () => { { attribute: 'brand', indexName: 'indexName', + indexId: 'indexName', label: 'brand', refine: expect.any(Function), refinements: [ From 820e2633bc5a8fa097f8a834d9253b338da0bc4c Mon Sep 17 00:00:00 2001 From: Dhaya <154633+dhayab@users.noreply.github.com> Date: Wed, 15 Mar 2023 09:46:38 +0100 Subject: [PATCH 2/2] fix type check issues --- .../__tests__/CurrentRefinements-test.tsx | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/packages/instantsearch.js/src/components/CurrentRefinements/__tests__/CurrentRefinements-test.tsx b/packages/instantsearch.js/src/components/CurrentRefinements/__tests__/CurrentRefinements-test.tsx index e342852d89..cc6ec636dc 100644 --- a/packages/instantsearch.js/src/components/CurrentRefinements/__tests__/CurrentRefinements-test.tsx +++ b/packages/instantsearch.js/src/components/CurrentRefinements/__tests__/CurrentRefinements-test.tsx @@ -27,6 +27,7 @@ describe('CurrentRefinements', () => { items: [ { indexName: 'indexName', + indexId: 'indexName', attribute: 'facet', label: 'facet', refine: () => {}, @@ -47,6 +48,7 @@ describe('CurrentRefinements', () => { }, { indexName: 'indexName', + indexId: 'indexName', attribute: 'facetExclude', label: 'facetExclude', refine: () => {}, @@ -62,6 +64,7 @@ describe('CurrentRefinements', () => { }, { indexName: 'indexName', + indexId: 'indexName', attribute: 'disjunctive', label: 'disjunctive', refine: () => {}, @@ -76,6 +79,7 @@ describe('CurrentRefinements', () => { }, { indexName: 'indexName', + indexId: 'indexName', attribute: 'hierarchical', label: 'hierarchical', refine: () => {}, @@ -90,6 +94,7 @@ describe('CurrentRefinements', () => { }, { indexName: 'indexName', + indexId: 'indexName', attribute: 'numeric', label: 'numeric', refine: () => {}, @@ -105,6 +110,7 @@ describe('CurrentRefinements', () => { }, { indexName: 'indexName', + indexId: 'indexName', attribute: 'tag', label: 'tag', refine: () => {}, @@ -147,6 +153,7 @@ describe('CurrentRefinements', () => { items: [ { indexName: 'indexName', + indexId: 'indexName', attribute: 'customFacet', label: 'customFacet', refine: () => {}, @@ -174,6 +181,7 @@ describe('CurrentRefinements', () => { items: [ { indexName: 'indexName', + indexId: 'indexName', attribute: 'customExcludeFacet', label: 'customExcludeFacet', refine: () => {}, @@ -202,6 +210,7 @@ describe('CurrentRefinements', () => { items: [ { indexName: 'indexName', + indexId: 'indexName', attribute: 'customDisjunctiveFacet', label: 'customDisjunctiveFacet', refine: () => {}, @@ -229,6 +238,7 @@ describe('CurrentRefinements', () => { items: [ { indexName: 'indexName', + indexId: 'indexName', attribute: 'customHierarchicalFacet', label: 'customHierarchicalFacet', refine: () => {}, @@ -256,6 +266,7 @@ describe('CurrentRefinements', () => { items: [ { indexName: 'indexName', + indexId: 'indexName', attribute: 'customNumericFilter', label: 'customNumericFilter', refine: () => {}, @@ -271,6 +282,7 @@ describe('CurrentRefinements', () => { }, { indexName: 'indexName', + indexId: 'indexName', attribute: 'customNumericFilter', label: 'customNumericFilter', refine: () => {}, @@ -286,6 +298,7 @@ describe('CurrentRefinements', () => { }, { indexName: 'indexName', + indexId: 'indexName', attribute: 'customNumericFilter', label: 'customNumericFilter', refine: () => {}, @@ -314,6 +327,7 @@ describe('CurrentRefinements', () => { items: [ { indexName: 'indexName', + indexId: 'indexName', attribute: '_tags', label: '_tags', refine: () => {}, @@ -341,6 +355,7 @@ describe('CurrentRefinements', () => { items: [ { indexName: 'indexName', + indexId: 'indexName', attribute: 'query', label: 'query', refine: () => {},