From 5c7dd2915705898afddf24db11cf1cfe33044bd7 Mon Sep 17 00:00:00 2001 From: Timur Karimov Date: Sat, 11 Jan 2025 20:59:58 +0100 Subject: [PATCH] broaden billing field queries from form-scoped to document-scoped --- client/checkout/classic/event-handlers.js | 2 +- client/checkout/utils/test/upe.test.js | 22 ++++++++++++++-------- client/checkout/utils/upe.js | 16 +++++++++------- 3 files changed, 24 insertions(+), 16 deletions(-) diff --git a/client/checkout/classic/event-handlers.js b/client/checkout/classic/event-handlers.js index 2ad729113f0..4ed1912250f 100644 --- a/client/checkout/classic/event-handlers.js +++ b/client/checkout/classic/event-handlers.js @@ -76,7 +76,7 @@ jQuery( function ( $ ) { } ); $checkoutForm.on( generateCheckoutEventNames(), function () { - if ( isBillingInformationMissing( this ) ) { + if ( isBillingInformationMissing() ) { return; } diff --git a/client/checkout/utils/test/upe.test.js b/client/checkout/utils/test/upe.test.js index 91ad592d5cf..de5a870acc2 100644 --- a/client/checkout/utils/test/upe.test.js +++ b/client/checkout/utils/test/upe.test.js @@ -36,6 +36,7 @@ function buildForm( fields ) { input.value = field.value; form.appendChild( input ); } ); + document.body.appendChild( form ); return form; } @@ -57,6 +58,11 @@ describe( 'UPE checkout utils', () => { } ); beforeEach( () => { + const existingCheckoutForm = document.querySelector( 'form' ); + if ( existingCheckoutForm ) { + existingCheckoutForm.remove(); + } + getUPEConfig.mockImplementation( ( argument ) => { if ( argument === 'enabledBillingFields' ) { return { @@ -99,7 +105,7 @@ describe( 'UPE checkout utils', () => { } ); it( 'should return false when the billing information is not missing', () => { - const form = buildForm( [ + buildForm( [ { id: 'billing_first_name', value: 'Test' }, { id: 'billing_last_name', value: 'User' }, { id: 'billing_email', value: 'test@example.com' }, @@ -108,11 +114,11 @@ describe( 'UPE checkout utils', () => { { id: 'billing_city', value: 'Anytown' }, { id: 'billing_postcode', value: '12345' }, ] ); - expect( isBillingInformationMissing( form ) ).toBe( false ); + expect( isBillingInformationMissing() ).toBe( false ); } ); it( 'should return true when the billing information is missing', () => { - const form = buildForm( [ + buildForm( [ { id: 'billing_first_name', value: 'Test' }, { id: 'billing_last_name', value: 'User' }, { id: 'billing_email', value: 'test@example.com' }, @@ -121,11 +127,11 @@ describe( 'UPE checkout utils', () => { { id: 'billing_city', value: 'Anytown' }, { id: 'billing_postcode', value: '' }, ] ); - expect( isBillingInformationMissing( form ) ).toBe( true ); + expect( isBillingInformationMissing() ).toBe( true ); } ); it( 'should use the defaults when there is no specific locale data for a country', () => { - const form = buildForm( [ + buildForm( [ { id: 'billing_first_name', value: 'Test' }, { id: 'billing_last_name', value: 'User' }, { id: 'billing_email', value: 'test@example.com' }, @@ -134,11 +140,11 @@ describe( 'UPE checkout utils', () => { { id: 'billing_city', value: 'Anytown' }, { id: 'billing_postcode', value: '' }, ] ); - expect( isBillingInformationMissing( form ) ).toBe( true ); + expect( isBillingInformationMissing() ).toBe( true ); } ); it( 'should return false when the locale data for a country has no required fields', () => { - const form = buildForm( [ + buildForm( [ { id: 'billing_first_name', value: 'Test' }, { id: 'billing_last_name', value: 'User' }, { id: 'billing_email', value: 'test@example.com' }, @@ -147,7 +153,7 @@ describe( 'UPE checkout utils', () => { { id: 'billing_city', value: 'Anytown' }, { id: 'billing_postcode', value: '' }, ] ); - expect( isBillingInformationMissing( form ) ).toBe( true ); + expect( isBillingInformationMissing() ).toBe( true ); } ); } ); diff --git a/client/checkout/utils/upe.js b/client/checkout/utils/upe.js index c8201ff1ba1..61bcfb71334 100644 --- a/client/checkout/utils/upe.js +++ b/client/checkout/utils/upe.js @@ -404,17 +404,18 @@ function getParsedLocale() { } } -export const isBillingInformationMissing = ( form ) => { +export const isBillingInformationMissing = () => { const enabledBillingFields = getUPEConfig( 'enabledBillingFields' ); // first name and last name are kinda special - we just need one of them to be at checkout const name = `${ - form.querySelector( + document.querySelector( `#${ SHORTCODE_BILLING_ADDRESS_FIELDS.first_name }` )?.value || '' } ${ - form.querySelector( `#${ SHORTCODE_BILLING_ADDRESS_FIELDS.last_name }` ) - ?.value || '' + document.querySelector( + `#${ SHORTCODE_BILLING_ADDRESS_FIELDS.last_name }` + )?.value || '' }`.trim(); if ( ! name && @@ -435,14 +436,15 @@ export const isBillingInformationMissing = ( form ) => { const country = billingFieldsToValidate.includes( SHORTCODE_BILLING_ADDRESS_FIELDS.country ) - ? form.querySelector( `#${ SHORTCODE_BILLING_ADDRESS_FIELDS.country }` ) - ?.value + ? document.querySelector( + `#${ SHORTCODE_BILLING_ADDRESS_FIELDS.country }` + )?.value : null; // We need to just find one field with missing information. If even only one is missing, just return early. return Boolean( billingFieldsToValidate.find( ( fieldName ) => { - const field = form.querySelector( `#${ fieldName }` ); + const field = document.querySelector( `#${ fieldName }` ); let isRequired = enabledBillingFields[ fieldName ]?.required; const locale = getParsedLocale();