Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add block name to binding args #21

Merged
merged 2 commits into from
Aug 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ function BoundBlockEdit( props: BoundBlockEditProps ) {
[ target ]: {
source: BLOCK_BINDING_SOURCE,
args: {
blockName,
field,
},
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { LoopTemplate } from '@/blocks/remote-data-container/components/loop-tem
describe( 'LoopTemplate', () => {
const mockGetInnerBlocks = () => [];
const mockRemoteData: RemoteData = {
blockName: 'test-block',
blockName: 'test/block',
isCollection: true,
metadata: {},
queryInput: {},
Expand All @@ -22,7 +22,7 @@ describe( 'LoopTemplate', () => {

it( 'renders "No results found" when there are no results', () => {
const emptyRemoteData: RemoteData = {
blockName: 'test-block',
blockName: 'test/block',
isCollection: true,
metadata: {},
queryInput: {},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ describe( 'withBlockBinding', () => {
const WrappedComponent = withBlockBinding( MockBlockEdit );
const testBlockConfig = {
config: {
'test-block': {
'test/block': {
availableBindings: { field1: { name: 'Field 1', type: 'string' } },
loop: false,
name: 'test-block',
name: 'test/block',
overrides: {},
selectors: [],
settings: {
Expand Down Expand Up @@ -55,7 +55,7 @@ describe( 'withBlockBinding', () => {
<WrappedComponent
attributes={ {} }
context={ {} }
name="test-block"
name="test/block"
setAttributes={ () => {} }
clientId="test-client-id"
isSelected={ false }
Expand All @@ -69,14 +69,14 @@ describe( 'withBlockBinding', () => {

it( 'renders BoundBlockEdit when remote data is available', async () => {
const remoteData = {
blockName: 'test-block',
blockName: 'test/block',
results: [ { field1: 'value1' } ],
};
render(
<WrappedComponent
attributes={ {} }
context={ { [ REMOTE_DATA_CONTEXT_KEY ]: remoteData } }
name="test-block"
name="test/block"
setAttributes={ () => {} }
clientId="test-client-id"
isSelected={ false }
Expand All @@ -93,7 +93,7 @@ describe( 'withBlockBinding', () => {

it( 'does not render BoundBlockEdit for synced pattern without enabled overrides', () => {
const remoteData = {
blockName: 'test-block',
blockName: 'test/block',
results: [ { field1: 'value1' } ],
};
render(
Expand All @@ -103,7 +103,7 @@ describe( 'withBlockBinding', () => {
[ REMOTE_DATA_CONTEXT_KEY ]: remoteData,
[ PATTERN_OVERRIDES_CONTEXT_KEY ]: [ 'test-pattern' ],
} }
name="test-block"
name="test/block"
setAttributes={ () => {} }
clientId="test-client-id"
isSelected={ false }
Expand All @@ -124,18 +124,18 @@ describe( 'withBlockBinding', () => {
bindings: {
content: {
source: BLOCK_BINDING_SOURCE,
args: { field: 'title' },
args: { blockName: 'test/block', field: 'title' },
},
},
},
},
context: {
[ REMOTE_DATA_CONTEXT_KEY ]: {
blockName: 'test-block',
blockName: 'test/block',
results: [ { title: 'New Title' } ],
},
},
name: 'test-block',
name: 'test/block',
setAttributes: mockSetAttributes,
clientId: 'test-client-id',
isSelected: false,
Expand Down Expand Up @@ -164,18 +164,18 @@ describe( 'withBlockBinding', () => {
bindings: {
content: {
source: BLOCK_BINDING_SOURCE,
args: { field: 'title' },
args: { blockName: 'test/block', field: 'title' },
},
},
},
},
context: {
[ REMOTE_DATA_CONTEXT_KEY ]: {
blockName: 'test-block',
blockName: 'test/block',
results: [ { title: 'Matching Title' } ],
},
},
name: 'test-block',
name: 'test/block',
setAttributes: mockSetAttributes,
clientId: 'test-client-id',
isSelected: false,
Expand Down
26 changes: 15 additions & 11 deletions tests/src/utils/block-binding.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,22 @@ import { getBoundAttributeEntries, getMismatchedAttributes } from '@/utils/block
describe( 'block-binding utils', () => {
describe( 'getBoundAttributeEntries', () => {
it( 'should return bound attribute entries', () => {
const blockName = 'test/block';
const attributes: ContextInnerBlockAttributes = {
metadata: {
bindings: {
content: { source: BLOCK_BINDING_SOURCE, args: { field: 'title' } },
url: { source: BLOCK_BINDING_SOURCE, args: { field: 'link' } },
alt: { source: 'other', args: { field: 'description' } },
content: { source: BLOCK_BINDING_SOURCE, args: { blockName, field: 'title' } },
url: { source: BLOCK_BINDING_SOURCE, args: { blockName, field: 'link' } },
alt: { source: 'other', args: { blockName, field: 'description' } },
},
},
};

const result = getBoundAttributeEntries( attributes );

expect( result ).toEqual( [
[ 'content', { source: BLOCK_BINDING_SOURCE, args: { field: 'title' } } ],
[ 'url', { source: BLOCK_BINDING_SOURCE, args: { field: 'link' } } ],
[ 'content', { source: BLOCK_BINDING_SOURCE, args: { blockName, field: 'title' } } ],
[ 'url', { source: BLOCK_BINDING_SOURCE, args: { blockName, field: 'link' } } ],
] );
} );

Expand All @@ -35,14 +36,15 @@ describe( 'block-binding utils', () => {

describe( 'getMismatchedAttributes', () => {
it( 'should return mismatched attributes', () => {
const blockName = 'test/block';
const attributes: ContextInnerBlockAttributes = {
content: 'Old content',
url: 'https://old-url.com',
alt: 'Old alt',
metadata: {
bindings: {
content: { source: BLOCK_BINDING_SOURCE, args: { field: 'title' } },
url: { source: BLOCK_BINDING_SOURCE, args: { field: 'link' } },
content: { source: BLOCK_BINDING_SOURCE, args: { blockName, field: 'title' } },
url: { source: BLOCK_BINDING_SOURCE, args: { blockName, field: 'link' } },
},
},
};
Expand All @@ -58,13 +60,14 @@ describe( 'block-binding utils', () => {
} );

it( 'should return an empty object when no mismatches are found', () => {
const blockName = 'test/block';
const attributes: ContextInnerBlockAttributes = {
content: 'Current content',
url: 'https://current-url.com',
metadata: {
bindings: {
content: { source: BLOCK_BINDING_SOURCE, args: { field: 'title' } },
url: { source: BLOCK_BINDING_SOURCE, args: { field: 'link' } },
content: { source: BLOCK_BINDING_SOURCE, args: { blockName, field: 'title' } },
url: { source: BLOCK_BINDING_SOURCE, args: { blockName, field: 'link' } },
},
},
};
Expand All @@ -77,13 +80,14 @@ describe( 'block-binding utils', () => {
} );

it( 'should handle missing results', () => {
const blockName = 'test/block';
const attributes: ContextInnerBlockAttributes = {
content: 'Old content',
url: 'https://old-url.com',
metadata: {
bindings: {
content: { source: BLOCK_BINDING_SOURCE, args: { field: 'title' } },
url: { source: BLOCK_BINDING_SOURCE, args: { field: 'link' } },
content: { source: BLOCK_BINDING_SOURCE, args: { blockName, field: 'title' } },
url: { source: BLOCK_BINDING_SOURCE, args: { blockName, field: 'link' } },
},
},
};
Expand Down
1 change: 1 addition & 0 deletions types/remote-data.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ interface MetaFieldSelection extends FieldSelection {
interface ContextBinding {
source: string;
args: {
blockName: string;
field: string;
};
}
Expand Down