Skip to content

Commit

Permalink
Block Editor: Use hooks instead of HoC in 'BlockModeToggle'
Browse files Browse the repository at this point in the history
  • Loading branch information
Mamaduka committed Aug 13, 2024
1 parent d34ddd4 commit ab4f7ae
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 54 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@
import { __ } from '@wordpress/i18n';
import { MenuItem } from '@wordpress/components';
import { getBlockType, hasBlockSupport } from '@wordpress/blocks';
import { withSelect, withDispatch } from '@wordpress/data';
import { compose } from '@wordpress/compose';
import { useDispatch, useSelect } from '@wordpress/data';

/**
* Internal dependencies
Expand All @@ -14,13 +13,23 @@ import { store as blockEditorStore } from '../../store';

const noop = () => {};

export function BlockModeToggle( {
blockType,
mode,
onToggleMode,
small = false,
isCodeEditingEnabled = true,
} ) {
export default function BlockModeToggle( { clientId, onToggle = noop } ) {
const { blockType, mode, isCodeEditingEnabled } = useSelect(
( select ) => {
const { getBlock, getBlockMode, getSettings } =
select( blockEditorStore );
const block = getBlock( clientId );

return {
mode: getBlockMode( clientId ),
blockType: block ? getBlockType( block.name ) : null,
isCodeEditingEnabled: getSettings().codeEditingEnabled,
};
},
[ clientId ]
);
const { toggleBlockMode } = useDispatch( blockEditorStore );

if (
! blockType ||
! hasBlockSupport( blockType, 'html', true ) ||
Expand All @@ -32,26 +41,14 @@ export function BlockModeToggle( {
const label =
mode === 'visual' ? __( 'Edit as HTML' ) : __( 'Edit visually' );

return <MenuItem onClick={ onToggleMode }>{ ! small && label }</MenuItem>;
return (
<MenuItem
onClick={ () => {
toggleBlockMode( clientId );
onToggle();
} }
>
{ label }
</MenuItem>
);
}

export default compose( [
withSelect( ( select, { clientId } ) => {
const { getBlock, getBlockMode, getSettings } =
select( blockEditorStore );
const block = getBlock( clientId );
const isCodeEditingEnabled = getSettings().codeEditingEnabled;

return {
mode: getBlockMode( clientId ),
blockType: block ? getBlockType( block.name ) : null,
isCodeEditingEnabled,
};
} ),
withDispatch( ( dispatch, { onToggle = noop, clientId } ) => ( {
onToggleMode() {
dispatch( blockEditorStore ).toggleBlockMode( clientId );
onToggle();
},
} ) ),
] )( BlockModeToggle );
Original file line number Diff line number Diff line change
Expand Up @@ -3,56 +3,59 @@
*/
import { render, screen } from '@testing-library/react';

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

/**
* Internal dependencies
*/
import { BlockModeToggle } from '../block-mode-toggle';
import BlockModeToggle from '../block-mode-toggle';

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

function setupUseSelectMock( mode, blockType, codeEditingEnabled = true ) {
useSelect.mockImplementation( () => {
return {
mode,
blockType,
isCodeEditingEnabled: codeEditingEnabled,
};
} );
}

describe( 'BlockModeToggle', () => {
it( "should not render the HTML mode button if the block doesn't support it", () => {
render(
<BlockModeToggle blockType={ { supports: { html: false } } } />
);
setupUseSelectMock( undefined, { supports: { html: false } } );
render( <BlockModeToggle /> );

expect(
screen.queryByRole( 'menuitem', { name: 'Edit as HTML' } )
).not.toBeInTheDocument();
} );

it( 'should render the HTML mode button', () => {
render(
<BlockModeToggle
blockType={ { supports: { html: true } } }
mode="visual"
/>
);
setupUseSelectMock( 'visual', { supports: { html: true } } );
render( <BlockModeToggle /> );

expect(
screen.getByRole( 'menuitem', { name: 'Edit as HTML' } )
).toBeVisible();
} );

it( 'should render the Visual mode button', () => {
render(
<BlockModeToggle
blockType={ { supports: { html: true } } }
mode="html"
/>
);
setupUseSelectMock( 'html', { supports: { html: true } } );
render( <BlockModeToggle /> );

expect(
screen.getByRole( 'menuitem', { name: 'Edit visually' } )
).toBeVisible();
} );

it( 'should not render the Visual mode button if code editing is disabled', () => {
render(
<BlockModeToggle
blockType={ { supports: { html: true } } }
mode="html"
isCodeEditingEnabled={ false }
/>
);
setupUseSelectMock( 'html', { supports: { html: true } }, false );
render( <BlockModeToggle /> );

expect(
screen.queryByRole( 'menuitem', { name: 'Edit visually' } )
Expand Down

0 comments on commit ab4f7ae

Please sign in to comment.