Skip to content
This repository has been archived by the owner on Feb 23, 2024. It is now read-only.

Prevent error when entering postcode for countries without specific validation rules #8987

Merged
merged 5 commits into from
Apr 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions packages/checkout/utils/validation/is-postcode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,10 @@ export interface IsPostcodeProps {
}

const isPostcode = ( { postcode, country }: IsPostcodeProps ): boolean => {
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
return DEFAULT_REGEXES.get( country )!.test( postcode );
// If the country is not in the list of regexes, trying to test it would result in an error, so we skip and assume
// that it is valid.
const postcodeTest = DEFAULT_REGEXES.get( country )?.test( postcode );
return typeof postcodeTest !== 'undefined' ? postcodeTest : true;
};

export default isPostcode;
4 changes: 2 additions & 2 deletions tests/e2e/fixtures/fixture-data.js
Original file line number Diff line number Diff line change
Expand Up @@ -278,15 +278,15 @@ const Settings = () => [
},
{
id: 'woocommerce_specific_allowed_countries',
value: [ 'DZ', 'CA', 'NZ', 'ES', 'GB', 'US' ],
value: [ 'AL', 'DZ', 'CA', 'NZ', 'ES', 'GB', 'US' ],
},
{
id: 'woocommerce_ship_to_countries',
value: 'specific',
},
{
id: 'woocommerce_specific_ship_to_countries',
value: [ 'DZ', 'CA', 'NZ', 'ES', 'GB', 'US' ],
value: [ 'AL', 'DZ', 'CA', 'NZ', 'ES', 'GB', 'US' ],
},
{
id: 'woocommerce_enable_coupons',
Expand Down
30 changes: 30 additions & 0 deletions tests/e2e/specs/shopper/cart-checkout/checkout.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,36 @@ describe( 'Shopper → Checkout', () => {
await shopper.block.verifyShippingDetails( SHIPPING_DETAILS );
await shopper.block.verifyBillingDetails( BILLING_DETAILS );
} );
it( 'User can add postcodes for different countries', async () => {
await shopper.block.goToShop();
await shopper.addToCartFromShopPage( SIMPLE_PHYSICAL_PRODUCT_NAME );
await shopper.block.goToCheckout();
await page.waitForSelector(
'.wc-block-checkout__use-address-for-billing input[type="checkbox"]'
);
await unsetCheckbox(
'.wc-block-checkout__use-address-for-billing input[type="checkbox"]'
);
await shopper.block.fillShippingDetails( {
...SHIPPING_DETAILS,
country: 'Albania',
state: 'Berat',
postcode: '1234',
} );

await shopper.block.fillBillingDetails( {
...BILLING_DETAILS,
country: 'United Kingdom',
postcode: 'SW1 1AA',
} );

await expect( page ).not.toMatchElement(
'.wc-block-components-validation-error p',
{
text: 'Please enter a valid postcode',
}
);
} );
} );

describe( 'Checkout Form Errors', () => {
Expand Down
12 changes: 10 additions & 2 deletions tests/utils/shopper.js
Original file line number Diff line number Diff line change
Expand Up @@ -290,13 +290,18 @@ export const shopper = {
await expect( page ).toFill( '#billing-address_1', customerBillingDetails.addressfirstline );
await expect( page ).toFill( '#billing-address_2', customerBillingDetails.addresssecondline );
await expect( page ).toFill( '#billing-city', customerBillingDetails.city );
await expect( page ).toFill( '#billing-state input', customerBillingDetails.state );

const stateInputField = await page.$( '#billing-state input' );
if ( stateInputField ) {
await expect( page ).toFill( '#billing-state input', customerBillingDetails.state );
}
await expect( page ).toFill( '#billing-postcode', customerBillingDetails.postcode );
await expect( page ).toFill( '#billing-phone', customerBillingDetails.phone );
await expect( page ).toFill( '#email', customerBillingDetails.email );
// Blur active field to trigger customer address update, then wait for requests to finish.
await page.evaluate( 'document.activeElement.blur()' );
await checkCustomerPushCompleted( 'billing', customerBillingDetails );

},

// prettier-ignore
Expand All @@ -313,7 +318,10 @@ export const shopper = {
await expect( page ).toFill( '#shipping-address_1', customerShippingDetails.addressfirstline );
await expect( page ).toFill( '#shipping-address_2', customerShippingDetails.addresssecondline );
await expect( page ).toFill( '#shipping-city', customerShippingDetails.city );
await expect( page ).toFill( '#shipping-state input', customerShippingDetails.state );
const stateInputField = await page.$( '#shipping-state input' );
if ( stateInputField ) {
await expect( page ).toFill( '#shipping-state input', customerShippingDetails.state );
}
await expect( page ).toFill( '#shipping-postcode', customerShippingDetails.postcode );
await expect( page ).toFill( '#shipping-phone', customerShippingDetails.phone );
// Blur active field to customer address update, then wait for requests to finish.
Expand Down