Skip to content

Commit

Permalink
Editor: Use hooks instead of HoCs in PostTypeSupportCheck (#53235)
Browse files Browse the repository at this point in the history
* Editor: Use hooks instead of HoCs in PostTypeSupportCheck

* Mock useSelect in tests

* Fix more tests
  • Loading branch information
tyxla authored Aug 1, 2023
1 parent 162e477 commit 52f2bc8
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 41 deletions.
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

1 comment on commit 52f2bc8

@github-actions
Copy link

Choose a reason for hiding this comment

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

Flaky tests detected in 52f2bc8.
Some tests passed with failed attempts. The failures may not be related to this commit but are still reported for visibility. See the documentation for more information.

🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/5729382789
📝 Reported issues:

Please sign in to comment.