Skip to content

Commit

Permalink
Change registration checks
Browse files Browse the repository at this point in the history
  • Loading branch information
SantosGuillamot committed Jul 16, 2024
1 parent b50f347 commit 020ef83
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 36 deletions.
21 changes: 6 additions & 15 deletions packages/blocks/src/api/registration.js
Original file line number Diff line number Diff line change
Expand Up @@ -766,8 +766,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 {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.getValues] Function to get the values from the source.
* @param {Function} [source.setValues] Function to update multiple values connected to the source.
* @param {Function} [source.getPlaceholder] Function to get the placeholder when the value is undefined.
* @param {Function} [source.canUserEditValue] Function to determine if the user can edit the value.
Expand All @@ -780,8 +779,7 @@ export const unregisterBlockVariation = ( blockName, variationName ) => {
* registerBlockBindingsSource( {
* name: 'plugin/my-custom-source',
* label: _x( 'My Custom Source', 'block bindings source' ),
* getValue: () => 'Value to place in the block attribute',
* setValue: () => updateMyCustomValue(),
* getValues: () => getSourceValues(),
* setValues: () => updateMyCustomValuesInBatch(),
* getPlaceholder: () => 'Placeholder text when the value is undefined',
* canUserEditValue: () => true,
Expand All @@ -792,8 +790,7 @@ export const registerBlockBindingsSource = ( source ) => {
const {
name,
label,
getValue,
setValue,
getValues,
setValues,
getPlaceholder,
canUserEditValue,
Expand Down Expand Up @@ -853,15 +850,9 @@ export const registerBlockBindingsSource = ( source ) => {
return;
}

// Check the `getValue` property is correct.
if ( getValue && typeof getValue !== 'function' ) {
warning( 'Block bindings source getValue must be a function.' );
return;
}

// Check the `setValue` property is correct.
if ( setValue && typeof setValue !== 'function' ) {
warning( 'Block bindings source setValue must be a function.' );
// Check the `getValues` property is correct.
if ( getValues && typeof getValues !== 'function' ) {
warning( 'Block bindings source getValues must be a function.' );
return;
}

Expand Down
27 changes: 6 additions & 21 deletions packages/blocks/src/api/test/registration.js
Original file line number Diff line number Diff line change
Expand Up @@ -1512,28 +1512,15 @@ describe( 'blocks', () => {
expect( getBlockBindingsSource( 'core/testing' ) ).toBeUndefined();
} );

// Check the `getValue` callback is correct.
it( 'should reject invalid getValue callback', () => {
// Check the `getValues` callback is correct.
it( 'should reject invalid getValues callback', () => {
registerBlockBindingsSource( {
name: 'core/testing',
label: 'testing',
getValue: 'should be a function',
getValues: 'should be a function',
} );
expect( console ).toHaveWarnedWith(
'Block bindings source getValue must be a function.'
);
expect( getBlockBindingsSource( 'core/testing' ) ).toBeUndefined();
} );

// Check the `setValue` callback is correct.
it( 'should reject invalid setValue callback', () => {
registerBlockBindingsSource( {
name: 'core/testing',
label: 'testing',
setValue: 'should be a function',
} );
expect( console ).toHaveWarnedWith(
'Block bindings source setValue must be a function.'
'Block bindings source getValues must be a function.'
);
expect( getBlockBindingsSource( 'core/testing' ) ).toBeUndefined();
} );
Expand Down Expand Up @@ -1581,8 +1568,7 @@ describe( 'blocks', () => {
it( 'should register a valid source', () => {
const sourceProperties = {
label: 'Valid Source',
getValue: () => 'value',
setValue: () => 'new value',
getValues: () => 'value',
setValues: () => 'new values',
getPlaceholder: () => 'placeholder',
canUserEditValue: () => true,
Expand All @@ -1603,8 +1589,7 @@ describe( 'blocks', () => {
label: 'Valid Source',
} );
const source = getBlockBindingsSource( 'core/valid-source' );
expect( source.getValue ).toBeUndefined();
expect( source.setValue ).toBeUndefined();
expect( source.getValues ).toBeUndefined();
expect( source.setValues ).toBeUndefined();
expect( source.getPlaceholder ).toBeUndefined();
expect( source.canUserEditValue() ).toBe( false );
Expand Down

0 comments on commit 020ef83

Please sign in to comment.