Decode fees name so special characters are rendered correctly (https://github.com/woocommerce/woocommerce-blocks/pull/3721)

* Decode fees name so special characters are rendered correctly

* Decode fees at hook level

* Fix several tests

* Remove unnecessary check
This commit is contained in:
Albert Juhé Lluveras 2021-02-04 18:05:47 +01:00 committed by GitHub
parent 65d98cef7d
commit 6ec197007f
7 changed files with 24 additions and 9 deletions

View File

@ -130,7 +130,7 @@ describe( 'Testing Payment Method Data Context Provider', () => {
} );
// need to clear the store resolution state between tests.
await dispatch( storeKey ).invalidateResolutionForStore();
await dispatch( storeKey ).receiveCart( defaultCartState );
await dispatch( storeKey ).receiveCart( defaultCartState.cartData );
} );
afterEach( async () => {
resetMockPaymentMethods();

View File

@ -29,6 +29,7 @@ describe( 'useStoreCart', () => {
const previewCartData = {
cartCoupons: previewCart.coupons,
cartItems: previewCart.items,
cartFees: previewCart.fees,
cartItemsCount: previewCart.items_count,
cartItemsWeight: previewCart.items_weight,
cartNeedsPayment: previewCart.needs_payment,
@ -37,7 +38,6 @@ describe( 'useStoreCart', () => {
cartIsLoading: false,
cartItemErrors: [],
cartErrors: [],
cartFees: [],
billingAddress: {
first_name: '',
last_name: '',
@ -75,6 +75,7 @@ describe( 'useStoreCart', () => {
const mockCartData = {
coupons: [],
items: mockCartItems,
fees: [],
itemsCount: 1,
itemsWeight: 10,
needsPayment: true,
@ -83,6 +84,8 @@ describe( 'useStoreCart', () => {
shippingAddress: mockShippingAddress,
shippingRates: [],
hasCalculatedShipping: true,
extensions: {},
errors: [],
};
const mockCartTotals = {
currency_code: 'USD',

View File

@ -27,8 +27,8 @@ const defaultBillingAddress = {
phone: '',
};
const decodeAddress = ( address ) =>
mapValues( address, ( value ) => decodeEntities( value ) );
const decodeValues = ( object ) =>
mapValues( object, ( value ) => decodeEntities( value ) );
/**
* @constant
@ -53,6 +53,7 @@ export const defaultCartData = {
cartHasCalculatedShipping: false,
paymentRequirements: [],
receiveCart: () => {},
extensions: {},
};
/**
@ -115,14 +116,17 @@ export const useStoreCart = ( options = { shouldSelect: true } ) => {
);
const shippingRatesLoading = store.isCustomerDataUpdating();
const { receiveCart } = dispatch( storeKey );
const billingAddress = decodeAddress( cartData.billingAddress );
const billingAddress = decodeValues( cartData.billingAddress );
const shippingAddress = cartData.needsShipping
? decodeAddress( cartData.shippingAddress )
? decodeValues( cartData.shippingAddress )
: billingAddress;
const cartFees = cartData.fees.map( ( fee ) =>
decodeValues( fee )
);
return {
cartCoupons: cartData.coupons,
cartItems: cartData.items || [],
cartFees: cartData.fees || [],
cartFees,
cartItemsCount: cartData.itemsCount,
cartItemsWeight: cartData.itemsWeight,
cartNeedsPayment: cartData.needsPayment,

View File

@ -20,7 +20,7 @@ describe( 'Testing cart', () => {
} );
// need to clear the store resolution state between tests.
await dispatch( storeKey ).invalidateResolutionForStore();
await dispatch( storeKey ).receiveCart( defaultCartState );
await dispatch( storeKey ).receiveCart( defaultCartState.cartData );
} );
afterEach( () => {
fetchMock.resetMocks();
@ -44,7 +44,9 @@ describe( 'Testing cart', () => {
it( 'renders empty cart if there are no items in the cart', async () => {
fetchMock.mockResponse( ( req ) => {
if ( req.url.match( /wc\/store\/cart/ ) ) {
return Promise.resolve( JSON.stringify( defaultCartState ) );
return Promise.resolve(
JSON.stringify( defaultCartState.cartData )
);
}
} );
render(

View File

@ -14,6 +14,7 @@ describe( 'cartReducer', () => {
cartData: {
coupons: [],
items: [],
fees: [],
itemsCount: 0,
itemsWeight: 0,
needsShipping: true,
@ -34,6 +35,7 @@ describe( 'cartReducer', () => {
response: {
coupons: [],
items: [],
fees: [],
itemsCount: 0,
itemsWeight: 0,
needsShipping: true,
@ -45,6 +47,7 @@ describe( 'cartReducer', () => {
expect( newState.cartData ).toEqual( {
coupons: [],
items: [],
fees: [],
itemsCount: 0,
itemsWeight: 0,
needsShipping: true,

View File

@ -20,6 +20,7 @@ describe( 'getCartData', () => {
const { value } = fulfillment.next( {
coupons: [],
items: [],
fees: [],
itemsCount: 0,
itemsWeight: 0,
needsShipping: true,
@ -29,6 +30,7 @@ describe( 'getCartData', () => {
receiveCart( {
coupons: [],
items: [],
fees: [],
itemsCount: 0,
itemsWeight: 0,
needsShipping: true,

View File

@ -16,6 +16,7 @@ export const defaultCartState = {
country: '',
},
items: [],
fees: [],
itemsCount: 0,
itemsWeight: 0,
needsShipping: true,