Skip to content

Commit

Permalink
Merge branch 'trunk' into add/google-sheets-fields-selection
Browse files Browse the repository at this point in the history
  • Loading branch information
shekharnwagh authored Dec 26, 2024
2 parents 00bf209 + 4dae381 commit c33efed
Show file tree
Hide file tree
Showing 11 changed files with 158 additions and 25 deletions.
2 changes: 1 addition & 1 deletion inc/Editor/DataBinding/BlockBindings.php
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ public static function get_remote_value( array $block_context, array $source_arg
return sprintf( '<span class="rdb-block-label">%s</span> %s', $source_args['label'], $value );
}

return $value;
return strval( $value );
}

public static function loop_block_render_callback( array $attributes, string $content, WP_Block $block ): string {
Expand Down
2 changes: 1 addition & 1 deletion inc/Validation/Types.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public static function button_text(): array {
}

public static function button_url(): array {
return self::generate_primitive_type( 'image_url' );
return self::generate_primitive_type( 'button_url' );
}

/**
Expand Down
4 changes: 1 addition & 3 deletions src/blocks/remote-data-container/components/InnerBlocks.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@ import { LoopTemplate } from '@/blocks/remote-data-container/components/loop-tem

interface InnerBlocksProps {
blockConfig: BlockConfig;
getInnerBlocks: (
result: Record< string, string >
) => BlockInstance< RemoteDataInnerBlockAttributes >[];
getInnerBlocks: ( result: RemoteDataResult ) => BlockInstance< RemoteDataInnerBlockAttributes >[];
remoteData: RemoteData;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,8 @@ export function FieldSelectionFromAvailableBindings( props: FieldSelectionWithFi
...acc,
[ fieldName ]: {
name: binding.name,
value: fieldValue,
// eslint-disable-next-line @typescript-eslint/no-base-to-string
value: fieldValue?.toString() ?? '',
},
};
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ interface ItemListProps {
blockName: string;
loading: boolean;
onSelect: ( data: RemoteDataQueryInput ) => void;
results?: RemoteData[ 'results' ];
results?: RemoteDataResult[];
searchTerms: string;
setSearchTerms: ( newValue: string ) => void;
}
Expand All @@ -31,7 +31,7 @@ export function ItemList( props: ItemListProps ) {
? item[ Object.keys( item ).find( key => /(^|_)(id)$/i.test( key ) ) as string ]
: instanceId,
}
) as RemoteData[ 'results' ];
) as RemoteDataResult[];
}, [ results ] );

// get fields from results data to use as columns
Expand Down Expand Up @@ -62,7 +62,7 @@ export function ItemList( props: ItemListProps ) {
id: string;
label: string;
enableGlobalSearch: boolean;
render?: ( { item }: { item: RemoteData[ 'results' ][ 0 ] } ) => JSX.Element;
render?: ( { item }: { item: RemoteDataResult } ) => JSX.Element;
enableSorting: boolean;
}[] = getFields.map( field => {
return {
Expand All @@ -71,7 +71,7 @@ export function ItemList( props: ItemListProps ) {
enableGlobalSearch: true,
render:
field === media
? ( { item }: { item: RemoteData[ 'results' ][ 0 ] } ) => {
? ( { item }: { item: RemoteDataResult } ) => {
return (
<img
// temporary until we pull in more data
Expand Down Expand Up @@ -134,7 +134,7 @@ export function ItemList( props: ItemListProps ) {
icon: <>{ __( 'Choose' ) }</>,
isPrimary: true,
label: '',
callback: ( items: RemoteData[ 'results' ] ) => {
callback: ( items: RemoteDataResult[] ) => {
items.map( item => onSelect( item ) );
},
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,7 @@ import { LoopTemplateInnerBlocks } from '@/blocks/remote-data-container/componen
import { LoopIndexContext } from '@/blocks/remote-data-container/context/LoopIndexContext';

interface LoopTemplateProps {
getInnerBlocks: (
result: Record< string, string >
) => BlockInstance< RemoteDataInnerBlockAttributes >[];
getInnerBlocks: ( result: RemoteDataResult ) => BlockInstance< RemoteDataInnerBlockAttributes >[];
remoteData: RemoteData;
}

Expand Down
6 changes: 3 additions & 3 deletions src/blocks/remote-data-container/hooks/usePatterns.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import { getBlockConfig } from '@/utils/localized-block-data';

export function cloneBlockWithAttributes(
block: BlockInstance,
attributes: Record< string, string >,
attributes: RemoteDataResult,
remoteDataBlockName: string
): BlockInstance {
const mismatchedAttributes = getMismatchedAttributes(
Expand Down Expand Up @@ -53,13 +53,13 @@ export function usePatterns( remoteDataBlockName: string, rootClientId: string =
const returnValue = {
defaultPattern,
getInnerBlocks: (
result: Record< string, string >
result: RemoteDataResult
): BlockInstance< RemoteDataInnerBlockAttributes >[] => {
return getBlocks< RemoteDataInnerBlockAttributes >( rootClientId ).map( block =>
cloneBlockWithAttributes( block, result, remoteDataBlockName )
);
},
getSupportedPatterns: ( result?: Record< string, string > ): BlockPattern[] => {
getSupportedPatterns: ( result?: RemoteDataResult ): BlockPattern[] => {
const supportedPatterns = __experimentalGetAllowedPatterns( rootClientId ).filter(
pattern =>
pattern?.blockTypes?.includes( remoteDataBlockName ) ||
Expand Down
9 changes: 5 additions & 4 deletions src/utils/block-binding.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,21 @@ function getAttributeValue( attributes: unknown, key: string | undefined | null
}

function getExpectedAttributeValue(
result?: Record< string, string >,
result?: Record< string, unknown >,
args?: RemoteDataBlockBindingArgs
): string | null {
if ( ! args?.field || ! result?.[ args.field ] ) {
return null;
}

let expectedValue = result[ args.field ];
// See comment on toString() in getAttributeValue.
let expectedValue = result[ args.field ]?.toString() ?? '';
if ( args.label ) {
const labelClass = getClassName( 'block-label' );
expectedValue = `<span class="${ labelClass }">${ args.label }</span> ${ expectedValue }`;
}

return expectedValue ?? null;
return expectedValue;
}

export function getBoundAttributeEntries(
Expand Down Expand Up @@ -64,7 +65,7 @@ export function getBoundBlockClassName(

export function getMismatchedAttributes(
attributes: RemoteDataInnerBlockAttributes,
results: RemoteData[ 'results' ],
results: RemoteDataResult[],
remoteDataBlockName: string,
index = 0
): Partial< RemoteDataInnerBlockAttributes > {
Expand Down
96 changes: 96 additions & 0 deletions tests/inc/Editor/DataBinding/BlockBindingsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -334,4 +334,100 @@ public function execute( HttpQueryInterface $query, array $input_variables ): ar
'test_input_field' => 'override_value transformed',
] );
}

/**
* @runInSeparateProcess
*/
public function test_get_remote_value(): void {
/**
* Mock the QueryRunner to return a result.
*/
$mock_qr = new MockQueryRunner();
$mock_qr->addResult( 'output_field', 'Test Output Value' );

$block_context = [
'blockName' => self::MOCK_BLOCK_NAME,
'queryInput' => [
'test_input_field' => 'test_value',
],
];

$input_schema = [
'test_input_field' => [
'name' => 'Test Input Field',
'type' => 'string',
],
];

$mock_block_config = [
'queries' => [
ConfigRegistry::DISPLAY_QUERY_KEY => MockQuery::from_array( [
'input_schema' => $input_schema,
'output_schema' => self::MOCK_OUTPUT_SCHEMA,
'query_runner' => $mock_qr,
] ),
],
];

$mock_config_store = Mockery::namedMock( ConfigStore::class );
$mock_config_store->shouldReceive( 'get_block_configuration' )
->once()
->with( self::MOCK_BLOCK_NAME )
->andReturn( $mock_block_config );

$source_args = [
'field' => self::MOCK_OUTPUT_FIELD_NAME,
];

$remote_value = BlockBindings::get_remote_value( $block_context, $source_args );
$this->assertSame( $remote_value, self::MOCK_OUTPUT_FIELD_VALUE );
}

/**
* @runInSeparateProcess
*/
public function test_get_remote_value_with_non_string(): void {
/**
* Mock the QueryRunner to return a result.
*/
$mock_qr = new MockQueryRunner();
$mock_qr->addResult( 'output_field', 123 );

$block_context = [
'blockName' => self::MOCK_BLOCK_NAME,
'queryInput' => [
'test_input_field' => 'test_value',
],
];

$input_schema = [
'test_input_field' => [
'name' => 'Test Input Field',
'type' => 'string',
],
];

$mock_block_config = [
'queries' => [
ConfigRegistry::DISPLAY_QUERY_KEY => MockQuery::from_array( [
'input_schema' => $input_schema,
'output_schema' => self::MOCK_OUTPUT_SCHEMA,
'query_runner' => $mock_qr,
] ),
],
];

$mock_config_store = Mockery::namedMock( ConfigStore::class );
$mock_config_store->shouldReceive( 'get_block_configuration' )
->once()
->with( self::MOCK_BLOCK_NAME )
->andReturn( $mock_block_config );

$source_args = [
'field' => self::MOCK_OUTPUT_FIELD_NAME,
];

$remote_value = BlockBindings::get_remote_value( $block_context, $source_args );
$this->assertSame( $remote_value, '123' );
}
}
38 changes: 38 additions & 0 deletions tests/src/utils/block-binding.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,5 +127,43 @@ describe( 'block-binding utils', () => {
content: '<span class="rdb-block-label">Title</span> My Title',
} );
} );

