-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
Navigation: use same default implementation of __experimentalFetchLinkSuggestions in post, site, navigation, widget editor #29993
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
da84a8e
Navigation: use shared default implementation of fetchLinkSuggestions…
gwwar 6f20c14
Navigation: remove lodash usage in fetchLinkSuggestions
gwwar 09c38f5
Navigation: make sure __experimentalFetchLinkSuggestions is invoked i…
gwwar 0b1c875
Navigation: move __experimental-fetch-link-suggestions to core-data
gwwar 20209c7
Navigation: avoid HoC for fetchLinkSuggestions, add navigation link s…
gwwar d42885e
Navigation: add test cases for /__experimental-fetch-link-suggestions
gwwar a7477d7
Navigation: add test cases for /__experimental-fetch-link-suggestions
gwwar File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
155 changes: 155 additions & 0 deletions
155
packages/core-data/src/fetch/__experimental-fetch-link-suggestions.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,155 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import apiFetch from '@wordpress/api-fetch'; | ||
import { addQueryArgs } from '@wordpress/url'; | ||
import { decodeEntities } from '@wordpress/html-entities'; | ||
import { __ } from '@wordpress/i18n'; | ||
|
||
/** | ||
* Filters the search by type | ||
* | ||
* @typedef { 'post' | 'term' | 'post-format' } WPLinkSearchType | ||
*/ | ||
|
||
/** | ||
* @typedef WPLinkSearchOptions | ||
* | ||
* @property {boolean} [isInitialSuggestions] Displays initial search suggestions, when true. | ||
* @property {WPLinkSearchType} [type] Filters by search type. | ||
* @property {string} [subtype] Slug of the post-type or taxonomy. | ||
* @property {number} [page] Which page of results to return. | ||
* @property {number} [perPage] Search results per page. | ||
*/ | ||
|
||
/** | ||
* @typedef WPLinkSearchResult | ||
* | ||
* @property {number} id Post or term id. | ||
* @property {string} url Link url. | ||
* @property {string} title Title of the link. | ||
* @property {string} type The taxonomy or post type slug or type URL. | ||
*/ | ||
|
||
/** | ||
* @typedef WPEditorSettings | ||
* | ||
* @property {boolean} [ disablePostFormats ] Disables post formats, when true. | ||
*/ | ||
|
||
/** | ||
* Fetches link suggestions from the API. | ||
* | ||
* @async | ||
* @param {string} search | ||
* @param {WPLinkSearchOptions} [searchOptions] | ||
* @param {WPEditorSettings} [settings] | ||
* | ||
* @example | ||
* ```js | ||
* import { __experimentalFetchLinkSuggestions as fetchLinkSuggestions } from '@wordpress/core-data'; | ||
* | ||
* //... | ||
* | ||
* export function initialize( id, settings ) { | ||
* | ||
* settings.__experimentalFetchLinkSuggestions = ( | ||
* search, | ||
* searchOptions | ||
* ) => fetchLinkSuggestions( search, searchOptions, settings ); | ||
* ``` | ||
* @return {Promise< WPLinkSearchResult[] >} List of search suggestions | ||
*/ | ||
const fetchLinkSuggestions = async ( | ||
search, | ||
searchOptions = {}, | ||
settings = {} | ||
) => { | ||
const { | ||
isInitialSuggestions = false, | ||
type = undefined, | ||
subtype = undefined, | ||
page = undefined, | ||
perPage = isInitialSuggestions ? 3 : 20, | ||
} = searchOptions; | ||
|
||
const { disablePostFormats = false } = settings; | ||
|
||
const queries = []; | ||
|
||
if ( ! type || type === 'post' ) { | ||
queries.push( | ||
apiFetch( { | ||
path: addQueryArgs( '/wp/v2/search', { | ||
search, | ||
page, | ||
per_page: perPage, | ||
type: 'post', | ||
subtype, | ||
} ), | ||
} ).catch( () => [] ) // fail by returning no results | ||
); | ||
} | ||
|
||
if ( ! type || type === 'term' ) { | ||
queries.push( | ||
apiFetch( { | ||
path: addQueryArgs( '/wp/v2/search', { | ||
search, | ||
page, | ||
per_page: perPage, | ||
type: 'term', | ||
subtype, | ||
} ), | ||
} ).catch( () => [] ) | ||
); | ||
} | ||
|
||
if ( ! disablePostFormats && ( ! type || type === 'post-format' ) ) { | ||
queries.push( | ||
apiFetch( { | ||
path: addQueryArgs( '/wp/v2/search', { | ||
search, | ||
page, | ||
per_page: perPage, | ||
type: 'post-format', | ||
subtype, | ||
} ), | ||
} ).catch( () => [] ) | ||
); | ||
} | ||
|
||
return Promise.all( queries ).then( ( results ) => { | ||
return results | ||
.reduce( | ||
( accumulator, current ) => accumulator.concat( current ), //flatten list | ||
[] | ||
) | ||
.filter( | ||
/** | ||
* @param {{ id: number }} result | ||
*/ | ||
( result ) => { | ||
return !! result.id; | ||
} | ||
) | ||
.slice( 0, perPage ) | ||
.map( | ||
/** | ||
* @param {{ id: number, url:string, title?:string, subtype?: string, type?: string }} result | ||
*/ | ||
( result ) => { | ||
return { | ||
id: result.id, | ||
url: result.url, | ||
title: | ||
decodeEntities( result.title || '' ) || | ||
__( '(no title)' ), | ||
type: result.subtype || result.type, | ||
}; | ||
} | ||
); | ||
} ); | ||
}; | ||
|
||
export default fetchLinkSuggestions; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { default as __experimentalFetchLinkSuggestions } from './__experimental-fetch-link-suggestions'; |
218 changes: 218 additions & 0 deletions
218
packages/core-data/src/fetch/test/__experimental-fetch-link-suggestions.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,218 @@ | ||
/** | ||
* Internal dependencies | ||
*/ | ||
import fetchLinkSuggestions from '../__experimental-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', | ||
}, | ||
{ | ||
id: 'quote', | ||
title: 'Quote', | ||
url: 'http://wordpress.local/type/quote/', | ||
type: 'post-format', | ||
}, | ||
] ); | ||
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', | ||
}, | ||
] ); | ||
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/', | ||
}, | ||
] ) | ||
); | ||
} ); | ||
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', | ||
}, | ||
{ | ||
id: 1, | ||
title: 'Uncategorized', | ||
url: 'http://wordpress.local/category/uncategorized/', | ||
type: 'category', | ||
}, | ||
] ) | ||
); | ||
} ); | ||
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', | ||
}, | ||
{ | ||
id: 'quote', | ||
title: 'Quote', | ||
url: 'http://wordpress.local/type/quote/', | ||
type: 'post-format', | ||
}, | ||
] ) | ||
); | ||
} ); | ||
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( 'returns suggestions from post, term, and post-format', () => { | ||
return fetchLinkSuggestions( '', {} ).then( ( suggestions ) => | ||
expect( suggestions ).toEqual( [ | ||
{ | ||
id: 37, | ||
title: 'Contact Page', | ||
url: 'http://wordpress.local/contact-page/', | ||
type: 'page', | ||
}, | ||
{ | ||
id: 9, | ||
title: 'Cats', | ||
url: 'http://wordpress.local/category/cats/', | ||
type: 'category', | ||
}, | ||
{ | ||
id: 1, | ||
title: 'Uncategorized', | ||
url: 'http://wordpress.local/category/uncategorized/', | ||
type: 'category', | ||
}, | ||
{ | ||
id: 'gallery', | ||
title: 'Gallery', | ||
url: 'http://wordpress.local/type/gallery/', | ||
type: 'post-format', | ||
}, | ||
{ | ||
id: 'quote', | ||
title: 'Quote', | ||
url: 'http://wordpress.local/type/quote/', | ||
type: 'post-format', | ||
}, | ||
] ) | ||
); | ||
} ); | ||
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', | ||
}, | ||
] ) | ||
); | ||
} ); | ||
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', | ||
}, | ||
] ) | ||
); | ||
} ); | ||
} ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It might be worth taking this opportunity to write some unit tests for this function since it has a bunch of conditional logic that is often error prone and
apiFetch
is easy to mock.