-
Notifications
You must be signed in to change notification settings - Fork 499
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
<RelatedProducts>
widget (#6162)
- Loading branch information
1 parent
0eedb22
commit 313b0ea
Showing
8 changed files
with
205 additions
and
10 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
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
75 changes: 75 additions & 0 deletions
75
packages/react-instantsearch/src/widgets/RelatedProducts.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,75 @@ | ||
import { createRelatedProductsComponent } from 'instantsearch-ui-components'; | ||
import React, { createElement, Fragment } from 'react'; | ||
import { useInstantSearch, useRelatedProducts } from 'react-instantsearch-core'; | ||
|
||
import type { | ||
RelatedProductsProps as RelatedProductsUiComponentProps, | ||
Pragma, | ||
RecordWithObjectID, | ||
} from 'instantsearch-ui-components'; | ||
import type { Hit } from 'instantsearch.js'; | ||
import type { UseRelatedProductsProps } from 'react-instantsearch-core'; | ||
|
||
type UiProps<TItem extends RecordWithObjectID> = Pick< | ||
RelatedProductsUiComponentProps<TItem>, | ||
| 'items' | ||
| 'itemComponent' | ||
| 'headerComponent' | ||
| 'fallbackComponent' | ||
| 'status' | ||
| 'sendEvent' | ||
>; | ||
|
||
export type RelatedProductsProps<TItem extends RecordWithObjectID> = Omit< | ||
RelatedProductsUiComponentProps<TItem>, | ||
keyof UiProps<TItem> | ||
> & | ||
UseRelatedProductsProps & { | ||
itemComponent?: RelatedProductsUiComponentProps<TItem>['itemComponent']; | ||
headerComponent?: RelatedProductsUiComponentProps<TItem>['headerComponent']; | ||
fallbackComponent?: RelatedProductsUiComponentProps<TItem>['fallbackComponent']; | ||
}; | ||
|
||
const RelatedProductsUiComponent = createRelatedProductsComponent({ | ||
createElement: createElement as Pragma, | ||
Fragment, | ||
}); | ||
|
||
export function RelatedProducts< | ||
TItem extends RecordWithObjectID = RecordWithObjectID | ||
>({ | ||
objectIDs, | ||
maxRecommendations, | ||
threshold, | ||
fallbackParameters, | ||
queryParameters, | ||
transformItems, | ||
itemComponent, | ||
headerComponent, | ||
fallbackComponent, | ||
...props | ||
}: RelatedProductsProps<TItem>) { | ||
const { status } = useInstantSearch(); | ||
const { recommendations } = useRelatedProducts( | ||
{ | ||
objectIDs, | ||
maxRecommendations, | ||
threshold, | ||
fallbackParameters, | ||
queryParameters, | ||
transformItems, | ||
}, | ||
{ $$widgetType: 'ais.relatedProducts' } | ||
); | ||
|
||
const uiProps: UiProps<TItem> = { | ||
items: recommendations as Array<Hit<TItem>>, | ||
itemComponent, | ||
headerComponent, | ||
fallbackComponent, | ||
status, | ||
sendEvent: () => {}, | ||
}; | ||
|
||
return <RelatedProductsUiComponent {...props} {...uiProps} />; | ||
} |
110 changes: 110 additions & 0 deletions
110
packages/react-instantsearch/src/widgets/__tests__/RelatedProducts.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,110 @@ | ||
/** | ||
* @jest-environment jsdom | ||
*/ | ||
|
||
import { | ||
createMultiSearchResponse, | ||
createSearchClient, | ||
createSingleSearchResponse, | ||
} from '@instantsearch/mocks'; | ||
import { InstantSearchTestWrapper } from '@instantsearch/testutils'; | ||
import { render, waitFor } from '@testing-library/react'; | ||
import React from 'react'; | ||
|
||
import { RelatedProducts } from '../RelatedProducts'; | ||
|
||
import type { SearchClient } from 'instantsearch.js'; | ||
|
||
describe('RelatedProducts', () => { | ||
test('renders with translations', async () => { | ||
const client = createMockedSearchClient(); | ||
const { container } = render( | ||
<InstantSearchTestWrapper searchClient={client}> | ||
<RelatedProducts | ||
objectIDs={['1']} | ||
translations={{ title: 'My related products' }} | ||
/> | ||
</InstantSearchTestWrapper> | ||
); | ||
|
||
await waitFor(() => { | ||
expect(client.search).toHaveBeenCalledTimes(1); | ||
}); | ||
|
||
await waitFor(() => { | ||
expect(container.querySelector('.ais-RelatedProducts')) | ||
.toMatchInlineSnapshot(` | ||
<section | ||
class="ais-RelatedProducts" | ||
> | ||
<h3 | ||
class="ais-RelatedProducts-title" | ||
> | ||
My related products | ||
</h3> | ||
<div | ||
class="ais-RelatedProducts-container" | ||
> | ||
<ol | ||
class="ais-RelatedProducts-list" | ||
> | ||
<li | ||
class="ais-RelatedProducts-item" | ||
> | ||
{ | ||
"objectID": "1" | ||
} | ||
</li> | ||
<li | ||
class="ais-RelatedProducts-item" | ||
> | ||
{ | ||
"objectID": "2" | ||
} | ||
</li> | ||
</ol> | ||
</div> | ||
</section> | ||
`); | ||
}); | ||
}); | ||
|
||
test('forwards custom class names and `div` props to the root element', () => { | ||
const { container } = render( | ||
<InstantSearchTestWrapper> | ||
<RelatedProducts | ||
objectIDs={['1']} | ||
className="MyRelatedProducts" | ||
classNames={{ root: 'ROOT' }} | ||
aria-hidden={true} | ||
/> | ||
</InstantSearchTestWrapper> | ||
); | ||
|
||
const root = container.firstChild; | ||
expect(root).toHaveClass('MyRelatedProducts', 'ROOT'); | ||
expect(root).toHaveAttribute('aria-hidden', 'true'); | ||
}); | ||
}); | ||
|
||
function createMockedSearchClient() { | ||
return createSearchClient({ | ||
getRecommendations: jest.fn((requests) => | ||
Promise.resolve( | ||
createMultiSearchResponse( | ||
// @ts-ignore | ||
// `request` will be implicitly typed as `any` in type-check:v3 | ||
// since `getRecommendations` is not available there | ||
...requests.map((request) => { | ||
return createSingleSearchResponse<any>({ | ||
hits: | ||
request.maxRecommendations === 0 | ||
? [] | ||
: [{ objectID: '1' }, { objectID: '2' }], | ||
}); | ||
}) | ||
) | ||
) | ||
) as SearchClient['getRecommendations'], | ||
}); | ||
} |
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
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