From 2701077b52365630295ab0a474796866add4d2cd Mon Sep 17 00:00:00 2001 From: Dave Smith Date: Tue, 5 Apr 2022 16:20:07 +0100 Subject: [PATCH 1/3] Update to remove __experimental prefix --- .../src/components/link-control/README.md | 13 ++++++------- .../src/components/link-control/index.js | 2 +- .../src/components/link-control/search-input.js | 2 +- .../components/link-control/use-search-handler.js | 3 +-- .../block-editor/src/components/url-input/index.js | 7 +++---- .../link-picker/link-picker-results.native.js | 9 +++++---- .../fetch/__experimental-fetch-link-suggestions.js | 4 ++-- packages/core-data/src/fetch/index.js | 2 +- packages/edit-navigation/src/index.js | 12 ++++-------- packages/edit-site/src/index.js | 4 ++-- packages/edit-widgets/src/index.js | 4 ++-- .../provider/use-block-editor-settings.js | 4 ++-- 12 files changed, 30 insertions(+), 36 deletions(-) diff --git a/packages/block-editor/src/components/link-control/README.md b/packages/block-editor/src/components/link-control/README.md index ba6a4bbba5268f..d79cbe3012cde5 100644 --- a/packages/block-editor/src/components/link-control/README.md +++ b/packages/block-editor/src/components/link-control/README.md @@ -28,7 +28,7 @@ When this suggestion is selected it will call the `createSuggestion` prop afford ### Data sources -By default `LinkControl` utilizes the `__experimentalFetchLinkSuggestions` API from `core/block-editor` in order to retrieve search suggestions for matching post-type entities. +By default `LinkControl` utilizes the `fetchLinkSuggestions` API from `core/block-editor` in order to retrieve search suggestions for matching post-type entities. By default this provides no functionality and so you must implement and provide this in your own Editor instance ([example](https://github.com/WordPress/gutenberg/blob/65c752816f46a9334b84f4801d80dea00ed76fba/packages/editor/src/components/provider/use-block-editor-settings.js#L114-L115)). @@ -69,9 +69,7 @@ An array of settings objects associated with a link (for example: a setting to d To disable settings, pass in an empty array. for example: ```jsx - + ``` ### onChange @@ -192,7 +190,8 @@ A `suggestion` should have the following shape: )} /> ``` -### renderControlBottom + +### renderControlBottom - Type: `Function` - Required: No @@ -262,9 +261,9 @@ The same as `value` in LinkControl, refer to an earlier section of this README f - Type: `Function` - Required: No -Custom search handler for suggestions. If specified, it's passed to `URLInput` as `__experimentalFetchLinkSuggestions`, if not, the default handler is used. +Custom search handler for suggestions. If specified, it's passed to `URLInput` as `fetchLinkSuggestions`, if not, the default handler is used. -Refer to URLInput's README.md for more details about `__experimentalFetchLinkSuggestions` and see the [createSuggestion](#createSuggestion) section of this file to learn more about suggestions. +Refer to URLInput's README.md for more details about `fetchLinkSuggestions` and see the [createSuggestion](#createSuggestion) section of this file to learn more about suggestions. ### onChange diff --git a/packages/block-editor/src/components/link-control/index.js b/packages/block-editor/src/components/link-control/index.js index b807adcbbd8806..cb11046b23523f 100644 --- a/packages/block-editor/src/components/link-control/index.js +++ b/packages/block-editor/src/components/link-control/index.js @@ -91,7 +91,7 @@ import { DEFAULT_LINK_SETTINGS } from './constants'; * @property {boolean=} showSuggestions Whether to present suggestions when typing the URL. * @property {boolean=} showInitialSuggestions Whether to present initial suggestions immediately. * @property {boolean=} withCreateSuggestion Whether to allow creation of link value from suggestion. - * @property {Object=} suggestionsQuery Query parameters to pass along to wp.blockEditor.__experimentalFetchLinkSuggestions. + * @property {Object=} suggestionsQuery Query parameters to pass along to wp.blockEditor.fetchLinkSuggestions. * @property {boolean=} noURLSuggestion Whether to add a fallback suggestion which treats the search query as a URL. * @property {string|Function|undefined} createSuggestionButtonText The text to use in the button that calls createSuggestion. * @property {Function} renderControlBottom Optional controls to be rendered at the bottom of the component. 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 2c5b6daf43ed71..5d793c66b59f35 100644 --- a/packages/block-editor/src/components/link-control/search-input.js +++ b/packages/block-editor/src/components/link-control/search-input.js @@ -133,7 +133,7 @@ const LinkControlSearchInput = forwardRef( __experimentalRenderSuggestions={ showSuggestions ? handleRenderSuggestions : null } - __experimentalFetchLinkSuggestions={ searchHandler } + fetchLinkSuggestions={ searchHandler } __experimentalHandleURLSuggestions={ true } __experimentalShowInitialSuggestions={ showInitialSuggestions diff --git a/packages/block-editor/src/components/link-control/use-search-handler.js b/packages/block-editor/src/components/link-control/use-search-handler.js index e08f071b4401fe..968d7982c130f2 100644 --- a/packages/block-editor/src/components/link-control/use-search-handler.js +++ b/packages/block-editor/src/components/link-control/use-search-handler.js @@ -132,8 +132,7 @@ export default function useSearchHandler( return { pageOnFront: getSettings().pageOnFront, - fetchSearchSuggestions: getSettings() - .__experimentalFetchLinkSuggestions, + fetchSearchSuggestions: getSettings().fetchLinkSuggestions, }; }, [] ); diff --git a/packages/block-editor/src/components/url-input/index.js b/packages/block-editor/src/components/url-input/index.js index 7df591e8b673ea..5d6a2a995b1c1f 100644 --- a/packages/block-editor/src/components/url-input/index.js +++ b/packages/block-editor/src/components/url-input/index.js @@ -136,7 +136,7 @@ class URLInput extends Component { updateSuggestions( value = '' ) { const { - __experimentalFetchLinkSuggestions: fetchLinkSuggestions, + fetchLinkSuggestions, __experimentalHandleURLSuggestions: handleURLSuggestions, } = this.props; @@ -580,13 +580,12 @@ export default compose( withSelect( ( select, props ) => { // If a link suggestions handler is already provided then // bail. - if ( isFunction( props.__experimentalFetchLinkSuggestions ) ) { + if ( isFunction( props.fetchLinkSuggestions ) ) { return; } const { getSettings } = select( blockEditorStore ); return { - __experimentalFetchLinkSuggestions: getSettings() - .__experimentalFetchLinkSuggestions, + fetchLinkSuggestions: getSettings().fetchLinkSuggestions, }; } ) )( URLInput ); diff --git a/packages/components/src/mobile/link-picker/link-picker-results.native.js b/packages/components/src/mobile/link-picker/link-picker-results.native.js index 60ffc4e1ddd6b8..934e4739735a87 100644 --- a/packages/components/src/mobile/link-picker/link-picker-results.native.js +++ b/packages/components/src/mobile/link-picker/link-picker-results.native.js @@ -39,10 +39,11 @@ export default function LinkPickerResults( { const { getSettings } = select( 'core/block-editor' ); const fetchLinkSuggestions = async ( { search } ) => { if ( meetsThreshold( search ) ) { - return await getSettings().__experimentalFetchLinkSuggestions( - search, - { page: nextPage.current, type: 'post', perPage: PER_PAGE } - ); + return await getSettings().fetchLinkSuggestions( search, { + page: nextPage.current, + type: 'post', + perPage: PER_PAGE, + } ); } }; const fetchMore = async ( { diff --git a/packages/core-data/src/fetch/__experimental-fetch-link-suggestions.js b/packages/core-data/src/fetch/__experimental-fetch-link-suggestions.js index bb1bf41d9e2cda..511ef7f153720f 100644 --- a/packages/core-data/src/fetch/__experimental-fetch-link-suggestions.js +++ b/packages/core-data/src/fetch/__experimental-fetch-link-suggestions.js @@ -65,13 +65,13 @@ import { __ } from '@wordpress/i18n'; * * @example * ```js - * import { __experimentalFetchLinkSuggestions as fetchLinkSuggestions } from '@wordpress/core-data'; + * import { fetchLinkSuggestions } from '@wordpress/core-data'; * * //... * * export function initialize( id, settings ) { * - * settings.__experimentalFetchLinkSuggestions = ( + * settings.fetchLinkSuggestions = ( * search, * searchOptions * ) => fetchLinkSuggestions( search, searchOptions, settings ); diff --git a/packages/core-data/src/fetch/index.js b/packages/core-data/src/fetch/index.js index 8d4d28e3b0db82..24310ba7319f01 100644 --- a/packages/core-data/src/fetch/index.js +++ b/packages/core-data/src/fetch/index.js @@ -1,2 +1,2 @@ -export { default as __experimentalFetchLinkSuggestions } from './__experimental-fetch-link-suggestions'; +export { default as fetchLinkSuggestions } from './__experimental-fetch-link-suggestions'; export { default as __experimentalFetchUrlData } from './__experimental-fetch-url-data'; diff --git a/packages/edit-navigation/src/index.js b/packages/edit-navigation/src/index.js index d37cb03181d984..8f3458d02fb091 100644 --- a/packages/edit-navigation/src/index.js +++ b/packages/edit-navigation/src/index.js @@ -10,7 +10,7 @@ import { dispatch, useDispatch } from '@wordpress/data'; import { render, useMemo } from '@wordpress/element'; import { __experimentalFetchUrlData, - __experimentalFetchLinkSuggestions as fetchLinkSuggestions, + fetchLinkSuggestions, store as coreStore, } from '@wordpress/core-data'; import { __ } from '@wordpress/i18n'; @@ -31,7 +31,7 @@ function NavEditor( { settings } ) { const __experimentalSetIsInserterOpened = setIsInserterOpened; // Provide link suggestions handler to fetch search results for Link UI. - const __experimentalFetchLinkSuggestions = ( search, searchOptions ) => { + const _fetchLinkSuggestions = ( search, searchOptions ) => { // Bump the default number of suggestions. // See https://github.com/WordPress/gutenberg/issues/34283. searchOptions.perPage = 10; @@ -41,15 +41,11 @@ function NavEditor( { settings } ) { const editorSettings = useMemo( () => { return { ...settings, - __experimentalFetchLinkSuggestions, + fetchLinkSuggestions: _fetchLinkSuggestions, __experimentalSetIsInserterOpened, __experimentalFetchRichUrlData: __experimentalFetchUrlData, }; - }, [ - settings, - __experimentalFetchLinkSuggestions, - __experimentalSetIsInserterOpened, - ] ); + }, [ settings, _fetchLinkSuggestions, __experimentalSetIsInserterOpened ] ); return ; } diff --git a/packages/edit-site/src/index.js b/packages/edit-site/src/index.js index 139a2f7a096a75..efa137d63284d7 100644 --- a/packages/edit-site/src/index.js +++ b/packages/edit-site/src/index.js @@ -9,7 +9,7 @@ import { import { dispatch, select } from '@wordpress/data'; import { render, unmountComponentAtNode } from '@wordpress/element'; import { - __experimentalFetchLinkSuggestions as fetchLinkSuggestions, + fetchLinkSuggestions, __experimentalFetchUrlData as fetchUrlData, } from '@wordpress/core-data'; import { store as editorStore } from '@wordpress/editor'; @@ -100,7 +100,7 @@ export function reinitializeEditor( target, settings ) { * @param {Object} settings Editor settings. */ export function initializeEditor( id, settings ) { - settings.__experimentalFetchLinkSuggestions = ( search, searchOptions ) => + settings.fetchLinkSuggestions = ( search, searchOptions ) => fetchLinkSuggestions( search, searchOptions, settings ); settings.__experimentalFetchRichUrlData = fetchUrlData; settings.__experimentalSpotlightEntityBlocks = [ 'core/template-part' ]; diff --git a/packages/edit-widgets/src/index.js b/packages/edit-widgets/src/index.js index 9fa62f7909f163..e0d46eb97cf61b 100644 --- a/packages/edit-widgets/src/index.js +++ b/packages/edit-widgets/src/index.js @@ -14,7 +14,7 @@ import { __experimentalGetCoreBlocks, __experimentalRegisterExperimentalCoreBlocks, } from '@wordpress/block-library'; -import { __experimentalFetchLinkSuggestions as fetchLinkSuggestions } from '@wordpress/core-data'; +import { fetchLinkSuggestions } from '@wordpress/core-data'; import { registerLegacyWidgetBlock, registerLegacyWidgetVariations, @@ -97,7 +97,7 @@ export function initialize( id, settings ) { registerBlock( widgetArea ); registerWidgetGroupBlock(); - settings.__experimentalFetchLinkSuggestions = ( search, searchOptions ) => + settings.fetchLinkSuggestions = ( search, searchOptions ) => fetchLinkSuggestions( search, searchOptions, settings ); // As we are unregistering `core/freeform` to avoid the Classic block, we must diff --git a/packages/editor/src/components/provider/use-block-editor-settings.js b/packages/editor/src/components/provider/use-block-editor-settings.js index 65f0577f9fd161..8d65eed0d346ee 100644 --- a/packages/editor/src/components/provider/use-block-editor-settings.js +++ b/packages/editor/src/components/provider/use-block-editor-settings.js @@ -10,7 +10,7 @@ import { Platform, useMemo } from '@wordpress/element'; import { useDispatch, useSelect } from '@wordpress/data'; import { store as coreStore, - __experimentalFetchLinkSuggestions as fetchLinkSuggestions, + fetchLinkSuggestions, __experimentalFetchUrlData as fetchUrlData, } from '@wordpress/core-data'; import { __ } from '@wordpress/i18n'; @@ -144,7 +144,7 @@ function useBlockEditorSettings( settings, hasTemplate ) { __experimentalReusableBlocks: reusableBlocks, __experimentalBlockPatterns: blockPatterns, __experimentalBlockPatternCategories: blockPatternCategories, - __experimentalFetchLinkSuggestions: ( search, searchOptions ) => + fetchLinkSuggestions: ( search, searchOptions ) => fetchLinkSuggestions( search, searchOptions, settings ), __experimentalFetchRichUrlData: fetchUrlData, __experimentalCanUserUseUnfilteredHTML: canUseUnfilteredHTML, From d79f55bdb2dde0f3464c5c236aa97df842beeb09 Mon Sep 17 00:00:00 2001 From: Dave Smith Date: Wed, 27 Apr 2022 10:36:10 +0100 Subject: [PATCH 2/3] Rename files and references --- .../src/navigation-link/test/edit.js | 2 +- ...ggestions.js => fetch-link-suggestions.js} | 0 packages/core-data/src/fetch/index.js | 2 +- .../__experimental-fetch-link-suggestions.js | 2 +- .../src/fetch/test/fetch-link-suggestions.js | 270 ++++++++++++++++++ 5 files changed, 273 insertions(+), 3 deletions(-) rename packages/core-data/src/fetch/{__experimental-fetch-link-suggestions.js => fetch-link-suggestions.js} (100%) create mode 100644 packages/core-data/src/fetch/test/fetch-link-suggestions.js diff --git a/packages/block-library/src/navigation-link/test/edit.js b/packages/block-library/src/navigation-link/test/edit.js index cdec00828329e4..f92d4f4d61bc08 100644 --- a/packages/block-library/src/navigation-link/test/edit.js +++ b/packages/block-library/src/navigation-link/test/edit.js @@ -6,7 +6,7 @@ import { updateNavigationLinkBlockAttributes } from '../edit'; describe( 'edit', () => { describe( 'updateNavigationLinkBlockAttributes', () => { // Data shapes are linked to fetchLinkSuggestions from - // core-data/src/fetch/__experimental-fetch-link-suggestions.js. + // core-data/src/fetch/fetch-link-suggestions.js. it( 'can update a post link', () => { const setAttributes = jest.fn(); const linkSuggestion = { diff --git a/packages/core-data/src/fetch/__experimental-fetch-link-suggestions.js b/packages/core-data/src/fetch/fetch-link-suggestions.js similarity index 100% rename from packages/core-data/src/fetch/__experimental-fetch-link-suggestions.js rename to packages/core-data/src/fetch/fetch-link-suggestions.js diff --git a/packages/core-data/src/fetch/index.js b/packages/core-data/src/fetch/index.js index 24310ba7319f01..7275bdf8e7faa5 100644 --- a/packages/core-data/src/fetch/index.js +++ b/packages/core-data/src/fetch/index.js @@ -1,2 +1,2 @@ -export { default as fetchLinkSuggestions } from './__experimental-fetch-link-suggestions'; +export { default as fetchLinkSuggestions } from './fetch-link-suggestions'; export { default as __experimentalFetchUrlData } from './__experimental-fetch-url-data'; diff --git a/packages/core-data/src/fetch/test/__experimental-fetch-link-suggestions.js b/packages/core-data/src/fetch/test/__experimental-fetch-link-suggestions.js index 23da95a611b595..d30b72e60abcc2 100644 --- a/packages/core-data/src/fetch/test/__experimental-fetch-link-suggestions.js +++ b/packages/core-data/src/fetch/test/__experimental-fetch-link-suggestions.js @@ -1,7 +1,7 @@ /** * Internal dependencies */ -import fetchLinkSuggestions from '../__experimental-fetch-link-suggestions'; +import fetchLinkSuggestions from '../fetch-link-suggestions'; jest.mock( '@wordpress/api-fetch', () => jest.fn( ( { path } ) => { diff --git a/packages/core-data/src/fetch/test/fetch-link-suggestions.js b/packages/core-data/src/fetch/test/fetch-link-suggestions.js new file mode 100644 index 00000000000000..d30b72e60abcc2 --- /dev/null +++ b/packages/core-data/src/fetch/test/fetch-link-suggestions.js @@ -0,0 +1,270 @@ +/** + * Internal dependencies + */ +import fetchLinkSuggestions from '../fetch-link-suggestions'; + +jest.mock( '@wordpress/api-fetch', () => + jest.fn( ( { path } ) => { + switch ( path ) { + case '/wp/v2/search?search=&per_page=20&type=post': + case '/wp/v2/search?search=Contact&per_page=20&type=post&subtype=page': + return Promise.resolve( [ + { + id: 37, + title: 'Contact Page', + url: 'http://wordpress.local/contact-page/', + type: 'post', + subtype: 'page', + }, + ] ); + case '/wp/v2/search?search=&per_page=20&type=term': + case '/wp/v2/search?search=cat&per_page=20&type=term&subtype=category': + return Promise.resolve( [ + { + id: 9, + title: 'Cats', + url: 'http://wordpress.local/category/cats/', + type: 'category', + }, + { + id: 1, + title: 'Uncategorized', + url: 'http://wordpress.local/category/uncategorized/', + type: 'category', + }, + ] ); + case '/wp/v2/search?search=&per_page=20&type=post-format': + return Promise.resolve( [ + { + id: 'gallery', + title: 'Gallery', + url: 'http://wordpress.local/type/gallery/', + type: 'post-format', + kind: 'taxonomy', + }, + { + id: 'quote', + title: 'Quote', + url: 'http://wordpress.local/type/quote/', + type: 'post-format', + kind: 'taxonomy', + }, + ] ); + case '/wp/v2/search?search=&per_page=3&type=post&subtype=page': + return Promise.resolve( [ + { + id: 11, + title: 'Limit Case', + url: 'http://wordpress.local/limit-case/', + type: 'post', + subtype: 'page', + }, + ] ); + case '/wp/v2/search?search=&page=11&per_page=20&type=post&subtype=page': + return Promise.resolve( [ + { + id: 22, + title: 'Page Case', + url: 'http://wordpress.local/page-case/', + type: 'post', + subtype: 'page', + }, + ] ); + case '/wp/v2/media?search=&per_page=20': + return Promise.resolve( [ + { + id: 54, + title: { + rendered: 'Some Test Media Title', + }, + type: 'attachment', + source_url: + 'http://localhost:8888/wp-content/uploads/2022/03/test-pdf.pdf', + }, + ] ); + default: + return Promise.resolve( [ + { + id: -1, + title: 'missing case or failed', + url: path, + type: 'missing case or failed', + }, + ] ); + } + } ) +); + +describe( 'fetchLinkSuggestions', () => { + it( 'filters suggestions by post-type', () => { + return fetchLinkSuggestions( 'Contact', { + type: 'post', + subtype: 'page', + } ).then( ( suggestions ) => + expect( suggestions ).toEqual( [ + { + id: 37, + title: 'Contact Page', + type: 'page', + url: 'http://wordpress.local/contact-page/', + kind: 'post-type', + }, + ] ) + ); + } ); + it( 'filters suggestions by term', () => { + return fetchLinkSuggestions( 'cat', { + type: 'term', + subtype: 'category', + } ).then( ( suggestions ) => + expect( suggestions ).toEqual( [ + { + id: 9, + title: 'Cats', + url: 'http://wordpress.local/category/cats/', + type: 'category', + kind: 'taxonomy', + }, + { + id: 1, + title: 'Uncategorized', + url: 'http://wordpress.local/category/uncategorized/', + type: 'category', + kind: 'taxonomy', + }, + ] ) + ); + } ); + it( 'filters suggestions by post-format', () => { + return fetchLinkSuggestions( '', { + type: 'post-format', + } ).then( ( suggestions ) => + expect( suggestions ).toEqual( [ + { + id: 'gallery', + title: 'Gallery', + url: 'http://wordpress.local/type/gallery/', + type: 'post-format', + kind: 'taxonomy', + }, + { + id: 'quote', + title: 'Quote', + url: 'http://wordpress.local/type/quote/', + type: 'post-format', + kind: 'taxonomy', + }, + ] ) + ); + } ); + it( 'filters does not return post-format suggestions when formats are not supported', () => { + return fetchLinkSuggestions( + '', + { + type: 'post-format', + }, + { disablePostFormats: true } + ).then( ( suggestions ) => expect( suggestions ).toEqual( [] ) ); + } ); + + it( 'filters suggestions by attachment', () => { + return fetchLinkSuggestions( '', { + type: 'attachment', + } ).then( ( suggestions ) => + expect( suggestions ).toEqual( [ + { + id: 54, + title: 'Some Test Media Title', + url: + 'http://localhost:8888/wp-content/uploads/2022/03/test-pdf.pdf', + type: 'attachment', + kind: 'media', + }, + ] ) + ); + } ); + + it( 'returns suggestions from post, term, post-format and media', () => { + return fetchLinkSuggestions( '', {} ).then( ( suggestions ) => + expect( suggestions ).toEqual( [ + { + id: 37, + title: 'Contact Page', + url: 'http://wordpress.local/contact-page/', + type: 'page', + kind: 'post-type', + }, + { + id: 9, + title: 'Cats', + url: 'http://wordpress.local/category/cats/', + type: 'category', + kind: 'taxonomy', + }, + { + id: 1, + title: 'Uncategorized', + url: 'http://wordpress.local/category/uncategorized/', + type: 'category', + kind: 'taxonomy', + }, + { + id: 'gallery', + title: 'Gallery', + url: 'http://wordpress.local/type/gallery/', + type: 'post-format', + kind: 'taxonomy', + }, + { + id: 'quote', + title: 'Quote', + url: 'http://wordpress.local/type/quote/', + type: 'post-format', + kind: 'taxonomy', + }, + { + id: 54, + title: 'Some Test Media Title', + url: + 'http://localhost:8888/wp-content/uploads/2022/03/test-pdf.pdf', + type: 'attachment', + kind: 'media', + }, + ] ) + ); + } ); + it( 'initial search suggestions limits results', () => { + return fetchLinkSuggestions( '', { + type: 'post', + subtype: 'page', + isInitialSuggestions: true, + } ).then( ( suggestions ) => + expect( suggestions ).toEqual( [ + { + id: 11, + title: 'Limit Case', + url: 'http://wordpress.local/limit-case/', + type: 'page', + kind: 'post-type', + }, + ] ) + ); + } ); + it( 'allows searching from a page', () => { + return fetchLinkSuggestions( '', { + type: 'post', + subtype: 'page', + page: 11, + } ).then( ( suggestions ) => + expect( suggestions ).toEqual( [ + { + id: 22, + title: 'Page Case', + url: 'http://wordpress.local/page-case/', + type: 'page', + kind: 'post-type', + }, + ] ) + ); + } ); +} ); From 59818dfd0678d74e69f20be2c9956af464a37e09 Mon Sep 17 00:00:00 2001 From: Dave Smith Date: Wed, 27 Apr 2022 11:35:38 +0100 Subject: [PATCH 3/3] Remove file Somehow missed first time around --- .../__experimental-fetch-link-suggestions.js | 270 ------------------ 1 file changed, 270 deletions(-) delete mode 100644 packages/core-data/src/fetch/test/__experimental-fetch-link-suggestions.js diff --git a/packages/core-data/src/fetch/test/__experimental-fetch-link-suggestions.js b/packages/core-data/src/fetch/test/__experimental-fetch-link-suggestions.js deleted file mode 100644 index d30b72e60abcc2..00000000000000 --- a/packages/core-data/src/fetch/test/__experimental-fetch-link-suggestions.js +++ /dev/null @@ -1,270 +0,0 @@ -/** - * Internal dependencies - */ -import fetchLinkSuggestions from '../fetch-link-suggestions'; - -jest.mock( '@wordpress/api-fetch', () => - jest.fn( ( { path } ) => { - switch ( path ) { - case '/wp/v2/search?search=&per_page=20&type=post': - case '/wp/v2/search?search=Contact&per_page=20&type=post&subtype=page': - return Promise.resolve( [ - { - id: 37, - title: 'Contact Page', - url: 'http://wordpress.local/contact-page/', - type: 'post', - subtype: 'page', - }, - ] ); - case '/wp/v2/search?search=&per_page=20&type=term': - case '/wp/v2/search?search=cat&per_page=20&type=term&subtype=category': - return Promise.resolve( [ - { - id: 9, - title: 'Cats', - url: 'http://wordpress.local/category/cats/', - type: 'category', - }, - { - id: 1, - title: 'Uncategorized', - url: 'http://wordpress.local/category/uncategorized/', - type: 'category', - }, - ] ); - case '/wp/v2/search?search=&per_page=20&type=post-format': - return Promise.resolve( [ - { - id: 'gallery', - title: 'Gallery', - url: 'http://wordpress.local/type/gallery/', - type: 'post-format', - kind: 'taxonomy', - }, - { - id: 'quote', - title: 'Quote', - url: 'http://wordpress.local/type/quote/', - type: 'post-format', - kind: 'taxonomy', - }, - ] ); - case '/wp/v2/search?search=&per_page=3&type=post&subtype=page': - return Promise.resolve( [ - { - id: 11, - title: 'Limit Case', - url: 'http://wordpress.local/limit-case/', - type: 'post', - subtype: 'page', - }, - ] ); - case '/wp/v2/search?search=&page=11&per_page=20&type=post&subtype=page': - return Promise.resolve( [ - { - id: 22, - title: 'Page Case', - url: 'http://wordpress.local/page-case/', - type: 'post', - subtype: 'page', - }, - ] ); - case '/wp/v2/media?search=&per_page=20': - return Promise.resolve( [ - { - id: 54, - title: { - rendered: 'Some Test Media Title', - }, - type: 'attachment', - source_url: - 'http://localhost:8888/wp-content/uploads/2022/03/test-pdf.pdf', - }, - ] ); - default: - return Promise.resolve( [ - { - id: -1, - title: 'missing case or failed', - url: path, - type: 'missing case or failed', - }, - ] ); - } - } ) -); - -describe( 'fetchLinkSuggestions', () => { - it( 'filters suggestions by post-type', () => { - return fetchLinkSuggestions( 'Contact', { - type: 'post', - subtype: 'page', - } ).then( ( suggestions ) => - expect( suggestions ).toEqual( [ - { - id: 37, - title: 'Contact Page', - type: 'page', - url: 'http://wordpress.local/contact-page/', - kind: 'post-type', - }, - ] ) - ); - } ); - it( 'filters suggestions by term', () => { - return fetchLinkSuggestions( 'cat', { - type: 'term', - subtype: 'category', - } ).then( ( suggestions ) => - expect( suggestions ).toEqual( [ - { - id: 9, - title: 'Cats', - url: 'http://wordpress.local/category/cats/', - type: 'category', - kind: 'taxonomy', - }, - { - id: 1, - title: 'Uncategorized', - url: 'http://wordpress.local/category/uncategorized/', - type: 'category', - kind: 'taxonomy', - }, - ] ) - ); - } ); - it( 'filters suggestions by post-format', () => { - return fetchLinkSuggestions( '', { - type: 'post-format', - } ).then( ( suggestions ) => - expect( suggestions ).toEqual( [ - { - id: 'gallery', - title: 'Gallery', - url: 'http://wordpress.local/type/gallery/', - type: 'post-format', - kind: 'taxonomy', - }, - { - id: 'quote', - title: 'Quote', - url: 'http://wordpress.local/type/quote/', - type: 'post-format', - kind: 'taxonomy', - }, - ] ) - ); - } ); - it( 'filters does not return post-format suggestions when formats are not supported', () => { - return fetchLinkSuggestions( - '', - { - type: 'post-format', - }, - { disablePostFormats: true } - ).then( ( suggestions ) => expect( suggestions ).toEqual( [] ) ); - } ); - - it( 'filters suggestions by attachment', () => { - return fetchLinkSuggestions( '', { - type: 'attachment', - } ).then( ( suggestions ) => - expect( suggestions ).toEqual( [ - { - id: 54, - title: 'Some Test Media Title', - url: - 'http://localhost:8888/wp-content/uploads/2022/03/test-pdf.pdf', - type: 'attachment', - kind: 'media', - }, - ] ) - ); - } ); - - it( 'returns suggestions from post, term, post-format and media', () => { - return fetchLinkSuggestions( '', {} ).then( ( suggestions ) => - expect( suggestions ).toEqual( [ - { - id: 37, - title: 'Contact Page', - url: 'http://wordpress.local/contact-page/', - type: 'page', - kind: 'post-type', - }, - { - id: 9, - title: 'Cats', - url: 'http://wordpress.local/category/cats/', - type: 'category', - kind: 'taxonomy', - }, - { - id: 1, - title: 'Uncategorized', - url: 'http://wordpress.local/category/uncategorized/', - type: 'category', - kind: 'taxonomy', - }, - { - id: 'gallery', - title: 'Gallery', - url: 'http://wordpress.local/type/gallery/', - type: 'post-format', - kind: 'taxonomy', - }, - { - id: 'quote', - title: 'Quote', - url: 'http://wordpress.local/type/quote/', - type: 'post-format', - kind: 'taxonomy', - }, - { - id: 54, - title: 'Some Test Media Title', - url: - 'http://localhost:8888/wp-content/uploads/2022/03/test-pdf.pdf', - type: 'attachment', - kind: 'media', - }, - ] ) - ); - } ); - it( 'initial search suggestions limits results', () => { - return fetchLinkSuggestions( '', { - type: 'post', - subtype: 'page', - isInitialSuggestions: true, - } ).then( ( suggestions ) => - expect( suggestions ).toEqual( [ - { - id: 11, - title: 'Limit Case', - url: 'http://wordpress.local/limit-case/', - type: 'page', - kind: 'post-type', - }, - ] ) - ); - } ); - it( 'allows searching from a page', () => { - return fetchLinkSuggestions( '', { - type: 'post', - subtype: 'page', - page: 11, - } ).then( ( suggestions ) => - expect( suggestions ).toEqual( [ - { - id: 22, - title: 'Page Case', - url: 'http://wordpress.local/page-case/', - type: 'page', - kind: 'post-type', - }, - ] ) - ); - } ); -} );