diff --git a/packages/checkout/utils/validation/is-postcode.ts b/packages/checkout/utils/validation/is-postcode.ts index 82ff5606916..bb3104f3d33 100644 --- a/packages/checkout/utils/validation/is-postcode.ts +++ b/packages/checkout/utils/validation/is-postcode.ts @@ -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; diff --git a/tests/e2e/fixtures/fixture-data.js b/tests/e2e/fixtures/fixture-data.js index 97194702bb5..1ad81f5a625 100644 --- a/tests/e2e/fixtures/fixture-data.js +++ b/tests/e2e/fixtures/fixture-data.js @@ -278,7 +278,7 @@ 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', @@ -286,7 +286,7 @@ const Settings = () => [ }, { 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', diff --git a/tests/e2e/specs/shopper/cart-checkout/checkout.test.js b/tests/e2e/specs/shopper/cart-checkout/checkout.test.js index 99917a815a0..c0cf8483e9a 100644 --- a/tests/e2e/specs/shopper/cart-checkout/checkout.test.js +++ b/tests/e2e/specs/shopper/cart-checkout/checkout.test.js @@ -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', () => { diff --git a/tests/utils/shopper.js b/tests/utils/shopper.js index e1b969dce33..99855e58b3d 100644 --- a/tests/utils/shopper.js +++ b/tests/utils/shopper.js @@ -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 @@ -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.