Skip to content

Commit

Permalink
feat(recommend): introduce useRelatedProducts Hook (#6159)
Browse files Browse the repository at this point in the history
  • Loading branch information
sarahdayan authored and dhayab committed May 21, 2024
1 parent b8602af commit 0eedb22
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 8 deletions.
2 changes: 1 addition & 1 deletion bundlesize.config.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
},
{
"path": "packages/react-instantsearch/dist/umd/ReactInstantSearch.min.js",
"maxSize": "59.5 kB"
"maxSize": "59.75 kB"
},
{
"path": "packages/vue-instantsearch/vue2/umd/index.js",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { createInstantSearchTestWrapper } from '@instantsearch/testutils';
import { renderHook } from '@testing-library/react-hooks';

import { useRelatedProducts } from '../useRelatedProducts';

describe('useRelatedProducts', () => {
test('returns the connector render state', async () => {
const wrapper = createInstantSearchTestWrapper();
const { result, waitForNextUpdate } = renderHook(
() => useRelatedProducts({ objectIDs: ['1'] }),
{
wrapper,
}
);

// Initial render state from manual `getWidgetRenderState`
expect(result.current).toEqual({ recommendations: expect.any(Array) });

await waitForNextUpdate();

// InstantSearch.js state from the `render` lifecycle step
expect(result.current).toEqual({ recommendations: expect.any(Array) });
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import connectRelatedProducts from 'instantsearch.js/es/connectors/related-products/connectRelatedProducts';

import { useConnector } from '../hooks/useConnector';

import type { AdditionalWidgetProperties } from '../hooks/useConnector';
import type { BaseHit } from 'instantsearch.js';
import type {
RelatedProductsConnector,
RelatedProductsConnectorParams,
RelatedProductsWidgetDescription,
} from 'instantsearch.js/es/connectors/related-products/connectRelatedProducts';

export type UseRelatedProductsProps<THit extends BaseHit = BaseHit> =
RelatedProductsConnectorParams<THit>;

export function useRelatedProducts<THit extends BaseHit = BaseHit>(
props: UseRelatedProductsProps<THit>,
additionalWidgetProperties?: AdditionalWidgetProperties
) {
return useConnector<
RelatedProductsConnectorParams<THit>,
RelatedProductsWidgetDescription<THit>
>(
connectRelatedProducts as RelatedProductsConnector<THit>,
props,
additionalWidgetProperties
);
}
1 change: 1 addition & 0 deletions packages/react-instantsearch-core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export * from './connectors/usePoweredBy';
export * from './connectors/useQueryRules';
export * from './connectors/useRange';
export * from './connectors/useRefinementList';
export * from './connectors/useRelatedProducts';
export * from './connectors/useSearchBox';
export * from './connectors/useSortBy';
export * from './connectors/useStats';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import {
useConnector,
useToggleRefinement,
useCurrentRefinements,
useRelatedProducts,
} from '..';

import type {
Expand All @@ -32,6 +33,7 @@ import type {
UsePaginationProps,
UseRefinementListProps,
UseToggleRefinementProps,
UseRelatedProductsProps,
} from '..';
import type { TestOptionsMap, TestSetupsMap } from '@instantsearch/tests';
import type {
Expand Down Expand Up @@ -313,7 +315,28 @@ const testSetups: TestSetupsMap<TestSuites> = {
</InstantSearch>
);
},
createRelatedProductsConnectorTests: () => {},
createRelatedProductsConnectorTests: ({
instantSearchOptions,
widgetParams,
}) => {
function CustomRelatedProducts(props: UseRelatedProductsProps) {
const { recommendations } = useRelatedProducts(props);

return (
<ul>
{recommendations.map((recommendation) => (
<li key={recommendation.objectID}>{recommendation.objectID}</li>
))}
</ul>
);
}

render(
<InstantSearch {...instantSearchOptions}>
<CustomRelatedProducts {...widgetParams} />
</InstantSearch>
);
},
createFrequentlyBoughtTogetherConnectorTests: () => {},
};

Expand All @@ -334,12 +357,7 @@ const testOptions: TestOptionsMap<TestSuites> = {
createNumericMenuConnectorTests: { act },
createRatingMenuConnectorTests: { act },
createToggleRefinementConnectorTests: { act },
createRelatedProductsConnectorTests: {
act,
skippedTests: {
options: true,
},
},
createRelatedProductsConnectorTests: { act },
createFrequentlyBoughtTogetherConnectorTests: {
act,
skippedTests: {
Expand Down

0 comments on commit 0eedb22

Please sign in to comment.