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 unintended search http request in <LinkControl> #32857

Merged
merged 2 commits into from
Jun 22, 2021
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
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,11 @@ import LinkControlSearchResults from './search-results';
import { CREATE_TYPE } from './constants';
import useSearchHandler from './use-search-handler';

const noopSearchHandler = Promise.resolve( [] );
// Must be a function as otherwise URLInput will default
// to the fetchLinkSuggestions passed in block editor settings
// which will cause an unintended http request.
const noopSearchHandler = () => Promise.resolve( [] );

const LinkControlSearchInput = forwardRef(
(
{
Expand Down Expand Up @@ -50,6 +54,7 @@ const LinkControlSearchInput = forwardRef(
withCreateSuggestion,
withURLSuggestion
);

const searchHandler = showSuggestions
? fetchSuggestions || genericSearchHandler
: noopSearchHandler;
Expand Down
25 changes: 25 additions & 0 deletions packages/block-editor/src/components/link-control/test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,31 @@ describe( 'Searching for a link', () => {
expect( mockFetchSuggestionsFirstArg ).toEqual( 'Hello' );
} );

it( 'should not call search handler when showSuggestions is false', async () => {
act( () => {
render( <LinkControl showSuggestions={ false } />, container );
} );

// Search Input UI
const searchInput = getURLInput();

// Simulate searching for a term
act( () => {
Simulate.change( searchInput, {
target: { value: 'anything' },
} );
} );

const searchResultElements = getSearchResults();

// fetchFauxEntitySuggestions resolves on next "tick" of event loop
await eventLoopTick();

// TODO: select these by aria relationship to autocomplete rather than arbitrary selector.
expect( searchResultElements ).toHaveLength( 0 );
expect( mockFetchSearchSuggestions ).not.toHaveBeenCalled();
} );

it.each( [
[ 'couldbeurlorentitysearchterm' ],
[ 'ThisCouldAlsoBeAValidURL' ],
Expand Down