-
Notifications
You must be signed in to change notification settings - Fork 514
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 TrendingItems
- Loading branch information
Showing
6 changed files
with
204 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
79 changes: 79 additions & 0 deletions
79
packages/react-instantsearch/src/widgets/TrendingItems.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,79 @@ | ||
import { createTrendingItemsComponent } from 'instantsearch-ui-components'; | ||
import React, { createElement, Fragment } from 'react'; | ||
import { useInstantSearch, useTrendingItems } from 'react-instantsearch-core'; | ||
|
||
import type { | ||
TrendingItemsProps as TrendingItemsUiComponentProps, | ||
Pragma, | ||
RecordWithObjectID, | ||
} from 'instantsearch-ui-components'; | ||
import type { Hit } from 'instantsearch.js'; | ||
import type { UseTrendingItemsProps } from 'react-instantsearch-core'; | ||
|
||
type UiProps<TItem extends RecordWithObjectID> = Pick< | ||
TrendingItemsUiComponentProps<TItem>, | ||
| 'items' | ||
| 'itemComponent' | ||
| 'headerComponent' | ||
| 'fallbackComponent' | ||
| 'status' | ||
| 'sendEvent' | ||
>; | ||
|
||
export type TrendingItemsProps<TItem extends RecordWithObjectID> = Omit< | ||
TrendingItemsUiComponentProps<TItem>, | ||
keyof UiProps<TItem> | ||
> & | ||
UseTrendingItemsProps & { | ||
itemComponent?: TrendingItemsUiComponentProps<TItem>['itemComponent']; | ||
headerComponent?: TrendingItemsUiComponentProps<TItem>['headerComponent']; | ||
fallbackComponent?: TrendingItemsUiComponentProps<TItem>['fallbackComponent']; | ||
}; | ||
|
||
const TrendingItemsUiComponent = createTrendingItemsComponent({ | ||
createElement: createElement as Pragma, | ||
Fragment, | ||
}); | ||
|
||
export function TrendingItems< | ||
TItem extends RecordWithObjectID = RecordWithObjectID | ||
>({ | ||
facetName, | ||
facetValue, | ||
maxRecommendations, | ||
threshold, | ||
fallbackParameters, | ||
queryParameters, | ||
transformItems, | ||
itemComponent, | ||
headerComponent, | ||
fallbackComponent, | ||
...props | ||
}: TrendingItemsProps<TItem>) { | ||
const facetParameters = | ||
facetName && facetValue ? { facetName, facetValue } : {}; | ||
|
||
const { status } = useInstantSearch(); | ||
const { recommendations } = useTrendingItems( | ||
{ | ||
...facetParameters, | ||
maxRecommendations, | ||
threshold, | ||
fallbackParameters, | ||
queryParameters, | ||
transformItems, | ||
}, | ||
{ $$widgetType: 'ais.TrendingItems' } | ||
); | ||
|
||
const uiProps: UiProps<TItem> = { | ||
items: recommendations as Array<Hit<TItem>>, | ||
itemComponent, | ||
headerComponent, | ||
fallbackComponent, | ||
status, | ||
sendEvent: () => {}, | ||
}; | ||
|
||
return <TrendingItemsUiComponent {...props} {...uiProps} />; | ||
} |
106 changes: 106 additions & 0 deletions
106
packages/react-instantsearch/src/widgets/__tests__/TrendingItems.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,106 @@ | ||
/** | ||
* @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 { TrendingItems } from '../TrendingItems'; | ||
|
||
import type { SearchClient } from 'instantsearch.js'; | ||
|
||
describe('TrendingItems', () => { | ||
test('renders with translations', async () => { | ||
const client = createMockedSearchClient(); | ||
const { container } = render( | ||
<InstantSearchTestWrapper searchClient={client}> | ||
<TrendingItems translations={{ title: 'My trending items' }} /> | ||
</InstantSearchTestWrapper> | ||
); | ||
|
||
await waitFor(() => { | ||
expect(client.search).toHaveBeenCalledTimes(1); | ||
}); | ||
|
||
await waitFor(() => { | ||
expect(container.querySelector('.ais-TrendingItems')) | ||
.toMatchInlineSnapshot(` | ||
<section | ||
class="ais-TrendingItems" | ||
> | ||
<h3 | ||
class="ais-TrendingItems-title" | ||
> | ||
My trending items | ||
</h3> | ||
<div | ||
class="ais-TrendingItems-container" | ||
> | ||
<ol | ||
class="ais-TrendingItems-list" | ||
> | ||
<li | ||
class="ais-TrendingItems-item" | ||
> | ||
{ | ||
"objectID": "1" | ||
} | ||
</li> | ||
<li | ||
class="ais-TrendingItems-item" | ||
> | ||
{ | ||
"objectID": "2" | ||
} | ||
</li> | ||
</ol> | ||
</div> | ||
</section> | ||
`); | ||
}); | ||
}); | ||
|
||
test('forwards custom class names and `div` props to the root element', () => { | ||
const { container } = render( | ||
<InstantSearchTestWrapper> | ||
<TrendingItems | ||
className="MyTrendingItems" | ||
classNames={{ root: 'ROOT' }} | ||
aria-hidden={true} | ||
/> | ||
</InstantSearchTestWrapper> | ||
); | ||
|
||
const root = container.firstChild; | ||
expect(root).toHaveClass('MyTrendingItems', '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