Skip to content

Commit

Permalink
Merge usesContext and add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
SantosGuillamot committed Jul 13, 2024
1 parent 85bc7a8 commit d87fd4c
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 1 deletion.
16 changes: 15 additions & 1 deletion packages/blocks/src/api/registration.js
Original file line number Diff line number Diff line change
Expand Up @@ -909,7 +909,21 @@ export const registerBlockBindingsSource = ( source ) => {
return;
}

unlock( dispatch( blocksStore ) ).addBlockBindingsSource( source );
// Merge context from server and client.
let mergedUsesContext = [
...( existingSource?.usesContext || [] ),
...( usesContext || [] ),
];
// Remove duplicates.
mergedUsesContext =
mergedUsesContext.length > 0
? [ ...new Set( mergedUsesContext ) ]
: undefined;

unlock( dispatch( blocksStore ) ).addBlockBindingsSource( {
...source,
usesContext: mergedUsesContext,
} );
};

/**
Expand Down
56 changes: 56 additions & 0 deletions packages/blocks/src/api/test/registration.js
Original file line number Diff line number Diff line change
Expand Up @@ -1536,6 +1536,62 @@ describe( 'blocks', () => {
);
} );

it( 'should add usesContext when only defined in the client', () => {
// Simulate server bootstrap.
registerBlockBindingsSource( {
name: 'core/testing',
label: 'testing',
usesContext: [ 'postId', 'postType' ],
} );
// Register source in the client without usesContext.
registerBlockBindingsSource( {
name: 'core/testing',
getValue: () => 'value',
} );
const source = getBlockBindingsSource( 'core/testing' );
unregisterBlockBindingsSource( 'core/testing' );
expect( source.usesContext ).toEqual( [ 'postId', 'postType' ] );
} );

it( 'should keep usesContext when it is not defined in the client', () => {
// Simulate server bootstrap.
registerBlockBindingsSource( {
name: 'core/testing',
label: 'testing',
} );
// Register source in the client with usesContext.
registerBlockBindingsSource( {
name: 'core/testing',
usesContext: [ 'postId', 'postType' ],
getValue: () => 'value',
} );
const source = getBlockBindingsSource( 'core/testing' );
unregisterBlockBindingsSource( 'core/testing' );
expect( source.usesContext ).toEqual( [ 'postId', 'postType' ] );
} );

it( 'should merge usesContext from server and client without duplicates', () => {
// Simulate server bootstrap.
registerBlockBindingsSource( {
name: 'core/testing',
label: 'testing',
usesContext: [ 'postId', 'postType' ],
} );
// Register source in the client with usesContext.
registerBlockBindingsSource( {
name: 'core/testing',
usesContext: [ 'postType', 'clientContext' ],
getValue: () => 'value',
} );
const source = getBlockBindingsSource( 'core/testing' );
unregisterBlockBindingsSource( 'core/testing' );
expect( source.usesContext ).toEqual( [
'postId',
'postType',
'clientContext',
] );
} );

// Check the `getValue` callback is correct.
it( 'should reject invalid getValue callback', () => {
registerBlockBindingsSource( {
Expand Down

0 comments on commit d87fd4c

Please sign in to comment.