This repository has been archived by the owner on Feb 23, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 218
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix: Make filter block contextual - Editor (#11218)
- Loading branch information
1 parent
c5afddb
commit 7cef728
Showing
8 changed files
with
199 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { DEFAULT_QUERY } from '@woocommerce/blocks/product-collection/constants'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import { sharedParams, mappedParams, formatQuery } from '../utils'; | ||
|
||
describe( 'formatQuery: transform Product Collection Block query to Product Collection Data Store API query', () => { | ||
it( 'shared param is carried over', () => { | ||
const formattedQuery = formatQuery( DEFAULT_QUERY ); | ||
sharedParams.forEach( ( key ) => { | ||
expect( formattedQuery ).toHaveProperty( | ||
key, | ||
DEFAULT_QUERY[ key ] | ||
); | ||
} ); | ||
} ); | ||
|
||
it( 'mapped param key is transformed', () => { | ||
const formattedQuery = formatQuery( DEFAULT_QUERY ); | ||
mappedParams.forEach( ( { key, map } ) => { | ||
expect( formattedQuery ).toHaveProperty( | ||
map, | ||
DEFAULT_QUERY[ key ] | ||
); | ||
} ); | ||
} ); | ||
|
||
it( 'taxQuery is transformed', () => { | ||
const queryWithTax = Object.assign( {}, DEFAULT_QUERY, { | ||
taxQuery: { | ||
product_cat: [ 1, 2 ], | ||
product_tag: [ 3, 4 ], | ||
custom_taxonomy: [ 5, 6 ], | ||
}, | ||
} ); | ||
const formattedQuery = formatQuery( queryWithTax ); | ||
expect( formattedQuery ).toHaveProperty( 'cat', [ 1, 2 ] ); | ||
expect( formattedQuery ).toHaveProperty( 'tag', [ 3, 4 ] ); | ||
expect( formattedQuery ).toHaveProperty( | ||
'_unstable_tax_custom_taxonomy', | ||
[ 5, 6 ] | ||
); | ||
} ); | ||
|
||
it( 'attribute query is transformed', () => { | ||
const woocommerceAttributes = [ | ||
{ termId: 11, taxonomy: 'pa_size' }, | ||
{ termId: 12, taxonomy: 'pa_color' }, | ||
{ termId: 13, taxonomy: 'pa_custom' }, | ||
{ termId: 14, taxonomy: 'pa_custom' }, | ||
]; | ||
const queryWithAttributes = Object.assign( {}, DEFAULT_QUERY, { | ||
woocommerceAttributes, | ||
} ); | ||
const formattedQuery = formatQuery( queryWithAttributes ); | ||
|
||
expect( formattedQuery ).toHaveProperty( 'attributes' ); | ||
|
||
woocommerceAttributes.forEach( ( { termId, taxonomy } ) => { | ||
expect( formattedQuery.attributes ).toEqual( | ||
expect.arrayContaining( [ | ||
{ term_id: termId, attribute: taxonomy }, | ||
] ) | ||
); | ||
} ); | ||
} ); | ||
} ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { ProductCollectionQuery } from '@woocommerce/blocks/product-collection/types'; | ||
|
||
export const sharedParams: Array< keyof ProductCollectionQuery > = [ | ||
'exclude', | ||
'offset', | ||
'order', | ||
'search', | ||
]; | ||
|
||
/** | ||
* There is an open dicussion around the shape of this object. Check it out on GH. | ||
* | ||
* @see {@link https://github.com/woocommerce/woocommerce-blocks/pull/11218#discussion_r1365171167 | #11218 review comment}. | ||
*/ | ||
export const mappedParams: { | ||
key: keyof ProductCollectionQuery; | ||
map: string; | ||
}[] = [ | ||
{ key: 'orderBy', map: 'orderby' }, | ||
{ key: 'pages', map: 'page' }, | ||
{ key: 'parents', map: 'parent' }, | ||
{ key: 'perPage', map: 'per_page' }, | ||
{ key: 'woocommerceStockStatus', map: 'stock_status' }, | ||
{ key: 'woocommerceOnSale', map: 'on_sale' }, | ||
{ key: 'woocommerceHandPickedProducts', map: 'include' }, | ||
]; | ||
|
||
function mapTaxonomy( taxonomy: string ) { | ||
const map = { | ||
product_tag: 'tag', | ||
product_cat: 'cat', | ||
}; | ||
|
||
return map[ taxonomy as keyof typeof map ] || `_unstable_tax_${ taxonomy }`; | ||
} | ||
|
||
function getTaxQueryMap( taxQuery: ProductCollectionQuery[ 'taxQuery' ] ) { | ||
return Object.entries( taxQuery ).map( ( [ taxonomy, terms ] ) => ( { | ||
[ mapTaxonomy( taxonomy ) ]: terms, | ||
} ) ); | ||
} | ||
|
||
function getAttributeQuery( | ||
woocommerceAttributes: ProductCollectionQuery[ 'woocommerceAttributes' ] | ||
) { | ||
if ( ! woocommerceAttributes ) { | ||
return {}; | ||
} | ||
return { | ||
attributes: woocommerceAttributes.map( ( attribute ) => ( { | ||
attribute: attribute.taxonomy, | ||
term_id: attribute.termId, | ||
} ) ), | ||
}; | ||
} | ||
|
||
export function formatQuery( query: ProductCollectionQuery ) { | ||
if ( ! query ) { | ||
return {}; | ||
} | ||
|
||
return Object.assign( | ||
{}, | ||
...sharedParams.map( | ||
( key ) => key in query && { [ key ]: query[ key ] } | ||
), | ||
...mappedParams.map( | ||
( param ) => | ||
param.key in query && { [ param.map ]: query[ param.key ] } | ||
), | ||
...getTaxQueryMap( query.taxQuery ), | ||
getAttributeQuery( query.woocommerceAttributes ) | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters