From 6ea8c21f7b62d7eb26f9ee8f682d70f79ed0b076 Mon Sep 17 00:00:00 2001 From: Jarda Snajdr Date: Sun, 6 Aug 2023 09:37:47 +0200 Subject: [PATCH 1/5] Migrate PostTypeSupportCheck usages from withSelect to hooks (#53330) * PostTypeSupportCheck: only default export * PageAttributesOrder: migrate from withSelect HoC to useSelect hook * PostExcerptCheck: explicit children prop * PostFeaturedImageCheck: explicit children prop * PostFormatCheck: migrate from withSelect HoC to useSelect hook * PostLastRevision: migrate from withSelect HoC to useSelect hook --- .../src/components/page-attributes/order.js | 38 +++++------- .../components/page-attributes/test/order.js | 61 ++++++++++++++----- .../src/components/post-excerpt/check.js | 8 ++- .../components/post-featured-image/check.js | 6 +- .../src/components/post-format/check.js | 27 ++++---- .../components/post-last-revision/check.js | 26 ++++---- .../components/post-last-revision/index.js | 22 ++++--- .../post-last-revision/test/check.js | 44 ++++++++----- .../post-type-support-check/index.js | 2 +- .../post-type-support-check/test/index.js | 2 +- 10 files changed, 141 insertions(+), 95 deletions(-) diff --git a/packages/editor/src/components/page-attributes/order.js b/packages/editor/src/components/page-attributes/order.js index f226de5a47366d..416636d14bdbf8 100644 --- a/packages/editor/src/components/page-attributes/order.js +++ b/packages/editor/src/components/page-attributes/order.js @@ -7,8 +7,7 @@ import { FlexBlock, __experimentalNumberControl as NumberControl, } from '@wordpress/components'; -import { withSelect, withDispatch } from '@wordpress/data'; -import { compose } from '@wordpress/compose'; +import { useSelect, useDispatch } from '@wordpress/data'; import { useState } from '@wordpress/element'; /** @@ -17,17 +16,25 @@ import { useState } from '@wordpress/element'; import PostTypeSupportCheck from '../post-type-support-check'; import { store as editorStore } from '../../store'; -export const PageAttributesOrder = ( { onUpdateOrder, order = 0 } ) => { +function PageAttributesOrder() { + const order = useSelect( + ( select ) => + select( editorStore ).getEditedPostAttribute( 'menu_order' ) ?? 0, + [] + ); + const { editPost } = useDispatch( editorStore ); const [ orderInput, setOrderInput ] = useState( null ); const setUpdatedOrder = ( value ) => { setOrderInput( value ); const newOrder = Number( value ); if ( Number.isInteger( newOrder ) && value.trim?.() !== '' ) { - onUpdateOrder( Number( value ) ); + editPost( { menu_order: newOrder } ); } }; - const value = orderInput === null ? order : orderInput; + + const value = orderInput ?? order; + return ( @@ -43,27 +50,12 @@ export const PageAttributesOrder = ( { onUpdateOrder, order = 0 } ) => { ); -}; +} -function PageAttributesOrderWithChecks( props ) { +export default function PageAttributesOrderWithChecks() { return ( - + ); } - -export default compose( [ - withSelect( ( select ) => { - return { - order: select( editorStore ).getEditedPostAttribute( 'menu_order' ), - }; - } ), - withDispatch( ( dispatch ) => ( { - onUpdateOrder( order ) { - dispatch( editorStore ).editPost( { - menu_order: order, - } ); - }, - } ) ), -] )( PageAttributesOrderWithChecks ); diff --git a/packages/editor/src/components/page-attributes/test/order.js b/packages/editor/src/components/page-attributes/test/order.js index 01b0bd269e07cf..245cbbb8fc71db 100644 --- a/packages/editor/src/components/page-attributes/test/order.js +++ b/packages/editor/src/components/page-attributes/test/order.js @@ -4,10 +4,43 @@ import { render, screen } from '@testing-library/react'; import userEvent from '@testing-library/user-event'; +/** + * WordPress dependencies + */ +import { useSelect, useDispatch } from '@wordpress/data'; + /** * Internal dependencies */ -import { PageAttributesOrder } from '../order'; +import PageAttributesOrder from '../order'; + +jest.mock( '@wordpress/data/src/components/use-select', () => jest.fn() ); +jest.mock( '@wordpress/data/src/components/use-dispatch', () => ( { + useDispatch: jest.fn(), +} ) ); + +function setupDataMock( order = 0 ) { + useSelect.mockImplementation( ( mapSelect ) => + mapSelect( () => ( { + getPostType: () => null, + getEditedPostAttribute: ( attr ) => { + switch ( attr ) { + case 'menu_order': + return order; + default: + return null; + } + }, + } ) ) + ); + + const editPost = jest.fn(); + useDispatch.mockImplementation( () => ( { + editPost, + } ) ); + + return editPost; +} describe( 'PageAttributesOrder', () => { /** @@ -22,9 +55,9 @@ describe( 'PageAttributesOrder', () => { it( 'should reject invalid input', async () => { const user = userEvent.setup(); - const onUpdateOrder = jest.fn(); + const editPost = setupDataMock(); - render( ); + render( ); const input = screen.getByRole( 'spinbutton', { name: 'Order' } ); await user.type( input, 'bad', typeOptions ); @@ -33,31 +66,29 @@ describe( 'PageAttributesOrder', () => { await user.type( input, '+', typeOptions ); await user.type( input, ' ', typeOptions ); - expect( onUpdateOrder ).not.toHaveBeenCalled(); + expect( editPost ).not.toHaveBeenCalled(); } ); it( 'should update with zero input', async () => { const user = userEvent.setup(); - const onUpdateOrder = jest.fn(); + const editPost = setupDataMock( 4 ); - render( - - ); + render( ); const input = screen.getByRole( 'spinbutton', { name: 'Order' } ); await user.type( input, '0', typeOptions ); - expect( onUpdateOrder ).toHaveBeenCalledWith( 0 ); + expect( editPost ).toHaveBeenCalledWith( { menu_order: 0 } ); } ); it( 'should update with valid positive input', async () => { const user = userEvent.setup(); - const onUpdateOrder = jest.fn(); + const editPost = setupDataMock(); - render( ); + render( ); await user.type( screen.getByRole( 'spinbutton', { name: 'Order' } ), @@ -65,15 +96,15 @@ describe( 'PageAttributesOrder', () => { typeOptions ); - expect( onUpdateOrder ).toHaveBeenCalledWith( 4 ); + expect( editPost ).toHaveBeenCalledWith( { menu_order: 4 } ); } ); it( 'should update with valid negative input', async () => { const user = userEvent.setup(); - const onUpdateOrder = jest.fn(); + const editPost = setupDataMock(); - render( ); + render( ); await user.type( screen.getByRole( 'spinbutton', { name: 'Order' } ), @@ -81,6 +112,6 @@ describe( 'PageAttributesOrder', () => { typeOptions ); - expect( onUpdateOrder ).toHaveBeenCalledWith( -1 ); + expect( editPost ).toHaveBeenCalledWith( { menu_order: -1 } ); } ); } ); diff --git a/packages/editor/src/components/post-excerpt/check.js b/packages/editor/src/components/post-excerpt/check.js index a94a5badec180c..7d77ba77cd029a 100644 --- a/packages/editor/src/components/post-excerpt/check.js +++ b/packages/editor/src/components/post-excerpt/check.js @@ -3,8 +3,12 @@ */ import PostTypeSupportCheck from '../post-type-support-check'; -function PostExcerptCheck( props ) { - return ; +function PostExcerptCheck( { children } ) { + return ( + + { children } + + ); } export default PostExcerptCheck; diff --git a/packages/editor/src/components/post-featured-image/check.js b/packages/editor/src/components/post-featured-image/check.js index 5481deaa81604b..24a9f80058ddcc 100644 --- a/packages/editor/src/components/post-featured-image/check.js +++ b/packages/editor/src/components/post-featured-image/check.js @@ -4,10 +4,12 @@ import PostTypeSupportCheck from '../post-type-support-check'; import ThemeSupportCheck from '../theme-support-check'; -function PostFeaturedImageCheck( props ) { +function PostFeaturedImageCheck( { children } ) { return ( - + + { children } + ); } diff --git a/packages/editor/src/components/post-format/check.js b/packages/editor/src/components/post-format/check.js index cc65bde0cdec24..0810c9d613aae4 100644 --- a/packages/editor/src/components/post-format/check.js +++ b/packages/editor/src/components/post-format/check.js @@ -1,7 +1,7 @@ /** * WordPress dependencies */ -import { withSelect } from '@wordpress/data'; +import { useSelect } from '@wordpress/data'; /** * Internal dependencies @@ -9,17 +9,22 @@ import { withSelect } from '@wordpress/data'; import PostTypeSupportCheck from '../post-type-support-check'; import { store as editorStore } from '../../store'; -function PostFormatCheck( { disablePostFormats, ...props } ) { +function PostFormatCheck( { children } ) { + const disablePostFormats = useSelect( + ( select ) => + select( editorStore ).getEditorSettings().disablePostFormats, + [] + ); + + if ( disablePostFormats ) { + return null; + } + return ( - ! disablePostFormats && ( - - ) + + { children } + ); } -export default withSelect( ( select ) => { - const editorSettings = select( editorStore ).getEditorSettings(); - return { - disablePostFormats: editorSettings.disablePostFormats, - }; -} )( PostFormatCheck ); +export default PostFormatCheck; diff --git a/packages/editor/src/components/post-last-revision/check.js b/packages/editor/src/components/post-last-revision/check.js index eddb917db76d47..44f96b9cf7acb0 100644 --- a/packages/editor/src/components/post-last-revision/check.js +++ b/packages/editor/src/components/post-last-revision/check.js @@ -1,7 +1,7 @@ /** * WordPress dependencies */ -import { withSelect } from '@wordpress/data'; +import { useSelect } from '@wordpress/data'; /** * Internal dependencies @@ -9,11 +9,16 @@ import { withSelect } from '@wordpress/data'; import PostTypeSupportCheck from '../post-type-support-check'; import { store as editorStore } from '../../store'; -export function PostLastRevisionCheck( { - lastRevisionId, - revisionsCount, - children, -} ) { +function PostLastRevisionCheck( { children } ) { + const { lastRevisionId, revisionsCount } = useSelect( ( select ) => { + const { getCurrentPostLastRevisionId, getCurrentPostRevisionsCount } = + select( editorStore ); + return { + lastRevisionId: getCurrentPostLastRevisionId(), + revisionsCount: getCurrentPostRevisionsCount(), + }; + }, [] ); + if ( ! lastRevisionId || revisionsCount < 2 ) { return null; } @@ -25,11 +30,4 @@ export function PostLastRevisionCheck( { ); } -export default withSelect( ( select ) => { - const { getCurrentPostLastRevisionId, getCurrentPostRevisionsCount } = - select( editorStore ); - return { - lastRevisionId: getCurrentPostLastRevisionId(), - revisionsCount: getCurrentPostRevisionsCount(), - }; -} )( PostLastRevisionCheck ); +export default PostLastRevisionCheck; diff --git a/packages/editor/src/components/post-last-revision/index.js b/packages/editor/src/components/post-last-revision/index.js index 97434422b30f3d..17df8e8c38d3bf 100644 --- a/packages/editor/src/components/post-last-revision/index.js +++ b/packages/editor/src/components/post-last-revision/index.js @@ -3,7 +3,7 @@ */ import { sprintf, _n } from '@wordpress/i18n'; import { Button } from '@wordpress/components'; -import { withSelect } from '@wordpress/data'; +import { useSelect } from '@wordpress/data'; import { backup } from '@wordpress/icons'; import { addQueryArgs } from '@wordpress/url'; @@ -13,7 +13,16 @@ import { addQueryArgs } from '@wordpress/url'; import PostLastRevisionCheck from './check'; import { store as editorStore } from '../../store'; -function LastRevision( { lastRevisionId, revisionsCount } ) { +function LastRevision() { + const { lastRevisionId, revisionsCount } = useSelect( ( select ) => { + const { getCurrentPostLastRevisionId, getCurrentPostRevisionsCount } = + select( editorStore ); + return { + lastRevisionId: getCurrentPostLastRevisionId(), + revisionsCount: getCurrentPostRevisionsCount(), + }; + }, [] ); + return (