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:
parent
f1236254f4
commit
0aa65908d7
|
@ -50,6 +50,12 @@ export const canMakePaymentWithExtensions =
|
||||||
|
|
||||||
Object.entries( extensionsCallbacks ).forEach(
|
Object.entries( extensionsCallbacks ).forEach(
|
||||||
( [ namespace, callbacks ] ) => {
|
( [ namespace, callbacks ] ) => {
|
||||||
|
if (
|
||||||
|
! ( paymentMethodName in callbacks ) ||
|
||||||
|
typeof callbacks[ paymentMethodName ] !== 'function'
|
||||||
|
) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
namespacedCallbacks[ namespace ] =
|
namespacedCallbacks[ namespace ] =
|
||||||
callbacks[ paymentMethodName ];
|
callbacks[ paymentMethodName ];
|
||||||
}
|
}
|
||||||
|
|
|
@ -88,6 +88,9 @@ describe( 'payment-method-config-helper', () => {
|
||||||
woopay: trueCallback,
|
woopay: trueCallback,
|
||||||
// testpay: one callback errors, one returns true
|
// testpay: one callback errors, one returns true
|
||||||
testpay: throwsCallback,
|
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(
|
registerPaymentMethodExtensionCallbacks(
|
||||||
|
@ -202,5 +205,14 @@ describe( 'payment-method-config-helper', () => {
|
||||||
expect( throwsCallback ).toHaveBeenCalledTimes( 1 );
|
expect( throwsCallback ).toHaveBeenCalledTimes( 1 );
|
||||||
expect( trueCallback ).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();
|
||||||
|
} );
|
||||||
} );
|
} );
|
||||||
} );
|
} );
|
||||||
|
|
Loading…
Reference in New Issue