Skip to content

Commit

Permalink
Dev: Improve test coverage of upe.js (#8945)
Browse files Browse the repository at this point in the history
Co-authored-by: Daniel Guerra <daniel.guerra@automattic.com>
  • Loading branch information
danielmx-dev and danielmx-dev committed Jun 12, 2024
1 parent 04b0465 commit 4b59f3b
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 7 deletions.
4 changes: 4 additions & 0 deletions changelog/dev-7387-add-tests-to-upe-js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: minor
Type: dev

Improve test coverage of upe.js and rename isPaymentMethodRestrictedToLocation to hasPaymentMethodCountryRestrictions
4 changes: 2 additions & 2 deletions client/checkout/classic/event-handlers.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
generateCheckoutEventNames,
getSelectedUPEGatewayPaymentMethod,
isLinkEnabled,
isPaymentMethodRestrictedToLocation,
hasPaymentMethodCountryRestrictions,
isUsingSavedPaymentMethod,
togglePaymentMethodForCountry,
} from '../utils/upe';
Expand Down Expand Up @@ -228,7 +228,7 @@ jQuery( function ( $ ) {
}

function restrictPaymentMethodToLocation( upeElement ) {
if ( isPaymentMethodRestrictedToLocation( upeElement ) ) {
if ( hasPaymentMethodCountryRestrictions( upeElement ) ) {
togglePaymentMethodForCountry( upeElement );

// this event only applies to the checkout form, but not "place order" or "add payment method" pages.
Expand Down
58 changes: 57 additions & 1 deletion client/checkout/utils/test/upe.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
getStripeElementOptions,
blocksShowLinkButtonHandler,
getSelectedUPEGatewayPaymentMethod,
hasPaymentMethodCountryRestrictions,
isUsingSavedPaymentMethod,
dispatchChangeEventFor,
togglePaymentMethodForCountry,
Expand Down Expand Up @@ -126,6 +127,62 @@ describe( 'UPE checkout utils', () => {
} );
} );

describe( 'hasPaymentMethodCountryRestrictions', () => {
let container;

beforeAll( () => {
container = document.createElement( 'div' );
container.innerHTML = `
<ul class="wc_payment_methods payment_methods methods">
<li class="wc_payment_method payment_method_woocommerce_payments_card" data-payment-method-type="card">
<input id="payment_method_woocommerce_payments" type="radio" class="input-radio">
</li>
<li class="wc_payment_method payment_method_woocommerce_payments_bancontact" data-payment-method-type="bancontact">
<input id="payment_method_woocommerce_payments_bancontact" type="radio" class="input-radio">
</li>
</ul>
`;
document.body.appendChild( container );
} );

afterAll( () => {
document.body.removeChild( container );
container = null;
} );

beforeEach( () => {
jest.clearAllMocks();
getUPEConfig.mockImplementation( ( argument ) => {
if ( argument === 'paymentMethodsConfig' ) {
return {
card: { countries: [] },
bancontact: { countries: [ 'BE' ] },
};
}
} );
} );

it( 'should be true when the payment method is restricted to the location', () => {
const bancontactUpeElement = document.querySelector(
'.payment_method_woocommerce_payments_bancontact'
);

expect(
hasPaymentMethodCountryRestrictions( bancontactUpeElement )
).toBe( true );
} );

it( 'should be false when the payment method is not restricted to the location', () => {
const cardUpeElement = document.querySelector(
'.payment_method_woocommerce_payments_card'
);

expect(
hasPaymentMethodCountryRestrictions( cardUpeElement )
).toBe( false );
} );
} );

describe( 'togglePaymentMethodForCountry', () => {
let container;

Expand Down Expand Up @@ -171,7 +228,6 @@ describe( 'UPE checkout utils', () => {
} );

afterEach( () => {
// document.getElementById('billing_country').value = '';
window.wcpayCustomerData = null;
} );

Expand Down
10 changes: 6 additions & 4 deletions client/checkout/utils/upe.js
Original file line number Diff line number Diff line change
Expand Up @@ -292,19 +292,21 @@ export const blocksShowLinkButtonHandler = ( linkAutofill ) => {
};

/**
* Hides payment method if it has set specific countries in the PHP class.
* Returns true if the payment method has configured with any country restrictions.
*
* @param {Object} upeElement The selector of the DOM element of particular payment method to mount the UPE element to.
* @param {HTMLElement} upeElement The selector of the DOM element of particular payment method to mount the UPE element to.
* @return {boolean} Whether the payment method is restricted to selected billing country.
**/
export const isPaymentMethodRestrictedToLocation = ( upeElement ) => {
export const hasPaymentMethodCountryRestrictions = ( upeElement ) => {
const paymentMethodsConfig = getUPEConfig( 'paymentMethodsConfig' );
const paymentMethodType = upeElement.dataset.paymentMethodType;
return !! paymentMethodsConfig[ paymentMethodType ].countries.length;
};

/**
* @param {Object} upeElement The selector of the DOM element of particular payment method to mount the UPE element to.
* Hides payment method if it has set specific countries in the PHP class.
*
* @param {HTMLElement} upeElement The selector of the DOM element of particular payment method to mount the UPE element to.
**/
export const togglePaymentMethodForCountry = ( upeElement ) => {
const paymentMethodsConfig = getUPEConfig( 'paymentMethodsConfig' );
Expand Down

0 comments on commit 4b59f3b

Please sign in to comment.