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

Commit

Permalink
Check that the callback for filtering payment methods is available an…
Browse files Browse the repository at this point in the history
…d is a function before trying to run it (#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
  • Loading branch information
opr authored and wavvves committed Oct 12, 2022
1 parent d246ff3 commit 3cee644
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 ];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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();
} );
} );
} );

0 comments on commit 3cee644

Please sign in to comment.