Skip to content

Commit

Permalink
Display address card for virtual products if shopper's address is kno…
Browse files Browse the repository at this point in the history
…wn (woocommerce#50127)

* Display address card for virtual products

* Remove obsolete dependency

* Optimise dependencies

* Add changefile(s) from automation for the following project(s): woocommerce-blocks

* Adjust core e2e tests

---------

Co-authored-by: github-actions <github-actions@github.com>
  • Loading branch information
nielslange and github-actions authored Jul 31, 2024
1 parent 6e2fbca commit 3dd6a40
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ const AddressCard = ( {
address,
formatToUse
);
const label =
target === 'shipping'
? __( 'Edit shipping address', 'woocommerce' )
: __( 'Edit billing address', 'woocommerce' );

return (
<div className="wc-block-components-address-card">
Expand Down Expand Up @@ -82,7 +86,7 @@ const AddressCard = ( {
className="wc-block-components-address-card__edit"
aria-controls={ target }
aria-expanded={ isExpanded }
aria-label={ __( 'Edit address', 'woocommerce' ) }
aria-label={ label }
onClick={ ( e ) => {
e.preventDefault();
onEdit();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
useCheckoutAddress,
useEditorContext,
noticeContexts,
useShippingData,
} from '@woocommerce/base-context';
import Noninteractive from '@woocommerce/base-components/noninteractive';
import type { ShippingAddress, FormFieldsConfig } from '@woocommerce/settings';
Expand Down Expand Up @@ -42,6 +43,7 @@ const Block = ( {
useBillingAsShipping,
} = useCheckoutAddress();
const { isEditor } = useEditorContext();
const { needsShipping } = useShippingData();

// Syncs shipping address with billing address if "Force shipping to the customer billing address" is enabled.
useEffectOnce( () => {
Expand Down Expand Up @@ -110,7 +112,7 @@ const Block = ( {
shippingAddress
);
const defaultEditingAddress =
isEditor || ! hasAddress || billingMatchesShipping;
isEditor || ! hasAddress || ( needsShipping && billingMatchesShipping );

return (
<>
Expand Down
34 changes: 31 additions & 3 deletions plugins/woocommerce-blocks/assets/js/blocks/checkout/test/block.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,11 @@ const CheckoutBlock = () => {
<Payment />
<AdditionalInformation />
<OrderNote />
<Terms checkbox={ true } text={ termsCheckboxDefaultText } />
<Terms
checkbox={ true }
showSeparator={ false }
text={ termsCheckboxDefaultText }
/>
<Actions />
</Fields>
<Totals>
Expand Down Expand Up @@ -148,7 +152,7 @@ describe( 'Testing Checkout', () => {
expect( fetchMock ).toHaveBeenCalledTimes( 1 );
} );

it( 'Renders the address card if the address is filled', async () => {
it( 'Renders the shipping address card if the address is filled and the cart contains a shippable product', async () => {
act( () => {
const cartWithAddress = {
...previewCart,
Expand Down Expand Up @@ -190,7 +194,7 @@ describe( 'Testing Checkout', () => {
await waitFor( () => expect( fetchMock ).toHaveBeenCalled() );

expect(
screen.getByRole( 'button', { name: 'Edit address' } )
screen.getByRole( 'button', { name: 'Edit shipping address' } )
).toBeInTheDocument();

expect(
Expand Down Expand Up @@ -258,6 +262,30 @@ describe( 'Testing Checkout', () => {
expect( fetchMock ).toHaveBeenCalledTimes( 1 );
} );

it( 'Renders the billing address card if the address is filled and the cart contains a virtual product', async () => {
act( () => {
const cartWithVirtualProduct = {
...previewCart,
needs_shipping: false,
};
fetchMock.mockResponse( ( req ) => {
if ( req.url.match( /wc\/store\/v1\/cart/ ) ) {
return Promise.resolve(
JSON.stringify( cartWithVirtualProduct )
);
}
return Promise.resolve( '' );
} );
} );
render( <CheckoutBlock /> );

await waitFor( () => expect( fetchMock ).toHaveBeenCalled() );

expect(
screen.getByRole( 'button', { name: 'Edit billing address' } )
).toBeInTheDocument();
} );

it( 'Ensures checkbox labels have unique IDs', async () => {
await act( async () => {
// Set required settings
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: minor
Type: fix

Display address card for virtual products if shopper's address is known
Original file line number Diff line number Diff line change
Expand Up @@ -440,7 +440,7 @@ test.describe(

// verify shipping details
await page
.getByLabel( 'Edit address', { exact: true } )
.getByLabel( 'Edit shipping address', { exact: true } )
.first()
.click();
await expect(
Expand Down Expand Up @@ -471,7 +471,7 @@ test.describe(

// verify billing details
await page
.getByLabel( 'Edit address', { exact: true } )
.getByLabel( 'Edit billing address', { exact: true } )
.last()
.click();
await expect(
Expand Down Expand Up @@ -782,7 +782,7 @@ test.describe(
).toBeVisible();

await page
.getByLabel( 'Edit address', { exact: true } )
.getByLabel( 'Edit shipping address', { exact: true } )
.first()
.click();

Expand Down

0 comments on commit 3dd6a40

Please sign in to comment.