Skip to content
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

fix: fetchSearch #4008

Merged
merged 4 commits into from
Dec 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion __tests__/src/components/SearchPanelControls.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,23 @@ describe('SearchPanelControls', () => {
await user.keyboard('somestring');
await user.click(await screen.findByText('somestring 12345'));
expect(fetchSearch).toHaveBeenCalledWith('window', 'cw', 'http://example.com/search?q=somestring+12345', 'somestring 12345');

fetch.resetMocks();
});
it('should fetch result only once', async () => {
const fetchSearch = jest.fn();
const user = userEvent.setup();

createWrapper({
autocompleteService: { id: 'http://example.com/autocomplete' },
fetchSearch,
searchService: { id: 'http://example.com/search', options: { resource: { id: 'abc' } } },
});

await user.click(screen.getByRole('combobox'));
await user.keyboard('somestring');
await user.keyboard('{Enter}');
expect(fetchSearch).toHaveBeenCalledTimes(1);
});

it('renders a text input through the renderInput prop', () => {
createWrapper();
Expand Down
17 changes: 13 additions & 4 deletions src/components/SearchPanelControls.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export class SearchPanelControls extends Component {
constructor(props) {
super(props);

this.state = { search: props.query, suggestions: [] };
this.state = { isSuggestionSelected: false, search: props.query, suggestions: [] };
this.handleChange = this.handleChange.bind(this);
this.submitSearch = this.submitSearch.bind(this);
this.getSuggestions = this.getSuggestions.bind(this);
Expand Down Expand Up @@ -66,6 +66,7 @@ export class SearchPanelControls extends Component {
return;
}
this.setState({
isSuggestionSelected: false,
search: value,
suggestions: [],
});
Expand Down Expand Up @@ -109,16 +110,24 @@ export class SearchPanelControls extends Component {
const {
companionWindowId, fetchSearch, searchService, windowId,
} = this.props;
const { search } = this.state;
event && event.preventDefault();
const { search, isSuggestionSelected } = this.state;
if (!search) return;

if (event && event.type === 'submit' && isSuggestionSelected) {
event.preventDefault();
return; // Do not trigger search again if a suggestion was selected
}

if (event) {
event.preventDefault();
}
fetchSearch(windowId, companionWindowId, `${searchService.id}?${new URLSearchParams({ q: search })}`, search);
}

/** */
selectItem(_event, selectedItem, _reason) {
if (selectedItem && getMatch(selectedItem)) {
this.setState({ search: getMatch(selectedItem) }, this.submitSearch);
this.setState({ isSuggestionSelected: true, search: getMatch(selectedItem) }, this.submitSearch);
}
}

Expand Down