Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Blocks: Add a warning when registering variation without a name #61037

Merged
merged 2 commits into from
Apr 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions packages/blocks/src/api/registration.js
Original file line number Diff line number Diff line change
Expand Up @@ -723,6 +723,10 @@ export const getBlockVariations = ( blockName, scope ) => {
* ```
*/
export const registerBlockVariation = ( blockName, variation ) => {
if ( typeof variation.name !== 'string' ) {
console.warn( 'Variation names must be unique strings.' );
Copy link
Member

Choose a reason for hiding this comment

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

We currently use console.error for similar errors during block (un)registration with registerBlockType and unregisterBlockType. Is there a good reason to prefer a warning to an error? Perhaps because we want the application to continue running as usual (which is why we don't return early too)?

Copy link
Member Author

@Mamaduka Mamaduka Apr 24, 2024

Choose a reason for hiding this comment

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

I decided to use the warning because, unlike registerBlockType, this doesn't prevent registration.

It should be more of a hint to folks that they're incorrectly registering block variation instead of a breaking change.

Copy link
Member

Choose a reason for hiding this comment

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

Thanks for confirming 👍

}
Comment on lines +726 to +728
Copy link
Member

Choose a reason for hiding this comment

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

This could use a simple unit test. We're already testing all the console.error instances in registerBlockType() for example.

Copy link
Member Author

Choose a reason for hiding this comment

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

Added in 238d4d0

Copy link
Member

Choose a reason for hiding this comment

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

Thanks! Feel free to 🚢 when green.


dispatch( blocksStore ).addBlockVariations( blockName, variation );
};

Expand Down
22 changes: 22 additions & 0 deletions packages/blocks/src/api/test/registration.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { select, dispatch } from '@wordpress/data';
import {
registerBlockType,
registerBlockCollection,
registerBlockVariation,
unregisterBlockCollection,
unregisterBlockType,
setFreeformContentHandlerName,
Expand All @@ -26,6 +27,7 @@ import {
getBlockType,
getBlockTypes,
getBlockSupport,
getBlockVariations,
hasBlockSupport,
isReusableBlock,
unstable__bootstrapServerSideBlockDefinitions, // eslint-disable-line camelcase
Expand Down Expand Up @@ -1410,6 +1412,26 @@ describe( 'blocks', () => {
expect( isReusableBlock( block ) ).toBe( false );
} );
} );

describe( 'registerBlockVariation', () => {
it( 'should warn when registering block variation without a name', () => {
registerBlockType( 'core/variation-block', defaultBlockSettings );
registerBlockVariation( 'core/variation-block', {
title: 'Variation Title',
description: 'Variation description',
} );

expect( console ).toHaveWarnedWith(
'Variation names must be unique strings.'
);
expect( getBlockVariations( 'core/variation-block' ) ).toEqual( [
{
title: 'Variation Title',
description: 'Variation description',
},
] );
} );
} );
} );

/* eslint-enable react/forbid-elements */
Loading