Skip to content

Commit

Permalink
Block Bindings: Allow label override when it is defined in client reg…
Browse files Browse the repository at this point in the history
…istration (#66160)

* Change registration to override label

* Change unit test

* Compare label values

* Keep server value if client is not defined

* Add unit test to ensure server label persists

Co-authored-by: SantosGuillamot <santosguillamot@git.wordpress.org>
Co-authored-by: cbravobernal <cbravobernal@git.wordpress.org>
  • Loading branch information
3 people authored Oct 16, 2024
1 parent f205b11 commit 2bdeb49
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 14 deletions.
12 changes: 4 additions & 8 deletions packages/blocks/src/api/registration.js
Original file line number Diff line number Diff line change
Expand Up @@ -854,14 +854,6 @@ export const registerBlockBindingsSource = ( source ) => {
}

// Check the `label` property is correct.
if ( label && existingSource?.label ) {
warning(
'Block bindings "' +
name +
'" source label is already defined in the server.'
);
return;
}

if ( ! label && ! existingSource?.label ) {
warning( 'Block bindings source must contain a label.' );
Expand All @@ -873,6 +865,10 @@ export const registerBlockBindingsSource = ( source ) => {
return;
}

if ( label && existingSource?.label && label !== existingSource?.label ) {
warning( 'Block bindings "' + name + '" source label was overriden.' );
}

// Check the `usesContext` property is correct.
if ( usesContext && ! Array.isArray( usesContext ) ) {
warning( 'Block bindings source usesContext must be an array.' );
Expand Down
26 changes: 22 additions & 4 deletions packages/blocks/src/api/test/registration.js
Original file line number Diff line number Diff line change
Expand Up @@ -1512,20 +1512,38 @@ describe( 'blocks', () => {
expect( getBlockBindingsSource( 'core/testing' ) ).toBeUndefined();
} );

it( 'should not override label from the server', () => {
it( 'should override label from the server', () => {
// Simulate bootstrap source from the server.
registerBlockBindingsSource( {
name: 'core/server',
name: 'core/testing',
label: 'Server label',
} );
// Override the source with a different label in the client.
registerBlockBindingsSource( {
name: 'core/server',
name: 'core/testing',
label: 'Client label',
} );
expect( console ).toHaveWarnedWith(
'Block bindings "core/server" source label is already defined in the server.'
'Block bindings "core/testing" source label was overriden.'
);
const source = getBlockBindingsSource( 'core/testing' );
unregisterBlockBindingsSource( 'core/testing' );
expect( source.label ).toEqual( 'Client label' );
} );

it( 'should keep label from the server when not defined in the client', () => {
// Simulate bootstrap source from the server.
registerBlockBindingsSource( {
name: 'core/testing',
label: 'Server label',
} );
// Override the source with a different label in the client.
registerBlockBindingsSource( {
name: 'core/testing',
} );
const source = getBlockBindingsSource( 'core/testing' );
unregisterBlockBindingsSource( 'core/testing' );
expect( source.label ).toEqual( 'Server label' );
} );

// Check the `usesContext` array is correct.
Expand Down
3 changes: 1 addition & 2 deletions packages/blocks/src/store/reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -403,8 +403,7 @@ export function blockBindingsSources( state = {}, action ) {
return {
...state,
[ action.name ]: {
// Don't override the label if it's already set.
label: state[ action.name ]?.label || action.label,
label: action.label || state[ action.name ]?.label,
usesContext: getMergedUsesContext(
state[ action.name ]?.usesContext,
action.usesContext
Expand Down

1 comment on commit 2bdeb49

@github-actions
Copy link

Choose a reason for hiding this comment

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

Flaky tests detected in 2bdeb49.
Some tests passed with failed attempts. The failures may not be related to this commit but are still reported for visibility. See the documentation for more information.

🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/11365621754
📝 Reported issues:

Please sign in to comment.