fix: mount filter controls in sidebar via InspectorControls

The FilterPanel was being mounted as a free-floating sibling of the block via an `editor.BlockEdit` HOC. In WordPress, a `PanelBody` only renders in the sidebar when wrapped in `<InspectorControls>`; without that wrapper, the panel renders wherever it lands in the React tree, which here was the editor canvas.

Move the InspectorControls mount into the Edit component itself (the idiomatic place for an own block to expose sidebar controls), and drop the `editor.BlockEdit` filter from `src/index.js`. Add an Edit test that pins the contract: the filter panel must mount inside InspectorControls and never float outside it. Add a manual jest mock for @wordpress/block-editor so the Edit test can render in an environment without WordPress globals.
This commit is contained in:
Keith Solomon
2026-08-06 15:21:25 -05:00
parent 860a192aff
commit 9ea16bf650
4 changed files with 219 additions and 80 deletions
@@ -0,0 +1,39 @@
/**
* Manual mock for @wordpress/block-editor.
*
* The package is provided by WordPress at runtime via the
* dependency-extraction-webpack-plugin and is not installed as a dev
* dependency. This mock lets the Jest environment resolve it.
*
* Slot components (`InspectorControls`, `BlockControls`) are rendered as
* lightweight `<div data-slot="...">` wrappers so tests can assert that
* `Edit` mounts them in the right place. Hooks (`useBlockProps`) are
* stubbed to return the supplied props unchanged, which lets the rest
* of the React tree render with stable className/style.
*/
import { createElement } from '@wordpress/element';
const Slot = ( { name, children } ) =>
createElement( 'div', { 'data-slot': name }, children );
function makeUseBlockProps( props = {} ) {
return {
className: props.className || '',
style: props.style || {},
};
}
const useBlockPropsFn = ( props ) => makeUseBlockProps( props );
useBlockPropsFn.save = ( props ) => makeUseBlockProps( props );
export const useBlockProps = useBlockPropsFn;
export const InspectorControls = ( { children } ) =>
createElement( Slot, { name: 'inspector-controls' }, children );
export const BlockControls = ( { children } ) =>
createElement( Slot, { name: 'block-controls' }, children );
export const MediaPlaceholder = ( props ) =>
createElement(
'div',
{ 'data-mock': 'MediaPlaceholder' },
props.labels ? props.labels.title : null
);
+98
View File
@@ -0,0 +1,98 @@
/**
* @jest-environment jsdom
*
* Tests for the Edit component. The core contract these tests pin down
* is that the filter controls are mounted in the sidebar via
* `InspectorControls` (not floating in the canvas). The block's `Edit`
* component is responsible for wrapping the `FilterPanel` in
* `InspectorControls` itself — see the comment block at the top of
* src/edit.js.
*/
// @wordpress/element, @wordpress/block-editor, @wordpress/components,
// @wordpress/i18n are all stubbed via __mocks__/* and inline jest.mock
// calls so the test environment has no WordPress globals.
import { render, screen } from '@testing-library/react';
import Edit from '../../src/edit';
const defaultAttributes = {
filter: 'normal',
intensity: 100,
imageId: 0,
imageUrl: '',
imageAlt: '',
width: 100,
height: 100,
linkUrl: '',
caption: '',
};
const baseProps = {
attributes: defaultAttributes,
setAttributes: jest.fn(),
isSelected: true,
clientId: 'test-client-id',
context: {},
name: 'ksolo/image-filter',
};
describe( 'Edit (Filtered Image block)', () => {
test( 'mounts the filter controls inside InspectorControls (sidebar)', () => {
const { container } = render( <Edit { ...baseProps } /> );
// InspectorControls is rendered as <div data-slot="inspector-controls">.
const sidebar = container.querySelector(
'[data-slot="inspector-controls"]'
);
expect( sidebar ).not.toBeNull();
// The PanelBody inside FilterPanel uses the title "Filter". Even
// in the empty/placeholder branch (no imageUrl), the inspector
// should be present so users can pick a preset before uploading.
expect(
sidebar.querySelector( '.ksolo-image-filter-panel' )
).not.toBeNull();
} );
test( 'does NOT mount a floating FilterPanel outside InspectorControls', () => {
// Regression guard: earlier the `FilterPanel` was rendered as a
// bare `<PanelBody>` (no InspectorControls wrapper) via an
// `editor.BlockEdit` HOC. That caused the controls to appear in
// the editor canvas rather than the sidebar. The new wiring
// guarantees the panel only appears inside InspectorControls.
const { container } = render( <Edit { ...baseProps } /> );
const allPanels = container.querySelectorAll(
'.ksolo-image-filter-panel'
);
expect( allPanels.length ).toBe( 1 );
expect(
allPanels[ 0 ].closest( '[data-slot="inspector-controls"]' )
).not.toBeNull();
} );
test( 'shows the MediaPlaceholder in the canvas when no image is set', () => {
render( <Edit { ...baseProps } /> );
// The MediaPlaceholder mock renders a div with
// `data-mock="MediaPlaceholder"` containing the labels.title
// string ("Filtered Image").
const placeholder = screen.getByText( 'Filtered Image' );
expect( placeholder ).not.toBeNull();
} );
test( 'renders the <img> in the canvas when an imageUrl is set', () => {
const props = {
...baseProps,
attributes: {
...defaultAttributes,
imageUrl: 'https://example.com/a.jpg',
imageAlt: 'A',
},
};
render( <Edit { ...props } /> );
const img = screen.getByAltText( 'A' );
expect( img ).not.toBeNull();
// The className on the <img> must include the preset class so the
// compiled CSS can target it.
expect( img.className ).toContain( 'ksolo-image-filter-img' );
expect( img.className ).toContain( 'has-filter-normal' );
} );
} );