diff --git a/packages/block-editor/src/components/link-control/search-input.js b/packages/block-editor/src/components/link-control/search-input.js index aea9a903d9f52c..3a90bfb75344b6 100644 --- a/packages/block-editor/src/components/link-control/search-input.js +++ b/packages/block-editor/src/components/link-control/search-input.js @@ -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( ( { @@ -50,6 +54,7 @@ const LinkControlSearchInput = forwardRef( withCreateSuggestion, withURLSuggestion ); + const searchHandler = showSuggestions ? fetchSuggestions || genericSearchHandler : noopSearchHandler; diff --git a/packages/block-editor/src/components/link-control/test/index.js b/packages/block-editor/src/components/link-control/test/index.js index 970302c08b69ef..27e1665d869ff2 100644 --- a/packages/block-editor/src/components/link-control/test/index.js +++ b/packages/block-editor/src/components/link-control/test/index.js @@ -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( , 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' ],