From 8702f70930f076565915611b93192ebf028caa87 Mon Sep 17 00:00:00 2001 From: Dave Smith Date: Mon, 21 Jun 2021 16:47:52 +0100 Subject: [PATCH 1/2] Ensure fallback fetch suggestions handler overides the URLInput fallback --- .../src/components/link-control/search-input.js | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) 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..187557c755ba28 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. +// This 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; From 16b45195617ec6b35a4ea2c8f23c45749490351f Mon Sep 17 00:00:00 2001 From: Dave Smith Date: Mon, 21 Jun 2021 17:05:23 +0100 Subject: [PATCH 2/2] Add test coverage --- .../components/link-control/search-input.js | 4 +-- .../src/components/link-control/test/index.js | 25 +++++++++++++++++++ 2 files changed, 27 insertions(+), 2 deletions(-) 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 187557c755ba28..3a90bfb75344b6 100644 --- a/packages/block-editor/src/components/link-control/search-input.js +++ b/packages/block-editor/src/components/link-control/search-input.js @@ -19,8 +19,8 @@ import { CREATE_TYPE } from './constants'; import useSearchHandler from './use-search-handler'; // Must be a function as otherwise URLInput will default -// to the fetchLinkSuggestions passed in block editor settings. -// This will cause an unintended http request. +// to the fetchLinkSuggestions passed in block editor settings +// which will cause an unintended http request. const noopSearchHandler = () => Promise.resolve( [] ); const LinkControlSearchInput = forwardRef( 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' ],