@wordpress/core-data
Version:
Access to and manipulation of core WordPress entities.
320 lines (313 loc) • 7.59 kB
JavaScript
/**
* 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',
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',
},
] )
);
} );
describe( 'Initial search suggestions', () => {
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( 'should allow custom search options for initial suggestions', () => {
return fetchLinkSuggestions( '', {
type: 'term',
subtype: 'category',
page: 11,
isInitialSuggestions: true,
initialSuggestionsSearchOptions: {
type: 'post',
subtype: 'page',
perPage: 20,
page: 11,
},
} ).then( ( suggestions ) =>
expect( suggestions ).toEqual( [
{
id: 22,
title: 'Page Case',
url: 'http://wordpress.local/page-case/',
type: 'page',
kind: 'post-type',
},
] )
);
} );
it( 'should default any missing initial search options to those from the main search options', () => {
return fetchLinkSuggestions( '', {
type: 'post',
subtype: 'page',
page: 11,
perPage: 20,
isInitialSuggestions: true,
initialSuggestionsSearchOptions: {
// intentionally missing.
// expected to default to those from the main search options.
},
} ).then( ( suggestions ) =>
expect( suggestions ).toEqual( [
{
id: 22,
title: 'Page Case',
url: 'http://wordpress.local/page-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',
},
] )
);
} );
} );