it( 'should handle mismatched types', () => {
const block = 'test/block';
const attributes: RemoteDataInnerBlockAttributes = {
content: '123',
metadata: {
bindings: {
content: { source: BLOCK_BINDING_SOURCE, args: { block, field: 'a_field' } },
},
},
};

const results: Record< string, unknown >[] = [ { a_field: 123 } ];

const result = getMismatchedAttributes( attributes, results, block );

expect( result ).toEqual( {} );
} );

it( 'should handle convert mismatched attributes to string', () => {
const block = 'test/block';
const attributes: RemoteDataInnerBlockAttributes = {
content: '123',
metadata: {
bindings: {
content: { source: BLOCK_BINDING_SOURCE, args: { block, field: 'a_field' } },
},
},
};

const results: Record< string, unknown >[] = [ { a_field: 1234 } ];

const result = getMismatchedAttributes( attributes, results, block );

expect( result ).toEqual( {
content: '1234',
} );
} );
} );
} );
9 changes: 5 additions & 4 deletions types/remote-data.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,17 @@ interface QueryInputOverride {
sourceType: 'query_var';
}

type RemoteDataResult = Record< string, unknown >;
type RemoteDataQueryInput = Record< string, unknown >;

interface RemoteData {
blockName: string;
isCollection: boolean;
metadata: Record< string, RemoteDataResultFields >;
queryInput: Record< string, string >;
queryInput: RemoteDataQueryInput;
queryInputOverrides?: Record< string, QueryInputOverride >;
resultId: string;
results: Record< string, string >[];
results: RemoteDataResult[];
}

interface RemoteDataBlockAttributes {
Expand Down Expand Up @@ -62,8 +65,6 @@ interface RemoteDataInnerBlockAttributes {
url?: string | RichTextData;
}

type RemoteDataQueryInput = Record< string, string >;

interface RemoteDataApiRequest {
block_name: string;
query_key: string;
Expand Down

0 comments on commit c33efed

Please sign in to comment.