diff --git a/packages/editor/src/components/post-author/test/check.js b/packages/editor/src/components/post-author/test/check.js index 014599500743cc..597ab97ea2a8ad 100644 --- a/packages/editor/src/components/post-author/test/check.js +++ b/packages/editor/src/components/post-author/test/check.js @@ -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( authors ); 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( authors ); expect( screen.queryByText( 'authors' ) ).not.toBeInTheDocument(); } ); it( 'should render control', () => { - useSelect.mockImplementation( () => ( { - hasAuthors: true, - hasAssignAuthorAction: true, - } ) ); + setupUseSelectMock( true, true ); render( authors ); expect( screen.getByText( 'authors' ) ).toBeVisible(); diff --git a/packages/editor/src/components/post-type-support-check/index.js b/packages/editor/src/components/post-type-support-check/index.js index 57a774fc17e422..cf6381eeeccdc5 100644 --- a/packages/editor/src/components/post-type-support-check/index.js +++ b/packages/editor/src/components/post-type-support-check/index.js @@ -1,7 +1,7 @@ /** * WordPress dependencies */ -import { withSelect } from '@wordpress/data'; +import { useSelect } from '@wordpress/data'; import { store as coreStore } from '@wordpress/core-data'; /** @@ -14,7 +14,6 @@ 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 @@ -22,7 +21,12 @@ import { store as editorStore } from '../../store'; * * @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 = ( @@ -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; diff --git a/packages/editor/src/components/post-type-support-check/test/index.js b/packages/editor/src/components/post-type-support-check/test/index.js index c9f9e0ab1ebe36..29e538bd089e7e 100644 --- a/packages/editor/src/components/post-type-support-check/test/index.js +++ b/packages/editor/src/components/post-type-support-check/test/index.js @@ -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( - + Supported ); @@ -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( - + Supported ); @@ -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( - + Supported ); @@ -48,16 +70,13 @@ describe( 'PostTypeSupportCheck', () => { } ); it( 'renders its children if some of keys supported', () => { - const postType = { + setupUseSelectMock( { supports: { title: true, }, - }; + } ); const { container } = render( - + Supported ); @@ -66,14 +85,11 @@ describe( 'PostTypeSupportCheck', () => { } ); it( 'does not render its children if none of keys supported', () => { - const postType = { + setupUseSelectMock( { supports: {}, - }; + } ); const { container } = render( - + Supported );