Add useProductMetadata hook (#42961)
* Add useProductMetadata hook to perform update in multiple meta entries at same time * Update type to string * Add test * Remove unused parameters
This commit is contained in:
parent
b04c45ee5e
commit
ddd877441c
|
@ -0,0 +1,4 @@
|
||||||
|
Significance: minor
|
||||||
|
Type: add
|
||||||
|
|
||||||
|
Add useProductMetadata hook to perform update in multiple meta entries at same time
|
|
@ -4,3 +4,4 @@ export { useVariationsOrder as __experimentalUseVariationsOrder } from './use-va
|
||||||
export { useCurrencyInputProps as __experimentalUseCurrencyInputProps } from './use-currency-input-props';
|
export { useCurrencyInputProps as __experimentalUseCurrencyInputProps } from './use-currency-input-props';
|
||||||
export { useVariationSwitcher as __experimentalUseVariationSwitcher } from './use-variation-switcher';
|
export { useVariationSwitcher as __experimentalUseVariationSwitcher } from './use-variation-switcher';
|
||||||
export { default as __experimentalUseProductEntityProp } from './use-product-entity-prop';
|
export { default as __experimentalUseProductEntityProp } from './use-product-entity-prop';
|
||||||
|
export { default as __experimentalUseProductMetadata } from './use-product-metadata';
|
||||||
|
|
|
@ -0,0 +1,63 @@
|
||||||
|
/**
|
||||||
|
* External dependencies
|
||||||
|
*/
|
||||||
|
import { renderHook } from '@testing-library/react-hooks';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Internal dependencies
|
||||||
|
*/
|
||||||
|
import useProductMetadata from '../use-product-metadata';
|
||||||
|
|
||||||
|
const mockFnMetadataProp = jest.fn();
|
||||||
|
jest.mock( '@wordpress/core-data', () => ( {
|
||||||
|
useEntityProp: jest.fn().mockImplementation( () => {
|
||||||
|
return [
|
||||||
|
[
|
||||||
|
{
|
||||||
|
key: 'field1',
|
||||||
|
value: 'value1',
|
||||||
|
},
|
||||||
|
{
|
||||||
|
key: 'field2',
|
||||||
|
value: 'value1',
|
||||||
|
},
|
||||||
|
{
|
||||||
|
key: 'existing_field',
|
||||||
|
value: 'value1',
|
||||||
|
},
|
||||||
|
],
|
||||||
|
mockFnMetadataProp,
|
||||||
|
];
|
||||||
|
} ),
|
||||||
|
} ) );
|
||||||
|
|
||||||
|
describe( 'useProductMetadata', () => {
|
||||||
|
it( 'should update all the metadata with new values and not replace existing fields', async () => {
|
||||||
|
const { updateMetadata } = renderHook( () => useProductMetadata() )
|
||||||
|
.result.current;
|
||||||
|
updateMetadata( [
|
||||||
|
{
|
||||||
|
key: 'field1',
|
||||||
|
value: 'value2',
|
||||||
|
},
|
||||||
|
{
|
||||||
|
key: 'field2',
|
||||||
|
value: 'value2',
|
||||||
|
},
|
||||||
|
] );
|
||||||
|
expect( mockFnMetadataProp ).toHaveBeenCalledWith( [
|
||||||
|
{
|
||||||
|
key: 'existing_field',
|
||||||
|
value: 'value1',
|
||||||
|
},
|
||||||
|
{
|
||||||
|
key: 'field1',
|
||||||
|
value: 'value2',
|
||||||
|
},
|
||||||
|
{
|
||||||
|
key: 'field2',
|
||||||
|
value: 'value2',
|
||||||
|
},
|
||||||
|
] );
|
||||||
|
} );
|
||||||
|
} );
|
|
@ -0,0 +1,30 @@
|
||||||
|
/**
|
||||||
|
* External dependencies
|
||||||
|
*/
|
||||||
|
import { useEntityProp } from '@wordpress/core-data';
|
||||||
|
/**
|
||||||
|
* Internal dependencies
|
||||||
|
*/
|
||||||
|
import { Metadata } from '../types';
|
||||||
|
|
||||||
|
function useProductMetadata( postType?: string ) {
|
||||||
|
const [ metadata, setMetadata ] = useEntityProp< Metadata< string >[] >(
|
||||||
|
'postType',
|
||||||
|
postType || 'product',
|
||||||
|
'meta_data'
|
||||||
|
);
|
||||||
|
|
||||||
|
return {
|
||||||
|
updateMetadata: ( entries: Metadata< string >[] ) => {
|
||||||
|
setMetadata( [
|
||||||
|
...metadata.filter(
|
||||||
|
( item ) =>
|
||||||
|
entries.findIndex( ( e ) => e.key === item.key ) === -1
|
||||||
|
),
|
||||||
|
...entries,
|
||||||
|
] );
|
||||||
|
},
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
export default useProductMetadata;
|
Loading…
Reference in New Issue