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

Editor: Use hooks instead of HoCs in PostTypeSupportCheck #53235

Merged
merged 3 commits into from
Aug 1, 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
30 changes: 18 additions & 12 deletions packages/editor/src/components/post-author/test/check.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,32 +19,38 @@ jest.mock( '@wordpress/data/src/components/use-select', () => {
return mock;
} );

function setupUseSelectMock( hasAssignAuthorAction, hasAuthors ) {
useSelect.mockImplementation( ( cb ) => {
return cb( () => ( {
getPostType: () => ( { supports: { author: true } } ),
getEditedPostAttribute: () => {},
getCurrentPost: () => ( {
_links: {
'wp:action-assign-author': hasAssignAuthorAction,
},
} ),
getUsers: () => Array( hasAuthors ? 1 : 0 ).fill( {} ),
} ) );
} );
}

describe( 'PostAuthorCheck', () => {
it( 'should not render anything if has no authors', () => {
useSelect.mockImplementation( () => ( {
hasAuthors: false,
hasAssignAuthorAction: true,
} ) );
setupUseSelectMock( false, true );

render( <PostAuthorCheck>authors</PostAuthorCheck> );
expect( screen.queryByText( 'authors' ) ).not.toBeInTheDocument();
} );

it( "should not render anything if doesn't have author action", () => {
useSelect.mockImplementation( () => ( {
hasAuthors: true,
hasAssignAuthorAction: false,
} ) );
setupUseSelectMock( true, false );

render( <PostAuthorCheck>authors</PostAuthorCheck> );
expect( screen.queryByText( 'authors' ) ).not.toBeInTheDocument();
} );

it( 'should render control', () => {
useSelect.mockImplementation( () => ( {
hasAuthors: true,
hasAssignAuthorAction: true,
} ) );
setupUseSelectMock( true, true );

render( <PostAuthorCheck>authors</PostAuthorCheck> );
expect( screen.getByText( 'authors' ) ).toBeVisible();
Expand Down
18 changes: 8 additions & 10 deletions packages/editor/src/components/post-type-support-check/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* WordPress dependencies
*/
import { withSelect } from '@wordpress/data';
import { useSelect } from '@wordpress/data';
import { store as coreStore } from '@wordpress/core-data';

/**
Expand All @@ -14,15 +14,19 @@ import { store as editorStore } from '../../store';
* type supports one of the given `supportKeys` prop.
*
* @param {Object} props Props.
* @param {string} [props.postType] Current post type.
* @param {WPElement} props.children Children to be rendered if post
* type supports.
* @param {(string|string[])} props.supportKeys String or string array of keys
* to test.
*
* @return {WPComponent} The component to be rendered.
*/
export function PostTypeSupportCheck( { postType, children, supportKeys } ) {
export function PostTypeSupportCheck( { children, supportKeys } ) {
const postType = useSelect( ( select ) => {
const { getEditedPostAttribute } = select( editorStore );
const { getPostType } = select( coreStore );
return getPostType( getEditedPostAttribute( 'type' ) );
}, [] );
let isSupported = true;
if ( postType ) {
isSupported = (
Expand All @@ -37,10 +41,4 @@ export function PostTypeSupportCheck( { postType, children, supportKeys } ) {
return children;
}

export default withSelect( ( select ) => {
const { getEditedPostAttribute } = select( editorStore );
const { getPostType } = select( coreStore );
return {
postType: getPostType( getEditedPostAttribute( 'type' ) ),
};
} )( PostTypeSupportCheck );
export default PostTypeSupportCheck;
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,37 @@
*/
import { render } from '@testing-library/react';

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

/**
* Internal dependencies
*/
import { PostTypeSupportCheck } from '../';

jest.mock( '@wordpress/data/src/components/use-select', () => {
// This allows us to tweak the returned value on each test.
const mock = jest.fn();
return mock;
} );

function setupUseSelectMock( postType ) {
useSelect.mockImplementation( ( cb ) => {
return cb( () => ( {
getPostType: () => postType,
getEditedPostAttribute: () => 'post',
} ) );
} );
}

describe( 'PostTypeSupportCheck', () => {
it( 'renders its children when post type is not known', () => {
setupUseSelectMock( undefined );

const { container } = render(
<PostTypeSupportCheck postType={ undefined } supportKeys="title">
<PostTypeSupportCheck supportKeys="title">
Supported
</PostTypeSupportCheck>
);
Expand All @@ -20,11 +42,11 @@ describe( 'PostTypeSupportCheck', () => {
} );

it( 'does not render its children when post type is known and not supports', () => {
const postType = {
setupUseSelectMock( {
supports: {},
};
} );
const { container } = render(
<PostTypeSupportCheck postType={ postType } supportKeys="title">
<PostTypeSupportCheck supportKeys="title">
Supported
</PostTypeSupportCheck>
);
Expand All @@ -33,13 +55,13 @@ describe( 'PostTypeSupportCheck', () => {
} );

it( 'renders its children when post type is known and supports', () => {
const postType = {
setupUseSelectMock( {
supports: {
title: true,
},
};
} );
const { container } = render(
<PostTypeSupportCheck postType={ postType } supportKeys="title">
<PostTypeSupportCheck supportKeys="title">
Supported
</PostTypeSupportCheck>
);
Expand All @@ -48,16 +70,13 @@ describe( 'PostTypeSupportCheck', () => {
} );

it( 'renders its children if some of keys supported', () => {
const postType = {
setupUseSelectMock( {
supports: {
title: true,
},
};
} );
const { container } = render(
<PostTypeSupportCheck
postType={ postType }
supportKeys={ [ 'title', 'thumbnail' ] }
>
<PostTypeSupportCheck supportKeys={ [ 'title', 'thumbnail' ] }>
Supported
</PostTypeSupportCheck>
);
Expand All @@ -66,14 +85,11 @@ describe( 'PostTypeSupportCheck', () => {
} );

it( 'does not render its children if none of keys supported', () => {
const postType = {
setupUseSelectMock( {
supports: {},
};
} );
const { container } = render(
<PostTypeSupportCheck
postType={ postType }
supportKeys={ [ 'title', 'thumbnail' ] }
>
<PostTypeSupportCheck supportKeys={ [ 'title', 'thumbnail' ] }>
Supported
</PostTypeSupportCheck>
);
Expand Down
Loading