Check that the callback for filtering payment methods is available and is a function before trying to run it (https://github.com/woocommerce/woocommerce-blocks/pull/7377)

* Check callback for payment method is available before trying to run it

* Check if callback is a function before trying to run it

* Update tests to ensure callbacks only run if they are registered
This commit is contained in:
Thomas Roberts 2022-10-12 15:29:04 +01:00 committed by GitHub
parent f1236254f4
commit 0aa65908d7
2 changed files with 18 additions and 0 deletions

View File

@ -50,6 +50,12 @@ export const canMakePaymentWithExtensions =
Object.entries( extensionsCallbacks ).forEach(
( [ namespace, callbacks ] ) => {
if (
! ( paymentMethodName in callbacks ) ||
typeof callbacks[ paymentMethodName ] !== 'function'
) {
return;
}
namespacedCallbacks[ namespace ] =
callbacks[ paymentMethodName ];
}

View File

@ -88,6 +88,9 @@ describe( 'payment-method-config-helper', () => {
woopay: trueCallback,
// testpay: one callback errors, one returns true
testpay: throwsCallback,
// Used to check that only valid callbacks run in each namespace. It is not present in
// 'other-woocommerce-marketplace-extension'.
blocks_pay: trueCallback,
}
);
registerPaymentMethodExtensionCallbacks(
@ -202,5 +205,14 @@ describe( 'payment-method-config-helper', () => {
expect( throwsCallback ).toHaveBeenCalledTimes( 1 );
expect( trueCallback ).toHaveBeenCalledTimes( 1 );
} );
it( 'Does not error when a callback for a payment method is in one namespace but not another', () => {
helpers.canMakePaymentWithExtensions(
() => true,
canMakePaymentExtensionsCallbacks,
'blocks_pay'
)( canMakePaymentArgument );
expect( console ).not.toHaveErrored();
} );
} );
} );