-
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 Looking Similar React Hook and widget (#6184)
* feat(recommend): add LookingSimilar and useLookingSimilar FX-2768 * test(cts): enable widget test
- Loading branch information
Showing
9 changed files
with
248 additions
and
11 deletions.
There are no files selected for viewing
28 changes: 28 additions & 0 deletions
28
packages/react-instantsearch-core/src/connectors/useLookingSimilar.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 connectLookingSimilar from 'instantsearch.js/es/connectors/looking-similar/connectLookingSimilar'; | ||
|
||
import { useConnector } from '../hooks/useConnector'; | ||
|
||
import type { AdditionalWidgetProperties } from '../hooks/useConnector'; | ||
import type { BaseHit } from 'instantsearch.js'; | ||
import type { | ||
LookingSimilarConnector, | ||
LookingSimilarConnectorParams, | ||
LookingSimilarWidgetDescription, | ||
} from 'instantsearch.js/es/connectors/looking-similar/connectLookingSimilar'; | ||
|
||
export type UseLookingSimilarProps<THit extends BaseHit = BaseHit> = | ||
LookingSimilarConnectorParams<THit>; | ||
|
||
export function useLookingSimilar<THit extends BaseHit = BaseHit>( | ||
props?: UseLookingSimilarProps<THit>, | ||
additionalWidgetProperties?: AdditionalWidgetProperties | ||
) { | ||
return useConnector< | ||
LookingSimilarConnectorParams<THit>, | ||
LookingSimilarWidgetDescription<THit> | ||
>( | ||
connectLookingSimilar as LookingSimilarConnector<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
72 changes: 72 additions & 0 deletions
72
packages/react-instantsearch/src/widgets/LookingSimilar.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,72 @@ | ||
import { createLookingSimilarComponent } from 'instantsearch-ui-components'; | ||
import React, { createElement, Fragment } from 'react'; | ||
import { useLookingSimilar, useInstantSearch } from 'react-instantsearch-core'; | ||
|
||
import type { | ||
LookingSimilarProps as LookingSimilarPropsUiComponentProps, | ||
Pragma, | ||
} from 'instantsearch-ui-components'; | ||
import type { Hit, BaseHit } from 'instantsearch.js'; | ||
import type { UseLookingSimilarProps } from 'react-instantsearch-core'; | ||
|
||
type UiProps<THit extends BaseHit> = Pick< | ||
LookingSimilarPropsUiComponentProps<Hit<THit>>, | ||
| 'items' | ||
| 'itemComponent' | ||
| 'headerComponent' | ||
| 'fallbackComponent' | ||
| 'status' | ||
| 'sendEvent' | ||
>; | ||
|
||
export type LookingSimilarProps<THit extends BaseHit> = Omit< | ||
LookingSimilarPropsUiComponentProps<Hit<THit>>, | ||
keyof UiProps<THit> | ||
> & | ||
UseLookingSimilarProps<THit> & { | ||
itemComponent?: LookingSimilarPropsUiComponentProps<THit>['itemComponent']; | ||
headerComponent?: LookingSimilarPropsUiComponentProps<THit>['headerComponent']; | ||
fallbackComponent?: LookingSimilarPropsUiComponentProps<THit>['fallbackComponent']; | ||
}; | ||
|
||
const LookingSimilarUiComponent = createLookingSimilarComponent({ | ||
createElement: createElement as Pragma, | ||
Fragment, | ||
}); | ||
|
||
export function LookingSimilar<THit extends BaseHit = BaseHit>({ | ||
objectIDs, | ||
maxRecommendations, | ||
threshold, | ||
queryParameters, | ||
fallbackParameters, | ||
transformItems, | ||
itemComponent, | ||
headerComponent, | ||
fallbackComponent, | ||
...props | ||
}: LookingSimilarProps<THit>) { | ||
const { status } = useInstantSearch(); | ||
const { recommendations } = useLookingSimilar<THit>( | ||
{ | ||
objectIDs, | ||
maxRecommendations, | ||
threshold, | ||
queryParameters, | ||
fallbackParameters, | ||
transformItems, | ||
}, | ||
{ $$widgetType: 'ais.lookingSimilar' } | ||
); | ||
|
||
const uiProps: UiProps<THit> = { | ||
items: recommendations, | ||
itemComponent, | ||
headerComponent, | ||
fallbackComponent, | ||
status, | ||
sendEvent: () => {}, | ||
}; | ||
|
||
return <LookingSimilarUiComponent {...props} {...uiProps} />; | ||
} |
107 changes: 107 additions & 0 deletions
107
packages/react-instantsearch/src/widgets/__tests__/LookingSimilar.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,107 @@ | ||
/** | ||
* @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 { LookingSimilar } from '../LookingSimilar'; | ||
|
||
import type { SearchClient } from 'instantsearch.js'; | ||
|
||
describe('LookingSimilar', () => { | ||
test('renders with translations', async () => { | ||
const client = createMockedSearchClient(); | ||
const { container } = render( | ||
<InstantSearchTestWrapper searchClient={client}> | ||
<LookingSimilar objectIDs={['1']} translations={{ title: 'My FBT' }} /> | ||
</InstantSearchTestWrapper> | ||
); | ||
|
||
await waitFor(() => { | ||
expect(client.search).toHaveBeenCalledTimes(1); | ||
}); | ||
|
||
await waitFor(() => { | ||
expect(container.querySelector('.ais-LookingSimilar')) | ||
.toMatchInlineSnapshot(` | ||
<section | ||
class="ais-LookingSimilar" | ||
> | ||
<h3 | ||
class="ais-LookingSimilar-title" | ||
> | ||
My FBT | ||
</h3> | ||
<div | ||
class="ais-LookingSimilar-container" | ||
> | ||
<ol | ||
class="ais-LookingSimilar-list" | ||
> | ||
<li | ||
class="ais-LookingSimilar-item" | ||
> | ||
{ | ||
"objectID": "1" | ||
} | ||
</li> | ||
<li | ||
class="ais-LookingSimilar-item" | ||
> | ||
{ | ||
"objectID": "2" | ||
} | ||
</li> | ||
</ol> | ||
</div> | ||
</section> | ||
`); | ||
}); | ||
}); | ||
|
||
test('forwards custom class names and `div` props to the root element', () => { | ||
const { container } = render( | ||
<InstantSearchTestWrapper> | ||
<LookingSimilar | ||
objectIDs={['1']} | ||
className="MyLookingSimilar" | ||
classNames={{ root: 'ROOT' }} | ||
aria-hidden={true} | ||
/> | ||
</InstantSearchTestWrapper> | ||
); | ||
|
||
const root = container.firstChild; | ||
expect(root).toHaveClass('MyLookingSimilar', '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