Skip to content
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

Default suggested links to pages #54622

Merged
merged 6 commits into from
Oct 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion packages/block-library/src/navigation-link/link-ui.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,15 @@ export function getSuggestionsQuery( type, kind ) {
if ( kind === 'post-type' ) {
return { type: 'post', subtype: type };
}
return {};
return {
// for custom link which has no type
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// for custom link which has no type
// for custom link which has no kind

Is it kind?

Copy link
Contributor Author

@draganescu draganescu Oct 2, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's type. The switch is on type and the default is for no type.

// always show pages as initial suggestions
initialSuggestionsSearchOptions: {
type: 'post',
subtype: 'page',
perPage: 20,
draganescu marked this conversation as resolved.
Show resolved Hide resolved
},
};
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,17 +85,28 @@ const fetchLinkSuggestions = async (
) => {
const {
isInitialSuggestions = false,
initialSuggestionsSearchOptions = undefined,
} = searchOptions;

const { disablePostFormats = false } = settings;

let {
type = undefined,
subtype = undefined,
page = undefined,
perPage = isInitialSuggestions ? 3 : 20,
draganescu marked this conversation as resolved.
Show resolved Hide resolved
draganescu marked this conversation as resolved.
Show resolved Hide resolved
} = searchOptions;

const { disablePostFormats = false } = settings;

/** @type {Promise<WPLinkSearchResult>[]} */
const queries = [];

if ( isInitialSuggestions && initialSuggestionsSearchOptions ) {
type = initialSuggestionsSearchOptions.type || type;
subtype = initialSuggestionsSearchOptions.subtype || subtype;
page = initialSuggestionsSearchOptions.page || page;
perPage = initialSuggestionsSearchOptions.perPage || perPage;
}

if ( ! type || type === 'post' ) {
queries.push(
apiFetch( {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -231,22 +231,73 @@ describe( 'fetchLinkSuggestions', () => {
] )
);
} );
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',
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( '', {
Expand Down
31 changes: 29 additions & 2 deletions test/e2e/specs/editor/blocks/navigation-list-view.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -195,8 +195,8 @@ test.describe( 'Navigation block - List view editing', () => {
await expect( blockResultOptions.nth( 1 ) ).toHaveText( 'Custom Link' );

// Select the Page Link option.
const pageLinkResult = blockResultOptions.nth( 0 );
await pageLinkResult.click();
const customLinkResult = blockResultOptions.nth( 1 );
await customLinkResult.click();

// Expect to see the Link creation UI be focused.
const linkUIInput = linkControl.getSearchInput();
Expand All @@ -209,7 +209,26 @@ test.describe( 'Navigation block - List view editing', () => {
await expect( linkUIInput ).toBeFocused();
await expect( linkUIInput ).toBeEmpty();

// Provides test coverage for feature whereby Custom Link type
// should default to `Pages` when displaying the "initial suggestions"
// in the Link UI.
// See https://github.com/WordPress/gutenberg/pull/54622.
const firstResult = await linkControl.getNthSearchResult( 0 );
const secondResult = await linkControl.getNthSearchResult( 1 );
const thirdResult = await linkControl.getNthSearchResult( 2 );

const firstResultType =
await linkControl.getSearchResultType( firstResult );

const secondResultType =
await linkControl.getSearchResultType( secondResult );

const thirdResultType =
await linkControl.getSearchResultType( thirdResult );

expect( firstResultType ).toBe( 'Page' );
expect( secondResultType ).toBe( 'Page' );
expect( thirdResultType ).toBe( 'Page' );

// Grab the text from the first result so we can check (later on) that it was inserted.
const firstResultText =
Expand Down Expand Up @@ -573,4 +592,12 @@ class LinkControl {
.locator( '.components-menu-item__item' ) // this is the only way to get the label text without the URL.
.innerText();
}

async getSearchResultType( result ) {
await expect( result ).toBeVisible();

return result
.locator( '.components-menu-item__shortcut' ) // this is the only way to get the type text.
.innerText();
}
}
Loading