diff --git a/packages/blocks/src/api/registration.js b/packages/blocks/src/api/registration.js index 5ffa2e6ae3a818..4e74aa57343066 100644 --- a/packages/blocks/src/api/registration.js +++ b/packages/blocks/src/api/registration.js @@ -767,6 +767,7 @@ export const unregisterBlockVariation = ( blockName, variationName ) => { * @param {Object} source Properties of the source to be registered. * @param {string} source.name The unique and machine-readable name. * @param {string} [source.label] Human-readable label. + * @param {Array} [source.usesContext] Array of context needed by the source only in the editor. * @param {Function} [source.getValue] Function to get the value of the source. * @param {Function} [source.setValue] Function to update the value of the source. * @param {Function} [source.setValues] Function to update multiple values connected to the source. @@ -793,6 +794,7 @@ export const registerBlockBindingsSource = ( source ) => { const { name, label, + usesContext, getValue, setValue, setValues, @@ -867,6 +869,12 @@ export const registerBlockBindingsSource = ( source ) => { return; } + // Check the `usesContext` property is correct. + if ( usesContext && ! Array.isArray( usesContext ) ) { + console.error( 'Block bindings source usesContext must be an array.' ); + return; + } + // Check the `getValue` property is correct. if ( getValue && typeof getValue !== 'function' ) { console.error( 'Block bindings source getValue must be a function.' ); diff --git a/packages/blocks/src/api/test/registration.js b/packages/blocks/src/api/test/registration.js index 9a5786996b6241..f026b0592b79fd 100644 --- a/packages/blocks/src/api/test/registration.js +++ b/packages/blocks/src/api/test/registration.js @@ -1524,6 +1524,18 @@ describe( 'blocks', () => { ); } ); + // Check the `usesContext` array is correct. + it( 'should reject invalid usesContext property', () => { + registerBlockBindingsSource( { + name: 'core/testing', + label: 'testing', + usesContext: 'should be an array', + } ); + expect( console ).toHaveErroredWith( + 'Block bindings source usesContext must be an array.' + ); + } ); + // Check the `getValue` callback is correct. it( 'should reject invalid getValue callback', () => { registerBlockBindingsSource( { @@ -1593,6 +1605,7 @@ describe( 'blocks', () => { it( 'should register a valid source', () => { const sourceProperties = { label: 'Valid Source', + usesContext: [ 'postId' ], getValue: () => 'value', setValue: () => 'new value', setValues: () => 'new values', @@ -1615,6 +1628,7 @@ describe( 'blocks', () => { label: 'Valid Source', } ); const source = getBlockBindingsSource( 'core/valid-source' ); + expect( source.usesContext ).toBeUndefined(); expect( source.getValue ).toBeUndefined(); expect( source.setValue ).toBeUndefined(); expect( source.setValues ).toBeUndefined();