-
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(react-instantsearch): introduce Frequently Bought Together Hook …
…and widget (#6155) --------- Co-authored-by: Dhaya <154633+dhayab@users.noreply.github.com> Co-authored-by: Aymeric Giraudet <aymeric.giraudet@algolia.com> Co-authored-by: Sarah Dayan <5370675+sarahdayan@users.noreply.github.com>
- Loading branch information
1 parent
99907be
commit 7714719
Showing
10 changed files
with
259 additions
and
18 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
28 changes: 28 additions & 0 deletions
28
packages/react-instantsearch-core/src/connectors/useFrequentlyBoughtTogether.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 connectFrequentlyBoughtTogether from 'instantsearch.js/es/connectors/frequently-bought-together/connectFrequentlyBoughtTogether'; | ||
|
||
import { useConnector } from '../hooks/useConnector'; | ||
|
||
import type { AdditionalWidgetProperties } from '../hooks/useConnector'; | ||
import type { BaseHit } from 'instantsearch.js'; | ||
import type { | ||
FrequentlyBoughtTogetherConnector, | ||
FrequentlyBoughtTogetherConnectorParams, | ||
FrequentlyBoughtTogetherWidgetDescription, | ||
} from 'instantsearch.js/es/connectors/frequently-bought-together/connectFrequentlyBoughtTogether'; | ||
|
||
export type UseFrequentlyBoughtTogetherProps<THit extends BaseHit = BaseHit> = | ||
FrequentlyBoughtTogetherConnectorParams<THit>; | ||
|
||
export function useFrequentlyBoughtTogether<THit extends BaseHit = BaseHit>( | ||
props?: UseFrequentlyBoughtTogetherProps<THit>, | ||
additionalWidgetProperties?: AdditionalWidgetProperties | ||
) { | ||
return useConnector< | ||
FrequentlyBoughtTogetherConnectorParams<THit>, | ||
FrequentlyBoughtTogetherWidgetDescription<THit> | ||
>( | ||
connectFrequentlyBoughtTogether as FrequentlyBoughtTogetherConnector<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
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
74 changes: 74 additions & 0 deletions
74
packages/react-instantsearch/src/widgets/FrequentlyBoughtTogether.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,74 @@ | ||
import { createFrequentlyBoughtTogetherComponent } from 'instantsearch-ui-components'; | ||
import React, { createElement, Fragment } from 'react'; | ||
import { | ||
useFrequentlyBoughtTogether, | ||
useInstantSearch, | ||
} from 'react-instantsearch-core'; | ||
|
||
import type { | ||
FrequentlyBoughtTogetherProps as FrequentlyBoughtTogetherPropsUiComponentProps, | ||
Pragma, | ||
} from 'instantsearch-ui-components'; | ||
import type { Hit, BaseHit } from 'instantsearch.js'; | ||
import type { UseFrequentlyBoughtTogetherProps } from 'react-instantsearch-core'; | ||
|
||
type UiProps<THit extends BaseHit> = Pick< | ||
FrequentlyBoughtTogetherPropsUiComponentProps<Hit<THit>>, | ||
| 'items' | ||
| 'itemComponent' | ||
| 'headerComponent' | ||
| 'fallbackComponent' | ||
| 'status' | ||
| 'sendEvent' | ||
>; | ||
|
||
export type FrequentlyBoughtTogetherProps<THit extends BaseHit> = Omit< | ||
FrequentlyBoughtTogetherPropsUiComponentProps<Hit<THit>>, | ||
keyof UiProps<THit> | ||
> & | ||
UseFrequentlyBoughtTogetherProps<THit> & { | ||
itemComponent?: FrequentlyBoughtTogetherPropsUiComponentProps<THit>['itemComponent']; | ||
headerComponent?: FrequentlyBoughtTogetherPropsUiComponentProps<THit>['headerComponent']; | ||
fallbackComponent?: FrequentlyBoughtTogetherPropsUiComponentProps<THit>['fallbackComponent']; | ||
}; | ||
|
||
const FrequentlyBoughtTogetherUiComponent = | ||
createFrequentlyBoughtTogetherComponent({ | ||
createElement: createElement as Pragma, | ||
Fragment, | ||
}); | ||
|
||
export function FrequentlyBoughtTogether<THit extends BaseHit = BaseHit>({ | ||
objectIDs, | ||
maxRecommendations, | ||
threshold, | ||
queryParameters, | ||
transformItems, | ||
itemComponent, | ||
headerComponent, | ||
fallbackComponent, | ||
...props | ||
}: FrequentlyBoughtTogetherProps<THit>) { | ||
const { status } = useInstantSearch(); | ||
const { recommendations } = useFrequentlyBoughtTogether<THit>( | ||
{ | ||
objectIDs, | ||
maxRecommendations, | ||
threshold, | ||
queryParameters, | ||
transformItems, | ||
}, | ||
{ $$widgetType: 'ais.frequentlyBoughtTogether' } | ||
); | ||
|
||
const uiProps: UiProps<THit> = { | ||
items: recommendations, | ||
itemComponent, | ||
headerComponent, | ||
fallbackComponent, | ||
status, | ||
sendEvent: () => {}, | ||
}; | ||
|
||
return <FrequentlyBoughtTogetherUiComponent {...props} {...uiProps} />; | ||
} |
110 changes: 110 additions & 0 deletions
110
packages/react-instantsearch/src/widgets/__tests__/FrequentlyBoughtTogether.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 { FrequentlyBoughtTogether } from '../FrequentlyBoughtTogether'; | ||
|
||
import type { SearchClient } from 'instantsearch.js'; | ||
|
||
describe('FrequentlyBoughtTogether', () => { | ||
test('renders with translations', async () => { | ||
const client = createMockedSearchClient(); | ||
const { container } = render( | ||
<InstantSearchTestWrapper searchClient={client}> | ||
<FrequentlyBoughtTogether | ||
objectIDs={['1']} | ||
translations={{ title: 'My FBT' }} | ||
/> | ||
</InstantSearchTestWrapper> | ||
); | ||
|
||
await waitFor(() => { | ||
expect(client.search).toHaveBeenCalledTimes(1); | ||
}); | ||
|
||
await waitFor(() => { | ||
expect(container.querySelector('.ais-FrequentlyBoughtTogether')) | ||
.toMatchInlineSnapshot(` | ||
<section | ||
class="ais-FrequentlyBoughtTogether" | ||
> | ||
<h3 | ||
class="ais-FrequentlyBoughtTogether-title" | ||
> | ||
My FBT | ||
</h3> | ||
<div | ||
class="ais-FrequentlyBoughtTogether-container" | ||
> | ||
<ol | ||
class="ais-FrequentlyBoughtTogether-list" | ||
> | ||
<li | ||
class="ais-FrequentlyBoughtTogether-item" | ||
> | ||
{ | ||
"objectID": "1" | ||
} | ||
</li> | ||
<li | ||
class="ais-FrequentlyBoughtTogether-item" | ||
> | ||
{ | ||
"objectID": "2" | ||
} | ||
</li> | ||
</ol> | ||
</div> | ||
</section> | ||
`); | ||
}); | ||
}); | ||
|
||
test('forwards custom class names and `div` props to the root element', () => { | ||
const { container } = render( | ||
<InstantSearchTestWrapper> | ||
<FrequentlyBoughtTogether | ||
objectIDs={['1']} | ||
className="MyFrequentlyBoughtTogether" | ||
classNames={{ root: 'ROOT' }} | ||
aria-hidden={true} | ||
/> | ||
</InstantSearchTestWrapper> | ||
); | ||
|
||
const root = container.firstChild; | ||
expect(root).toHaveClass('MyFrequentlyBoughtTogether', '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