-
Notifications
You must be signed in to change notification settings - Fork 553
feat(autocomplete): support header template for autocomplete index #6782
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 2 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
884864b
feat(autocomplete): support header template for autocomplete index
dhayab dce153d
add test
dhayab 5f7f8a8
Merge branch 'master' into feat/autocomplete-header-template
dhayab 28991c4
add styles
dhayab 7848b57
fix test
dhayab 399fee0
bump bundlesize again
dhayab File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,123 @@ | ||
| import { | ||
| createMultiSearchResponse, | ||
| createSearchClient, | ||
| createSingleSearchResponse, | ||
| } from '@instantsearch/mocks'; | ||
| import { wait } from '@instantsearch/testutils'; | ||
| import userEvent from '@testing-library/user-event'; | ||
| import React from 'react'; | ||
|
|
||
| import type { AutocompleteWidgetSetup } from '.'; | ||
| import type { TestOptions } from '../../common'; | ||
|
|
||
| export function createTemplatesTests( | ||
| setup: AutocompleteWidgetSetup, | ||
| { act }: Required<TestOptions> | ||
| ) { | ||
| describe('templates', () => { | ||
| test('renders indices headers', async () => { | ||
| const searchClient = createMockedSearchClient( | ||
| createMultiSearchResponse( | ||
| createSingleSearchResponse({ | ||
| index: 'indexName2', | ||
| hits: [ | ||
| { objectID: '1', query: 'hello' }, | ||
| { objectID: '2', query: 'world' }, | ||
| ], | ||
| }), | ||
| // @ts-expect-error - ignore second response type | ||
| createSingleSearchResponse({ | ||
| index: 'indexName', | ||
| hits: [ | ||
| { objectID: '1', name: 'Item 1' }, | ||
| { objectID: '2', name: 'Item 2' }, | ||
| { objectID: '3', name: 'Item 3' }, | ||
| ], | ||
| }) | ||
| ) | ||
| ); | ||
|
|
||
| await setup({ | ||
| instantSearchOptions: { | ||
| indexName: 'indexName', | ||
| searchClient, | ||
| }, | ||
| widgetParams: { | ||
| javascript: { | ||
| indices: [ | ||
| { | ||
| indexName: 'indexName', | ||
| templates: { | ||
| item: (props) => props.item.name, | ||
| header: (props) => | ||
| `<span>${props.items.length} results</span>`, | ||
| }, | ||
| cssClasses: { header: 'HEADER' }, | ||
| }, | ||
| ], | ||
| showSuggestions: { | ||
| indexName: 'indexName2', | ||
| templates: { | ||
| header: (props) => `<span>${props.items.length} results</span>`, | ||
| }, | ||
| cssClasses: { header: 'HEADER' }, | ||
| }, | ||
| }, | ||
| react: { | ||
| indices: [ | ||
| { | ||
| indexName: 'indexName', | ||
| itemComponent: (props) => props.item.name, | ||
| headerComponent: (props) => ( | ||
| <span>{props.items.length} results</span> | ||
| ), | ||
| classNames: { header: 'HEADER' }, | ||
| }, | ||
| ], | ||
| showSuggestions: { | ||
| indexName: 'indexName2', | ||
| headerComponent: (props) => ( | ||
| <span>{props.items.length} results</span> | ||
| ), | ||
| classNames: { header: 'HEADER' }, | ||
| }, | ||
| }, | ||
| vue: {}, | ||
| }, | ||
| }); | ||
|
|
||
| await act(async () => { | ||
| await wait(0); | ||
| // JS currently doesn't refine on focus | ||
| const input = document.querySelector('.ais-SearchBox-input')!; | ||
| userEvent.click(input); | ||
| userEvent.type(input, 'a'); | ||
| userEvent.clear(input); | ||
|
|
||
| await wait(0); | ||
| }); | ||
|
|
||
| const headers = [ | ||
| ...document.querySelectorAll('.ais-AutocompleteIndexHeader'), | ||
| ]; | ||
| expect(headers).toHaveLength(2); | ||
| expect(headers.map((header) => header.className)).toEqual([ | ||
| 'ais-AutocompleteIndexHeader ais-AutocompleteSuggestionsHeader HEADER', | ||
| 'ais-AutocompleteIndexHeader HEADER', | ||
| ]); | ||
| expect(headers.map((header) => header.textContent)).toEqual([ | ||
| '2 results', | ||
| '3 results', | ||
| ]); | ||
| }); | ||
| }); | ||
| } | ||
|
|
||
| function createMockedSearchClient( | ||
| response: ReturnType<typeof createMultiSearchResponse> | ||
| ) { | ||
| return createSearchClient({ | ||
| // @ts-expect-error - doesn't properly handle multi index, expects all responses to be of the same type | ||
| search: jest.fn(() => Promise.resolve(response)), | ||
| }); | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.