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

Commit

Permalink
Prevent error when entering postcode for countries without specific v…
Browse files Browse the repository at this point in the history
…alidation rules (#8987)

* Allow countries not covered by postcode-validator to pass validaiton

* Add "skipPushCheck" option when filling billing and shipping details

* Allow albania as a shipping/selling country

* Add test to ensure adding a postcode for a country without rules works

* Remove skipPushCheck option

no longer needed
  • Loading branch information
opr authored Apr 10, 2023
1 parent 99f9def commit 0b6429a
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 6 deletions.
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

0 comments on commit 0b6429a

Please sign in to comment.