Skip to content

Commit

Permalink
Handle usesContext validation
Browse files Browse the repository at this point in the history
  • Loading branch information
SantosGuillamot committed Jul 12, 2024
1 parent 8abc556 commit 85bc7a8
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
8 changes: 8 additions & 0 deletions packages/blocks/src/api/registration.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -793,6 +794,7 @@ export const registerBlockBindingsSource = ( source ) => {
const {
name,
label,
usesContext,
getValue,
setValue,
setValues,
Expand Down Expand Up @@ -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.' );
Expand Down
14 changes: 14 additions & 0 deletions packages/blocks/src/api/test/registration.js
Original file line number Diff line number Diff line change
Expand Up @@ -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( {
Expand Down Expand Up @@ -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',
Expand All @@ -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();
Expand Down

0 comments on commit 85bc7a8

Please sign in to comment.