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

Remove redundant type badges in navigation link control #24885

Merged
merged 2 commits into from
Sep 1, 2020
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
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ const LinkControlSearchInput = forwardRef(
withCreateSuggestion,
currentInputValue: value,
createSuggestionButtonText,
suggestionsQuery,
handleSuggestionClick: ( suggestion ) => {
if ( props.handleSuggestionClick ) {
props.handleSuggestionClick( suggestion );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export const LinkControlSearchItem = ( {
onClick,
isURL = false,
searchTerm = '',
shouldShowType = false,
} ) => {
return (
<Button
Expand Down Expand Up @@ -54,7 +55,7 @@ export const LinkControlSearchItem = ( {
{ isURL && __( 'Press ENTER to add this link' ) }
</span>
</span>
{ suggestion.type && (
{ shouldShowType && suggestion.type && (
<span className="block-editor-link-control__search-item-type">
{ /* Rename 'post_tag' to 'tag'. Ideally, the API would return the localised CPT or taxonomy label. */ }
{ suggestion.type === 'post_tag' ? 'tag' : suggestion.type }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export default function LinkControlSearchResults( {
isLoading,
isInitialSuggestions,
createSuggestionButtonText,
suggestionsQuery,
} ) {
const resultsListClasses = classnames(
'block-editor-link-control__search-results',
Expand All @@ -45,6 +46,8 @@ export default function LinkControlSearchResults( {
withCreateSuggestion &&
! isSingleDirectEntryResult &&
! isInitialSuggestions;
// If the query has a specified type, then we can skip showing them in the result. See #24839.
const shouldShowSuggestionsTypes = ! suggestionsQuery?.type;

// According to guidelines aria-label should be added if the label
// itself is not visible.
Expand Down Expand Up @@ -128,6 +131,7 @@ export default function LinkControlSearchResults( {
suggestion.type.toLowerCase()
) }
searchTerm={ currentInputValue }
shouldShowType={ shouldShowSuggestionsTypes }
/>
);
} ) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,29 +7,29 @@ export const fauxEntitySuggestions = [
{
id: uniqueId(),
title: 'Hello Page',
type: 'Page',
type: 'page',
kevin940726 marked this conversation as resolved.
Show resolved Hide resolved
info: '2 days ago',
url: `?p=${ uniqueId() }`,
},
{
id: uniqueId(),
title: 'Hello Post',
type: 'Post',
type: 'post',
info: '19 days ago',
url: `?p=${ uniqueId() }`,
},
{
id: uniqueId(),
title: 'Hello Another One',
type: 'Page',
type: 'page',
info: '19 days ago',
url: `?p=${ uniqueId() }`,
},
{
id: uniqueId(),
title:
'This is another Post with a much longer title just to be really annoying and to try and break the UI',
type: 'Post',
type: 'post',
info: '1 month ago',
url: `?p=${ uniqueId() }`,
},
Expand Down
75 changes: 71 additions & 4 deletions packages/block-editor/src/components/link-control/test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
*/
import { render, unmountComponentAtNode } from 'react-dom';
import { act, Simulate } from 'react-dom/test-utils';
import { queryByText, queryByRole } from '@testing-library/react';
import { first, last, nth, uniqueId } from 'lodash';
/**
* WordPress dependencies
Expand Down Expand Up @@ -1217,10 +1218,9 @@ describe( 'Selecting links', () => {
expect( currentLinkHTML ).toEqual(
expect.stringContaining( selectedLink.title )
);
expect( currentLinkHTML ).toEqual(
expect.stringContaining( selectedLink.type )
);
expect( currentLinkHTML ).toEqual( expect.stringContaining( 'Edit' ) );
expect(
queryByRole( currentLink, 'button', { name: 'Edit' } )
).toBeTruthy();
Comment on lines -1220 to +1223
Copy link
Member Author

Choose a reason for hiding this comment

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

Fixed a bad test where it's asserting the HTML to contain a Page text anywhere. It was passing because the text contains Hello Page, but not we want to test against.

Fixed it by using RTL to search for a button with the Edit text.

expect( currentLinkAnchor ).not.toBeNull();
} );

Expand Down Expand Up @@ -1686,3 +1686,70 @@ describe( 'Addition Settings UI', () => {
expect( settingControlsInputs[ 1 ].checked ).toEqual( true );
} );
} );

describe( 'Post types', () => {
it( 'should display post type in search results of link', async () => {
const searchTerm = 'Hello world';

act( () => {
render( <LinkControl />, container );
} );

// Search Input UI
const searchInput = getURLInput();

// Simulate searching for a term
act( () => {
Simulate.change( searchInput, { target: { value: searchTerm } } );
} );

// fetchFauxEntitySuggestions resolves on next "tick" of event loop
await eventLoopTick();

const searchResultElements = getSearchResults();

searchResultElements.forEach( ( resultItem, index ) => {
expect(
queryByText( resultItem, fauxEntitySuggestions[ index ].type )
).toBeTruthy();
} );
} );

it.each( [ 'page', 'post', 'tag', 'post_tag', 'category' ] )(
'should NOT display post type in search results of %s',
async ( postType ) => {
const searchTerm = 'Hello world';

act( () => {
render(
<LinkControl suggestionsQuery={ { type: postType } } />,
container
);
} );

// Search Input UI
const searchInput = getURLInput();

// Simulate searching for a term
act( () => {
Simulate.change( searchInput, {
target: { value: searchTerm },
} );
} );

// fetchFauxEntitySuggestions resolves on next "tick" of event loop
await eventLoopTick();

const searchResultElements = getSearchResults();

searchResultElements.forEach( ( resultItem, index ) => {
expect(
queryByText(
resultItem,
fauxEntitySuggestions[ index ].type
)
).toBeFalsy();
} );
}
);
} );