diff --git a/packages/blocks/src/api/test/registration.js b/packages/blocks/src/api/test/registration.js index ee12ad19159881..cc339e59d27053 100644 --- a/packages/blocks/src/api/test/registration.js +++ b/packages/blocks/src/api/test/registration.js @@ -1706,7 +1706,7 @@ describe( 'blocks', () => { it( 'should correctly merge properties when bootstrap happens after registration', () => { // Register source in the client. - const clientProperties = { + const clientOnlyProperties = { getValues: () => 'values', setValues: () => 'new values', getPlaceholder: () => 'placeholder', @@ -1715,7 +1715,8 @@ describe( 'blocks', () => { registerBlockBindingsSource( { name: 'core/custom-source', label: 'Client Label', - ...clientProperties, + usesContext: [ 'postId', 'postType' ], + ...clientOnlyProperties, } ); // Bootstrap source from the server. @@ -1724,14 +1725,17 @@ describe( 'blocks', () => { ).addBootstrappedBlockBindingsSource( { name: 'core/custom-source', label: 'Server Label', - usesContext: [ 'postId' ], + usesContext: [ 'postId', 'serverContext' ], } ); // Check that the bootstrap values prevail and the client properties are still there. expect( getBlockBindingsSource( 'core/custom-source' ) ).toEqual( { + // Should use the server label. label: 'Server Label', - usesContext: [ 'postId' ], - ...clientProperties, + // Should merge usesContext from server and client. + usesContext: [ 'postId', 'postType', 'serverContext' ], + // Should keep client properties. + ...clientOnlyProperties, } ); unregisterBlockBindingsSource( 'core/custom-source' ); diff --git a/packages/blocks/src/store/reducer.js b/packages/blocks/src/store/reducer.js index 74d03c20b2f987..048c2196899fc4 100644 --- a/packages/blocks/src/store/reducer.js +++ b/packages/blocks/src/store/reducer.js @@ -372,19 +372,19 @@ export function collections( state = {}, action ) { } export function blockBindingsSources( state = {}, action ) { + // Merge usesContext with existing values, potentially defined in the server registration. + let mergedUsesContext = [ + ...( state[ action.name ]?.usesContext || [] ), + ...( action.usesContext || [] ), + ]; + // Remove duplicates. + mergedUsesContext = + mergedUsesContext.length > 0 + ? [ ...new Set( mergedUsesContext ) ] + : undefined; + switch ( action.type ) { case 'ADD_BLOCK_BINDINGS_SOURCE': - // Merge usesContext with existing values, potentially defined in the server registration. - let mergedUsesContext = [ - ...( state[ action.name ]?.usesContext || [] ), - ...( action.usesContext || [] ), - ]; - // Remove duplicates. - mergedUsesContext = - mergedUsesContext.length > 0 - ? [ ...new Set( mergedUsesContext ) ] - : undefined; - return { ...state, [ action.name ]: { @@ -407,7 +407,7 @@ export function blockBindingsSources( state = {}, action ) { */ ...state[ action.name ], label: action.label, - usesContext: action.usesContext, + usesContext: mergedUsesContext, }, }; case 'REMOVE_BLOCK_BINDINGS_SOURCE':