Skip to content
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
4 changes: 2 additions & 2 deletions packages/block-library/src/post-date/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ export default function PostDateEdit( props ) {
<ToolsPanelItem
hasValue={ () => isLink !== false }
label={
postType?.labels.singular_name
postType?.labels?.singular_name
? sprintf(
// translators: %s: Name of the post type e.g: "post".
__( 'Link to %s' ),
Expand All @@ -223,7 +223,7 @@ export default function PostDateEdit( props ) {
>
<ToggleControl
label={
postType?.labels.singular_name
postType?.labels?.singular_name
? sprintf(
// translators: %s: Name of the post type e.g: "post".
__( 'Link to %s' ),
Expand Down
2 changes: 1 addition & 1 deletion packages/block-library/src/post-featured-image/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@ export default function PostFeaturedImageEdit( {
>
<ToolsPanelItem
label={
postType?.labels.singular_name
postType?.labels?.singular_name
? sprintf(
// translators: %s: Name of the post type e.g: "post".
__( 'Link to %s' ),
Expand Down
2 changes: 1 addition & 1 deletion packages/editor/src/components/post-card-panel/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ export default function PostCardPanel( {
{ sprintf(
// translators: %s: Name of the plural post type e.g: "Posts".
__( 'Changes will be applied to all selected %s.' ),
labels?.name.toLowerCase()
labels?.name?.toLowerCase()
) }
</WCText>
) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,17 @@ describe( 'PostTypeSupportCheck', () => {
expect( container ).not.toHaveTextContent( 'Supported' );
} );

it( 'does not crash when the post type has no `supports` object', () => {
setupUseSelectMock( {} );
const { container } = render(
<PostTypeSupportCheck supportKeys="title">
Supported
</PostTypeSupportCheck>
);

expect( container ).not.toHaveTextContent( 'Supported' );
} );

it( 'renders its children when post type is known and supports', () => {
setupUseSelectMock( {
supports: {
Expand Down
2 changes: 1 addition & 1 deletion packages/editor/src/components/post-url/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ export default function PostURL( { onClose } ) {
postSlug: safeDecodeURIComponent(
select( editorStore ).getEditedPostSlug()
),
viewPostLabel: postType?.labels.view_item,
viewPostLabel: postType?.labels?.view_item,
postLink: post.link,
permalinkPrefix: permalinkParts?.prefix,
permalinkSuffix: permalinkParts?.suffix,
Expand Down
2 changes: 1 addition & 1 deletion packages/editor/src/components/post-view-link/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export default function PostViewLink() {
return {
permalink: select( editorStore ).getPermalink(),
isPublished: select( editorStore ).isCurrentPostPublished(),
label: postType?.labels.view_item,
label: postType?.labels?.view_item,
hasLoaded: !! postType,
showIconLabels: get( 'core', 'showIconLabels' ),
};
Expand Down
71 changes: 71 additions & 0 deletions packages/editor/src/components/post-view-link/test/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/**
* External dependencies
*/
import { render, screen } from '@testing-library/react';

/**
* WordPress dependencies
*/
import { useSelect } from '@wordpress/data';

/**
* Internal dependencies
*/
import PostViewLink from '../';

jest.mock( '@wordpress/data/src/components/use-select', () => jest.fn() );

const DEFAULTS = {
postType: { labels: { view_item: 'View Post' } },
permalink: 'https://example.com/sample/',
isPublished: true,
showIconLabels: false,
};

function setupUseSelectMock( overrides = {} ) {
// Use object spread (not destructure defaults) so an explicit `undefined`
// override actually overrides the default value.
const data = { ...DEFAULTS, ...overrides };
useSelect.mockImplementation( ( cb ) =>
cb( () => ( {
getCurrentPostType: () => 'post',
getPostType: () => data.postType,
getPermalink: () => data.permalink,
isCurrentPostPublished: () => data.isPublished,
get: () => data.showIconLabels,
} ) )
);
}

describe( 'PostViewLink', () => {
it( 'renders the post type-specific view label when available', () => {
setupUseSelectMock();
render( <PostViewLink /> );
expect( screen.getByLabelText( 'View Post' ) ).toBeInTheDocument();
} );

it( 'falls back to "View post" without crashing when the post type has no labels', () => {
// See https://github.com/WordPress/gutenberg/issues/62918.
setupUseSelectMock( { postType: {} } );
render( <PostViewLink /> );
expect( screen.getByLabelText( 'View post' ) ).toBeInTheDocument();
} );

it( 'renders nothing if the post is not published', () => {
setupUseSelectMock( { isPublished: false } );
const { container } = render( <PostViewLink /> );
expect( container ).toBeEmptyDOMElement();
} );

it( 'renders nothing if no permalink is available', () => {
setupUseSelectMock( { permalink: undefined } );
const { container } = render( <PostViewLink /> );
expect( container ).toBeEmptyDOMElement();
} );

it( 'renders nothing while the post type has not loaded', () => {
setupUseSelectMock( { postType: undefined } );
const { container } = render( <PostViewLink /> );
expect( container ).toBeEmptyDOMElement();
} );
} );
Loading