-
Notifications
You must be signed in to change notification settings - Fork 497
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(recommend): introduce
useRelatedProducts
Hook (#6159)
- Loading branch information
1 parent
b8602af
commit 0eedb22
Showing
5 changed files
with
79 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
packages/react-instantsearch-core/src/connectors/__tests__/useRelatedProducts.test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) }); | ||
}); | ||
}); |
28 changes: 28 additions & 0 deletions
28
packages/react-instantsearch-core/src/connectors/useRelatedProducts.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters