Skip to content

Commit

Permalink
feat(current-refinements): provide indexId of refinements in transfor…
Browse files Browse the repository at this point in the history
…mItems (#5546)
  • Loading branch information
dhayab committed Mar 15, 2023
1 parent 62efb0f commit 89781db
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 10 deletions.
Expand Up @@ -27,6 +27,7 @@ describe('CurrentRefinements', () => {
items: [
{
indexName: 'indexName',
indexId: 'indexName',
attribute: 'facet',
label: 'facet',
refine: () => {},
Expand All @@ -47,6 +48,7 @@ describe('CurrentRefinements', () => {
},
{
indexName: 'indexName',
indexId: 'indexName',
attribute: 'facetExclude',
label: 'facetExclude',
refine: () => {},
Expand All @@ -62,6 +64,7 @@ describe('CurrentRefinements', () => {
},
{
indexName: 'indexName',
indexId: 'indexName',
attribute: 'disjunctive',
label: 'disjunctive',
refine: () => {},
Expand All @@ -76,6 +79,7 @@ describe('CurrentRefinements', () => {
},
{
indexName: 'indexName',
indexId: 'indexName',
attribute: 'hierarchical',
label: 'hierarchical',
refine: () => {},
Expand All @@ -90,6 +94,7 @@ describe('CurrentRefinements', () => {
},
{
indexName: 'indexName',
indexId: 'indexName',
attribute: 'numeric',
label: 'numeric',
refine: () => {},
Expand All @@ -105,6 +110,7 @@ describe('CurrentRefinements', () => {
},
{
indexName: 'indexName',
indexId: 'indexName',
attribute: 'tag',
label: 'tag',
refine: () => {},
Expand Down Expand Up @@ -147,6 +153,7 @@ describe('CurrentRefinements', () => {
items: [
{
indexName: 'indexName',
indexId: 'indexName',
attribute: 'customFacet',
label: 'customFacet',
refine: () => {},
Expand Down Expand Up @@ -174,6 +181,7 @@ describe('CurrentRefinements', () => {
items: [
{
indexName: 'indexName',
indexId: 'indexName',
attribute: 'customExcludeFacet',
label: 'customExcludeFacet',
refine: () => {},
Expand Down Expand Up @@ -202,6 +210,7 @@ describe('CurrentRefinements', () => {
items: [
{
indexName: 'indexName',
indexId: 'indexName',
attribute: 'customDisjunctiveFacet',
label: 'customDisjunctiveFacet',
refine: () => {},
Expand Down Expand Up @@ -229,6 +238,7 @@ describe('CurrentRefinements', () => {
items: [
{
indexName: 'indexName',
indexId: 'indexName',
attribute: 'customHierarchicalFacet',
label: 'customHierarchicalFacet',
refine: () => {},
Expand Down Expand Up @@ -256,6 +266,7 @@ describe('CurrentRefinements', () => {
items: [
{
indexName: 'indexName',
indexId: 'indexName',
attribute: 'customNumericFilter',
label: 'customNumericFilter',
refine: () => {},
Expand All @@ -271,6 +282,7 @@ describe('CurrentRefinements', () => {
},
{
indexName: 'indexName',
indexId: 'indexName',
attribute: 'customNumericFilter',
label: 'customNumericFilter',
refine: () => {},
Expand All @@ -286,6 +298,7 @@ describe('CurrentRefinements', () => {
},
{
indexName: 'indexName',
indexId: 'indexName',
attribute: 'customNumericFilter',
label: 'customNumericFilter',
refine: () => {},
Expand Down Expand Up @@ -314,6 +327,7 @@ describe('CurrentRefinements', () => {
items: [
{
indexName: 'indexName',
indexId: 'indexName',
attribute: '_tags',
label: '_tags',
refine: () => {},
Expand Down Expand Up @@ -341,6 +355,7 @@ describe('CurrentRefinements', () => {
items: [
{
indexName: 'indexName',
indexId: 'indexName',
attribute: 'query',
label: 'query',
refine: () => {},
Expand Down
Expand Up @@ -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: [
Expand Down Expand Up @@ -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: [
Expand Down Expand Up @@ -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: [
Expand Down Expand Up @@ -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: [
Expand All @@ -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: [
Expand Down Expand Up @@ -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({});
Expand All @@ -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: [
Expand All @@ -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',
}),
]),
},
Expand All @@ -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: [
Expand All @@ -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: [
Expand All @@ -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',
Expand All @@ -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',
Expand Down
Expand Up @@ -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.
*/
Expand Down Expand Up @@ -229,6 +234,7 @@ const connectCurrentRefinements: CurrentRefinementsConnector =
getRefinementsItems({
results: {},
helper,
indexId: helper.state.index,
includedAttributes,
excludedAttributes,
}),
Expand All @@ -244,6 +250,7 @@ const connectCurrentRefinements: CurrentRefinementsConnector =
getRefinementsItems({
results: scopedResult.results,
helper: scopedResult.helper,
indexId: scopedResult.indexId,
includedAttributes,
excludedAttributes,
}),
Expand Down Expand Up @@ -271,11 +278,13 @@ const connectCurrentRefinements: CurrentRefinementsConnector =
function getRefinementsItems({
results,
helper,
indexId,
includedAttributes,
excludedAttributes,
}: {
results: SearchResults | Record<string, never>;
helper: AlgoliaSearchHelper;
indexId: string;
includedAttributes: CurrentRefinementsConnectorParams['includedAttributes'];
excludedAttributes: CurrentRefinementsConnectorParams['excludedAttributes'];
}): CurrentRefinementsConnectorParamsItem[] {
Expand All @@ -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
Expand Down
Expand Up @@ -16,6 +16,7 @@ exports[`currentRefinements() render() DOM output renders correctly 1`] = `
"items": [
{
"attribute": "facet",
"indexId": "index_name",
"indexName": "indexName",
"label": "facet",
"refine": [Function],
Expand All @@ -30,6 +31,7 @@ exports[`currentRefinements() render() DOM output renders correctly 1`] = `
},
{
"attribute": "facetExclude",
"indexId": "index_name",
"indexName": "indexName",
"label": "facetExclude",
"refine": [Function],
Expand All @@ -44,6 +46,7 @@ exports[`currentRefinements() render() DOM output renders correctly 1`] = `
},
{
"attribute": "rating",
"indexId": "index_name",
"indexName": "indexName",
"label": "rating",
"refine": [Function],
Expand All @@ -64,6 +67,7 @@ exports[`currentRefinements() render() DOM output renders correctly 1`] = `
},
{
"attribute": "brand",
"indexId": "index_name",
"indexName": "indexName",
"label": "brand",
"refine": [Function],
Expand All @@ -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],
Expand All @@ -100,6 +105,7 @@ exports[`currentRefinements() render() DOM output renders correctly 1`] = `
},
{
"attribute": "price",
"indexId": "index_name",
"indexName": "indexName",
"label": "price",
"refine": [Function],
Expand All @@ -122,6 +128,7 @@ exports[`currentRefinements() render() DOM output renders correctly 1`] = `
},
{
"attribute": "_tags",
"indexId": "index_name",
"indexName": "indexName",
"label": "_tags",
"refine": [Function],
Expand Down Expand Up @@ -177,6 +184,7 @@ exports[`currentRefinements() render() should render twice <CurrentRefinements .
"items": [
{
"attribute": "facet",
"indexId": "index_name",
"indexName": "index_name",
"label": "facet",
"refine": [Function],
Expand Down Expand Up @@ -211,6 +219,7 @@ exports[`currentRefinements() render() should render twice <CurrentRefinements .
"items": [
{
"attribute": "facet",
"indexId": "index_name",
"indexName": "index_name",
"label": "facet",
"refine": [Function],
Expand Down
Expand Up @@ -68,6 +68,7 @@ describe('useCurrentRefinements', () => {
{
attribute: 'brand',
indexName: 'indexName',
indexId: 'indexName',
label: 'brand',
refine: expect.any(Function),
refinements: [
Expand All @@ -93,6 +94,7 @@ describe('useCurrentRefinements', () => {
{
attribute: 'brand',
indexName: 'indexName',
indexId: 'indexName',
label: 'brand',
refine: expect.any(Function),
refinements: [
Expand Down

0 comments on commit 89781db

Please sign in to comment.